Search This Blog

Friday, May 31, 2013

ColdFusion: Working with JSON

JSON has been gaining popularity to represent data in JavaScript applications. Below is an example of JSON data with some array and a ColdFusion parse function to convert JSON data to ColdFusion structure.

<cfsavecontent variable="personnel_record">
{
 "firstname": "John",
 "lastname": "Smith",
 "age": 36,
 "address": {
  "number": "12345",
  "street": "Any Street",
  "city": "Any City",
  "state": "AA",
  "zip": "90876"
 },
 "children": [
  {
   "firstname": "Ian",
   "age": 7,
   "married": false
  },
  {
   "firstname": "Heather",
   "age": 6,
   "married": false
  }
 ]
}
</cfsavecontent>

<cfdump var="#deserializeJSON(personnel_record)#" />

Wednesday, May 29, 2013

ColdFusion: Reduce/Suppress Whitespaces

Without implementing proper technique, by default ColdFusion generates unnecessary whitespaces on result page. This will cause the page size bigger and consequently it will take more time to load the page. To suppress the whitespaces, we can enable whitespace management in CF Admin and/or do one of the following techniques:

<cfsilent>...</cfsilent>
 Absolutely no output. Not even <cfoutput> nor <cfdump>.
 Require closing tag </cfsilent>.
 
output="false"
 One of the attributes in <cfcomponent> and <cffunction>.
 It works like <cfsilent>.
 
<cfsetting enablecfoutputonly="true">
 Only output things that are wrapped with <cfoutput>. 
 It works with <cfdump>.
 The effect goes to <cfinclude> as well.
 Matching tag, <cfsetting enablecfoutputonly="false"> is highly recommended.
 Closing tag </cfsetting> is optional and does not have any effect.
 Multiple declarations behave like a counter. 

<cfprocessingdirective suppresswhitespace="true">...</cfprocessingdirective >
 It reduces whitespaces, but it is not as effective as <cfsilent> or <cfsetting>.
 <cfoutput> is not required.
 Require closing tag </cfprocessingdirective>.

<!--- BONUS --->
<cfsetting showdebugoutput="false">
 Disable debugging mode when Debugging Settings in CF Admin is enabled.

SEE ALSO:
ColdFusion Whitespace Options

Thursday, January 3, 2013

Service-Oriented Architecture (SOA)

I recently found a well-written article about Service-Oriented Architecture (SOA) from Microsoft. Although it is not directly related to ColdFusion, the article gives a nice guidance on how to architect applications to deal with service-based collaboration. 

Here's the link: http://msdn.microsoft.com/en-us/library/ff648490.aspx

Friday, September 21, 2012

ColdFusion: Taking Advantage of Java Classes

ColdFusion is built on top of Java; and it has plumbing to access Java classes. This is pretty cool. That means we can extend ColdFusion's capability.

Here is an example on how to utilize Java's Stack class. What we need to do is to create an object from Java class in question. Here, we created a Stack. As you can see, we can immediately call and utilize its methods.

<cfset objStack = CreateObject("java","java.util.Stack").init() />
<cfdump var="#objStack#" />

<cfdump var="#objStack.empty()#" />
<cfset objStack.push("a") />
<cfset objStack.push("b") />
<cfset objStack.push("c") />
<cfdump var="#objStack.empty()#" />

<cfdump var="#objStack.peek()#" />

<!--- The top element is always one --->
<cfdump var="#objStack.search('c')#" />

NOTE: Often, when we work with electronic payments, the third-party may require us to use certain algorithm for encryption. In this case, Java has more options to choose from than the ones already built in ColdFusion. So, take advantage of Java classes.

Reference:

Monday, September 10, 2012

ColdFusion: Scale Attribute in <cfprocparam>

Want to insert a numerical value that contains decimal using <cfprocparam>, but the decimal portion is not showed up in the database? Most likely, we forgot to specify scale attribute. scale attribute is used to specify number of decimal places. If not specified, the default value is 0.

<cfstoredproc procedure="spuInsertBoxLength" datasource="#myDataSource#">
    <cfprocparam cfsqltype="CF_SQL_FLOAT" scale="2" value="123.45" />
</cfstoredproc>
Reference:

Thursday, August 23, 2012

ColdFusion: Converting Records From Excel to Query

Have you ever needed to get data from Excel to ColdFusion to be manipulated? Before ColdFusion 9, reading records from Excel was cumbersome. <cfspreadsheet> makes the task handy. You can convert the records to spreadsheet object, query, CSV, or even HTML.

Here is an example on how to read an Excel spreadsheet and convert its rows to ColdFusion query.

<cfspreadsheet  
    action = "read" 
    src = "spreadsheet.xls" 
    sheet = "1"
    excludeHeaderRow = "true" 
    headerrow = "1" 
    query = "queryName"
    rows = "2-10" />
 
<cfdump var="#queryName#" />

Attribute rows is useful during development. Instead of reading all records in once, you can just output several records for testing/debugging. For more information on other attributes, please see the reference below.

Reference:

Wednesday, May 2, 2012

ColdFusion: returnCode Attribute in <cfstoredproc>

Have you ever wondered how to use returnCode attribute in <cfstoredproc>? If returnCode is set to true, it will return a status code from SQL RETURN statement. The status code could be an SQL error number, number of affected rows, or any integer number that is meaningful for you.

There are two ways to retrieve status code:

  • cfstoredproc.statusCode
  • result_var.statusCode --result_var is defined in result attribute
Even though both ways will generate the same result, it is better to use result attribute because if you dump the variable, the structure will give you an extra info. about caching.

Tip: Even though you do not have return code in your stored proc., turning on returnCode will not hurt. Later you can always go back to your stored proc. to add return code.

-- This stored proc. demonstrates how we could use RETURN statement.
CREATE PROCEDURE spu_customer_update
 @customer_id INT = 0
 , @email  VARCHAR(100) = ''
AS
BEGIN
 SET NOCOUNT ON;
 
 DECLARE @updated_rowcount INT = 0;
 
 IF @customer_id <> 0 AND @email <> ''
 BEGIN
  UPDATE customer
  SET email = @email
  WHERE customer_id = @customer_id;
  
  -- @@ROWCOUNT returns the number of rows affected by the last statement.
  -- If the UPDATE statement success, @@ROWCOUNT will be 1.
  SET @updated_rowcount = @@ROWCOUNT;
 END;
 
 RETURN @updated_rowcount;
END;
<cfstoredproc 
  procedure="spu_customer_update" 
  datasource="mydatabase"
  result="proc_result"
  returnCode="true">
 <cfprocparam cfsqltype="cf_sql_integer" value="12345" />
 <cfprocparam cfsqltype="cf_sql_varchar" value="myemail@email.com" />
 
</cfstoredproc>

<cfdump var="#proc_result#" />

References: