| 1 | <!----------------------------------------------------------------------- |
|---|
| 2 | ******************************************************************************** |
|---|
| 3 | Copyright 2005-2007 ColdBox Framework by Luis Majano and Ortus Solutions, Corp |
|---|
| 4 | www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com |
|---|
| 5 | ******************************************************************************** |
|---|
| 6 | |
|---|
| 7 | Author: Luis Majano |
|---|
| 8 | Date: July 28, 2006 |
|---|
| 9 | Description: This is the framework's simple bean factory. |
|---|
| 10 | |
|---|
| 11 | Modifications: |
|---|
| 12 | 07/29/2006 - Added more hints. |
|---|
| 13 | 12/08/2006 - Added makeBean method thanks to Sana Ullah. It will create and or populate a bean with the same request collection field names. |
|---|
| 14 | 03/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 | <cfset evaluate("beanInstance.set#FieldKey#(fields[FieldKey])")> |
|---|
| 82 | </cfif> |
|---|
| 83 | </cfloop> |
|---|
| 84 | |
|---|
| 85 | <cfcatch type="any"> |
|---|
| 86 | <cfthrow type="Framework.plugins.beanFactory.PopulateBeanException" message="Error populating bean." detail="#cfcatch.Detail#<br>#cfcatch.message#"> |
|---|
| 87 | </cfcatch> |
|---|
| 88 | </cftry> |
|---|
| 89 | |
|---|
| 90 | <cfreturn beanInstance /> |
|---|
| 91 | </cffunction> |
|---|
| 92 | |
|---|
| 93 | <!--- ************************************************************* ---> |
|---|
| 94 | |
|---|
| 95 | </cfcomponent> |
|---|