06 November 2009

Javascript display time in browser local format

This is something i found useful and I'm sure I will again in the future.

Use coldfusion to set the UTC date and time. Then we'll have JS convert that time to the users local time.

Obviously it's preferable to have the user create an account and have them select their own, but in this case that wasn't an option.


function getDstOffset(myInputDate)
{
//get clients timezone offset so we can display in local format (60000 forces to use milliseconds)
var clientOffset = -myInputDate.getTimezoneOffset() * 60000;
return clientOffset;
}
function displayMyLocalTime(divname)
{
//take the passed in datetime(handled by server) and convert it to browser local
//this function takes the name of the dov to update with the datetime
var bigBenTime = new Date();

//set the js time to match that given to us by server
bigBenTime.setDate(#day(futuredate)#);
bigBenTime.setMonth(#month(futuredate)-1#);
bigBenTime.setYear(#year(futuredate)#);
bigBenTime.setHours(#hour(futuredate)#);
bigBenTime.setMinutes(#minute(futuredate)#);
bigBenTime.setSeconds(#second(futuredate)#);

cfif LEN(futureTime)
//time is defined, set it
bigBenTime.setHours(#hour(futuretime)#);
bigBenTime.setMinutes(#minute(futuretime)#);
bigBenTime.setSeconds(#second(futuretime)#);
/cfif

///get milliseconds since epoc and milliseconds of timezoneoffset
localBenTime = bigBenTime.getTime() + (getDstOffset(bigBenTime));

//put milliseconds back into useful format
benTime = new Date(localBenTime);

//set div to time
$(divname).innerHTML = benTime;
}

20 October 2009

Odd bug in Query of Queries

Not sure if this is a known bug or if it will really help anyone but it stumped me for a bit. As you can see ordering of uppercase values is handled completely differently than lowercase values! Frustrating.


cfscript
hello = queryNew('lastname,firstname');
queryAddRow(hello);
querySetcell(hello,'lastname','YANKEE');
querySetcell(hello,'firstname','DOODLE');
queryAddRow(hello);
querySetcell(hello,'lastname','ableson');
querySetcell(hello,'firstname','abe');
/cfscript
cfdump var="#hello#"



cfquery name="qhello" dbtype="query"
select lastname, firstname from hello
order by lastname,firstname
/cfquery
cfdump var="#qhello#"






Obviously the solution is "select lower(lastname), lower(firstname) from"....

I'd love to see CFML improve query of queries, it is a useful tool.

02 October 2009

SQL "list" Functionality

Hey

I recently had to take a FullName field in sql and split it into first and last names. To complicate matters further the fullname had an initial in it and the fields were backwards:

Adams, Douglas B
Fox, Michael J
Spears, Britney M
Rose, Axel

Obviously in cfml this would be easy-peasy using list functions. I'm sure aspx, jsp and php make it fairly trivial too.

SQL However, wasn't built for this, but alas, that was the way it had to be. If it helps anyone the lastname was fairly simple:

left(column_1,charindex(',',column_1)-1) as lastName

Of course firstname become infinitely more complicated as you have to get everything after the comma and THEN strip everything after the space. PLUS not every user has a middle name (eg Axel Rose). Sounds easy..it's not.
Now subqueries make this a little more straightforward but in this case i didn't have that luxury.
If you need to do that try something like:


select fullString, lastname, calculateFirstName(otherName) as firstname from
(
select fullstring, lastname, calculateWhatsLeft(lastname) as otherName from
(
select fullString, calculateLastName() as lastname from tablename
)
)


(excuse the functions in there, it's just shorthand / pseudocode, you actually need to do the left(...charindex(...)) type stuff i just don't wanna retype.

Anyway as i said i couldn't use subqueries so onward with my plight. I tried coalesce but it needs nulls...urg! I ended up with some hideous case statement.
....back to the drawing board.

i stumbled accross this excellent idea by someone i've never met Daryl Banttari:
http://cfprimer.blogspot.com/2009/01/listfirst-listrest-in-sql-server.html

User functions, not normally a big fan of using custom functions in sql, but this was desperation. I modified the functions to take in a delimiter parameter:



CREATE FUNCTION [dbo].[listFirst] (@list nvarchar(4000),@delim nvarchar(2))
RETURNS nvarchar(4000)
AS BEGIN
IF(@delim is null) SET @delim = ','
DECLARE @pos int
DECLARE @ret nvarchar(4000)
SET @pos = charindex(@delim, @list)
IF @pos > 0
SET @ret = left(@list, @pos-1)
ELSE
set @ret = @list
RETURN @ret
END
GO

CREATE FUNCTION [dbo].[listRest] (@list nvarchar(4000),@delim nvarchar(2))
RETURNS nvarchar(4000)
AS BEGIN
IF(@delim is null) SET @delim = ','
DECLARE @pos int
DECLARE @ret nvarchar(4000)
SET @pos = charindex(@delim, @list)
IF @pos > 0
SET @ret = substring(@list, @pos+1, len(@list)-@pos)
ELSE
SET @ret = ''
RETURN @ret
END



and hey presto:


select column_1,
dbo.listFirst(column_1) as lastname,
dbo.[listFirst2](ltrim(dbo.[listRest](column_1)),' ') as firstname
from tableName

24 September 2009

Open Blue Draggon and Google App Engine

Hey Guys,

Anyone following cloud computing and coldFusion should absolutely check out some amazing work by a friend of mine Paul Kukiel:

http://blog.kukiel.net/2009/09/coldfusion-on-google-app-engine-with.html

He's built on some great work Google / Blue Dragon have been doing with porting a basic cloud computing style java cf instance (open Blue Dragon) onto Google app engine. It looks like it's work in progress but it's super exciting to see.

You can even try it yourself, go on it's easier than you think!

17 August 2009

Using If exists drop table in Apache Derby and CF8

All credit to Giancarlo Gomez for this blog post, awesome:

http://fusecf.blogspot.com/2007/08/coldfusion-8-and-derby-db-create-and.html

Using apache derby as a testing database is really useful and this article describes a great method of checking if a table exists and dropping it or re-creating it.

I find it very useful when testing and developing a new site to create a cfm page that just drops all tables and re-creates them with new data in. This saves a lot of time fiddling when you suddenly decide you need a new column or something.

18 June 2009

cfspreadsheet

OK i've from time to time been known to be un-thrilled with Adobe's cf agenda. No more (maybe) this is epic.

cfspreadsheet in Centaur could be the single greatest thing Adobe have ever done. It could have a huge impact on my daily working life.

http://forta.com/blog/index.cfm/2009/6/18/Working-With-Spreadsheets-In-ColdFusion

Good job Adobe, now get out there and market it, and market the snot out of the product and how awesome it is. Be proud, be loud, convince those php nerf-erters that they're playing tennis with ping pong bats.

29 April 2009

directory and sub directorys

This is actually kinda fun little bit of code, i quite liked it, times like this make you glad to be a l33t cf c0der.

Say you want to put something in a sub-directory of your current directory. Expland path function works great but it tags the filename on the end. Sure you could do some funky string functions to strip the filename and then add it back on after...but that's not nearly as fun!


<cfset LocalPath = ExpandPath("customSSL.css") />

<cfset LocalPath = getDirectoryFromPath(LocalPath) & "parsed\" &

listLast(localPath,"\") />

01 April 2009

Friendly URLs and Web Servers

OK so recently needed to produce a method of getting friendly URL's out there. Tried a bunch of things but nothing worked well, it needed to be easy to expand and not require constant changes to load balances / code etc.
Seemed like Adobe had the perfect solution in:
www.adobe.com/go/*
So anything after the go url would get mapped to wherever you wanted.

I came up with 2 solutions for this:

1) Work with ColdFusion, although this solution is wicked awesome i think it may only be appropriate if you're using tomcat or cf itself as your web server.

- Create a java servlet which strips everything after the /go/ keyword and add it as a variable. This servlet must then forward the client to a new set location. In my case i created a go.cfm file inside the application framework i was using. (i also set this location to be read from file so it's not hardcoded)

- edit your WEB-INF/web.xml file in your webserver add a new servlet definition and map it to your servlet

- have the go.cfm file query the database (or whatever you want) for the url param so for example:
www.myurl.com/go/parisHilton
maps to
www.myurl.com/index.cfm?fuseaction=home.celebrities&name=paris=fname=hilton

Have the go.cfm do another redirect.

There is also a way to do this in IIS:

1) Create a directory in webroot called go
2) Open up IIS
3) Right click on the go directory in IIS and click properties
4) Under the directory tab change the selection to “A redirection to a URL”
5) Enter the url:
webpath/appname/go.cfm?Urlvar=
6) None of the check boxes should be checked (I don’t know what they do anyways)

As you can see IIS uses the same go.cfm method, it just doesn't use the java servlet as IIS is not java dependent.

Hope this helps.

14 January 2009

Charting not available in Flex SDK

This may seem obvious to me but it bloody wasn't to me. Spent ages on this >:( Oh well hopefully this post will help someone else.

Charting is not a feature of the flex sdk and charts can not be used wiht just the sdk.

In order to use a chart in flex you need to purchase their flex builder proffessional product.


Error: Type was not found or was not a compile-time constant: BarChart.

mx:BarChart id="myChart"