EventContext

Package: MachII.framework
The framework workhorse. Handles event-queue functionality and event-command execution. Controls the event queue and event processing mechanism for a request/event lifecycle.

<!--- License: Copyright 2006 Mach-II Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Copyright: Mach-II Corporation Author: Ben Edwards (ben@ben-edwards.com) $Id: EventContext.cfc 4352 2006-08-29 20:35:15Z pfarrell $ Created version: 1.0.0 Updated version: 1.1.1 Notes: - Added request event name functionality. (pfarrell) --->

Method Summary
public EventContext init(AppManager appManager, [string requestEventName=""])

Initalizes the event-context.

public void announceEvent(string eventName, [struct eventArgs="#StructNew()#"])

Queues an event for the framework to handle.

public void clearEventMappings()

Clears the current event mappings.

public void clearEventQueue()

Clears the event queue.

private Exception createException([string type=""], [string message=""], [string errorCode=""], [string detail=""], [string extendedInfo=""], [array tagContext="#ArrayNew(1)#"])

Creates an exception object (with no cfcatch).

public void displayView(Event event, string viewName, [string contentKey=""], [string contentArg=""], [boolean append="false"])

Displays a view.

private any getAppManager()
public Event getCurrentEvent()

Gets the current event object.

private EventHandler getCurrentEventHandler()
public numeric getEventCount()

Returns the number of events that have been processed for this context.

public string getEventMapping(string eventName)

Gets an event mappiong by the event name.

private SizedQueue getEventQueue()
public string getExceptionEventName()
private string getIsException()
private boolean getIsProcessing()
public numeric getMaxEvents()
public Event getNextEvent()

Peeks at the next event in the queue.

public Event getPreviousEvent()

Returns the previous handled event.

private string getRequestEventName()
private any getViewContext()
private void handleEvent(Event event)

Handles the current event.

public void handleException(Exception exception, [boolean clearEventQueue="true"])

Handles an exception.

private void handleNextEvent()

Handles the next event in the queue.

public boolean hasCurrentEvent()

Checks if the current event has an event object.

public boolean hasMoreEvents()

Checks if there are more events in the queue.

public boolean hasNextEvent()

Peeks at the next event in the queue.

public boolean hasPreviousEvent()

Returns whether or not getPreviousEvent() can be called to return an event.

private void incrementEventCount()

Increments the current event count by 1.

public void processEvents()

Begins processing of queued events. Can only be called once.

private void setAppManager(AppManager appManager)
private void setCurrentEvent(Event currentEvent)
private void setCurrentEventHandler(EventHandler currentEventHandler)
public string setEventMapping(string eventName, string mappingName)

Sets an event mapping.

private void setEventQueue(SizedQueue eventQueue)
public void setExceptionEventName(string exceptionEventName)
private void setIsException(boolean isException)
private void setIsProcessing(boolean isProcessing)
public void setMaxEvents(numeric maxEvents)
private void setPreviousEvent(Event previousEvent)
private void setRequestEventName(string requestEventName)
private void setViewContext(ViewContext viewContext)
private Exception wrapException(any caughtException)

Creates an exception object (with cfcatch).

Method Detail
announceEvent

public void announceEvent( string eventName, [struct eventArgs="#StructNew()#"] )

Queues an event for the framework to handle.

Parameters:
string eventName
[struct eventArgs="#StructNew()#"]

Code:

	<cffunction name="announceEvent" access="public" returntype="void" output="true"
		hint="Queues an event for the framework to handle.">
		<cfargument name="eventName" type="string" required="true" />
		<cfargument name="eventArgs" type="struct" required="false" default="#StructNew()#" />
		
		<cfset var nextEventName = "" />
		<cfset var nextEvent = "" />
		<cfset var exception = "" />
		
		<cftry>
			
			<cfset nextEventName = getEventMapping(arguments.eventName) />
			
			<cfset nextEvent = getAppManager().getEventManager().createEvent(nextEventName, arguments.eventArgs) />
			
			<cfset nextEvent.setRequestName(getRequestEventName()) />
			
			<cfset getEventQueue().put(nextEvent) />
			
			<cfcatch>
				<cfset exception = wrapException(cfcatch) />
				<cfset handleException(exception, true) />
			</cfcatch>
		</cftry>
	</cffunction> 

