Posts Tagged ‘Coldfusion 8’

Setting print margins using Eclipse to maintain line character length

Friday, May 14th, 2010

Sometimes you may have standard line lengths with which to work when writing code. If this is the case and your using Coldfusion and Eclipse with the cfEclipse plugin you can maintain the character length consistency by setting up the print margins to n character length.

Point being that when running the cfeclipse plugin you need to choose CFEclipse > Editor in the preferences left navigation. In the options you will check ‘Show print margin’, set the ‘Print margin column’ text field to the desired character length, and if you choose the ‘Print margin’ option in the ‘Appearance color options’ select box. Then choose the color you wish the margin to be. This will set a line length that will show in the code editor pane. Of course it will not wrap the line. It is still your duty as a developer to wrap appropriately. Nonetheless it does give you a guide for where the line should wrap at least.

If you’re writing Java then you would select General > Editor > Text Editors in the preferences pane.  Although the display is slightly different, generally follow the same steps as above for showing the print margin.

If you’re using Coldfusion Builder you will follow the same exact directions as listed just above for showing the print margins writing Java.

Lastly, if you’re using Flex Builder 3 you will again follow the same directions as above for showing print margins writing Java.

Connecting to the MailChimp API with Coldfusion

Sunday, April 25th, 2010

I have just started playing with the MailChimp API with Coldfusion. As usual, there’s not a lot of Coldfusion documentation or examples. So I thought I’d start posting some of the how-to’s as I myself figure them out. Here will demonstrate how to make a simple connection with the API so you can start doing more productive things like adding emails to lists and getting stats.

Let’s begin at the beginning of using any API. We need an account and an API key. Go ahead and create an account as if you’re a client, or if you are doing this for an actual client go ahead and create an account for them. MailChimp makes this super simple. Once you have an account click on the ‘account’ button in the top left corner of the dashboard. This will take you to a set of links. Click the ‘API Keys & Info’ link. This will take you to a page where you can manage your API keys. Clicking ‘Add A Key’ will generate a new key for you to use.

Now that we have our key and account we’re ready to make our connection via Coldfusion. Before looking at any code, let’s make sure we know where our documentation is at. You can find the MailChimp docs here. Requesting data from MailChimp requires you to pass the method and parameters via the url: http://us1.api.mailchimp.com/1.2/. As parameters you need to indicate the type of output MailChimp should return data as. In our case we’ll use JSON. So we’ll attach ‘?output=json’. Next we need to specify the method. MailChimp has a ping() method meant for no other reason than to make sure the connection is working. So this is what we’ll use here. To do this we attach the method parameter ‘&method=ping’. If you look at the docs you’ll see that this requires only one parameter to work, which is the API key (this is required for all methods). So we’ll attach another parameter ‘&apikey=[yourapikey]‘.

So let’s look at how to write this using Coldfusion.

<!--- SET URL --->
<cfset VARIABLES.api_url = "http://us1.api.mailchimp.com/1.2/?output=json&method=ping&apikey=[your key]-us1">

<!--- SEND DATA VIA CFHTTP TAG --->
<cfhttp name="call" url="#VARIABLES.api_url#" result="VARIABLES.mycall">

<!--- BECAUSE RETURNED AS JSON WE NEED TO DESERIALIZE THE RESPONSE --->
<cfset VARIABLES.deserialized = deserializeJSON(VARIABLES.mycall.fileContent)>

<!--- DUMP RESPONSE --->
<cfdump var="#VARIABLES.deserialized#">

First we set our URL. In this case you’ll, of course, change the ‘[your key]‘ to whatever your actual key is that MailChimp generated for you in your account. Next we use the Coldfusion tag to send the url to MailChimp. MailChimp then processes it and sends back the result which we store as ‘VARIABLES.mycall’. Because we specified to MailChimp to return data back to us as JSON we need to make sure we deserialize it before displaying it. We do this using the ‘deserializeJSON()’ method. Also the response is passed back as a struct with the actual data from MailChimp stored in the ‘fileContent’ key. Lastly we just dump the data so we can see that ‘Everything’s Chimpy!’.

At this point you should be able to start playing more with the API to do more complex and practical things.

Using Coldfusion to specify which pages to run under an SSL

Sunday, April 25th, 2010

I recently had a site that ran under an SSL certificate. As this was the case each page of the site ran under the ‘https://’ protocol. A few weeks ago the client noticed that when run in Internet Explorer they were receiving a message stating that not all of the data was being passed via the SSL. It would ask if the user wished to load in the insecure data as well as the secure. If the user chose yes everything loaded as normal. If they chose no everything except AJAX data would load onto the page.

While this makes sense I’m not sure why it just started happening. Nonetheless, I noted that it was not necessary to run the entire site under the SSL. Since I, of course, do not use AJAX anywhere in the checkout process, I decided that the simple solution was to run the SSL only under the checkout. This would allow the rest of the site to run under the ‘http://’ protocol without any security prompts concerning AJAX from Internet Explorer. So let’s take a look at how to run the SSL under only select pages in your site.

<!--- ***************  MAKE SURE THE SSL IS IN PLACE  *************** --->
<cfset var storepath = "http://www.yourdomain.com/checkout/">
<cfset var url = "http://" & CGI.HTTP_HOST & CGI.SCRIPT_NAME>
<cfset var isinstore = findNoCase(storepath, url)>

<!--- IF FOUND CHANGE TO SECURE PROTOCOL --->
<cfif isinstore neq 0 AND CGI.SERVER_PORT neq "443">
	<!--- SET TO SECURE PROTOCOL --->
	<cfset url = "https://" & CGI.HTTP_HOST & CGI.SCRIPT_NAME>

	<!--- SET QUERY STRING IF NECESSARY --->
	<cfif CGI.QUERY_STRING neq "">
		<cfset url = url & "?" & CGI.QUERY_STRING>
	</cfif>

	<!--- REDIRECT USER --->
	<cflocation url="#url#" addtoken="false">
</cfif>

This snippet of code is run under the onRequestStart() method in the Application.cfc. This allows the program to check the url with each request that is made. Some pre-planning is required to choose where the SSL is run without just doing a page by page check. In this case we have made sure that we have placed all of our secure pages under a single directory. In this case we are wanting to secure our ‘checkout’ directory. If we are under this directory we also want to make sure that we are not running on the secure port 443. If not we can take the current url, run the secure protocol at the beginning, attach the query string if necessary, and then redirect the user to the secure page.

With each request Coldfusion checks if the page falls under the ‘checkout’ directory and if so it redirects to itself using the ‘https://’ protocol. For this protocol to work, it of course assumes that you have set up the SSL in IIS or whatever you use. I also recommend going into the site and changing any absolute paths linking to any of the pages running under your secure directory to use the secure protocol by default.

Stripping out HTML tags from text with Coldfusion 9

Wednesday, April 7th, 2010

I’ve built a fairly simple UDF for stripping out HTML from a chunk of text. I’ll quickly explain my logic and what this UDF can do.

First, there are two types of tags in HTML. One is a wrapping tag. An example of this is the bold tag.

<b>Some content</b>

Next is a self-contained tag.

<img src="my/path/image.jpg" />

If the tag is a wrapping tag we need to make the decision of whether or not to delete the content the tag is wrapping. For example the content inside a bold tag we will most likely want to keep. While the content inside a script tag we will want to delete.

When it comes right down to it, we need to make three decisions about the HTML being deleted.

  1. What tag needs to be deleted.
  2. Is this a wrapping tag.
  3. If a wrapping tag, should the content be deleted.

So let’s take a look at the UDF.

