Home | Projects | Contact Me

My First Steps With Transfer : Day 1

I will preface this by saying the only Transfer experience I have thus far is installing it. Since that only required copying a folder onto the web server and adding a ColdFusion mapping, I am far from a Transfer expert and this will end up being a bit of a learning experience. I was going to put all the database interaction inside the WhosOn.cfc component, but after some consideration I thought it might be better to follow best practices and separate my service object from the gateway object.

My initial impression is that I may have made my gateway object a little too "smart" and perhaps I should have kept some of the logic in the actual service layer but I suppose it could (and probably will) berefactored.

First I setup my transfer.xml file and setup the package that we will be using with WhosOnCFC. For a base installation I have tried to keep things fairly simple so setting up transfer.xml for basic user information was extremely easy.

<?xml version="1.0" encoding="UTF-8"?>

<transfer xsi:noNamespaceSchemaLocation="/transfer/resources/xsd/transfer.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


<InvalidTagDefinitions>

<package name="whosoncfc">

<InvalidTag name="user" table="ActiveUsers">
<id name="ClientID" type="string"/>
<property name="UserID" type="string" />
<property name="Roles" type="string" nullable="true" />
<property name="Created" type="date" />
<property name="LastUpdated" type="date" />
<property name="Referer" type="string" nullable="true" />
<property name="IP" type="string" />
<property name="HostName" type="string" />
<property name="HideClient" type="boolean" />
<property name="Coords" type="string" nullable="true" />
<property name="Country" type="string" nullable="true" />
<property name="City" type="string" nullable="true" />
<property name="EntryPage" type="string" />
<property name="CurrentPage" type="string" />
<property name="HitCount" type="numeric" />
<property name="UserAgent" type="string" nullable="true" />
</object>

</package>

</objectDefinitions>

</transfer>

Nice and straight forward. This is not taking the user's page history into account, but I do plan on adding that functionality later on. The next thing to do was to write the actual gateway component thatWhosOnCFC will communicate with to store and retrieve its information.

<cfcomponent output="false" hint="WhosOn database gateway object">

<cffunction name="init" returntype="whosongateway" hint="I return an instance of whosongateway">
<cfargument name="transfer" type="any" required="true" hint="I am the transfer object" />

<cfset variables.transfer=arguments.transfer />
<cfreturn this />
</cffunction>

<cffunction name="saveUser" access="public" returntype="boolean" hint="I write hits to the user database">
<cfargument name="userInfo" type="struct" required="true" />

<cfset var transfer=variables.transfer.getTransfer() />
<cfset var user = "" />
<cfset var myCount=1>

<cfscript>
user=transfer.get('whosoncfc.user',userInfo.ClientID);

if(not user.getIsPersisted()){
// New User, insert all detals into database
user.setClientID(userInfo.ClientID);
user.setUserID(userInfo.UserID);
user.setRoles(userInfo.Roles);
user.setCreated(Now());
user.setLastUpdated(Now());
user.setIP(userInfo.IP);
user.setHostName(userInfo.Hostname);
user.setCoords(userInfo.Coords);
user.SetCountry(userInfo.Country);
user.SetCity(userInfo.City);
user.setHideClient(userInfo.HideClient);
user.setEntryPage(userInfo.EntryPage);
user.setHitCount(myCount);
user.setUserAgent(userInfo.UserAgent);
} else {
// Updating an existing client
myCount=user.getHitCount();

user.setUserID(userInfo.UserID);
user.setRoles(userInfo.Roles);
user.setLastUpdated(Now());
user.setIP(userInfo.IP);
user.setCurrentPage(userInfo.CurrentPage);
user.setHitCount(myCount+1);
}

transfer.save(user);
</cfscript>

<cfreturn true />
</cffunction>

<cffunction name="setCurrentControlSet" access="public" returntype="void" hint="I get the configuration from whoson">
<cfargument name="controlset" type="struct" required="true" />

<cfset variables.controlset=arguments.controlset />
</cffunction>

</cfcomponent>

Currently the gateway object is a little bare. There is one utility function, setCurrentControlSet(), that WhosOnCFC uses to pass its current configuration to the gateway. My thinking is the gateway will use this information for purging old entries out of the database, but I am thinking now it should probably be moved. The gateway shouldn't need to know about configurations if its purpose is to support the service layer.

Other than that, the only other function is currently the saveUser() function. This is the "meat and potatoes" of the gateway, so to speak. First we tell transfer to look in our database and see if it can find a record matching theClientID passed to it from WhosOnCFC . If no matching ID is found we have a new client and we insert a new record into our table. If a matching ID is found, we update the information that is already in the database.

I am pleased with my first day of really using Transfer and I would have to say it does make life a lot easier. It takes a little bit of extra time setting up the transfer.xml but the time you save not having to hand-coding queries is well worth it in my opinion.

Next I will be adding the users page history in, but that is another blog post.

Related Blog Entries

Comments
Layout: Shane Zehnder ::: BlogCFC was created by Raymond Camden. ::: This blog is running version 5.9.