clearEventMappings

public void clearEventMappings( )

Clears the current event mappings.

Parameters:

Code:

	<cffunction name="clearEventMappings" access="public" returntype="void" output="false"
		hint="Clears the current event mappings.">
		<cfset StructClear(variables.mappings) />
	</cffunction> 

clearEventQueue

public void clearEventQueue( )

Clears the event queue.

Parameters:

Code:

	<cffunction name="clearEventQueue" access="public" returntype="void" output="false"
		hint="Clears the event queue.">
		<cfset getEventQueue().clear() />
	</cffunction> 

createException

private Exception createException( [string type=""], [string message=""], [string errorCode=""], [string detail=""], [string extendedInfo=""], [array tagContext="#ArrayNew(1)#"] )

Creates an exception object (with no cfcatch).

Parameters:
[string type=""]
[string message=""]
[string errorCode=""]
[string detail=""]
[string extendedInfo=""]
[array tagContext="#ArrayNew(1)#"]

Code:

	<cffunction name="createException" access="private" returntype="MachII.util.Exception" output="false"
		hint="Creates an exception object (with no cfcatch).">
		<cfargument name="type" type="string" required="false" default="" />
		<cfargument name="message" type="string" required="false" default="" />
		<cfargument name="errorCode" type="string" required="false" default="" />
		<cfargument name="detail" type="string" required="false" default="" />
		<cfargument name="extendedInfo" type="string" required="false" default="" />
		<cfargument name="tagContext" type="array" required="false" default="#ArrayNew(1)#" />
		
		<cfset var exception = CreateObject('component', 'MachII.util.Exception') />
		<cfset exception.init(arguments.type, arguments.message, arguments.errorCode, arguments.detail, arguments.extendedInfo, arguments.tagContext) />
		<cfset setIsException(true) />
		
		<cfreturn exception />
	</cffunction> 

displayView

public void displayView( Event event, string viewName, [string contentKey=""], [string contentArg=""], [boolean append="false"] )

Displays a view.

Parameters:
Event event
string viewName
[string contentKey=""]
[string contentArg=""]
[boolean append="false"]

Code:

	<cffunction name="displayView" access="public" returntype="void" output="true"
		hint="Displays a view.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		<cfargument name="viewName" type="string" required="true" />
		<cfargument name="contentKey" type="string" required="false" default="" />
		<cfargument name="contentArg" type="string" required="false" default="" />
		<cfargument name="append" type="boolean" required="false" default="false" />
		
		
		<cfset getAppManager().getPluginManager().preView(this) />
		
		<cfset getViewContext().displayView(arguments.event, arguments.viewName, arguments.contentKey, arguments.contentArg, arguments.append) />
		
		
		<cfset getAppManager().getPluginManager().postView(this) />
	</cffunction> 

getAppManager

private any getAppManager( )

Parameters:

Code:

	<cffunction name="getAppManager" access="private" type="MachII.framework.AppManager" output="false">
		<cfreturn variables.appManager />
	</cffunction> 

getCurrentEvent

public Event getCurrentEvent( )

Gets the current event object.

Parameters:

Code:

	<cffunction name="getCurrentEvent" access="public" returntype="MachII.framework.Event" output="false"
		hint="Gets the current event object.">
		<cfreturn variables.currentEvent />
	</cffunction> 

getCurrentEventHandler

private EventHandler getCurrentEventHandler( )

Parameters:

Code:

	<cffunction name="getCurrentEventHandler" access="private" returntype="MachII.framework.EventHandler" output="false">
		<cfreturn variables.currentEventHandler />
	</cffunction> 