<cfcomponent displayname="Strip HTML" hint="Strips out of a set of given text any specified html tags." output="false">

	<!--- ************************  STRIP HTML  ************************ --->
	<cffunction name="stripHtml" displayname="Strip HTML" description="Strips out specified HTML tags." access="public" output="false" returntype="struct">

		<!--- ARGUMENTS --->
		<cfargument name="text" displayName="Text" type="string" hint="Text to strip out html tags from." required="true" />
		<cfargument name="tags" displayName="Tags" type="string" hint="Tags to be striped from the text.  Ex. '[string:tag name],[what to remove - {string:tag | string:content}],[is it a wrapping tag? {boolean}]'. Tags are delimited with semi-colons." required="true" />

		<!--- SET SOME LOCAL VARS --->
		<cfset var textbytes = "">
		<cfset var counter = 1>
		<cfset var delete = false>
		<cfset var temp = "">
		<cfset var tagtoberemoved = "">
		<cfset var whatgetsremoved = "">
		<cfset var wrappingtag = "">

		<!--- BUILD STRUCT --->
		<cfset var data = structNew()>
		<cfset data.success = true>
		<cfset data.message = "">
		<cfset data.orginaltext = ARGUMENTS.text>
		<cfset data.strippedtext = ARGUMENTS.text>

		<!--- CHECK IF ALL CONTENT SHOULD BE REMOVED --->
		<cfif ARGUMENTS.tags eq "all">
			<!--- REMOVE HTML TAGS --->
			<cfset data.strippedtext = rereplaceNoCase(ARGUMENTS.text, "<[^>]*>", "", "all")>
		<cfelse>
			<!--- LOOP OVER THE LIST OF TAGS TO BE REMOVED --->
			<cfloop list="#ARGUMENTS.tags#" index="VARIABLES.i" delimiters=";">
				<!--- SET ATTRIBUTES OF TAG TO BE DELETED --->
				<cfset tagtoberemoved = listFirst(VARIABLES.i, ",")>
				<cfset whatgetsremoved = listGetAt(VARIABLES.i, 2, ",")>
				<cfset wrappingtag = listLast(VARIABLES.i, ",")>

				<!--- IF REMOVING JUST THE TAG --->
				<cfif whatgetsremoved eq "tag">
					<!--- CHECK IF IT IS A WRAPPING TAG --->
					<cfif wrappingtag eq true>
						<!--- REMOVE WRAPPING TAG, BUT NOT THE CONTENT --->
						<cfset data.strippedtext = rereplaceNoCase(data.strippedtext, "<#tagtoberemoved#>", "", "all")>
						<cfset data.strippedtext = rereplaceNoCase(data.strippedtext, "</#tagtoberemoved#>", "", "all")>
					<cfelse>
						<!--- REMOVE CONTAINED TAG --->
						<cfset data.strippedtext = rereplaceNoCase(data.strippedtext, "<#tagtoberemoved# />", "", "all")>
					</cfif>

				<!--- IF REMOVING TAG AND CONTENT --->
				<cfelseif whatgetsremoved eq "content">
					<!--- CHECK IF IT IS A WRAPPING TAG --->
					<cfif wrappingtag eq true>
						<!--- REMOVE THE TAG AND CONTENT --->
						<cfset data.strippedtext = rereplaceNoCase(data.strippedtext, "<#tagtoberemoved#>.*</#tagtoberemoved#>", "", "all")>
					<cfelse>
						<!--- REMOVE CONTAINED TAG --->
						<cfset data.strippedtext = rereplaceNoCase(data.strippedtext, "<#tagtoberemoved# />", "", "all")>
					</cfif>
				</cfif>
			</cfloop>
		</cfif>

		<!--- RETURN STRUCT --->
		<cfreturn data>

	</cffunction>

</cfcomponent>

The method takes two arguments. First is the chunk of text to have HTML stripped out of. Second is information about the HTML to strip out of the text. The latter argument accepts a string. Multiple tags can be specified by separating them with semi-colons. For each tag that needs deleted three pieces of information needs to be provided. Each of the three pieces are delimited with a comma. These are the three arguments listed above. First is the name of the tag. So if we are wanting to remove an img tag we would specify “img”, for bold “b”. Second, we specify if we want just the tag removed or the tag and the content it wraps. There are two acceptable strings here – “tag” (for just removing the tag) and “content” (for removing the tag and the content). Lastly, specify whether this is a wrapping or contained tag. For an image tag we would specify “false” because it is not a wrapping tag. For bold we would specify “true” because it is a wrapping tag.

One last note, if you want to strip out all HTML tags you can just pass in the string “all” to the method. But note that this will just strip out the HTML and any content being wrapped by the tags will be left alone.

So let’s look at an example of how to call this tag.

