Create, Retrieve, Update, and Delete in ColdFusion

Mike has a great little tool running over on his site that does creates crud for you (where CRUD stands for Create, Retrieve, Update, and Delete). In another sense, he’s a couple steps away from creating an object-relational mapping tool similiar to what some open source Java tools do.
Mike, go one step higher and think about your data as an object and dynamically create CFC’s with the proper methods for creating, retrieving, updating, and deleting. This would mean that other developers using your system could use code that looks like this to create an event (or news or press release… given that MB’s system first created a table for an event and then created a CFC called event):

myEvent = createObject(“component”, “com.michaelbuffington.event”);
myEvent.setId(variables.id);
myEvent.setLabel(form.label);
myEvent.setStartDate(form.startDate);
myEvent.update();

Now other developers on your team a) don’t have to know what the names of the db tables are, b) you don’t have to worry about having multiple update queries (some of which might be buggy) and c) it’s just plain easier than writing

<cfquery name=”updateEvent” datasource=”#request.dsn#”>
update event set label = ‘#form.label#’, startdate = #form.startdate#
where id = #form.id#
</cfquery>

Other benefits to using a CFC that does your create(), update() and delete() for you: a) you can insert logging statements into your delete() and update() methods so that all updates and deletes are logged to the file system. I had a system setup like this on the footjoy.com project I worked on this past summer. It came in really handy when some of the content editors began putting in content… in fact they created over 100 pieces of content and then saved that content. Only the update() statement wasn’t working and wasn’t throwing a visible error to them. In my log file I had update statement so I only needed to parse out the bad code from the log file and drop that into Query Analyzer to get back all the updates. b) you can handle deleting objects on the file system that might be related to that piece of content. For instance, many press releases have a PDF file associated with them. If you used this:

myPressRelease = createObject(“component”, “com.michaelbuffington.pressrelease”);
myPressRelease.delete();

you could make sure that the press release PDF file was deleted from the file system at the same time that the press release is deleted from the database….

Anyway…. great job Mike! Looks like a great tool!

The future of instant messaging

Rafe postulates on the future of instant messaging. “A Microsoft product manager says that the future of IM is all companies managing the identities of people inside their organization, and the usage of a clearinghouse to broker connections between organizations.” Of course, this is *exactly* what Jabber does. A Jabber server is exactly analogous to an email server. Each email server is responsible for one or more domains, email clients connect to email servers. A Jabber server is responsible for one or more domains, Jabber clients connect to Jabber servers. And just like email, the protocol is open for anyone and everyone to use and implement.

rsync: installation and implementation

