Java RegEx replaceAll In ColdFusion

In an odd turn, I was given text like below to display on a page.

view plain print about
1Some Title<br />
2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A.&amp;nbsp;&amp;nbsp;Some&amp;nbsp;subheader&amp;nbsp;here<br />
3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;B.&amp;nbsp;&amp;nbsp;Some&amp;nbsp;other&amp;nbsp;subheader&amp;nbsp;here<br />

Two issues here (aside from the fact that it should have been an HTML list): 1) it needed to retain the spacing format, and 2)it needed to wrap within a sized element. A non-breaking space, when viewed, appears as a space ( ), but isn't an actual space, so the browser doesn't know where to break the text when wrapping it in an element. Hmmmm....

[More]

MSOC Part 6: onApplication Event Handlers

In our last MSOC post we gave a brief introduction to the ColdFusion application framework's event handlers. With this post, we'll give a brief explanation of the onApplication handlers.

onApplicationStart

The CFC constructor is the first thing that processes on a request, but the first request to an application will then run the onApplicationStart() method. This method will run prior to any other code in the request, and is your opportunity to setup variables in the shared APPLICATION scope. Within the onApplication handlers, you are able to manipulate the APPLICATION scope without being required to lock access to the scope, as no other process can access the variables until the method is completed, and preventing race conditions.

The APPLICATION scope is a where you place those variables you will use throughout the entire application. For instance, you may wish to keep a simple variable that holds the phone number for your Customer Support team, so that if the number changes it can instantly update throughout the site. The APPLICATION scope is also where you would store objects that you would use throughout your application, like a user or page manager.

onApplicationEnd

As you might have guessed, this is the last method to fire during the lifetime of your application. In your constructor you would have set THIS.applicationTimeout. This is the length of time that the application will continue to live after it's last request. After this timeout limit is reached is when the onApplicationEnd will fire. This allows you to do some cleanup, or some application level logging. It is important to note that this only occurrs if the application does timeout. It will not fire if the server instance is shutdown/restarted.

Consider This

When you want to write to the APPLICATION scope from one of the other event handlers, you must lock access to prevent race conditions. You may, in your onServerStart method, have created a struct to log each active application (start up, last hit, timeout, number of sessions). When accessing the SERVER scope, you must also lock access.

You also have to heavily consider how much work you're putting into each of these event handlers, especially within the onApplicationStart. The more process you have there, the longer the startup time is for your application. It will be up to you just how much of a trade off that startup time is to the overall performance of your application.

The last thing you must consider (and I'll say this a lot during this series) is what variables and objects belong in what scopes. Remember that everything you place in the APPLICATION scope adds to the overall RAM utilization of that app. Everything you place in the SESSION scope will add to the overall RAM utilization of that session. This is especially important to consider in an MSOC environment. Try to think of it in these terms: the size of the SERVER scope + THE size of every APPLICATION + the size of every SESSION in every application must be less than the amount of RAM allocated to your ColdFusion instance. Careful planning is critical.

In our next piece we'll discuss the onSession event handlers. Hopefully you're finding this series beneficial. As always, any feedback/comments/questions are welcome.

Using The DataTables JQuery Plugin

For adminstrative applications, most of my readers know I'm a huge proponent of the Ext JS library. But for front-end, consumer facing sites, I'm often pushed to use JQuery. JQuery is very light weight, and wonderful for DOM manipulation, but it isn't a component library. When you want widgets for advanced data display, you have to use something like JQueryUI. Unfortunately, JQueryUI doesn't yet have a grid component (though they are working on it). So when I recently needed a dynamic, paging data grid, I started looking for something that used server-side data requests and could be skinned using the ThemeRoller. That's when I came upon the DataTables plugin.

It took me some time to figure out the works of how the plugin makes server-side requests. What I found was that, by default the plugin passes an extreme amount of data on a request, and not typically in a format very conducive for our needs. I also had to find a way to pass the method name and data returnFormat needed. That's when I discovered that I could override it's default request. Once I figured that out, I wrote a method to parse the data to create a request object more conducive to a ColdFusion Component request. It passes the following arguments along in a request:

  • iDisplayStart - The number of the first record in the requested set.
  • iDisplayLength - The number of records to return in the requested set.
  • sEcho - A DataTables request identifier, to be echoed back with the return.
  • aoSort - If present, this will be a JSON string representation of sort columns and orders. It's an array of objects:
    • colName - the column name
    • sortDir - the sort direction (asc|desc)

After getting data to my CFC, I had to build my paging query. For my example here I wanted to use the MySQL database I use for my blog, so this was a learning experience for me. The biggest trick for me was getting the TotalCount of records, as this is extremely different from MS SQL, requiring two separate SQL statements for the query and the count. Since DataTables can also sort off of different columns, I needed a way to dynamically set the ORDER BY clause of the query. You can't bind parameters to the ORDER BY clause, but you want to protect your server from SQL injection attack, so you have to validate that part of the request (especially as it's an ajax request, which would be easier to manipulate). Pete Frietag came up with a little regex expression that could be used in this case.

We set up our component to return a structure in the following format:

  • success - A boolean to denote success or failure of the request.
  • message - Only returned if the request fails, a message to state why the failure occurred.
  • totalCount - The total number of records available for the filters applied.
  • result - The paging query.

The last piece of the puzzle was back on the client-side again, where the ColdFusion return had to be put back into a format that can be consumed by the DataTables plugin. This was actually very easy because of the way that ColdFusion returns query data.

Once I had these methods, I wanted to find out how to write a feature plugin for DataTables. One where I could identify additional config arguments in DataTables, and have it automatically work. I contacted Allan Jardine, who wrote DataTables (and has some great web dev tools on his site). He never wrote in that capability, saying that the method override was the only way to make this happen. What I did discover was that I could add options to the standard DataTables config. I created a new option for DataTables, oCFReaderDT, which takes an object of options. Only one argument is required, "method", to define the method to call in the CFC request. I also setup the processor to accept an option, "sForm", as a string selector of a form whose values you may need in the request (i.e.: 'form#myForm'). Then I wrote a custom function that encapsulated the previously written methods into one method, that could then be used as the value of the "fnServerData" option in the DataTables configuration object.

In the download link below is a zip file with all of the files for the example, which has been heavily commented so you know what's going on. Though written for ColdFusion 9, I have included both scripted and non-scripted CFC's. I hope you find this useful, and please leave any comments/questions/suggestions through the Comment Form or Contact links below.

MSOC Part 5: Application Event Handlers, An Intro

Handling the "big picture" application flow in ColdFusion is extremely easy, as a large part of it occurs directly in your application's Application.cfc file. Getting a handle on what's going on, in an MSOC app or any other app, is a matter of managing that application flow effectively. If you have a solid understanding of the event handlers available to you, then you can really begin to see the power behind ColdFusion's application framework.

[More]

MSOC Part 4: Application Constructor

OK, we're still working through setting up our Application for a Multi Site One Codebase scenario, and we haven't really gotten into the application yet (aside from dynamically setting the application name). Now is where we really have to take some serious consideration to variable scoping and application workflow. Thing is, there are dozens of ways to skin this cat (no, the view layer stuff comes later). The things we'll talk about in these next several posts aren't MSOC specific either, as they can apply to most any application. The first step is getting a hard handle on what you can do within the Application.cfc, and start to use it as intended.

[More]