<!--- TEXT TO STRIP HTML FROM --->
<cfset VARIABLES.text = "Lorem <img />ipsum <b>dolor</b> sit amet, <em>consectetur</em> adipiscing elit.>

<!--- STRIP OUT ALL HTML --->
<cfset stripHtml(VARIABLES.text, "all")>

<!--- STRIP OUT IMG, B, AND EM TAGS --->
<cfset stripHtml(VARIABLES.text, "img,tag,false;b,tag,true;em,content,true")>

Feel free to copy this and use it as you please.

Directory Watcher Gateway for file management logging

Sunday, April 4th, 2010

I wanted to play with gateways a little bit in Coldfusion 9 this weekend, but wanted to build something at least slightly practical.  What I ended up with is  a gateway that keeps up with file additions, changes, and deletions for a given project and logs those changes to a file.  This was surprisingly easy to accomplish with the directory-watcher gateway that ships with Coldfusion.  All in all this is a tool I may implement for development purposes just to have a running log of file changes that occur within a given project over the given amount of time.

So let’s get this ball rolling by creating a couple projects under the ‘wwwroot’ folder in our Coldfusion9 directory.  If you are still using Coldfusion 8 I don’t see any reason why this won’t work for you as well.  I haven’t tested it though.  For me, I named the first project ‘my_site’ and the second ‘my_othersite’.  Now set up two projects in Coldfusion Builder that respectively points to these two directories.

Now we’ll create a set of directories in each of these projects.  Under the project you should create a directory called ‘gateways’.  Under that directory create another directory called ‘cfcs’ and another one called ‘config’.  Note that this is how that I’ve chosen to do this.  You can set up your file structure however you want.  The main thing to note is that you will need the configuration file for the directory-watcher gateway in your project.  This way when you create your instance of the gateway in the Coldfusion Administrator you can point it to this specific project.  You will then be able to set a new configuration file in another project (which we’ll do) allowing you to watch multiple directories at once.  Inside of the ‘cfcs’ directory you can create a component called ‘directory_watcher.cfc’, and under the ‘config’ directory create a file called ‘directory-watcher.cfg’.  So your file structure should look like the following.

File Structure

File Structure

Now we’ll set up the directory-watcher.cfg file.  Under your ‘ColdFusion9′ directory navigate to ‘gateway/config’ and open the ‘directory-watcher.cfg’ file.  Copy the contents to your file in the my_site project.  Note: you could also just copy the entire file to your ‘config’ directory if you like.  The contents will look like the following.

directory-watcher.cfg

#
# DirectoryWatcherGateway configuration file
#

# The directory you want to watch.  If you are entering a Windows path
# either use forward slashes (C:/mydir) or escape the back slashes (C:\\mydir).
directory=

# Should we watch the directory and all subdirectories too
# Default is no.  Set to 'yes' to do the recursion.
recurse=no

# The interval between checks, in miliseconds
# Default is 60 seconds
interval=60000

# The comma separated list of extensions to match.
# Default is * - all files
extensions=*

# CFC Function for file Change events
# Default is onChange, set to nothing if you don't want to see these events
changeFunction=onChange

# CFC Function for file Add events
# Default is onAdd, set to nothing if you don't want to see these events
addFunction=onAdd

# CFC Function for file Delete events
# Default is onDelete, set to nothing if you don't want to see these events
deleteFunction=onDelete

We’re going to make just a couple changes to this file.  First and foremost we need to add the directory for the gateway to watch.  This is the my_site directory we created under the ‘wwwroot’ directory.  I’m on a mac so I would write ‘/Applications/ColdFusion9/wwwroot/my_site’ after the ‘directory=’ at the beginning of the file.  Next we need to set ‘recurse=no’ to ‘recurse=yes’.  This will allow our gateway to watch this directory and all directories beneath it.  Lastly, just so we can see our changes more quickly we can set the interval to ‘10000′ milliseconds.  By default it runs every minute.  So our new file will look like:

(more…)

Deleting from multiple SQL tables with a single query

Wednesday, March 31st, 2010

