Shaun Mccran

My digital playground
 
13
O
C
T
2009

ColdFusion structures and case sensitivity examples

Having been using Flex a little more recently I stumbled upon an old issue that I had previously addressed, but it had become second nature, and so I had forgotten about having to learn a workaround in the past. Passing objects from ColdFusion to Flex can be tricky at times, especially as ColdFusion is generally not case sensitive, but Flex is, and this can lead to problems.

I had previously found that how you build your Structure will have an impact the case of the keys. Example one is a Structure built using the traditional dot notation, in a CF 7 and below method:

view plain print about
1<cfscript>
2    newStruct = structNew();
3    newStruct.starter = "Prawn Salad";
4    newStruct.maincourse = "Roast Chicken";
5    newStruct.desert = "Apple Pie";
6
</cfscript>
7
8<cfdump var="#newStruct#" label="Dot notation">

This is the older way of building a Structure, notice how all the keys are uppercase.

The second example is very similar, except that it is using associative array notation, IE brackets to denote the key values, rather than dot notation.

view plain print about
1<cfscript>
2    newStruct = structNew();
3    newStruct["starter"] = "Prawn Salad";
4    newStruct["maincourse"] = "Roast Chicken";
5    newStruct["desert"] = "Apple Pie";
6
</cfscript>
7
8<cfdump var="#newStruct#" label="Struct notation">

Since I originally looked at this ColdFusion 8 (and now 9!) have been released. ColdFusion 8 introduced a new way of creating structures, what casing does this use?

view plain print about
1<cfset newStruct = {
2    starter = "Prawn Salad",
3    maincourse = "Roast Chicken",
4    desert = "Apple Pie"
5} /
>

6
7<cfdump var="#newStruct#" label="CF 8 method">

It also creates an uppercase structure. I haven't really played around with this method enough to see if there is another way of creating a lowercase structure, so for now I'll be sticking to the older associative array method of creating my structures. That way they are easily transferred as a Flex object.

 
26
A
U
G
2009

Example of inserting a Struct() into a database using keys

A while ago a colleague and I were working on a timesheet application in Flex. The idea was that you could commit a custom timebar object, generated in flex, and it would update the dataset in the back end using the ColdFusion flex gateway.

I came across the code recently, and decided to tidy it up a bit, and make the query dynamic, based on the Struct contents. The obvious limitation to this is that your Struct and your database schema have to match exactly.

I won't go into the Flex application here, but I've emulated its input arguments here with a pre-populated structure.

view plain print about
1<cfscript>
2 timesheetTask = StructNew();
3 StructInsert(timesheetTask, "employeeid", '36');
4 StructInsert(timesheetTask, "timesheetDT", '0');
5 StructInsert(timesheetTask, "projectid", '6');
6 StructInsert(timesheetTask, "weekid", '25');
7 StructInsert(timesheetTask, "taskid", '39');
8 StructInsert(timesheetTask, "hours", '8');
9 StructInsert(timesheetTask, "comment", 'Comments for this task live here');
10 StructInsert(timesheetTask, "szStatus", '1');
11 StructInsert(timesheetTask, "iFirstLineApproval", '23');
12 StructInsert(timesheetTask, "iSecondLineApproval", '34');
13 StructInsert(timesheetTask, "iCurrentApprover", '');
14 StructInsert(timesheetTask, "szRejectReason", '');
15 StructInsert(timesheetTask, "szDescription", '');
16
17 updateTimesheet = createObject("component", "timesheet");
18 updateTimesheet.updateTask(timesheetTask);
19
</cfscript>

Notice that this code also calls the CFC object at the end. The data itself isn't massively important, it's a time object for recording tasks.

Next we have the function, which accepts a Struct() argument called 'taskStruct'. I then loop through the structure, and populate a SQL query using the keys from a collection. The only logic is a check to see if it is the last structure element, as this controls the ',' placement.