I wrote up these directions (based on directions @ http://www.hksilver.net/rsync/ on a couple weeks ago when I was doing some work with rsync. Someday I’m sure I’ll need them again… and maybe someone else will benefit. So here goes nothing:

Download this zip file (which contains cygwin1.dll,instsrv.exe,rsync.exe and srvany.exe) (I mirrored it here)

Decompress the contents to c:\rsync.

Copy out cygwin1.dll to c:\(system folder)\system32, where system folder is either winnt or windows depending upon your OS.

Open a command prompt and type:

cd c:\rsync
instsrv Rsync “c:\rsync\srvany.exe”
exit

Click on Start, run, and type in regedit.

Once you’re in the Registry Editor, make the following changes (Quotes ARE important!):

HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Services -> Rsync

Right click on Rsync, New -> Key Key Name: Parameters
Right click on Parameters New -> String Value
Value Name: AppDirectory
Value: “c:\rsync”

Right click on Parameters New -> String Value
Value Name: Application
Value: “c:\rsync\rsync.exe”

Right click on Parameters New -> String Value
Value Name: AppParameters
Value: –config=”c:\rsync\rsyncd.conf” –daemon

Close the Registry Editor.

SOURCE MACHINE
******************************************************
If this is the source machine (ie: a machine you’d like other machines to copy/sync with), open up c:\rsync\rsyncd.conf in notepad and modify the rsync share setting giving it a well-known, descriptive name. Modify the path to point to a directory that you want to make available for others to get access too. Save and close rsyncd.conf.

Open up Administrative Tools either off the Start Menu or in Control Panel. Double click Services. Scroll down the list to Rsync. Highlight it and start the service.

To test it, open up a command prompt and type:

telnet localhost 873

If you get a connection, rsync was installed successfully. Congratulations!

DESTINATION MACHINE
******************************************************
If this is a machine you’d like to have copy/sync with a remote host, then bring up a command line and type:

cd c:\rsync
rsync -rt –progress –delete [INSERT HOST]::[INSERT SHARE] [INSERT DESTINATION PATH]
exit

For example, if I had a staging server located at ip address 10.10.1.162 with a share called “staging.cephas.net” that I wanted to sync with, I’d type something like this:

rsync -rt –progress –delete 10.10.1.162::staging.cephas.net f:/fInetPub/hosts/live.cephas.net/wwwroot/images/

where 10.10.1.162 is my staging server, staging.cephas.net is the rsync share, and f:/fInetPub/hosts/live.cephas.net/wwwroot/images/ is the destination that I want to be synced up. Note that if you don’t want files deleted from destination that have been deleted in the source, you should remove the –delete option.

If you’d like run through a sync as a test, add the -n flag like this:

rsync -rtn –progress –delete 10.10.1.162::staging.cephas.net f:/fInetPub/hosts/live.cephas.net/wwwroot/images/

Rsync will list out the files that it *would* have copied/deleted.

The synchronization can (and probably should) be run as a scheduled task using Windows Task Scheduler. To do this, create a .bat file, add the following text (substituting your source, share, and destinations):

cd c:\rsync
rsync -rt –progress –delete 10.10.1.162::staging.cephas.net f:/finetpub/hosts/live.cephas.net/wwwroot/images/

and then use the Windows Scheduler (Start –> Programs –> Accessories –> System Tools –> Scheduled Tasks) to create a task that fires every n (minutes? hours?) per day.

Questions or problems? Email me.

Lions and tigers and bluetooth!

Ole Bluetooth, King Harald, Would’ve Loved This : “I am in Levi, Finland, at a meeting where we are talking and playing and working wireless. I pull out my laptop, fire up the bluetooth manager to connect with my cell phone to dial up a connection and … whoa … is this slooooow … where *did* the connection go … and just when I am about to give up I see hundreds of bluetooth devices on my laptop’s bluetooth manager! Headsets, phones, devices, all revealing themselves in low security mode.” [via boingboing.net]

Fung Wah Fun

On a personal note, Karen and I just got back from a long weekend in NYC.. we took the Fung Wah bus from Boston’s Chinatown to NYC’s Chinatown Friday morning ($15 per person each way, a steal!) and arrived a little after noon. Per a friends suggestion, we went to Times Square and got a couple tickets for 42nd Street and then moseyed on to have a cupcake at the Cupcake Cafe. Checked into the hotel, went to the show, had dinner at Les Halles (where we sat 2 tables away from Gwen Stefani and Gavin Rossdale… behind us was Angela from ‘The Bachelor‘ !!!!). Saturday we had a late breakfast, went to Chelsea Market to look at cooking stuff for Karen and then took the ferry over to see my grandparents in Staten Island. They took us to Pal Joey’s for some fabulous Italian food and then I got my grandpa to break out the 20 year slide projector and watch slides from the 50’s and 60’s. Took the bus back today… New York city is a really fun place.

Swarm Intelligence: An Interview with Eric Bonabeau

On openp2p.com: Interesting interview w/ Eric Bonabeau, relevant quotes:

And that swarm intelligence offers an alternative way of designing “intelligent” systems in which autonomy, emergence, and distributedness replace control, preprogramming, and centralization.” — The words ‘autonomy, emergence and distributedness’ are somewhat freeing from a software development standpoint. In complex systems, instead of taking x years to development a specification document and then another y years to implement the system and then z more years to debug the system, you spend the time upfront to make the system adaptable to its environment, able to evolve in some sense.

In social insects, errors and randomness are not “bugs”; rather, they contribute very strongly to their success by enabling them to discover and explore in addition to exploiting. Self-organization feeds itself upon errors to provide the colony with flexibility (the colony can adapt to a changing environment) and robustness (even when one or more individuals fail, the group can still perform its tasks).” — Bugs (the software kind) are inevitable, we can’t write perfect code. Doesn’t it make sense to use errors to our advantage then? (one might argue that the same system that takes advantage of errors will have errors, do we have to write software to take advantage of the errors written into the program that takes advantage of errors?)

The article mentions routing and UAVs as applications of swarm intelligence. My domain of knowledge currently only wraps around web applications… how might one use swarm intelligence in web applications? Implicit personalization might be a place to start. The pheromones that ants leave behind for others to follow aren’t that different from the ‘paths’ one leaves behind on a site that others might follow. Knowledge management comes to mind too. Could the aggregate of referrers and queries sent to a site become something greater than the sum of the individual parts? On heavily trafficked sites, you’ll always have to make a choice about what to cache and what to get from persistent storage. Perhaps a case can be made for letting the system compute what the most expensive procedures are (disk I/O, DB transactions, web service calls, etc…) and cache those, instead of caching everything (or nothing!), thereby maximizing your use of memory and processor utilization. Any others you can think of?

If you’re interested, Eric has a book on the subject: “Swarm Intelligence: From Natural to Artificial Systems“.

Constructing Intelligent Agents Using Java: Professional Developer’s Guide

Couple tidbits from Constructing Intelligent Agents Using Java: Professional Developer’s Guide.

Page 3, an Alan Turing quote: “.. if a machine is expected to be infallible, it cannot also be intelligent.

On the classifications of agents (agency, intelligence, mobility): “…Agency deals with the degree of autonomy the software agent has in representing the user to other agents, applications and computer systems…. Intelligence refers to the ability of the agent to capture and apply domain specific knowledge and processing to solve problems…. An agent is mobile if it can move between systems in a network.” [page 10]

On processing strategies: “… reactive or reflex agents, which respond in the event-condition-action mode. Reflex agents do not have internal models of the world. They respond solely to external stimuli and the information available from their sensing of the environment…. Deliberative or goal directed agents have domain knowledge and the planning capability necessary to take a sequence of actions in the hope of reaching or achieving a specific goal…. Collaborative agents work together to solve problems. Communication between agents is an important element, and while each individual agent is autonomous, it is the synergy resulting from their cooperation that makes collaborative agents interesting and useful.” [page 10]

…intelligent agent software is practical software. It just gets the job done. If intelligent agent software introduces another level of complexity that the user has to deal with, then it will be a failure. Intelligent agents must be enabling and automating, not frustrating or intrusive.” [page 13]

Using Google & ColdFusion

I’m sure this has been on Macromedia’s site for a bit, but it’s an interesting article for two reasons:

a) I’ve never really looked into the CFX documentation, but it appears from this article that creating a CFX, at least in Java, is a snap. Basically you implement the CustomTag interface, which means you only have to write one method, processRequest(), which looks very much like the doGet() method of the HttpServlet. After that you can write to the screen (ie: out.println() in servlet land == response.write() in CFX land), create a Query object for a CF page to loop over, or simply set a variable and return that variable to the calling template (response.setVariable() which is probably very much like request.setAttribute() in servlet land).

b) Couldn’t the author have done everything in a CF script using createObject() rather than using a CFX?