Posts Tagged ‘Coldfusion’

cfmodule tag duplicating data

Wednesday, May 11th, 2011

This is a quick note on using the cfmodule tag in Coldfusion. Personally, I don’t use this tag. However, I was working on an application built on the Fusebox framework. The application essentially uses the cfmodule tag to call certain fuse action templates. I was writing a new action and, following suit implemented, the call to the action via the cfmodule tag. Running the application I noticed that the content executed by the action was duplicated.

So I began plugging in some logging to determine where the duplication was beginning and try to narrow it down. I noticed essentially that my action was being executed twice, but still no idea why. When coding I have a tendency to always close out any CF tags that do not have a closing tag. So for example I write cfset tags as

<cfset variables.myVar = "" />

Notice the closing ‘/>’.

Well, I now know that you shouldn’t do this with the cfmodule tag. The reason is because it is a wrapping tag. If it does not have a closing tag it will execute the template parameter. So let’s say I have two files: ‘index.cfm’ and ‘content.cfm’. Let’s look at index.cfm.

(more…)

Using Ref Cursor with Oracle’s ThinClient JDBC driver and Coldfusion via .Net

Tuesday, May 10th, 2011

So it’s a known issue that Coldfusion does not support returning a ref cursor as an out parameter of a stored procedure when using the Oracle Thin Client drivers. Since I have experienced this problem in the past I thought I would post a solution that uses C# to manage the Oracle stored procedure and return the result set to Coldfusion. So this solution will use Coldfusion’s ability to create object instances of a C# class using the ‘cfobject’ tag or the ‘createObject()’ function. Also keep in mind that if Coldfusion is returned a type DataTable from the C# method it will automatically convert it into a Coldfusion Query object that can then be handled like normal. So with this in mind let’s take a quick look at a basic Oracle stored procedure and the C# class that will execute the procedure and return a DataTable object.

Oracle Stored Procedure

create or replace
PROCEDURE "SP_TEST_CF9"
(
      arg_ArgumentOne IN NVARCHAR2,
      l_cursor OUT sys_refcursor
)
AS 

BEGIN

      OPEN l_cursor FOR
      SELECT column_name, column_id
      FROM  column_table
      WHERE column_argument_one = arg_ArgumentOne;

END SP_TEST_CF9;

(more…)

Accessing local Coldfusion development sites from multiple devices

Sunday, May 1st, 2011

Although it’s difficult to fit the entire idea in a single title, the point of this article is explain how to setup a local Coldfusion development site that can be access by any machine or device on the local network without having to upload to a web server.

So just to be clear what I’m working with is Coldfusion 9 on Mac OSX Snow Leopard using MAMP. At a very high level configuring this setup includes a Coldfusion instance setup to use an Apache webserver and a root directory of /Users/~User/Sites/. Here ‘~User’ is whatever your machine user name is.

I’m not going to go into detail on how to set this up since there are some really good tutorials already out there:
Setting Up a Solid Local Development Environment using ColdFusion 9, Apache, and MySQL on Mac OS X
Fixing Apache connection issues between MAMP Pro and ColdFusion 9

One note of caution. If after installing MAMP and CF and you have trouble getting the Apache server to start pay particular attention to the ‘Fixing Apache connection issues between MAMP Pro and ColdFusion 9′ video. In this video he explains how to get an uncorrupted mod_jrun20.so file from the installer package. You can also get the file from an extracted wsconfig.jar file, but it appears that the file is also corrupted in this archive as well. At least it was for me during my installation attempts. Nonetheless, the mod_jrun20.so file pulled from the installation package worked perfect.

Another note of caution. The ‘Webservice Connector Utility’ that can be launched from either the CF Launcher or the wsconfig.jar would create the webserver configuration, but would not create the jrunserver.store file. If you find this to be the case for you as well you can just create this file and add ‘proxyservers=51800′ if you are using CF9 and drop it into the /runtime/lib/wsconfig/1/ directory.

(more…)

Removing the last character in a string

Tuesday, March 8th, 2011

Often when building a list of some sort, an unnecessary delimiter ends up tacked onto the end of the string.  While it’s certainly ok to keep up with the loop that is building the list, and if it is the last loop to not add the list delimiter.  However, often that creates extra code that could be eliminated if there were a simple way to just build the string, add the delimiter to the end within the loop, and once complete remove the last character from the completed list.  This can be easily accomplished with Coldfusion’s removeChars() function.  Let’s take an example of building a url with a set of parameters.

Build a URL

<cfset variables.url = "http://www.afakedomain.com?">

<cfset variables.urlObj = {} />

<cfset variables.urlObj.1 = {} />
<cfset variables.urlObj.2 = {} />
<cfset variables.urlObj.3 = {} />

