EventContext

Package: MachII.framework
Controls the event queue and event processing mechanism for a request/event lifecycle.
Method Summary
public void init(AppManager appManager)

Initalizes the event-context.

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

Queues an event for the framework to handle.

public void clearEventMappings()
public void clearEventQueue()

Clears the event queue.

private Exception createException([string type=""], [string message=""], [string errorCode=""], [string detail=""], [string extendedInfo=""], [array tagContext="#ArrayNew(1)#"])
public void displayView(Event event, string viewName, [string contentKey=""], [boolean append="false"])
private any getAppManager()
public Event getCurrentEvent()
private EventHandler getCurrentEventHandler()
public numeric getEventCount()

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

public string getEventMapping(string eventName)
private SizedQueue getEventQueue()
public string getExceptionEventName()
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 any getViewContext()
private void handleEvent(Event event)
public void handleException(Exception exception, [boolean clearEventQueue="true"])
private void handleNextEvent()
public boolean hasCurrentEvent()
public boolean hasMoreEvents()
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()
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)
private void setEventQueue(SizedQueue eventQueue)
public void setExceptionEventName(string exceptionEventName)
private void setIsProcessing(boolean isProcessing)
public void setMaxEvents(numeric maxEvents)
private void setPreviousEvent(Event previousEvent)
private void setViewContext(ViewContext viewContext)
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 getEventQueue().put(nextEvent) />
			
			<cfcatch>
				<cfset exception = createException(cfcatch.type, cfcatch.message, cfcatch.errorCode, cfcatch.detail, cfcatch.extendedInfo, cfcatch.tagContext) />
				<cfset handleException(exception, true) />
			</cfcatch>
		</cftry>
	</cffunction> 

clearEventMappings

public void clearEventMappings( )

Parameters:

Code:

	<cffunction name="clearEventMappings" access="public" returntype="void" output="false">
		<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)#"] )

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">
		<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) />
		
		<cfreturn exception />
	</cffunction> 

displayView

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

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

Code:

	<cffunction name="displayView" access="public" returntype="void" output="true">
		<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="append" type="boolean" required="false" default="false" />
		
		
		<cfset getAppManager().getPluginManager().preView(this) />
		
		<cfset getViewContext().displayView(arguments.event, arguments.viewName, arguments.contentKey, 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( )

Parameters:

Code:

	<cffunction name="getCurrentEvent" access="public" returntype="MachII.framework.Event" output="false">
		<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 )

Parameters:
string eventName

Code:

	<cffunction name="getEventMapping" access="public" returntype="string" output="false">
		<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> 

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> 

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 )

Parameters:
Event event

Code:

	<cffunction name="handleEvent" access="private" returntype="void" output="true">
		<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"] )

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

Code:

	<cffunction name="handleException" access="public" returntype="void" output="true">
		<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.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( )

Parameters:

Code:

	<cffunction name="handleNextEvent" access="private" returntype="void" output="true">
		<cfset var exception = 0 />
		
		<cftry>
			<cfset incrementEventCount() />
			<cfset handleEvent(getEventQueue().get()) />
			
			<cfcatch type="AbortEventException">
				
			</cfcatch>
			<cfcatch type="any">
				<cfset exception = createException(cfcatch.type, cfcatch.message, cfcatch.errorCode, cfcatch.detail, cfcatch.extendedInfo, cfcatch.tagContext) />
				<cfset handleException(exception, true) />
			</cfcatch>
		</cftry>
	</cffunction> 

hasCurrentEvent

public boolean hasCurrentEvent( )

Parameters:

Code:

	<cffunction name="hasCurrentEvent" access="public" returntype="boolean" output="false">
		<cfreturn IsObject(variables.currentEvent) />
	</cffunction> 

hasMoreEvents

public boolean hasMoreEvents( )

Parameters:

Code:

	<cffunction name="hasMoreEvents" access="public" returntype="boolean" output="false">
		<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( )

Parameters:

Code:

	<cffunction name="incrementEventCount" access="private" returntype="void" output="false">
		<cfset variables.eventCount = variables.eventCount + 1 />
	</cffunction> 

init

public void init( AppManager appManager )

Initalizes the event-context.

Parameters:
AppManager appManager

Code:

	<cffunction name="init" access="public" returntype="void" output="false"
		hint="Initalizes the event-context.">
		<cfargument name="appManager" type="MachII.framework.AppManager" required="true" />
		
		<cfset var eventQueue = 0 />
		<cfset var viewContext = 0 />
		
		<cfset setAppManager(arguments.appManager) />
		<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) />
	</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('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 )

Parameters:
string eventName
string mappingName

Code:

	<cffunction name="setEventMapping" access="public" returntype="string" output="false">
		<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> 

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> 

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>