view plain print about
1<cffunction name="updateTask" access="remote" returntype="string" hint="Creates a record for timesheet tasks">
2 <cfargument name="taskStruct" type="struct" required="yes">
3 <cfset var count = 0>
4
5 <cfdump var="#arguments.taskStruct#">
6 <cfset variables.structSize = structCount(arguments.taskStruct)>
7
8 <cfquery datasource="#application.dsn#">
9 INSERT INTO [dbo].[timesheet]
10 (<cfloop collection="#arguments.taskStruct#" item="key">
11 [#key#]
12 <cfset count = count + 1>
13 <cfif count LT variables.structSize>,</cfif>
14 </cfloop>)
15
16 <cfset count = 0>
17
18 VALUES(<cfloop collection="#arguments.taskStruct#" item="key">
19 '#arguments.taskStruct[key]#'
20 <cfset count = count + 1>
21 <cfif count LT variables.structSize>,</cfif>
22 </cfloop>)
23 </cfquery>
24
25 <cfreturn true>
26 </cffunction>

That will insert your Struct into a database, in small and tidy manner. It was somewhere around here that we started using cfproperty tags, and creating strongly typed objects for Flex.

 
09
J
U
L
2009

Cfscript looping over lists

I really like cfscript, but as with any skill I don't use it often in my current role (its not 'standard') so I find that my knowledge of it is on the wane.

I do develop extensively outside of a working environment, so I find myself deliberately writing cfscript code rather than 'normal' coldfusion. I think it is because it is more like javascript, actionScript or .Net, I like the script formatting.

So I find myself looping over a series of lists in a form action template, I thought I would experiment with the different ways of doing the loop, cfscript and non script. So I create a simple list.

view plain print about
1<cfset variables.myList = "Banana,Apple,Orange,Pear,Strawberry,Kiwi,Mandarin,Melon">

Now loop over it in the traditional way.

view plain print about
1<cfoutput>
2    <cfloop from="1" to="#listLen(variables.myList)#" index="L">
3    #ListGetAt(variables.myList, L)#<br/>
4    </cfloop>
5</cfoutput>

And now the cfscript way.

view plain print about
1<cfscript>
2 For (i=1;i LTE ListLen(variables.myList); i=i+1)
3 writeoutput(ListGetAt(variables.myList, i)&'<BR>');
4
</cfscript>

Both code blocks give the same output, I much prefer the code style of the second block though.

 
11
J
U
N
2009

Rounding up to the nearest timed value - Ex every 15 minutes

A watercooler moment threw up an interesting dilemma yesterday. I was talking to a colleague about a scheduled application that would run through a timed loop over the course of a day. It would chop off the top N records from a record set and perform an operation on them. Rather than having to manually run the task every 15 minutes, it would be much better to have it run automatically, at 00,15,30 and 45min markers.

So how to do this?

Initial thoughts wandered through various looping-over type conversations, and creating structures of the timed triggers etc.

First stab coding function looks a little something like this:

view plain print about
1<cf script>
2function RoundUp(input){
3var roundval = 15;
4var result = 0;
5
6if(ArrayLen(arguments) GTE 2)
7roundval = arguments[2];
8if(roundval EQ 0)
9roundval = 1;
10
11if((input MOD roundval) NEQ 0)
12{
13result = input + (roundval - (input MOD roundval));
14}
15else
16{
17result = input;
18}
19return result;
20}
21</cfscript>
22
23<cfset variables.time = RoundUp(#timeformat(NOW(),"mm")#)>
24<cfoutput>variables.time = #variables.time#</cfoutput>

Which successfully returns a rounded up value to the nearest 15 minutes.

It is a bit long winded though and after some more thought and consultation with more team members another solution was reached.

view plain print about
1dateadd("n", 15-(minute(now()) mod 15), now())

Which does exactly the same thing! Maybe not as extensible, you can't exactly build it into a dynamic function, but considerably smaller.

 
23
F
E
B
2009

Ever needed to make a template 'sleep' for a defined period?

One of the code snippets I've had scattered around is a short java command to make the current thread sleep, for a set period.

Its handy if you want to add a set period of delay to an application, for any reason.

view plain print about
1<cfscript>
2
3thread = createObject("java", "java.lang.Thread");
4thread.sleep(javaCast("long", 5000));
5
6
</cfscript>

It creates the java thread object, and uses the sleep method to pause the thread for whatever numeric value you give it. In this example 5000ms.

 
12
A
U
G
2008

Using if and else in Cfscript

Every CF developer is intimately familiar with the code. I was recently re-writing something that was entirely CfScript based, apart from a large chunk of 'IFS' in the middle of the template. So I thought I'd re-write it into CfScript. Turns out its very easy!

[More]


This content is purely my opinon, any offence or errors are unintentional, please comment your views appropriately
Site Credits
Aggregated by ColdfusionBloggers.org Powered by Coldfusion

Technology & Science Blogs - BlogCatalog Blog Directory Blog Directory & Search engine