<cfset variables.urlObj.1.key = "paramOne" />
<cfset variables.urlObj.1.value = "paramOneValue" />

<cfset variables.urlObj.2.key = "paramTwo" />
<cfset variables.urlObj.2.value = "paramTwoValue" />

<cfset variables.urlObj.3.key = "paramThree" />
<cfset variables.urlObj.3.value = "paramThreeValue" />

<cfloop collection="#variables.urlObj#" item="variables.i">
	<cfset variables.url = variables.url & variables.urlObj[variables.i].key & "=" & variables.urlObj[variables.i].value & "&" />
</cfloop>

As this code currently stands the resulting url will be:

http://www.afakedomain.com?paramThree=paramThreeValue&paramTwo=paramTwoValue&paramOne=paramOneValue&

Notice the ampersand at the end of the url. This is the result of creating a delimited list via a loop. Depending on how you build your list, a delimiter will always exist at the beginning or end of the completed list. In the past I’ve always handled this by checking if the loop index was the last index, and if so did not add the delimiter. However, the last time I ran into this I figured that it was time to build a better way of doing this, or dig into the docs and look for a better way.

Searching the docs yielded the ‘removeChars()’ method. The method signature takes three parameters – the string to remove characters from, where to start removing characters, and how many characters to remove. So to make this work with our URL we need to remove the last character. To do this we can add the following line of code immediately after the loop completes.

<cfset variables.url = removeChars(variables.url, len(variables.url), 1) />

Now if the variables.url var is dumped we get

http://www.afakedomain.com?paramThree=paramThreeValue&paramTwo=paramTwoValue&paramOne=paramOneValue

The ampersand (or last character) was removed. Hope this helps.

Just Finished Reading: Object-Oriented Programming in ColdFusion

Thursday, January 6th, 2011

I’m really glad to see this book (by Matt Gifford) mostly because it’s a book with a somewhat narrow focus around Coldfusion.  When I say ‘narrow’ I’m certainly not implying that CF or OOP is a narrow topic, but more or less that this is a book about Coldfusion programming that focuses its scope giving it more depth than a comprehensive study of how to program using CF.  You know, it’s not often that a new CF book rolls through my ‘Just Released’ column on my Safari library account.  So I was very pleased to see this one slide up the list.  On the other side of the token however, CF has an incredible blogging community that almost single-handedly serves as the gold mine of CF knowledge.

I’ll just quickly throw out first the negative critique to get it out of the way so no one dwells on it.  Minor at best, I noticed a number of editorial mistakes around grammar and spelling, especially towards the end of the book.  Nonetheless, as stated, trivial at best.

To be a book on programming, I found it to be an easy read with simple and cleanly explained concepts that can be pretty difficult to explain.  I particularly loved the focus on the use of components.  I myself am a 99% fan of CFCs and a 1% fan of a few custom tag wrappers that I’ve really liked.  The flow and organization of the book really worked for me.  The first three chapters are critical in that you should be able to formulate a stable self-documenting CFC that fully encapsulates its logic before learning how to use a set of CFCs in the larger scheme of an application.  Hence, the focus of the remaining four chapters.

Overall, I’m glad I took the time to read it and I definitely feel I have a more stable grounding in not only implementing an OO application, but more so understanding what it is I’m doing and why.

Setting up a read-only DSN in Coldfusion

Monday, January 3rd, 2011

In the case that you may be worried about database availability due to heavy stress on reporting queries, you may be considering data replication/streaming, if your specific database provider offers such a service (like Oracle’s Data Replication capabilities), to use for read-only reporting. This method helps ensure the availability of the database when data manipulation needs to occur. However, trying to write to a read-only database will, of course, bomb out.

Fortunately, Coldfusion offers a very easy way to guarantee that the read-only database is only used for SELECT statements. To begin with, both the writable database and the read-only database will need their own datasources set up in the CF Administrator.

(more…)

Writing recursive functions in Coldfusion

Sunday, January 2nd, 2011

A recursive function is a function that references itself. It allows a single function to use itself to continue processing. Let’s take a simple example of a struct with a nested struct as one of its values. The goal is to create a very simple function that will grab all values from the parent struct and convert them into an array. Our struct looks like the following.

Data Struct

Data struct

And in case you need it the code to create the above struct.

(more…)

Query Of Queries runtime error. The aggregate function [SUM(expression)] cannot operate on an operand of type [VARCHAR].

Tuesday, December 14th, 2010

I ran into the above error today when trying to run a SUM() function on a query retrieved from a database using the Coldfusion Query of Queries sweetness. While the error itself is fairly explanatory I couldn’t figure out where my column being summed was being typed as a string as opposed to an integer. As such I began reading up in the Coldfusion docs on how to type data columns via a Query of Queries. It turns out that it’s very doable with a Cast() function. So while not using Cast() would throw the above error, using it to cast the column data type as an Integer resolved the issue.

Won’t work while My_Total is a type string (VARCHAR)

SELECT SUM(My_String_Column) AS My_Total
FROM Some_Table

Will work while My_Total is a type string (VARCHAR)

SELECT SUM(Cast(My_String_Column AS INTEGER)) AS My_Total
FROM Some_Table

So, fairly straight-forward usage. Any column can be cast as a number of data types on the fly within the Query of Queries. Just as a side-note, I did resolve the actual issue after discovering the Cast() function. Essentially, I typed my return variable in my Oracle Function as an NVARCHAR2. Hence my value was returning as a string. Once I changed my return data type I no longer needed the Cast() function in my Query of Queries. Nonetheless, it’s pretty cool functionality provided by Coldfusion.

Using Java to compare Coldfusion objects

Monday, November 22nd, 2010

This past weekend I was building some unit tests when I came across a situation where I needed to compare two Coldfusion objects for equality. If I had a way to simply assert true the equality of two objects would make the test infinitely more simple than what it need be.

I have never really had a need to do this before and am not aware of any way in coldfusion to compare complex objects against one another. However, since CF is built on the very powerful Java libraries it didn’t take much digging to turn up the Java equals() function that accepts an object to be compared against. It essentially works with something like:

isequal = object1.equals(object2);

Pretty straight forward. Let’s look at an example illustrating how to grab this function and utilize it using Coldfusion on Coldfusion objects.

Java Equals() in Coldfusion

<!--- get the Java Comparator class --->
<cfset variables.comparator = createObject("java", "java.util.Comparator") />

<!--- build the first object --->
<cfset variables.objectOne = structNew() />
<cfset variables.objectOne.name = "matt" />
<cfset variables.objectOne.lastname = "cook" />

<!--- build the second object --->
<cfset variables.objectTwo = structNew() />
<cfset variables.objectTwo.name = "matt" />
<cfset variables.objectTwo.lastname = "cook" />

<!--- compare the objects --->
<cfset variables.isEqual = variables.objectTwo.Equals(variables.objectOne) />

<!--- output the boolean --->
<cfdump var="#variables.isEqual#" />

(more…)

Focusing on application configurability and data portability

Tuesday, November 2nd, 2010

The past half year or so of developing for a company within the boundaries of a large application that spans multiple teams and multiple languages was quite a departure from my previous few years of experience freelancing. While freelancing I happily kept within the confines of the Adobe products, and probably 95% of the time within Coldfusion. Sure I would use Javascript, HTML, and CSS, but my applications were built with Coldfusion and Coldfusion alone for the dynamic portion.

As a freelancer I built code with Coldfusion knowing it could be executed by Coldfusion. Now I have to build code that is capable of transcending development languages, as well as performing optimally in the transition. While not all code moves between the languages we work with, I have to assume that it could at some point.

Applications should be configurable to some point, and when I performed Coldfusion centric development I most likely would have built my configuration in CF. This isn’t bad if your application utilizes CF and CF alone. In fact, it is certainly the best route for performance. However, as I now know, we don’t always get to develop CF centric applications. So the question arises – how can I build a configuration and data objects that can be passed to most any language and still work?

I began to, and still primarily, use XML to build configuration settings for an application. XML is a well defined and portable format that can cross most any boundary you want it to. XML can also have transformation objects (XSLT) and schema validation (XSD) applied to it for further control. It allows the configuration options to be externalized from the core components of the application. This means that Coldfusion, .net, Java, etc. can tap into that configuration for their own use. It also means that an application does not necessarily need to be regression tested in the case that data or configuration is manipulated.

Beyond configurability, we sometimes need to maintain a log of complex data that can be read across systems. For this purpose I’ve found JSON to be most useful. It’s light-weight, super portable, and is more than capable of handling objects as character strings. Here an application built with Java can log complex objects as JSON strings. A customer-facing Coldfusion application could then parse through the logs evaluating the JSON string objects and displaying appropriately.

Lastly, using JSON or XML allows complex data to be stored in a language that is more clear than the dynamic languages. For example, I probably couldn’t just jump right into a .net object and start parsing through it and utilizing the data the same way a .net developer probably couldn’t jump into a Coldfusion object and do the same. However, it is highly likely that both the CF and .net developers both understand XML, and maybe a little less likely JSON. This means that if the object structure or data needs to change the Java, .net, or Coldfusion developer can easily make the necessary changes to the externalized XML file and assume that the processing language will be capable of handling the change.

So, just some thoughts on architecting an application in a cross-language environment, while also possibly adding externalized application configurability that certainly reduces core functionality regression testing when changes need to be made.