getEventCount

public numeric getEventCount( )

Returns the number of events that have been processed for this context.

Parameters:

Code:

	<cffunction name="getEventCount" access="public" returntype="numeric" output="false"
		hint="Returns the number of events that have been processed for this context.">
		<cfreturn variables.eventCount />
	</cffunction> 

getEventMapping

public string getEventMapping( string eventName )

Gets an event mappiong by the event name.

Parameters:
string eventName

Code:

	<cffunction name="getEventMapping" access="public" returntype="string" output="false"
		hint="Gets an event mappiong by the event name.">
		<cfargument name="eventName" type="string" required="true" />
		<cfif StructKeyExists(variables.mappings, arguments.eventName)>
			<cfreturn variables.mappings[arguments.eventName] />
		<cfelse>
			<cfreturn arguments.eventName />
		</cfif>
	</cffunction> 

getEventQueue

private SizedQueue getEventQueue( )

Parameters:

Code:

	<cffunction name="getEventQueue" access="private" returntype="MachII.util.SizedQueue" output="false">
		<cfreturn variables.eventQueue />
	</cffunction> 

getExceptionEventName

public string getExceptionEventName( )

Parameters:

Code:

	<cffunction name="getExceptionEventName" access="public" returntype="string" output="false">
		<cfreturn variables.exceptionEventName />
	</cffunction> 

getIsException

private string getIsException( )

Parameters:

Code:

	<cffunction name="getIsException" access="private" returntype="string" output="false">
		<cfreturn variables.isException />
	</cffunction> 

getIsProcessing

private boolean getIsProcessing( )

Parameters:

Code:

	<cffunction name="getIsProcessing" access="private" returntype="boolean" output="false">
		<cfreturn variables.isProcessing />
	</cffunction> 

getMaxEvents

public numeric getMaxEvents( )

Parameters:

Code:

	<cffunction name="getMaxEvents" access="public" returntype="numeric" output="false">
		<cfreturn variables.maxEvents />
	</cffunction> 

getNextEvent

public Event getNextEvent( )

Peeks at the next event in the queue.

Parameters:

Code:

	<cffunction name="getNextEvent" access="public" returntype="MachII.framework.Event" output="false"
		hint="Peeks at the next event in the queue.">
		<cfreturn getEventQueue().peek() />
	</cffunction> 

getPreviousEvent

public Event getPreviousEvent( )

Returns the previous handled event.

Parameters:

Code:

	<cffunction name="getPreviousEvent" access="public" returntype="MachII.framework.Event" output="false"
		hint="Returns the previous handled event.">
		<cfreturn variables.previousEvent />
	</cffunction> 

getRequestEventName

private string getRequestEventName( )

Parameters:

Code:

	<cffunction name="getRequestEventName" access="private" returntype="string" output="false">
		<cfreturn variables.requestEventName />
	</cffunction> 

getViewContext

private any getViewContext( )

Parameters:

Code:

	<cffunction name="getViewContext" access="private" type="MachII.framework.ViewContext" output="false">
		<cfreturn variables.viewContext />
	</cffunction> 

handleEvent

private void handleEvent( Event event )

Handles the current event.

Parameters:
Event event

Code:

	<cffunction name="handleEvent" access="private" returntype="void" output="true"
		hint="Handles the current event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var eventName = "" />
		<cfset var eventHandler = 0 />
		
		<cfif hasCurrentEvent()>
			<cfset setPreviousEvent(getCurrentEvent()) />
		</cfif>
		<cfset setCurrentEvent(arguments.event) />
		<cfset request.event = arguments.event />
		
		<cfset eventName = arguments.event.getName() />
		
		<cfset eventHandler = getAppManager().getEventManager().getEventHandler(eventName) />
		<cfset setCurrentEventHandler(eventHandler) />
		
		
		<cfset getAppManager().getPluginManager().preEvent(this) />
		
		<cfset eventHandler.handleEvent(arguments.event, this) />
		
		
		<cfset getAppManager().getPluginManager().postEvent(this) />
		
		
		<cfset clearEventMappings() />
	</cffunction> 

