Archive

Archive for March 5, 2012

Learning OO in ColdFusion Pt. 3

March 5, 2012 2 comments

In my previous post I took a look at selecting an existing event, retrieving its details and populating an edit form with those details, then updating the event with the new values.  Today we will look at a very similar operation–adding a new event.  The process is quite similar.  First we instantiate an Event bean.  Now we can display our form and enter the details of our new event.  We submit the form, passing the form values to each corresponding Event bean property.  After that we just call our create() method in the EventDAO which we persisted in the Application scope.  That’s pretty much all there is to it.  At the top of my display file, I add some logic to check if the form has been submitted.  If it has, call the setters and pass in the corresponding form values.  The code looks like this.


<cfset objEventBean = createObject("component","eventRegistration2.components.barrett.beans.Event").init()/>
<cfif structKeyExists(form, "submit")>
<cfset objEventBean.setEventName(form.eventName)/>
<cfset objEventBean.setEventDescription(form.eventDescription)/>
<cfset objEventBean.setContactName(form.contactName)/>
<cfset objEventBean.setContactPhone(form.contactPhone)/>
<cfset Application.EventDAO.create(objEventBean)/>
<cflocation url="event_select.cfm" addtoken="false" />
</cfif>

Now let’s see if we can eliminate the duplicate code that exists between the editEvent form and the addEvent form.  Both pages begin by instantiating the Event bean.  The difference is that for edit, we have passed an eventID which we then use to retrieve an existing record.  In the case of a new event, no eventID is passed, rather the Event bean has the default value of 0.  How about we use Event.getEventID() to check the value?  If it’s not 0, we know it’s an existing record and we can then use EventDAO.retrieveByID() to grab the details and populate form.  That takes care of the retrieval and display.  We can now use one display page to handle creating a new event as well as editing an existing one.

On submit, we now need to know if we should call EventDAO.create() or EventDAO.update().  Let’s create an exists() method to check to see if  we have a new or existing record.  The exists() method will accept an Event bean as an argument. Just like we did above, the exists() method will check the eventID value.  If the value not 0 we return true.  Otherwise, we return false.  All this method does is answer the question, “Does this eventID exist already?”  That’s it.  Of course now we need to create our save() method.  The save() method looks like this:


<cffunction name="save" access="public" output="false" hint="I handle saving an Event" return="string">
<cfargument name="Event" type="eventRegistration2.components.barrett.beans.Event" required="true" hint="I am the Event bean"/>
<cfset var success = ""/>
<cfif exists(arguments.Event)>
<cfset success = update(arguments.Event)/>
<cfelse>
<cfset success = create(arguments.Event)/>
</cfif>
<cfreturn success/>
</cffunction>

The save() method is a sort of traffic cop.  It passes the Event bean argument to the exists() method and then asks what the method returned?  If true, the event exists and then calls the update() method.  Otherwise, the event is new and it will call the create() method.

One last thing.  Since we are no longer directly calling the create() or update() methods, we could change their access attribute to private.  I’ve been reading Object Oriented Thought Process and it is recommended that we expose only as much of our objects as is absolutely necessary.  So it makes sense to change these to private as they will be called by the exists() method now.

Categories: ColdFusion Tags: