I don't know about anyone else, but wanting to create little ad hoc Java classes in my CF apps in a common occurrence for me. With my CF Groovy project, it's both possible and very easy. No Java, no compiling, no classloading hacks.
Here's a simple example. I'm going to create an array of structs and then sort the array based on a specific field value (the 'date' field) of the structs. Doing this in CF usually means making a lookup struct, sorting that, and then rebuilding the array (but only if you have unique values), or converting to a query and using QofQ to do it. Both have serious drawbacks, as well as being very circuitous/obscuring.
First the array construction (six items, each with 'letter' and 'date' fields, holding what you'd expect). Nothing very interesting here.
<cfscript> a = []; month = createDate(year(now()), month(now()), 1); for (i = 1; i LTE 6; i = i + 1) { s = { letter = chr(65 + randRange(0, 25)), date = dateAdd("d", randRange(0, 30), month) }; arrayAppend(a, s); } </cfscript>
Now, using the <g:script> tag from CF Groovy, we'll sort it by date using a java.util.Comparator:
<g:script> Collections.sort(variables.a, {o1, o2 -> o1.date.compareTo(o2.date) } as Comparator) </g:script>
If you want to compare based on the letter, just change the two ".date" references to ".letter". You can, of course, make the comparator as complicated as you'd like, including referencing other Groovy classes through CF Groovy's classloading, or other Java classes on your classpath.
You can see it in action at the demo page, or get the full source from Subversion at https://ssl.barneyb.com/svn/barneyb/cfgroovy/trunk/demo/.
Hello dear Barney !
I for one have very much interest in Your "CF with Groovy" efforts :-) ! When will You make it publicly available with enough documentation to get us all up to speed :-) ?
Until now I have been a ColdFusion purist; but recently I have come to think that leveraging Java – e.g. for the domain modeling – could be in one's best interest …
So one has several choices:
- Leaving the ColdFusion camp completely and perhaps enter the Grails/Groovy community
- Still use ColdFusion where it is appropriate and leverage Your "CF with Groovy" micro framework where it is appropriate
The questions are then:
- What can ColdFusion still do better than Grails/Groovy ? The following items come to mind immediately: Event gateways; CFCs which can be configured to return JSON, XML, or web services; existing CFC functionality; tags like cfoutput …
- Which CF framework do You recommend for leveraging Your micro framework "CF with Groovy" to its fullest potential ?
- As somebody who has NOT been in the ColdFusion/Java/Grails/Groovy trenches himself, it may be difficult to judge in which situation to leverage which approach; so yes: some guidelines/explanations with regard to that complex topic could be valuable, too :-) !
I am looking forward to "CF with Groovy" :-) !
Best Regards and Tschüss
Kai
Kai,
Are volunteering to write up some docs? There's not really much to document. Just check out the code from Subversion and read the comment at the top of groovyEngine/script.cfm. You can optionally read the comment atop groovyEngine/setPath.cfm for more advanced usage.
CF is a templating language that has had some OO-like stuff cobbled on. Groovy is an OO language that doesn't really do templating (though it does have a primitive templating framework). CF is good at the view, Groovy is good at the back end.
I'm planning to add Groovy object support to ColdSpring, so you can transparently use CF Groovy to load Groovy classes with ColdSpring. But that's for more advanced usage. If you just want to embed Groovy scriptlets, it's completely framework agnostic.
Like so many other things, necessity is the mother of invention. If you don't know why you'd want to use Groovy (or really why you wouldn't want to use CF), you probably don't have a need to use it. If CF is sufficient for the task at hand, CF's likely the right choice.
[...] Contact « Comparators in CF with Groovy [...]
[...] and java.util.Map respectively, greatly easing Java integration. This also allows the Comparators CF Groovy example I published to work correctly on [...]
Awesome!! Thanks this was a great help in sorting lists in my grails app!! Thanks again.
Thanks for the example!