handleException

public void handleException( Exception exception, [boolean clearEventQueue="true"] )

Handles an exception.

Parameters:
Exception exception
[boolean clearEventQueue="true"]

Code:

	<cffunction name="handleException" access="public" returntype="void" output="true"
		hint="Handles an exception.">
		<cfargument name="exception" type="MachII.util.Exception" required="true" />
		<cfargument name="clearEventQueue" type="boolean" required="false" default="true" />
		
		<cfset var nextEventName = "" />
		<cfset var exceptionEvent = "" />
		
		<cftry>
			<cfset nextEventName = getEventMapping(getExceptionEventName()) />
			<cfset exceptionEvent = getAppManager().getEventManager().createEvent(nextEventName) />
			
			<cfset exceptionEvent.setRequestName(getRequestEventName()) />
			
			<cfset exceptionEvent.setArg('exception', arguments.exception) />
			
			<cfif hasCurrentEvent()>
				<cfset exceptionEvent.setArg('exceptionEvent', getCurrentEvent()) />
			</cfif>
			
			<cfset getAppManager().getPluginManager().handleException(this, arguments.exception) />
			
			<cfif arguments.clearEventQueue>
				<cfset variables.clearEventQueue() />
			</cfif>
			
			
			
			<cfset getEventQueue().put(exceptionEvent) />
			
			<cfcatch type="any">
				<cfrethrow />
			</cfcatch>
		</cftry>
	</cffunction> 

handleNextEvent

private void handleNextEvent( )

Handles the next event in the queue.

Parameters:

Code:

	<cffunction name="handleNextEvent" access="private" returntype="void" output="true"
		hint="Handles the next event in the queue.">
		<cfset var exception = 0 />
		
		<cftry>
			<cfset incrementEventCount() />
			<cfset handleEvent(getEventQueue().get()) />
			
			<cfcatch type="AbortEventException">
				
			</cfcatch>
			<cfcatch type="any">
				<cfif getIsException()>
					<cfrethrow />
				<cfelse>
					<cfset exception = wrapException(cfcatch) />
					<cfset handleException(exception, true) />
				</cfif>
			</cfcatch>
		</cftry>
	</cffunction> 

hasCurrentEvent

public boolean hasCurrentEvent( )

Checks if the current event has an event object.

Parameters:

Code:

	<cffunction name="hasCurrentEvent" access="public" returntype="boolean" output="false"
		hint="Checks if the current event has an event object.">
		<cfreturn IsObject(variables.currentEvent) />
	</cffunction> 

hasMoreEvents

public boolean hasMoreEvents( )

Checks if there are more events in the queue.

Parameters:

Code:

	<cffunction name="hasMoreEvents" access="public" returntype="boolean" output="false"
		hint="Checks if there are more events in the queue.">
		<cfreturn NOT getEventQueue().isEmpty() />
	</cffunction> 

hasNextEvent

public boolean hasNextEvent( )

Peeks at the next event in the queue.

Parameters:

Code:

	<cffunction name="hasNextEvent" access="public" returntype="boolean" output="false"
		hint="Peeks at the next event in the queue.">
		<cfreturn hasMoreEvents() />
	</cffunction> 

hasPreviousEvent

public boolean hasPreviousEvent( )

Returns whether or not getPreviousEvent() can be called to return an event.

Parameters:

Code:

	<cffunction name="hasPreviousEvent" access="public" returntype="boolean" output="false"
		hint="Returns whether or not getPreviousEvent() can be called to return an event.">
		<cfreturn IsObject(variables.previousEvent) />
	</cffunction> 

incrementEventCount

private void incrementEventCount( )

Increments the current event count by 1.

Parameters:

