Wednesday, October 24, 2007

Creating Account Names

A neat question. One thing to consider is if you have a really large organization you could have 5 'Joe Smith' records so what happens if you run out out of letters in their name?

J_Smith
Jo_Smith
Joe_Smith
???
???

Another is what happens if you have someone with an extremely long last name? A friend of mine has a 15 letter last name.

A lot of organizations have a similar issue and their solution has been to implement the following business rule:
First Letter of First Name + Up to first 7 letters of last name + numeric value (if needed)

So you would end up with something like this:
JSmith
JSmith1
JSmith2

Anyways here is my solution:

<cfparam name="posted" default="FALSE">

<cfif isDefined('form.submit')>
<cfset posted = TRUE>
</cfif>

<cfif posted>

<!--- AS REQUESTED --->

<!--- QUERY YOUR DATABASE TO FIND OUT IF THERE ALREADY IS A USER WITH
A SIMILAR NAME STRUCTURE --->
<cfquery name="getUserName" datasource="#request.dsn#">
SELECT *
FROM EMPLOYEES
WHERE First_Name LIKE '#FORM.FirstName#%'
AND Last_Name = '#FORM.LastName#'
</cfquery>

<!--- CREATE A NEW USER NAME WITHIN THE BOUNDRIES OF THE BUSINESS LOGIC --->
<cfset newUserName2 = left(#FORM.firstName#, #getUserName.RecordCount#) & '_' & left(#FORM.lastName#, 7)>

New User Name: <cfdump var="#newUserName2#"><br>


<!--- RECOMENDED WAY --->

<!--- CREATE A NEW USER NAME WITHIN THE BOUNDRIES OF THE BUSINESS LOGIC --->
<cfset newUserName = left(#FORM.firstName#, 1) & left(#FORM.lastName#, 7)>

<!--- QUERY YOUR DATABASE TO FIND OUT IF THERE ALREADY IS A USER WITH
A SIMILAR NAME STRUCTURE --->
<cfquery name="getUserName" datasource="#request.dsn#">
SELECT *
FROM EMPLOYEES
WHERE First_Name LIKE '#FORM.FirstName#%'
AND Last_Name = '#FORM.LastName#'
</cfquery>

<!--- debugging for your query
<cfdump var="#getUserName#">
--->

<!--- IF THERE ALREADY IS A ACCOUNT NAME THAT MATCHES THE CRITERIA WE HAVE USED
ADD A NUMBER TO THE END OF THE ACCOUNT NAME --->
<cfif getUserName.recordCount GT 0>
<cfset newUserName = newUserName & (getUserName.recordCount + 1)>
</cfif>

<!--- CHECK TO BE SURE THAT THE GENERATED USERNAME DOES NOT CONFLICT WITH
ONE THAT IS ALREADY IN THE SYSTEM --->
<cfquery name="verifyUserName" datasource="#request.dsn#">
SELECT *
FROM EMPLOYEES
WHERE account_name = '#newUserName#'
</cfquery>

<!--- IF THERE IS DROP AN ERROR! --->
<cfif verifyUserName.recordCount GT 0>
<cfabort showerror="there was a problem generating the account name">
</cfif>

New User Name: <cfdump var="#newUserName#">


<cfelse>

<form name="createUser" method="post">
First Name: <input type="text" name="firstName" /><br />
Last Name: <input type="text" name="lastName" /><br />
<input type="submit" name="submit" />

</form>

</cfif>

Grr!!! I Don't Like CFUPDATE and CFINSERT

Heres a response to a question on EE that I see all too often:

Generally it is considered best practice to use booleans (or in MS SQL server bits) to represent active/inactive status and to shy away from using cfupdate and cfinsert.

That said, it sounds as though there is something funky going on in your CF form. your select box should look something like this.

<select name="UsesrActive">                                                                
<option value="Active">Active</option>
<option value="InActive">InActive</option>
</select>


Then your cfupdate tag should look like:

<cfupdate datasource="#request.dsn#"
tablename="Users"
formfields="UsesrActive,YOUR FIELDS">


If your code already looks like this check to make sure that you have all of your fields properly entered into the cfupdate tag.

I really dislike the use of the CFUPDATE and CFINSERT tags. Don't get me wrong they are a great tool/trick for new developers but for anyone who is doing development for a living there really isn't any excuse to use them. And don't get me started on the database design ;)

CFMail Example

It should be pretty simple to use a query to specify which members you want to receive an email.

Here is an example adapted from: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-pta.htm

<cfmail
query="QUERYNAME"
to = "#QUERYNAME.EmailAdd#"
from = "#form.mailFrom#"
subject = "#form.subject#">
Dear #QUERYNAME.Full_Name#,
This message was sent by an automatic mailer built with cfmail:
= = = = = = = = = = = = = = = = = = = = = = = = = = =
#form.body#
</cfmail>

Once you get to this point the most important thing to remember is to check your query results very CAREFULLY to make sure that you are only sending out emails to the people you want to receive them.

Monday, October 15, 2007

Application.cfm Info

(Yes I usually work in a UNIX environment with its damned case sensitivity ;) )

Yea, so beyond that I saw a question about how Coldfusion processes Application.cfm pages and thought I would put a link to the live doc note about it from Adobe.

HERE it is

Something else that is worth noting for those of us who get to work in really state of the art environments where we are fighting for every millisecond of processing time; It really pays to have an Application.cfm page in every application even if it is blank because CF takes its sweet time looking up the directory tree.

At the same time every folder should have a OnRequestEnd.cfm page even if its blank. I've seen a 10 ms difference without it. See the live doc here.

Get a YouTube Thumbnail of a Video

I wasn't a big YouTube fan until I bought my iPhone... Opps.

Anyways I was looking through unanswered questions on EE and found someone asking questions about generating a thumbnail image for YouTube videos. I was kind of amazed that no one had answered it so I jumped on Google and took a look at their API docs at http://code.google.com/apis/youtube/developers_guide_protocol.html and found that even if you don't want to use their "GData" feeds you can get a Thumbnail for a video if you have the unique identifier for the video (url.v).

Its a simple matter of putting the unique identifier into the following link template http://img.youtube.com/vi/#url.v#/1.jpg

Wednesday, October 10, 2007

Damn you Ray!

and now I'm hooked... there goes the next two productive hours of my work week.

Friday, October 05, 2007

A Photo Of Me

A couple of people have asked me how I picture myself, so here you go: