Changeset 1893

Show
Ignore:
Timestamp:
11/24/08 03:52:51 (4 years ago)
Author:
lmajano
Message:

Finalization of Model Objects.

Location:
coldbox/trunk
Files:
2 added
1 removed
4 modified

Legend:

Unmodified
Added
Removed
  • coldbox/trunk/system/config/readme.txt

    r1891 r1893  
    223223 * #640 New Application Template and Code Generators 
    224224 * #641 New component metadata attributes for autowiring control: autowire_stoprecursion (classname), autowire_setterinjection (boolean) 
    225  
     225 * #642 New Model Integration using new model conventions and methods: getModel() and populateModel() and more. 
     226  
    226227== Version 2.6.1 (August 2008) CODENAME: FAITH == 
    227228 
  • coldbox/trunk/system/frameworkSupertype.cfc

    r1818 r1893  
    100100<!------------------------------------------- FRAMEWORK FACADES -------------------------------------------> 
    101101 
     102        <!--- Get Model ---> 
     103        <cffunction name="getModel" access="public" returntype="any" hint="Create or retrieve model objects by convention" output="false" > 
     104                <!--- ************************************************************* ---> 
     105                <cfargument name="name"                                 required="true"  type="string" hint="The name of the model to retrieve"> 
     106                <cfargument name="useSetterInjection"   required="false" type="boolean" default="false" hint="Whether to use setter injection alongside the annotations property injection. cfproperty injection takes precedence."> 
     107                <cfargument name="onDICompleteUDF"              required="false" type="string"  default="onDIComplete" hint="After Dependencies are injected, this method will look for this UDF and call it if it exists. The default value is onDIComplete"> 
     108                <cfargument name="debugMode"                    required="false" type="boolean" default="false" hint="Debugging Mode or not"> 
     109                <!--- ************************************************************* ---> 
     110                <cfreturn getPlugin("beanFactory").getModel(argumentCollection=arguments)> 
     111        </cffunction> 
     112         
     113        <!--- Populate a model object from the request Collection ---> 
     114        <cffunction name="populateModel" access="public" output="false" returntype="Any" hint="Populate a named or instantiated model (java/cfc) from the request collection items"> 
     115                <!--- ************************************************************* ---> 
     116                <cfargument name="model"                        required="true"  type="any"     hint="The name of the model to get and populate or the acutal model object. If you already have an instance of a model, then use the populateBean() method"> 
     117                <cfargument name="scope"                        required="false" type="string"  default=""   hint="Use scope injection instead of setters population. Ex: scope=variables.instance."/> 
     118                <cfargument name="trustedSetter"        required="false" type="boolean" default="false" hint="If set to true, the setter method will be called even if it does not exist in the bean"/> 
     119                <!--- ************************************************************* ---> 
     120                <cfreturn getPlugin("beanFactory").populateModel(argumentCollection=arguments)> 
     121        </cffunction> 
     122 
    102123        <!--- View Rendering Facades ---> 
    103124        <cffunction name="renderView"         access="private" hint="Facade to plugin's render view." output="false" returntype="Any"> 
  • coldbox/trunk/system/plugins/beanFactory.cfc

    r1891 r1893  
    2323                <!--- ************************************************************* ---> 
    2424                <cfscript> 
     25                        var modelMappingsFile = "/"; 
     26                         
     27                        /* Super Init */ 
    2528                        super.Init(arguments.controller); 
    2629                         
     
    3336                        setDICacheDictionary(CreateObject("component","coldbox.system.util.BaseDictionary").init('DIMetadata')); 
    3437                         
     38                        /* Model Mappings */ 
     39                        instance.modelMappings = structnew(); 
     40                         
     41                        /* Run Model Mappings */ 
     42                        if( fileExists(getSetting("ApplicationPath") & "config/modelMappings.cfm") ){ 
     43                                try{ 
     44                                        /* If AppMapping is not Blank check */ 
     45                                        if( getSetting('AppMapping') neq "" ){ 
     46                                                modelMappingsFile = modelMappingsFile & getSetting('AppMapping'); 
     47                                        } 
     48                                        modelMappingsFile = modelMappingsFile & "/config/modelMappings.cfm"; 
     49                                        /* Include it */ 
     50                                        include(modelMappingsFile); 
     51                                } 
     52                                catch(Any e){ 
     53                                        throw("Error including model mappings file: #e.message#",e.detail,"plugin.beanFactory.ModelMappingsIncludeException"); 
     54                                } 
     55                        } 
    3556                         
    3657                        /* Return instance */ 
     
    4061 
    4162<!------------------------------------------- PUBLIC -------------------------------------------> 
     63         
     64        <!--- Get Model Mappings ---> 
     65        <cffunction name="getModelMappings" access="public" returntype="struct" hint="Get the model mappings structure" output="false" > 
     66                <cfreturn instance.modelMappings> 
     67        </cffunction> 
     68         
     69        <!--- Add Model Mapping ---> 
     70        <cffunction name="addModelMapping" access="public" returntype="void" hint="Add a new model mapping. Ex: addModelMapping('myBean','security.test.FormBean')" output="false" > 
     71                <!--- ************************************************************* ---> 
     72                <cfargument name="alias" required="true" type="string" hint="The model alias"> 
     73                <cfargument name="model" required="true" type="string" hint="The model class path (From the model conventions downward)"> 
     74                <!--- ************************************************************* ---> 
     75                <cfset var mappings = getModelMappings()> 
     76                <cfset mappings[arguments.alias] = arguments.model> 
     77        </cffunction> 
    4278 
    4379        <!--- Just create and call init, simple ---> 
     
    79115                        var ModelsPath = getSetting("ModelsPath"); 
    80116                        var ModelsInvocationPath = getSetting("ModelsInvocationPath"); 
    81                         var checkPath = ModelsPath & "/" & replace(arguments.name,".","/","all") & ".cfc"; 
    82                         var modelClassPath = ModelsInvocationPath & "." & arguments.name; 
     117                        var checkPath = 0; 
     118                        var modelClassPath = 0; 
    83119                        var md = 0; 
    84                 </cfscript> 
    85                  
     120                        var modelMappings = getModelMappings(); 
     121                         
     122                        /* Resolve name in Alias Checks */ 
     123                        if( structKeyExists(modelMappings,arguments.name) ){ 
     124                                arguments.name = modelMappings[arguments.name]; 
     125                        } 
     126                        /* Setup Paths */ 
     127                        checkPath = ModelsPath & "/" & replace(arguments.name,".","/","all") & ".cfc"; 
     128                        modelClassPath = ModelsInvocationPath & "." & arguments.name; 
     129                </cfscript> 
    86130                 
    87131                <!--- Verify if model exists in cache ---> 
    88132                <cfif ( getColdboxOCM().lookup(arguments.name) )> 
    89133                        <cfset oModel = getColdBoxOCM().get(arguments.name)> 
    90                 <!--- Else Create It ---> 
     134                <!--- Else Create It if it exists ---> 
    91135                <cfelseif ( fileExists(checkPath) )> 
    92136                        <cflock name="beanfactory.createmodel.#arguments.name#" type="exclusive" timeout="20" throwontimeout="true"> 
     
    132176                <cfreturn oModel> 
    133177        </cffunction> 
     178         
     179        <!--- Populate a model object from the request Collection ---> 
     180        <cffunction name="populateModel" access="public" output="false" returntype="Any" hint="Populate a named or instantiated model (java/cfc) from the request collection items"> 
     181                <!--- ************************************************************* ---> 
     182                <cfargument name="model"                        required="true"  type="any"     hint="The name of the model to get and populate or the acutal model object. If you already have an instance of a model, then use the populateBean() method"> 
     183                <cfargument name="scope"                        required="false" type="string"  default=""   hint="Use scope injection instead of setters population. Ex: scope=variables.instance."/> 
     184                <cfargument name="trustedSetter"        required="false" type="boolean" default="false" hint="If set to true, the setter method will be called even if it does not exist in the bean"/> 
     185                <!--- ************************************************************* ---> 
     186                <cfscript> 
     187                        var rc = controller.getRequestService().getContext().getCollection(); 
     188                        var oModel = 0; 
     189                         
     190                        /* Do we have a model or name */ 
     191                        if( isSimpleValue(arguments.model) ){ 
     192                                oModel = getModel(model); 
     193                        } 
     194                        else{ 
     195                                oModel = arguments.model; 
     196                        } 
     197                         
     198                        /* Inflate from Request Collection */ 
     199                        return populateFromStruct(oModel,rc,arguments.scope,arguments.trustedSetter);                    
     200                </cfscript> 
     201        </cffunction> 
    134202 
    135203        <!--- Populate a bean from the request Collection ---> 
    136204        <cffunction name="populateBean" access="public" output="false" returntype="Any" hint="Populate a named or instantiated bean (java/cfc) from the request collection items"> 
    137205                <!--- ************************************************************* ---> 
    138                 <cfargument name="formBean"             required="true"         type="any"      hint="This can be an instantiated bean object or a bean instantitation path as a string. If you pass an instantiation path and the bean has an 'init' method. It will be executed. This method follows the bean contract (set{property_name}). Example: setUsername(), setfname()"> 
     206                <cfargument name="formBean"             required="true"         type="any"      hint="This can be an instantiated bean object or a bean instantitation path as a string. This method follows the bean contract (set{property_name}). Example: setUsername(), setfname()"> 
    139207                <cfargument name="scope"                        required="false"        type="string"   default=""   hint="Use scope injection instead of setters population. Ex: scope=variables.instance."/> 
    140208                <cfargument name="trustedSetter"        required="false" type="boolean" default="false" hint="If set to true, the setter method will be called even if it does not exist in the bean"/> 
     
    144212                         
    145213                        /* Inflate from Request Collection */ 
    146                         return populateFromStruct(arguments.formBean,rc,arguments.scope);                        
     214                        return populateFromStruct(arguments.formBean,rc,arguments.scope,arguments.trustedSetter);                        
    147215                </cfscript> 
    148216        </cffunction> 
     
    163231                         
    164232                        /* populate and return */ 
    165                         return populateFromStruct(arguments.formBean,inflatedStruct,arguments.scope); 
     233                        return populateFromStruct(arguments.formBean,inflatedStruct,arguments.scope,arguments.trustedSetter); 
    166234                </cfscript> 
    167235        </cffunction> 
     
    316384                /* We are now assured that the DI cache has data. */ 
    317385                targetDIEntry = getDICacheDictionary().getKey(targetCacheKey); 
     386                 
    318387                /* Do we Inject Dependencies, are we AutoWiring */ 
    319388                if ( targetDIEntry.autowire ){ 
     
    329398                                thisDependency = targetDIEntry.dependencies[x]; 
    330399                                 
    331                                 /* Determine Type of Injection */ 
     400                                /* Determine Type of Injection according to Type */ 
    332401                                if( thisDependency.type eq "ioc" ){ 
    333402                                        injectIOC(thisDependency,targetObject,arguments.debugMode); 
     
    336405                                        injectOCM(thisDependency,targetObject,arguments.debugMode); 
    337406                                } 
    338                                 else{ 
     407                                else if ( listFirst(thisDependency.type,":") eq "coldbox" ){ 
    339408                                        /* Try to inject coldbox dependencies */ 
    340409                                        injectColdboxDSL(thisDependency,targetObject,arguments.debugMode); 
    341                                 }                                                        
     410                                } 
     411                                else if ( listFirst(thisDependency.type,":") eq "model" ){ 
     412                                        /* Try to inject coldbox dependencies */ 
     413                                        injectModelDSL(thisDependency,targetObject,arguments.debugMode); 
     414                                }                                                                
    342415                                 
    343416                        }//end for loop of dependencies. 
     
    354427<!------------------------------------------- PRIVATE -------------------------------------------> 
    355428         
    356         <!--- injectColdboxDSL ---> 
    357         <cffunction name="injectColdboxDSL" access="private" returntype="void" hint="Inject dependencies using the coldbox dependency DSL" output="false" > 
     429        <!--- injectModelDSL ---> 
     430        <cffunction name="injectModelDSL" access="private" returntype="void" hint="Inject dependencies using the model dependency DSL" output="false" > 
    358431                <!--- ************************************************************* ---> 
    359432                <cfargument name="Definition"   required="true" type="any" hint="The dependency definition structure"> 
     
    369442                        var locatedDependency = "_NOT_FOUND_"; 
    370443                         
    371                         /* 1 stage dependency */ 
    372                         if( thisTypeLen eq 1 ){ 
    373                                 /* Coldbox Reference is the only one available */ 
    374                                 locatedDependency = getController(); 
    375                         } 
    376                         /* 2 stage dependencies */ 
    377                         else if(thisTypeLen eq 2){ 
    378                                 /* configBean or mailsettingsbean */ 
    379                                 thisLocationType = listLast(thisType,":"); 
    380                                 if( thisLocationType eq "configbean" ){ 
    381                                         locatedDependency = getSettingsBean(); 
    382                                 }        
    383                                 else if( thisLocationType eq "mailsettingsbean" ){ 
    384                                         locatedDependency = getMailSettings(); 
    385                                 } 
    386                         } 
    387                         /* 3 stage dependencies */ 
     444                        /* 2 stage dependency dsl */ 
     445                        if(thisTypeLen eq 2){ 
     446                                thisLocationType = getToken(thisType,2,":"); 
     447                                /* Get model object*/ 
     448                                locatedDependency = getModel(thisLocationType); 
     449                        } 
     450                        /* 3 stage dependency dsl */ 
    388451                        else if(thisTypeLen eq 3){ 
    389452                                thisLocationType = getToken(thisType,2,":"); 
    390453                                thisLocationKey = getToken(thisType,3,":"); 
    391                                 /* Fork on types */ 
    392                                 if( thisLocationType eq "setting" ){ 
    393                                         locatedDependency = getSetting(thisLocationKey); 
    394                                 } 
    395                                 else if( thisLocationType eq "plugin" ){ 
    396                                         locatedDependency = getPlugin(thisLocationKey); 
    397                                 } 
    398                                 else if( thisLocationType eq "myplugin" ){ 
    399                                         locatedDependency = getMyPlugin(thisLocationKey); 
    400                                 } 
    401                                 else if( thisLocationType eq "datasource" ){ 
    402                                         locatedDependency = getDatasource(thisLocationKey); 
    403                                 } 
    404                                 else if( thisLocationType eq "model" ){ 
    405                                         locatedDependency = getModel(thisLocationKey); 
    406                                 } 
     454                                /* Call model method to get dependency */ 
     455                                locatedDependency = evaluate("getModel(thisLocationType).#thisLocationKey#()"); 
    407456                        }//end 3 stage DSL 
    408457                         
     
    427476                </cfscript> 
    428477        </cffunction> 
     478         
     479        <!--- injectColdboxDSL ---> 
     480        <cffunction name="injectColdboxDSL" access="private" returntype="void" hint="Inject dependencies using the coldbox dependency DSL" output="false" > 
     481                <!--- ************************************************************* ---> 
     482                <cfargument name="Definition"   required="true" type="any" hint="The dependency definition structure"> 
     483                <cfargument name="targetObject" required="true" type="any" hint="The target object"> 
     484                <cfargument name="debugMode"    required="true" type="boolean" hint="The debug mode"> 
     485                <!--- ************************************************************* ---> 
     486                <cfscript> 
     487                        var thisDependency = arguments.Definition; 
     488                        var thisType = thisDependency.type; 
     489                        var thisTypeLen = listLen(thisType,":"); 
     490                        var thisLocationType = ""; 
     491                        var thisLocationKey = ""; 
     492                        var locatedDependency = "_NOT_FOUND_"; 
     493                         
     494                        /* 1 stage dependency */ 
     495                        if( thisTypeLen eq 1 ){ 
     496                                /* Coldbox Reference is the only one available on 1 stage DSL */ 
     497                                locatedDependency = getController(); 
     498                        } 
     499                        /* 2 stage dependencies. Model:Test or Coldbox:etc */ 
     500                        else if(thisTypeLen eq 2){ 
     501                                thisLocationKey = getToken(thisType,2,":"); 
     502                                if( thisLocationKey eq "configbean" ){ 
     503                                        locatedDependency = getSettingsBean(); 
     504                                }        
     505                                else if( thisLocationKey eq "mailsettingsbean" ){ 
     506                                        locatedDependency = getMailSettings(); 
     507                                } 
     508                        } 
     509                        /* 3 stage dependencies */ 
     510                        else if(thisTypeLen eq 3){ 
     511                                thisLocationType = getToken(thisType,2,":"); 
     512                                thisLocationKey = getToken(thisType,3,":"); 
     513                                /* Fork on types */ 
     514                                if( thisLocationType eq "setting" ){ 
     515                                        locatedDependency = getSetting(thisLocationKey); 
     516                                } 
     517                                else if( thisLocationType eq "plugin" ){ 
     518                                        locatedDependency = getPlugin(thisLocationKey); 
     519                                } 
     520                                else if( thisLocationType eq "myplugin" ){ 
     521                                        locatedDependency = getMyPlugin(thisLocationKey); 
     522                                } 
     523                                else if( thisLocationType eq "datasource" ){ 
     524                                        locatedDependency = getDatasource(thisLocationKey); 
     525                                } 
     526                        }//end 3 stage DSL 
     527                         
     528                        /* Verify injetion */ 
     529                        if( isSimpleValue(locatedDependency) AND locatedDependency EQ "_NOT_FOUND_" ){ 
     530                                /* Only log if debugmode, else no injection */ 
     531                                if( arguments.debugMode ){ 
     532                                        getPlugin("logger").logEntry("warning","Dependency: #thisDependency.toString()# --> not found in factory"); 
     533                                } 
     534                        } 
     535                        else{ 
     536                                /* Inject Dependency */ 
     537                                injectBean(targetBean=arguments.targetObject, 
     538                                                   beanName=thisDependency.name, 
     539                                                   beanObject=locatedDependency, 
     540                                                   scope=thisDependency.scope); 
     541                                /* Debug Mode Check */ 
     542                                if( arguments.debugMode ){ 
     543                                        getPlugin("logger").logEntry("information","Dependency: #thisDependency.toString()# --> injected into #getMetadata(arguments.targetObject).name#."); 
     544                                } 
     545                        } 
     546                </cfscript> 
     547        </cffunction> 
    429548 
    430549        <!--- injectIOC ---> 
     
    501620                        var cbox_reserved_functions = "setSetting,setDebugMode,setNextEvent,setNextRoute,setController,settingExists,setPluginName,setPluginVersion,setPluginDescription,setProperty,setproperties"; 
    502621                        var foundDependencies = ""; 
     622                        var DSLNamespaces = "coldbox,ioc,ocm"; 
    503623                         
    504624                        /* Look for Object's attributes, and override if found. */ 
     
    513633                        if( structKeyExists(md,"properties") and ArrayLen(md.properties) gt 0){ 
    514634                                for(x=1; x lte ArrayLen(md.properties); x=x+1 ){ 
    515                                         /* Check types */ 
     635                                        /* Check types are valid for autowiring. */ 
    516636                                        if( structKeyExists(md.properties[x],"type") AND  
    517                                            (md.properties[x].type eq "ioc" OR md.properties[x].type eq "ocm" OR findNoCase("coldbox",md.properties[x].type)) ){ 
     637                                                ( listFindNoCase(DSLNamespaces,md.properties[x].type) OR 
     638                                                  findnocase("model",md.properties[x].type) OR 
     639                                                  findnocase("coldbox",md.properties[x].type) )          
     640                                        ){ 
    518641                                                /* New MD Entry */ 
    519642                                                entry = structnew(); 
  • coldbox/trunk/testharness/handlers/ehGeneral.cfc

    r1891 r1893  
    2727        <cfproperty name="MailSettingsBean"                     type="coldbox:mailsettingsBean"         scope="instance"> 
    2828        <cfproperty name="MySiteDSN"                                    type="coldbox:datasource:mysite"        scope="instance"> 
    29         <cfproperty name="testModel"                                    type="coldbox:model:testModel"          scope="instance"> 
     29        <cfproperty name="testModel"                                    type="model:testModel"                          scope="instance"> 
     30        <cfproperty name="initDate"                                     type="model:formBean:getinitDate"       scope="instance"> 
    3031         
    3132 
     
    9293            <cfset var complexStruct = ""> 
    9394            <cfset var complete = ""> 
    94                 <cfdump var="#instance#"><cfabort> 
    95                 <!--- <cfset Event.setValue("MailBean",getmyMailSettings())> ---> 
     95                 
     96                <!--- <cfset Event.setValue("MailBean",getmyMailSettings())> ---> 
    9697                <cfset Event.setValue("MailBean",getmyMailSettings())> 
    9798 
     
    205206                var rc = Event.getCollection(); 
    206207                //populate bean 
    207                 rc.FormBean = controller.getPlugin("beanfactory").populateBean("#controller.getSetting("AppMapping")#.model.formBean"); 
     208                rc.FormBean = populateModel("MyFormBean"); 
    208209                Event.setView("vwFormBean"); 
    209210                </cfscript>