Code:

	<cffunction name="incrementEventCount" access="private" returntype="void" output="false"
		hint="Increments the current event count by 1.">
		<cfset variables.eventCount = variables.eventCount + 1 />
	</cffunction> 

init

public EventContext init( AppManager appManager, [string requestEventName=""] )

Initalizes the event-context.

Parameters:
AppManager appManager
[string requestEventName=""]

Code:

	<cffunction name="init" access="public" returntype="EventContext" output="false"
		hint="Initalizes the event-context.">
		<cfargument name="appManager" type="MachII.framework.AppManager" required="true" />
		<cfargument name="requestEventName" type="string" required="false" default="" />
		
		<cfset var eventQueue = 0 />
		<cfset var viewContext = 0 />
		
		<cfset setAppManager(arguments.appManager) />
		<cfset setRequestEventName(arguments.requestEventName) />
		<cfset setExceptionEventName(getAppManager().getPropertyManager().getProperty('exceptionEvent')) />
		<cfset setMaxEvents(getAppManager().getPropertyManager().getProperty('maxEvents')) />
		
		
		<cfset eventQueue = CreateObject('component', 'MachII.util.SizedQueue') />
		<cfset eventQueue.init(getMaxEvents()) />
		<cfset setEventQueue(eventQueue) />
		
		
		<cfset viewContext = CreateObject('component', 'MachII.framework.ViewContext') />
		<cfset viewContext.init(getAppManager()) />
		<cfset setViewContext(viewContext) />
		
		<cfreturn this />
	</cffunction> 

processEvents

public void processEvents( )

Begins processing of queued events. Can only be called once.

Parameters:

Code:

	<cffunction name="processEvents" access="public" returntype="void" output="true"
		hint="Begins processing of queued events. Can only be called once.">
	
		<cfset var pluginManager = "" />
		<cfset var eventManager = "" />
		<cfset var exception = "" />
		
		<cfif getIsProcessing()>
			<cfthrow message="The EventContext is already processing the events in the queue. The processEvents() method can only be called once." />
		</cfif>
		<cfset setIsProcessing(true) />
		
		<cfset pluginManager = getAppManager().getPluginManager() />
		<cfset eventManager = getAppManager().getEventManager() />
	
		
		<cfset pluginManager.preProcess(this) />
		
		<cfloop condition="hasMoreEvents() AND getEventCount() LT getMaxEvents()">
			<cfset handleNextEvent() />
		</cfloop>
		
		
		<cfif NOT getEventQueue().isEmpty()>
			<cfset exception = createException("MachII.framework.MaxEventsExceeded", "The maximum number of events (#getMaxEvents()#) the framework will process for a single request has been exceeded.") />
			<cfset handleException(exception, true) />
		</cfif>
		
		
		<cfset pluginManager.postProcess(this) />
		<cfset setIsProcessing(false) />
	</cffunction> 

setAppManager

private void setAppManager( AppManager appManager )

Parameters:
AppManager appManager

Code:

	<cffunction name="setAppManager" access="private" returntype="void" output="false">
		<cfargument name="appManager" type="MachII.framework.AppManager" required="true" />
		<cfset variables.appManager = arguments.appManager />
	</cffunction> 

setCurrentEvent

private void setCurrentEvent( Event currentEvent )

Parameters:
Event currentEvent

Code:

	<cffunction name="setCurrentEvent" access="private" returntype="void" output="false">
		<cfargument name="currentEvent" type="MachII.framework.Event" required="true" />
		<cfset variables.currentEvent = arguments.currentEvent />
	</cffunction> 

setCurrentEventHandler

private void setCurrentEventHandler( EventHandler currentEventHandler )

Parameters:
EventHandler currentEventHandler

Code:

	<cffunction name="setCurrentEventHandler" access="private" returntype="void" output="false">
		<cfargument name="currentEventHandler" type="MachII.framework.EventHandler" required="true" />
		<cfset variables.currentEventHandler = arguments.currentEventHandler />
	</cffunction> 