I’m by no means an expert using SQL, but as of yet I’ve found no way to delete data from multiple tables without writing a single query specifically for each table that data needs deleted from. So if you’re reading this expecting a native SQL statement that will delete data from multiple tables then I have nothing to offer. However, I do have a solution. One which requires a programming language, Coldfusion in my case.

The idea is that we have a number of tables.  Lets say we have five tables holding data about products.  So we’ll have tables named ‘products_tbl’, ‘products_name_tbl’, ‘products_description_tbl’.  Using just SQL we would need to write three delete statements like:

Straight SQL

<cfquery name="VARIABLES.deletetable" datasource="myds">
	DELETE
	FROM product_tbl
	WHERE product_id = 9
</cfquery>

<cfquery name="VARIABLES.deletetable" datasource="myds">
	DELETE
	FROM product_name_tbl
	WHERE product_id = 9
</cfquery>

<cfquery name="VARIABLES.deletetable" datasource="myds">
	DELETE
	FROM product_description_tbl
	WHERE product_id = 9
</cfquery>

Now let’s say that halfway through the project the client suddenly wants the price to be included. Really not a big deal, but when we now go to delete the product we realize that we need to go find our component or whatever that is handling the deleting transactions and add a new delete statement.

<cfquery name="VARIABLES.deletetable" datasource="myds">
	DELETE
	FROM product_price_tbl
	WHERE product_id = 9
</cfquery>

I personally hate this, and like most things the more complicated the data becomes the more burdensome it gets on the developer. So what’s the solution?

The idea is that we need to bundle all of these related sets of data into a group. When I say related I simply mean that in this case all the tables containing the parts and pieces of a product. The product is an object, and when that object is deleted we need to visit each of the tables containing information for that product and get rid of it. If we could simply group each of the table names we could run a loop with a delete statement inside where the table name dynamically changes with each loop. This would allow us to update our group and not have to worry about updating any actual SQL.

In my case, I’ve created the group under the APPLICATION scope. This let’s me access the group from anywhere within my application. As such my Application file would contain inside the onApplicationStart() method, for this case, the following.

<cfparam name="APPLICATION.producttables.productTbl" type="string" default="product_tbl">
<cfparam name="APPLICATION.producttables.productNameTbl" type="string" default="product_name_tbl">
<cfparam name="APPLICATION.producttables.productDescriptionTbl" type="string" default="product_description_tbl">

Now that we have our table names stored in the APPLICATION scope under a group called ‘producttables’ we can build a function that will get all the data in the ‘producttables’ group and set it into an array. We can then loop over this array of table names deleting from each the product data associated with the specified product id. In this case our function would look like.

<cfcomponent displayname="Looping Delete" hint="Loops over a portion of a struct with table names and deletes from those tables." output="false">

	<cffunction name="deleteProduct" displayname="Delete Product" description="Deletes a product." access="public" output="false" returntype="void">

		<!--- PRODUCT ID --->
		<cfargument name="productid" displayname="Product ID" hint="Id of the product to be deleted." type="numeric" required="true" />

		<!--- CREATE AN ARRAY OF TABLE NAMES STORED IN APPLICATION SCOPE --->
		<cfset var tableArray = structkeyarray(APPLICATION.producttables)>

		<!--- LOOP OVER THE ARRAY --->
		<cfloop array="#tblArray#" index="VARIABLES.i">
			<cfquery name="deleteproduct" datasource="#APPLICATION.dataSource#">
				DELETE
				FROM schema_name.#APPLICATION.producttables[VARIABLES.i]#
				WHERE product_id = <cfqueryparam value="#ARGUMENTS.productid#" cfsqltype="cf_sql_integer" maxlength="10">
			</cfquery>
		</cfloop>

	</cffunction>

</cfcomponent>

So what we’ve done here is made it much easier to add data to the product without ever having to touch our component. Instead all we really need to do is add the data to the APPLICATION scope to the producttables group. So for instance now if we need to add a price to our product all we have to do is go into the Application.cfc file and set

<cfparam name="APPLICATION.producttables.productPriceTbl" type="string" default="product_price_tbl">

That’s all that we need now to make the update. The loop will now find this table in our group and delete from it the same as the others.

Entity has incorrect type for being called as a function in Coldfusion 8

Wednesday, March 17th, 2010

There’s some stuff on the web about this error in Coldfusion that I found helpful, but because it is such a useless error in the sense of telling you what the problem is I figured I would post my specific problem and solution.

In my case I was running a primary public function that called a few other private functions. The gist of my problem was that I had an argument in my primary public function with the name ‘generatepassword’. One of my private functions was also named ‘generatepassword’. When the private function was being called I suppose it wasn’t able to determine whether the argument or the method was to be called, and hence threw the error that really gives no explanation of this whatsoever.

I suppose that the ‘incorrect type’ is the argument that it was trying to call as a function. Who knows. Whatever the error may say, that was the problem. My search through google turned up similar cases. If you find yourself having this problem check your variable names and make sure that none of them conflict with your method names.

Random Password Generater UDF

Tuesday, March 16th, 2010

I realize that the web is full of password generators, but thought I’d add to the clutter with my own. It’s fairly simple with a few user-defined options for how the password is generated. Also you’ll need Coldfusion to run this method. Let’s take a quick look at the method, and as always feel free to use the method. Just drop it into a cfc, or on the page if that’s what fits your application best, call it with any special instructions via the arguments, and it will return back a struct with the appropriate data.

password_generator.cfc

<cfcomponent displayname="Generate Password" hint="Generates a random password." output="false">

	<!--- ************************  GENERATE PASSWORD  ************************ --->
	<cffunction name="generatePassword" displayname="Generate Password" description="Generates a random password." access="public" output="false" returntype="struct">

		<!--- ARGUMENTS --->
		<cfargument name="pwdlength" displayName="Password Length" type="numeric" default="10" required="false" />
		<cfargument name="includenumbers" displayName="Include Numbers" type="boolean" hint="Whether or not to include numbers in password." default="true" required="false" />
		<cfargument name="includeuppercase" displayname="Include Upper Case" type="boolean" hint="Whether or not to include uppercase characters." default="false" required="false" />
		<cfargument name="hashpwd" displayname="Hash Password" type="boolean" hint="Whether or not a hashed version of the password should be returned." default="false" required="false" />
		<cfargument name="hashtype" displayname="Hash Type" type="string" hint="Type of hash to perform." default="SHA-256" required="false" />

		<!--- SET CHARACTER RANGES --->
		<cfset var number = "48,57">
		<cfset var uppercase = "65,90">
		<cfset var lowercase = "97,122">

		<!--- SET A COUNTER --->
		<cfset var counter = 1>

		<!--- BUILD STRUCT --->
		<cfset var data = structnew()>
		<cfset data.passwordraw = "">
		<cfset data.passwordhashed = "">

		<cfscript>
			/* WHILE THE PASSWORD LENGTH IS LESS THAN THE SPECIFIED LENGTH */
			do
			{
				/* RETURN A RANDOM CHARACTER */
				character = randrange(listfirst(number, ","), listlast(lowercase, ","));

				/* IF THE CHARACTER IS LOWERCASE */
				if(character gte listfirst(lowercase, ",") AND character lte listlast(lowercase, ","))
				{
					/* SET CHARACTER INTO STRING */
					data.passwordraw = data.passwordraw & chr(character);
				}

				/* CAN WE USE UPPERCASE CHARACTERS */
				if(ARGUMENTS.includeuppercase eq true)
				{
					/* IF CHARACTER IS UPPERCASE */
					if(character gte listfirst(uppercase, ",") AND character lte listlast(uppercase, ","))
					{
						/* SET CHARACTER INTO STRING */
						data.passwordraw = data.passwordraw & chr(character);
					}
				}

				/* CAN WE USE NUMBERS */
				if(ARGUMENTS.includenumbers eq true)
				{
					/* IF CHARACTER IS NUMBER */
					if(character gte listfirst(number, ",") AND character lte listlast(number, ","))
					{
						/* SET CHARACTER INTO STRING */
						data.passwordraw = data.passwordraw & chr(character);
					}
				}
			}
			while(len(data.passwordraw) lt ARGUMENTS.pwdlength);
		</cfscript>

		<!--- SHOULD WE HASH THE PASSWORD --->
		<cfif ARGUMENTS.hashpwd eq true>
			<cfset data.passwordhashed = hash(data.passwordraw, ARGUMENTS.hashtype, "us-ascii")>
		</cfif>

		<cfreturn data>

	</cffunction>

