OOPhoto: Facade Layer Integrated Between Controller And Service Layers

<cfcomponent
	extends="BaseFacade"
	output="false"
	hint="I provide value-based methods for Photo Gallery functionality.">
 
 
	<!---
		The following items will be injected:
 
		VARIABLES.PhotoGalleryService
		VARIABLES.PhotoService
	--->
 
 
	<cffunction
		name="Init"
		access="public"
		returntype="any"
		output="false"
		hint="I return an initialized object.">
 
		<!--- Return This reference. --->
		<cfreturn THIS />
	</cffunction>
 
 
	<cffunction
		name="DeletePhotoGallery"
		access="public"
		returntype="any"
		output="false"
		hint="I delete the photo gallery with the given ID. If the ID is invalid, I throw an exception.">
 
		<!--- Define arguments. --->
		<cfargument
			name="ID"
			type="numeric"
			required="true"
			hint="I am the ID of the photo gallery to delete."
			/>
 
		<!--- Define the local scope. --->
		<cfset var LOCAL = {} />
 
		<!--- Get the photo gallery. --->
		<cfset LOCAL.PhotoGallery = VARIABLES.PhotoGalleryService
			.Load( ARGUMENTS.ID )
			/>
 
		<!--- Delete the photo galleru and return. --->
		<cfreturn LOCAL.PhotoGallery.DeleteWithTransaction() />
	</cffunction>
 
 
	<cffunction
		name="GetPhotoGalleriesByKeyword"
		access="public"
		returntype="array"
		output="false"
		hint="I return an array of photo galleries that match the given keywords.">
 
		<!--- Define arguments. --->
		<cfargument
			name="Keywords"
			type="string"
			required="true"
			hint="I am the keywords on which we are searching."
			/>
 
		<!--- Return keyword search. --->
		<cfreturn VARIABLES.PhotoGalleryService
			.GetGalleriesByKeyword( ARGUMENTS.Keywords )
			/>
	</cffunction>
 
 
	<cffunction
		name="GetPhotoGallery"
		access="public"
		returntype="any"
		output="false"
		hint="I load the photo gallery with the given ID. If the ID is invalid, I throw an exception, unless flagged to return an empty gallery object.">
 
		<!--- Define arguments. --->
		<cfargument
			name="ID"
			type="numeric"
			required="true"
			hint="I am the ID of the photo gallery to load."
			/>
 
		<cfargument
			name="ReturnNewIfInvalid"
			type="boolean"
			required="false"
			default="false"
			hint="I am a boolean that flags the desire to return a New() photo gallery rather than raising an excpetion if the given ID is invalid."
			/>
 
		<!--- Define the local scope. --->
		<cfset var LOCAL = {} />
 
		<!--- Try to load the photo gallery. --->
		<cftry>
 
			<!--- Get the photo gallery. --->
			<cfset LOCAL.PhotoGallery = VARIABLES.PhotoGalleryService
				.Load( ARGUMENTS.ID )
				/>
 
			<!--- Catch error if gallery is invalid. --->
			<cfcatch>
 
				<!---
					The photo gallery was not valid - check to
					see if the user wants to raise an exception
					or return a new gallery.
				--->
				<cfif ARGUMENTS.ReturnNewIfInvalid>
 
					<!--- Load new gallery. --->
					<cfset LOCAL.PhotoGallery =
						VARIABLES.PhotoGalleryService.New() />
 
				<cfelse>
 
					<!--- No override - rethrow exception. --->
					<cfrethrow />
 
				</cfif>
 
			</cfcatch>
		</cftry>
 
		<!--- Return the photo gallery. --->
		<cfreturn LOCAL.PhotoGallery />
	</cffunction>
 
 
	<cffunction
		name="GetPhotoGalleryByJumpCode"
		access="public"
		returntype="any"
		output="false"
		hint="I return the photo gallery with the given jump code. If the jump code is invalid, I throw an exception.">
 
		<!--- Define arguments. --->
		<cfargument
			name="JumpCode"
			type="string"
			required="true"
			hint="I am the jump code on which we are searching."
			/>
 
		<!--- Return gallery. --->
		<cfreturn VARIABLES.PhotoGalleryService
			.GetGalleryByJumpCode( ARGUMENTS.JumpCode )
			/>
	</cffunction>
 
 
	<cffunction
		name="SavePhotoGallery"
		access="public"
		returntype="struct"
		output="false"
		hint="I save a photo galery based on the given properties.">
 
		<!--- Define arguments. --->
		<cfargument
			name="ID"
			type="numeric"
			required="false"
			default="0"
			hint="I am the ID of the photo."
			/>
 
		<cfargument
			name="Name"
			type="string"
			required="false"
			default=""
			hint="I am the name of the gallery."
			/>
 
		<cfargument
			name="Description"
			type="string"
			required="false"
			default=""
			hint="I am the description of the gallery."
			/>
 
		<cfargument
			name="PhotoIDList"
			type="string"
			required="false"
			default=""
			hint="I am the list of photo IDs associated with this gallery."
			/>
 
		<!--- Define the local scope. --->
		<cfset var LOCAL = {} />
 
		<!--- Load the photo gallery (or create a new one). --->
		<cfset LOCAL.PhotoGallery = THIS.GetPhotoGallery(
			ARGUMENTS.ID,
			true
			) />
 
		<!--- Set the gallery properties. --->
		<cfset LOCAL.PhotoGallery
			.SetName( ARGUMENTS.Name )
			.SetDescription( ARGUMENTS.Description )
			.SetPhotos(
				VARIABLES.PhotoService.GetPhotosByIDList(
					ARGUMENTS.PhotoIDList
					)
				)
			/>
 
		<!---
			Create the return struct. Because this method needs
			to validate and commit the data, we need a way to
			return errors AND successful data responses.
			Therefore, we need to create a packaging struct.
		--->
		<cfset LOCAL.Return = {
			Success = true,
			Errors = LOCAL.PhotoGallery.Validate(),
			PhotoGallery = LOCAL.PhotoGallery
			} />
 
		<!--- Check to see if we have any validation errors. --->
		<cfif StructCount( LOCAL.Return.Errors )>
 
			<!--- Flag no success. --->
			<cfset LOCAL.Return.Success = false />
 
		<cfelse>
 
			<!--- There were no errors, so save photo gallery. --->
			<cfset LOCAL.PhotoGallery.SaveWithTransaction() />
 
		</cfif>
 
		<!--- Return package. --->
		<cfreturn LOCAL.Return />
	</cffunction>
 
</cfcomponent>

For Cut-and-Paste