PageRenderTime 162ms CodeModel.GetById 60ms app.highlight 2ms RepoModel.GetById 99ms app.codeStats 0ms

/SampleApplication/ColdBox3/model/abstract/AbstractService.cfc

http://github.com/bobsilverberg/ValidateThisColdBoxPlugin
ColdFusion CFScript | 161 lines | 82 code | 23 blank | 56 comment | 5 complexity | 50e45b767262f36c408f3a6f309bf289 MD5 | raw file
  1/**
  2Abstract Service
  3*/
  4component
  5{
  6
  7	/*
  8	---------------------------------------------------------------------------
  9	autowire
 10	---------------------------------------------------------------------------
 11	*/
 12	property name="BeanFactory" type="coldbox:plugin:BeanFactory" scope="instance";
 13	property name="ValidateThis" type="coldbox:myplugin:ValidateThisCBPlugin" scope="instance";
 14
 15
 16	/*
 17	---------------------------------------------------------------------------
 18	instance variables
 19	---------------------------------------------------------------------------
 20	*/
 21	instance = {};
 22
 23	/*
 24	---------------------------------------------------------------------------
 25	constructor
 26	---------------------------------------------------------------------------
 27	*/
 28	any function init()
 29	{
 30		return this;
 31	}
 32
 33	/*
 34	---------------------------------------------------------------------------
 35	public  methods
 36	---------------------------------------------------------------------------
 37	*/
 38
 39	
 40	
 41	/**
 42	* validates, then inserts or updates a persisted entity (if valid)
 43	*/	
 44	ValidateThis.util.Result function save( required entity )
 45	{
 46		var ValidatorResult = "";
 47		
 48		transaction
 49		{
 50			instance.BeanFactory.populateModel( arguments.entity );
 51			ValidatorResult = instance.ValidateThis.validate( theObject=arguments.entity );
 52			
 53			if ( ValidatorResult.getIsSuccess() )
 54			{
 55				// Save entity and commit transaction
 56				EntitySave( arguments.entity );
 57				transactionCommit();
 58			}
 59			else
 60			{
 61				// rollback setter calls
 62				transactionRollback();
 63			}
 64		}
 65		
 66		return ValidatorResult;
 67	}
 68	
 69	/**
 70	* deletes a persisted entity
 71	*/	
 72	void function delete( required entity )
 73	{
 74		transaction
 75		{
 76			EntityDelete( arguments.entity );
 77		}
 78	}
 79	
 80	/**
 81	* returns a new validation result
 82	*/	
 83	ValidateThis.util.Result function newValidationResult()
 84	{
 85		return instance.ValidateThis.newResult();
 86	}
 87	
 88	
 89	/*
 90	---------------------------------------------------------------------------
 91	private methods
 92	---------------------------------------------------------------------------
 93	*/
 94	
 95	/**
 96	* helper method to delete an entity by id
 97	*/
 98	private boolean function deleteById( required entityname, required id )
 99	{
100		var deleted = false;
101		transaction{
102			var entity = EntityLoadByPK( arguments.entityname, arguments.id );
103			if ( !IsNull( entity ) )
104			{
105				EntityDelete( entity );
106				deleted = true;
107			}
108		}
109		
110		return deleted;
111	}
112	
113	/**
114	* returns an array of entities matching the filter
115	*/
116	private array function findByfilter( required entityname, filter=StructNew(), sortorder="", options=StructNew() )
117	{
118		return EntityLoad( arguments.entityname, arguments.filter, arguments.sortorder, arguments.options );
119	}
120		
121	/**
122	* retrieves an entity by name and id
123	*/	
124	private any function getById( required entityname, id="" )
125	{
126		var result = "";
127		if ( Len( arguments.id ) != 0 )
128		{
129			// id passed so retrieve
130			result = EntityLoadByPK( arguments.entityname, arguments.id );
131			if ( IsNull( result ) ) 
132			{
133				// not found
134				result = new( arguments.entityname );
135			}
136		}
137		else
138		{
139			result = new( arguments.entityname );
140		}
141		return result;
142	}
143	
144	
145	/**
146	* return a struct of required fields for the named entity
147	*/	
148	private struct function getRequiredFields( required entityname )
149	{
150		return instance.ValidateThis.getRequiredFields( objectType=arguments.entityname );
151	}
152	
153	/**
154	* returns a new persisted entity
155	*/	
156	private any function new( required entityname )
157	{
158		return EntityNew( arguments.entityname );
159	}
160	
161}