Changes between Initial Version and Version 1 of Training/Section03

Show
Ignore:
Timestamp:
12/29/09 20:00:05 (16 years ago)
Author:
cfgrok (IP: 64.30.223.5)
Comment:

Adding new training section

Legend:

Unmodified
Added
Removed
Modified
  • Training/Section03

    v1 v1  
     1[[TOC(heading=Training Section Contents, Training*)]] 
     2 
     3= Section 3 – Dealing with Data = 
     4 
     5The basis of all applications involves retrieving information from a data source and putting information back into a data source. A helpful acronym CRUD represents the various phases of single data record management. 
     6 
     7'''C''' - (create) create a data record or entity 
     8 
     9'''R''' - (read) retrieval of a specific data record or entity 
     10 
     11'''U''' - (update) modification and persistence of a data record or entity 
     12 
     13'''D''' - (delete) removal of a specific data record or entity 
     14 
     15 
     16In our applications, we work with all phases of CRUD. List is another phase of data management commonly used in applications. List deals with retrieval of multiple data records, but isn't included in CRUD because it ruins the cleverness. 
     17 
     18Each phase has an associated pattern in Model-Glue. By learning these patterns, you will be able to quickly develop applications that work with data. Also, by understanding these patterns, you will be able to quickly understand Model-Glue applications written by other developers.  
     19 
     20It is important to note, Model-Glue enforces the Model/View/Controller (MVC) pattern. As such, much of Model-Glue specific development revolves around the Controller and to a lesser extent the View. The Model is generally left unaware of Model-Glue as a best practice. 
     21 
     22== And Look at This One I Made Before == 
     23 
     24In this module, we will deal with a number of pre-made files. These files contain content that is not relevant to Model-Glue. In some cases, HTML markup has been built for you, in other cases model objects have been made for you. Since this class is compressed, we'll explain how all these relate together and do exercises on the Model-Glue specific items. 
     25 
     26== Exercise: Dealing with Data == 
     27 
     28 * Open up your /PlantOMatic/config/ModelGlue.xml 
     29 * Paste the following Controller definition inside the <controllers> section 
     30 
     31{{{ 
     32 
     33<controller id="ShipmentController" type="PlantOMatic.controller.ShipmentController"> 
     34  <message-listener message="handleShipment" /> 
     35  <message-listener message="needShipmentBean" /> 
     36  <message-listener message="needShipmentDetail" /> 
     37  <message-listener message="needShipmentList" /> 
     38</controller> 
     39 
     40}}} 
     41 
     42 * Paste the following Event Handler definitions inside the <event-handlers> section 
     43 
     44{{{ 
     45 
     46<event-handler name="Shipment.List" type="templatedPage"> 
     47  <broadcasts> 
     48    <message name="needShipmentList" /> 
     49  </broadcasts> 
     50  <views> 
     51    <include name="Primary" template="List.Shipment.cfm"> 
     52      <value name="xe_Display" value="Shipment.Display" /> 
     53      <value name="xe_Form" value="Shipment.Form" /> 
     54    </include> 
     55  </views>               
     56</event-handler> 
     57 
     58<event-handler name="Shipment.Display" type="templatedPage"> 
     59  <broadcasts> 
     60    <message name="needShipmentDetail" /> 
     61  </broadcasts>                  
     62  <views> 
     63    <include name="primary" template="Display.Shipment.cfm"> 
     64      <value name="xe_ShipmentList" value="Shipment.List" /> 
     65      <value name="xe_ItemForm" value="Item.Form" /> 
     66      <value name="xe_ShipmentDisplay" value="Shipment.Display" /> 
     67      <value name="xe_ItemRemove" value="doItem.Remove" /> 
     68    </include> 
     69  </views> 
     70</event-handler> 
     71 
     72<event-handler name="Shipment.Form" type="templatedPage"> 
     73  <broadcasts> 
     74    <message name="needShipmentBean" /> 
     75  </broadcasts> 
     76  <views> 
     77    <include name="Primary" template="Form.Shipment.cfm"> 
     78      <value name="xe_HandleForm" value="doShipment.Form" /> 
     79    </include> 
     80  </views>               
     81</event-handler> 
     82 
     83<event-handler name="doShipment.Form"> 
     84  <broadcasts> 
     85    <message name="handleShipment" /> 
     86  </broadcasts> 
     87  <results> 
     88    <result name="Failure" do="Shipment.Form" /> 
     89    <result name="Success" do="Shipment.List" redirect="true" /> 
     90  </results> 
     91</event-handler> 
     92 
     93}}} 
     94 
     95 * Open /PlantOMatic/config/ColdSpring.xml and add the following bean definition 
     96 
     97{{{ 
     98 
     99<bean id="ShipmentManager" class="PlantOMatic.model.ShipmentManager"> 
     100  <constructor-arg name="dsn"> 
     101    <bean factory-bean="reactorConfiguration" factory-method="getDsn" /> 
     102  </constructor-arg> 
     103</bean> 
     104 
     105}}} 
     106 
     107 * Create a new file in /PlantOMatic/controller named !ShipmentController.cfc 
     108 * Use the following code as the body of !ShipmentController.cfc 
     109 
     110{{{ 
     111 
     112<cfcomponent output="false" hint="I am a Model-Glue controller." extends="ModelGlue.gesture.controller.Controller" beans="ShipmentManager"> 
     113  <cffunction name="handleShipment" output="false" access="public" returntype="void"  
     114        hint="I handle a submitted shipment list"> 
     115    <cfargument name="event" type="any" /> 
     116    <cfset var shipment = arguments.event.makeEventBean( "PlantOMatic.model.Shipment" ) /> 
     117    <cfset var MessageContainer = arguments.event.getValue( "MessageContainer" ) /> 
     118    <cfif shipment.validate( MessageContainer ) IS true> 
     119      <cfset beans.ShipmentManager.save( shipment ) /> 
     120      <cfset MessageContainer.addSuccess( "Your Shipment was added!" )> 
     121      <cfset arguments.event.addResult( "Success" ) /> 
     122    <cfelse> 
     123      <cfset MessageContainer.addError( "Your Shipment could not be added, probably because you didn't add a name" ) /> 
     124      <cfset arguments.event.addResult( "Failure" ) />                   
     125    </cfif> 
     126  </cffunction> 
     127 
     128  <cffunction name="needShipmentBean" output="false" access="public" returntype="void"  
     129        hint="I find and return information about a shipment"> 
     130    <cfargument name="event" type="any" /> 
     131    <cfset var Shipment = arguments.event.makeEventBean( "PlantOMatic.model.Shipment", "ShipmentID" ) /> 
     132    <cfset Shipment = beans.ShipmentManager.load( Shipment ) /> 
     133    <cfset arguments.event.setValue( "ShipmentBean",  arguments.event.makeEventBean( Shipment ) ) /> 
     134  </cffunction> 
     135 
     136  <cffunction name="needShipmentDetail" output="false" access="public" returntype="void"  
     137        hint="I return detail of a specific Shipment"> 
     138    <cfargument name="event" type="any" /> 
     139    <cfset var Shipment = arguments.event.makeEventBean( "PlantOMatic.model.Shipment", "ShipmentID" ) /> 
     140    <cfset arguments.event.setValue( "ShipmentDetail", beans.ShipmentManager.loadDetail( Shipment ) ) /> 
     141  </cffunction>  
     142 
     143  <cffunction name="needShipmentList" output="false" access="public" returntype="void"  
     144        hint="I return stored shipments"> 
     145    <cfargument name="event" type="any" /> 
     146    <cfset arguments.event.setValue( "ShipmentList", beans.ShipmentManager.loadCatalog() ) /> 
     147  </cffunction>          
     148 
     149</cfcomponent> 
     150 
     151}}} 
     152 
     153 * Open /PlantOMatic/views/Display.Shipment.cfm and prepend the following: 
     154 
     155{{{ 
     156 
     157<cfsilent> 
     158  <cfset event.copyToScope( variables,  "ShipmentDetail,xe_ItemForm,xe_ItemRemove,xe_ShipmentList,xe_ShipmentDisplay" ) /> 
     159  <cfset linkItemForm = event.linkTo( xe_ItemForm, "shipmentID" ) />     
     160  <cfset linkPrint = event.linkTo(  xe_ShipmentDisplay, "shipmentID" ) /> 
     161  <cfset linkItemRemove = event.linkTo( xe_ItemRemove, "shipmentID" ) /> 
     162  <cfset linkShipment = event.linkTo( xe_ShipmentList ) /> 
     163</cfsilent> 
     164 
     165}}} 
     166 
     167 * Open /PlantOMatic/views/Form.Shipment.cfm and prepend the following: 
     168 
     169{{{ 
     170 
     171<cfsilent> 
     172  <cfimport prefix="uform" taglib="/PlantOMatic/uniform" /> 
     173  <cfset event.copyToScope( variables, "CFUniformConfig,ShipmentBean,xe_HandleForm" ) /> 
     174  <cfset handleSubmitForm = event.linkto( xe_HandleForm ) /> 
     175  <cfset bean = ShipmentBean /> 
     176</cfsilent> 
     177 
     178}}} 
     179 
     180 * Open /PlantOMatic/views/List.Shipment.cfm and prepend the following: 
     181 
     182{{{ 
     183 
     184<cfsilent> 
     185  <cfset event.copyToScope( variables, "ShipmentList,xe_Display,xe_Form" ) /> 
     186  <cfset linkDisplay = event.linkTo( xe_Display ) /> 
     187  <cfset linkEdit = event.linkTo( xe_Form ) /> 
     188  <cfset linkPrint = event.linkTo( xe_Display ) & "&requestformat=print" /> 
     189</cfsilent> 
     190 
     191}}} 
     192 
     193== Putting It All Together == 
     194 
     195Once you successfully complete the above tasks, run your application and click the home navigation element. You should see a screen load with the title of Saved Shipments (see figure below). You may interact with this screen, pressing Edit, Print, Display and the other links in the User Interface. All of them will work, except for New Item. We'll cover New Item in a future section. 
     196 
     197[[Image(training03-1.png, nolink)]][[BR]] 
     198'' Shipment List View '' 
     199 
     200---- 
     201 
     202Back: [wiki:Training/Section02 Training Section 2: Show Me Some Skinning] 
     203 
     204Next: [wiki:Training/Section04 Training Section 4: Request Formats Are Your Friend]