</cfcomponent>

It takes five arguments. First is the length of the password you want to generate. So do you want it to be 4 characters, 10 characters, or whatever. Second, do you want numbers to be included in the password? If so set it to true. This defaults to true if not specified. Third, do you want any letters that are uppercased? If using a case sensitive password you may wish to specify this to true. It defaults to false. Fourth, do you want to also return a hashed version of the password. If storing in a database you may want this. And if you are returning a hashed version, which algorithm should be used in the hash process. This defaults to “SHA-256″. You can use any algorithm supported by Coldfusion.

Next I set some variables that hold the lowest and highest byte code representing numbers, uppercase, and lowercase according to ascii. Build the struct to return, and then set some script that will pump out our random password according to the specifications.

Lastly, if the password should be hashed it is.

Enjoy.

Setting up ORM in Coldfusion 9

Saturday, March 13th, 2010

First things first, you will need a database and Coldfusion 9 for this to work.  Once you have  these two things you’ll need to create your Application.cfc file.

Remember in Coldfusion 8 using the THIS scope at the top of the component outside of any functions to set things like the name of the application, the session management, and the application timeout.  Well, now you need to add to that list a few new settings in the THIS scope.  You’ll need to add THIS.ormenabled = true and THIS.datasource = “yourdatasource”.

So our very simple Application.cfc file will look like the following.

Application.cfc

<cfcomponent displayname="Application File" hint="Application File" output="false">

	<!--- SET UP ORM --->
	<cfset THIS.name = "applicationTest">
	<cfset THIS.datasource = "datasource">
	<cfset THIS.ormenabled = true>

</cfcomponent>

Keep in mind that setting the datasource in the THIS scope is the same thing you did in Coldfusion 8 when you would set the datasource in the tag. Point being that this datasource is pretty useless without setting up the datasource in the Coldfusion Administrator first. Take a look at Setting up a datasource to learn more about this.  The really freakin awesome thing about this though is that setting the datasource here sets it globally across your application, which means for every call to the database you do not need to specify the datasource.  This is a big deal in my book.

So ORM should be all set in your application.

DISCLAIMER** This is a record of what I’m learning in ORM in Coldfusion 9. While what I post should work, I lay no claim to best practices. My hope here is that other ORM beginners like myself may benefit from this information on the assumption that their path to learning ORM might be similar to my own. To learn more about ORM by those who know more than myself check out (of course) the adobe docs and a couple very nice presentations on cfmeetup.com by Raymond Camden and Bob Silverberg.

Setting up a datasource in Coldfusion Administrator

Saturday, March 13th, 2010

Quick post on how to set up a datasource in the Coldfusion Administrator. First you’ll want to open the Coldfusion Administrator. I’m going to assume that you will be doing this locally since if you don’t have a datasource you’ve probably are just at the beginning stages of development. As such let’s open the admin.

Working locally it is likely that you will use the following link http://localhost:8500/CFIDE/administrator/index.cfm for Coldfusion 8 and http://127.0.0.1:8500/CFIDE/administrator/index.cfm for Coldfusion 9. Remember though if you have multiple versions of the Coldfusion server running on your machine the port number may not be 8500. Instead as ports are used Coldfusion will increment the port number by 1 until reaching the next available port. So for example if you had Coldfusion 8 running, and are now running Coldfusion 9 as well your port number is most likely 8501. Nonetheless, if this is the case, my guess is that your not reading this article.

Once into the administrator click on ‘Data Sources’ under the ‘Data & Services’ tab on the left.

Data Sources menu in Coldfusion 9 Administrator

Next create a name for your datasource.  This can be whatever you want it to be.  This is the label that will represent the connection setup between Coldfusion and this particular database.  You will also need to select a driver from the drop-down.  In my case I’m using MySql 5 so I’ll choose ‘MySql 4/5′ from the drop-down.  You will choose whatever is appropriate for your particular database.

(more…)