root/coldbox/trunk/src/system/plugins/beanFactory.cfc @ 634

Revision 634, 4.3 kB (checked in by lmajano, 6 years ago)

code cleanup

Line 
1<!-----------------------------------------------------------------------
2********************************************************************************
3Copyright 2005-2007 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
4www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com
5********************************************************************************
6
7Author: Luis Majano
8Date:   July 28, 2006
9Description: This is the framework's simple bean factory.
10
11Modifications:
1207/29/2006 - Added more hints.
1312/08/2006 - Added makeBean method thanks to Sana Ullah. It will create and or populate a bean with the same request collection field names.
1403/22/2007 - Modified Sana's code to only make one call to the trim() method
15----------------------------------------------------------------------->
16<cfcomponent name="beanFactory"
17                         hint="I am a simple bean factory and you can use me if you want."
18                         extends="coldbox.system.plugin"
19                         output="false"
20                         cache="true">
21
22        <!--- ************************************************************* --->
23
24        <cffunction name="init" access="public" returntype="beanFactory" output="false">
25                <cfargument name="controller" type="any" required="true">
26                <cfscript>
27                super.Init(arguments.controller);
28                setpluginName("Bean Factory");
29                setpluginVersion("1.0");
30                setpluginDescription("I am a simple bean factory");
31                return this;
32                </cfscript>
33        </cffunction>
34
35        <!--- ************************************************************* --->
36
37        <cffunction name="create" hint="Create a named bean, simple as that. This method will append {Bean} to the path+name passed in." access="public" output="false" returntype="Any">
38                <!--- ************************************************************* --->
39                <cfargument name="bean"                 required="true"  type="string" hint="The type of bean to create and return. Uses full cfc path mapping.Ex: coldbox.beans.exceptionBean">
40                <cfargument name="callInitFlag" required="false" type="boolean" default="false" hint="Flag to call an init method on the bean.">
41                <!--- ************************************************************* --->
42                <cfscript>
43                try{
44                        if ( arguments.callInitFlag )
45                                return createObject("component","#arguments.bean#").init();
46                        else
47                                return createObject("component","#arguments.bean#");
48                }
49                Catch(Any e){
50                        throw("Error creating bean: #arguments.bean#","#e.Detail#<br>#e.message#","Framework.plugins.beanFactory.BeanCreationException");
51                }
52                </cfscript>
53        </cffunction>
54
55        <!--- ************************************************************* --->
56
57        <cffunction name="populateBean" access="public" output="false" returntype="Any" hint="Populate a named or instantiated bean (java/cfc) from the request collection items">
58                <!--- ************************************************************* --->
59                <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()">
60                <!--- ************************************************************* --->
61                <cfset var beanInstance = "" />
62                <cfset var FieldKey = "" />
63                <cfset var fields = controller.getRequestService().getContext().getCollection() />
64
65                <cftry>
66                        <cfif isSimpleValue(arguments.FormBean)>
67                                <cfset beanInstance = CreateObject("component",arguments.FormBean)>
68                                <cfif structKeyExists(beanInstance,"init")>
69                                        <cfset beanInstance.init()>
70                                </cfif>
71                        <cfelse>
72                                <cfset beanInstance = arguments.FormBean>
73                        </cfif>
74                        <!--- Populate Bean --->
75                        <cfloop collection="#fields#" item="FieldKey">
76                                <cfset FieldKey = Trim(FieldKey)>
77                                <cfif structKeyExists(beanInstance, "set" & FieldKey)>
78                                        <cfinvoke component="#beanInstance#" method="set#FieldKey#">
79                                                <cfinvokeargument name="#FieldKey#" value="#fields[FieldKey]#">
80                                        </cfinvoke>
81                                </cfif>
82                        </cfloop>
83
84                        <cfcatch type="any">
85                                <cfthrow type="Framework.plugins.beanFactory.PopulateBeanException" message="Error populating bean." detail="#cfcatch.Detail#<br>#cfcatch.message#">
86                        </cfcatch>
87                </cftry>
88
89                <cfreturn beanInstance />
90        </cffunction>
91
92        <!--- ************************************************************* --->
93
94</cfcomponent>
Note: See TracBrowser for help on using the browser.