Introducing ColdFusion 9
Introduction
For close to a decade and a half, ColdFusion has constantly raised the application development productivity bar with each new release. This does not happen by accident. In fact, in planning each new version, the ColdFusion product team meets with numerous customers and partners to ascertain exactly how the product is used, so as to be able to focus on improvements that have real value for users. This is how innovative features like reporting and event gateways and server monitoring become a reality.
And ColdFusion 9 is no exception. This new ColdFusion boasts an impressive array of new and enhanced features, all intended to improve productivity for developers, IT administrators, and business decision makers alike. In fact, to cover all of ColdFusion 9 you’d need a book (or perhaps 3 books!). But, in this article I’d like to introduce you to some of what’s new and exciting in ColdFusion 9.
ColdFusion Functionality Exposed As Services
Have you ever stopped to think about just how much functionality is baked into ColdFusion? We use <CFQUERY> to work with databases, allowing for highly flexible and dynamic SQL as well as query caching and more, and this is powered by a sophisticated internal engine. We use tags like <CFCHART> and expect charts to be generated and displayed, without every really paying attention to the fact that an entire Java based charting engine is built in and actually doing the heavy lifting. The same is true for <CFPDF>, <CFIMAGE>, <CFSEARCH>, support for SOAP and Web Services, XMPP and JMS integration, and so much more. While most of us focus on CFML the language, the truth is that the bulk of ColdFusion, the majority of what gets installed, is not the language but the extensive array of integrated services, services that are exposed to ColdFusion via CFML tags and functions.
But what if these services could be accessed outside of ColdFusion? If a PHP developer in the next cube over needed to merge PDF files, why couldn't he invoke ColdFusion's PDF manipulation services? If a .NET developer needed to access Microsoft Exchange, why couldn't she use ColdFusion's brilliant Exchange tags (rather than having to write lots and lots of .NET code, and I do mean lots and lots)? What about the Java developer who needs to easily manipulate spreadsheet files without tinkering with low level libraries?
And while we're at it, what about the Flex developer who needs to generate an e-mail message? Flex (well, Flash) has no built in SMTP libraries, and so Flex developers who need to programmatically generate e-mail messages do so by writing code on the server. For ColdFusion developers this means creating a ColdFusion Component which accepts data from a Flex application (likely via an AMF call) and then passes that same data to a <CFMAIL> tag. In other words, code is being written on the server just to be able to pass data from Flex on the client to the <CFMAIL> tag. So why couldn't a Flex developer just invoke <CFMAIL> directly, passing it name=value pairs so it can generate an e-mail?
Well, with the upcoming ColdFusion 9, the answer to all of these questions is yes, these are all doable! In ColdFusion 9 we're exposing lots of those integrated ColdFusion services via AMF (Flash Remoting) and SOAP (Web Services). The PHP, .NET, and Java developers can invoke ColdFusion built-in Web Services, pass in data, and get back results. And the Flex developer can include a ColdFusion SWC file exposing ActionScript classes and MXML tags via simple abstracted AMF calls. Simply include the SWC in your Flash Builder project, define the ColdFusion name space like this:
<mx:Application xmlns:cf="coldfusion.service.mxml.*">
and you'll have access to CFML tags within your Flex project. For example, to send an e-mail you could use the following:
<cf:Mail id="cfMail" to="{to.text}" from="{from.text}" subject="{subject.text}" subject="{subject.text}" content="{body.text}" type="html" />
The above code creates an instance of the Mail object and names it "cfMail", and sets to, from, subject, etc., with the values of other Flex objects. To actually send the mail all you'd need is to invoke the following (possibly when a Send button is clicked):
cfMail.execute();
There is much more to this "ColdFusion as a Service" functionality, including lots more services exposed, and a sophisticated security model.
But the bottom line is that ColdFusion is now poised to become even more valuable to Flex and AIR developers, and now even of value to developers using other platforms and languages.
Working With Spreadsheets In ColdFusion
Spreadsheets are key to just about all businesses and organizations, and ColdFusion developers have long sought a way to access and manipulate spreadsheet data programmatically. The truth is, ColdFusion has supported spreadsheet access for a while in a variety of ways, it's been possible to access Excel files from an ODBC driver, and it's been possible to generate these files using CFReport as well as by generating HTML or CSV content and then setting the appropriate MIME type to force the browser to display the data using Excel.
But ColdFusion developers have been asking for more, greater and more control, and the ability to read and write specific parts of spreadsheet files. And as I demoed in my usergroup presentations last week, this is indeed planned for ColdFusion 9.
Just like we did with the ColdFusion image manipulation functionality, spreadsheets in ColdFusion are manipulated using a tag or functions, or some combination thereof. The <CFSPREADSHEET> tag is used to read a sheet from a spreadsheet file (as a spreadsheet object, a query, a CSV string, or HTML), write sheets to XLS files, and add sheets to XLS files. The over 30 supporting functions like SpreadsheetSetCellValue(), SpreadsheetAddRow(), and SpreadsheetSetCellFormula() allow for more granular spreadsheet manipulation, and can be used in conjunction with supporting functions like SpreadsheetNew() to create a new spreadsheet object, and SpreadsheetInfo() which returns title, subject, sheet names, last saved date and time, and more.
Here are some of the examples I used at my presentations. This first example reads an entire spreadsheet as a query and dumps the contents:
<!--- Read spreadsheet ---> <cfspreadsheet action="read" src="Sales.xls" query="myQuery"> <cfdump var="#myQuery#">
<CFSPREADSHEET> is also used to write (or overwrite) a spreadsheet, as seen here:
<!--- Write spreadsheet ---> <cfspreadsheet action="write" overwrite="true" filename="Sales.xls" name="sObj" />
To update a specific cell, you need to read, update, and save, like this:
<!--- Read spreadsheet ---> <cfspreadsheet action="read" src="Sales.xls" name="sObj" /> <!--- Set cell value ---> <cfset spreadsheetSetCellValue(sObj, FORM.sales, FORM.row, FORM.col)> <!--- Write spreadsheet ---> <cfspreadsheet action="write" overwrite="true" filename="Sales.xls" name="sObj" />
<cfspreadsheet> and its 30+ supporting functions can do lots more, but this should give you a taste of just what's possible using this innovative new feature.
ORM – Rethinking Database Integration
Database integration is a hallmark of ColdFusion applications. Indeed, the <cfquery> tag was one of the first added to the language, and to this day remains ones of the most used. There is an inherent simplicity and flexibility to being able to create database queries on the fly, so as to be able to refer to column names to work with results. There's also a real downside to this type of database integration. After all, consider what would happen to your code if a table name changed, or if columns were refactored and split, or if whole schemas were updated. Scary, huh? The downside of how most of us integrate databases into our ColdFusion apps is that we tend to write database specific code even at the client level, the code generating output or working with form fields, for example. In addition, we inevitably must write DBMS specific SQL, and that DBMS specific code needs to be managed by our applications.
Before I go any further, I must point out that <cfquery> is not going away, and it remains a very simple and powerful database integration option. For many of us, and for many apps, is, and remains, highly appropriate. But, having said that, in ColdFusion 9 we're adding support for a newer way to think about database integration, leveraging Object Relational Mapping (ORM). ColdFusion's ORM support is built on Hibernate, the premier ORM implementation in Java development, and in fact, Hibernate is built right into the next version of ColdFusion, and exposed via ColdFusion Components and language elements.
ColdFusion's ORM support requires extensive coverage, and are way beyond the scope of this article, but here are the basics. In ORM, you never write SQL statements, and never really consider tables of rows and columns, at least not when writing client code. Instead, developers using ORM work with objects. For example, a table containing books with columns named title and author and ISBN, would have a corresponding object with the same properties. When using ORM, instead of retrieving a row from a table, you'd retrieve a book object (which is automatically populated by the contents of the table row). To retrieve all rows you'd not use a SELECT * FROM Books, instead you'd use Entity functions to request an array of book objects, populated and ready to use. And then instead of referring to column names in your code, you'd use Get methods in the returned objects. This is actually far less confusing than it sounds, so let's look at an example. First the Application.cfc:
<cfcomponent> <cfset this.ormenabled=true> <cfset this.datasource="cfbookclub"> </cfcomponent>
this.datasource is used to define the datasource to be used, and then this.ormenabled is set to TRUE to turn on ORM support.
Next we'll need an object that represents the table to be used, and in ColdFusion objects are implemented as ColdFusion Components (if you are working with multiple tables you'd have multiple CFCs, one for each table). Here's Books.cfc which maps to the Books table in the specified datasource:
<cfcomponent persistent="true"> <cfproperty name="BOOKID" column="BOOKID" datatype="integer" length="10" /> <cfproperty name="AUTHORID" column="AUTHORID" datatype="integer" length="10" /> <cfproperty name="TITLE" column="TITLE" datatype="string" length="255" /> <cfproperty name="BOOKDESCRIPTION" column="BOOKDESCRIPTION" datatype="clob" length="2147483647" /> <cfproperty name="BOOKIMAGE" column="BOOKIMAGE" datatype="string" length="50" /> <cfproperty name="THUMBNAILIMAGE" column="THUMBNAILIMAGE" datatype="string" length="50" /> <cfproperty name="ISSPOTLIGHT" column="ISSPOTLIGHT" datatype="character" length="1" /> <cfproperty name="GENRE" column="GENRE" datatype="string" length="50" /> </cfcomponent>
Notice that there are no methods (functions) in this CFC, there are only <cfproperty> tags that correspond to the table columns. And actually, the <cfproperty> tags are optional, and if omitted ColdFusion will read the database table to implicitly define the properties for you! Also, note that the CFC is named Books.cfc, so it maps to the Books table. if the table name changed, or if you needed to use a different CFC name, you could use the optional <cfcomponent> table attribute to specify the table name to use.
Oh, and it's worth noting that new ColdFusion Builder (more on that in a moment) comes with wizards to generate these table CFCs for you.
And finally, here's how to retrieve the Books data. First we'll retrieve all books (this is equivalent to a SELECT *, but instead of returning a query, an array of Books objects is returned):
<!--- Get data ---> <cfset data=EntityLoad("Books")> <!--- Display titles ---> <cfloop from="1" to="#ArrayLen(data)#" index="i"> <cfoutput>#data[i].GetTitle()#<br></cfoutput> </cfloop>
Of course, sorting and filtering and more are all supported. For example, to retrieve books with an author id of 10 (equivalent to a SQL WHERE clause) you could use the following:
<!--- Get data ---> <cfset data=EntityLoad("Books", {authorid=10})>
Notice that to access the Title property, a GetTitle() method is used. This is a getter, and it is automatically created by ColdFusion. There are also setters, used to set properties within an object. To save a new or updated object you'd simple do the following:
<cfset EntitySave(data)>
This would work for an insert or an update, just save the object and ColdFusion and Hibernate figure out whether to update an existing row or insert a new one.
And we've even made it easier to bridge between using ORM and workign with queries (for example, to be able to use , and more), just use the EntityToQuery() function to convert basic arrays of entity objects to familiar ColdFusion queries.
There's a lot more to ORM and ColdFusion's Hibernate integration. And ColdFusion developers get access to all of Hibernate, be it support for relational tables, lazy loading, caching, query optimization (by tweaking HQL directly), controlling all ORM settings, and much more, too. ColdFusion 9 gives you the power of Hibernate with the productivity that is uniquely ColdFusion.
The Big News: ColdFusion Gets An IDE
If all of what we’ve looked at thus far were not enough, the next “feature” is probably the one that most excites ColdFusion developers. ColdFusion is getting its very own IDE, ColdFusion Builder. This new tool really needs an entire article unto itself, but here is a quick list of the important points to note:
- ColdFusion Builder is built on Eclipse, the same platform which powers Flash Builder, and is actually designed to be installed along with Flash Builder for seamless integration.
- ColdFusion Builder can work with local or remote servers, and can also stop / start / restart / manager / monitor those servers.
- ColdFusion Builder features rich code coloring, context sensitive help, tag attribute help, and more.
- ColdFusion Builder features toolbars with shortcuts for things like wrapping a block of code in<cfoutput> and more.
- RDS is integrated, as is a debugger, a log file viewer, and more.
- ColdFusion Builder has an integrated extensibility model which allows extensions to be written in CFML (instead of Java, the way Eclipse extensions are usually written). And numerous ColdFusion Builder extensions have already been posted to http://riaforge.org/.
- ColdFusion Builder has been a long time coming, and we have big plans for how to further enhance it in the future.
Where to go from here
There’s a lot more to ColdFusion 9 beyond what I’ve covered here. From SharePoint and portal server integration, to important language enhancements, to support for offline AIR applications, and more, ColdFusion 9 is an extensive release chock full of powerful and useful features intended to make you more productive than ever before.
If you want to learn more about ColdFusion 9 and ColdFusion Builder, your best bet is to visit the Adobe ColdFusion page at http://www.coldfusion.com/.
Ben Forta is Adobe Systems Inc.'s Director of Platform Evangelism, and is one of the best known and most trusted names in the ColdFusion community. Ben is the author of numerous books on ColdFusion, SQL, Regular Expressions, and related technologies, and 1/2 million Ben Forta books have been printed in more than a dozen languages worldwide. He is currently working on updating his bestselling series of books: ColdFusion 9 Web Application Construction Kit (Peachpit Press).