setEventMapping

public string setEventMapping( string eventName, string mappingName )

Sets an event mapping.

Parameters:
string eventName
string mappingName

Code:

	<cffunction name="setEventMapping" access="public" returntype="string" output="false"
		hint="Sets an event mapping.">
		<cfargument name="eventName" type="string" required="true" />
		<cfargument name="mappingName" type="string" required="true" />
		<cfset variables.mappings[arguments.eventName] = arguments.mappingName />
	</cffunction> 

setEventQueue

private void setEventQueue( SizedQueue eventQueue )

Parameters:
SizedQueue eventQueue

Code:

	<cffunction name="setEventQueue" access="private" returntype="void" output="false">
		<cfargument name="eventQueue" type="MachII.util.SizedQueue" required="true" />
		<cfset variables.eventQueue = arguments.eventQueue />
	</cffunction> 

setExceptionEventName

public void setExceptionEventName( string exceptionEventName )

Parameters:
string exceptionEventName

Code:

	<cffunction name="setExceptionEventName" access="public" returntype="void" output="false">
		<cfargument name="exceptionEventName" type="string" required="true" />
		<cfset variables.exceptionEventName = arguments.exceptionEventName />
	</cffunction> 

setIsException

private void setIsException( boolean isException )

Parameters:
boolean isException

Code:

	<cffunction name="setIsException" access="private" returntype="void" output="false">
		<cfargument name="isException" type="boolean" required="true" />
		<cfset variables.isException = arguments.isException />
	</cffunction> 

setIsProcessing

private void setIsProcessing( boolean isProcessing )

Parameters:
boolean isProcessing

Code:

	<cffunction name="setIsProcessing" access="private" returntype="void" output="false">
		<cfargument name="isProcessing" type="boolean" required="true" />
		<cfset variables.isProcessing = arguments.isProcessing />
	</cffunction> 

setMaxEvents

public void setMaxEvents( numeric maxEvents )

Parameters:
numeric maxEvents

Code:

	<cffunction name="setMaxEvents" access="public" returntype="void" output="false">
		<cfargument name="maxEvents" required="true" type="numeric" />
		<cfset variables.maxEvents = arguments.maxEvents />
	</cffunction> 

setPreviousEvent

private void setPreviousEvent( Event previousEvent )

Parameters:
Event previousEvent

Code:

	<cffunction name="setPreviousEvent" access="private" returntype="void" output="false">
		<cfargument name="previousEvent" type="MachII.framework.Event" required="true" />
		<cfset variables.previousEvent = arguments.previousEvent />
	</cffunction> 

setRequestEventName

private void setRequestEventName( string requestEventName )

Parameters:
string requestEventName

Code:

	<cffunction name="setRequestEventName" access="private" returntype="void" output="false">
		<cfargument name="requestEventName" type="string" required="true" />
		<cfset variables.requestEventName = arguments.requestEventName />
	</cffunction> 

setViewContext

private void setViewContext( ViewContext viewContext )

Parameters:
ViewContext viewContext

Code:

	<cffunction name="setViewContext" access="private" returntype="void" output="false">
		<cfargument name="viewContext" type="MachII.framework.ViewContext" required="true" />
		<cfset variables.viewContext = arguments.viewContext />
	</cffunction> 

wrapException

private Exception wrapException( any caughtException )

Creates an exception object (with cfcatch).

Parameters:
any caughtException

Code:

	<cffunction name="wrapException" access="private" returntype="MachII.util.Exception" output="false"
		hint="Creates an exception object (with cfcatch).">
		<cfargument name="caughtException" type="any" required="true" />
		
		<cfset var exception = CreateObject('component', 'MachII.util.Exception') />
		<cfset exception.wrapException(arguments.caughtException) />
		<cfset setIsException(true) />
		
		<cfreturn exception />
	</cffunction>