EventContext

Package: MachII.framework
Handles event-command execution and event processing mechanism for an event lifecycle.
Method Summary
public EventContext init(RequestHandler requestHandler, SizedQueue eventQueue)

Initalizes the event-context.

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

Queues an event for the framework to handle.

public void clearEventMappings()

Clears the current event mappings.

public void clearEventQueue()

Clears the event queue.

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

Displays a view.

public boolean executeSubroutine(string subroutineName, Event event)

Executes a subroutine.

public AppManager getAppManager()

Sets the appManager that pertains to context of currently executing event.

public Event getCurrentEvent()

Gets the current event object.

public numeric getEventCount()

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

public struct getEventMapping(string eventName)

Gets an event mapping by the event name.

private SizedQueue getEventQueue()
public string getExceptionEventName()
public Event getNextEvent()

Peeks at the next event in the queue.

public Event getPreviousEvent()

Returns the previous handled event.

private any getRequestHandler()
private any getViewContext()
public void handleException(Exception exception, [boolean clearEventQueue="true"])

Handles an exception.

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.

public boolean isEventMappingDefined(string eventName)

Checks if an event mapping is defined.

private void setAppManager(AppManager appManager)

Sets the appManager that pertains to context of currently executing event.

private void setCurrentEvent(Event currentEvent)
public void setEventMapping(string eventName, string mappingName, [string mappingModuleName="#getAppManager().getModuleName()#"])

Sets an event mapping.

private void setEventQueue(SizedQueue eventQueue)
public void setExceptionEventName(string exceptionEventName)
private void setPreviousEvent(Event previousEvent)
private void setRequestHandler(RequestHandler requestHandler)
public void setup(AppManager appManager, [any currentEvent=""])

Sets up the event-context.

private void setViewContext(ViewContext viewContext)
Method Detail
announceEvent

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

Queues an event for the framework to handle.

Parameters:
string eventName
[struct eventArgs="#StructNew()#"]
[string moduleName="#getAppManager().getModuleName()#"]

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()#" />
		<cfargument name="moduleName" type="string" required="false" default="#getAppManager().getModuleName()#" />
		
		<cfset var mapping = "" />
		<cfset var nextEvent = "" />
		<cfset var nextModuleName = arguments.moduleName />
		<cfset var nextEventName = arguments.eventName />
		<cfset var exception = "" />
		
		<cftry>
			
			<cfif isEventMappingDefined(arguments.eventName)>
				<cfset mapping = getEventMapping(arguments.eventName) />
				<cfset nextModuleName = mapping.moduleName />
				<cfset nextEventName = mapping.eventName />
			</cfif>
			
			<cfset nextEvent = getAppManager().getEventManager().createEvent(nextModuleName, nextEventName, arguments.eventArgs, getRequestHandler().getRequestEventName(), getRequestHandler().getRequestModuleName()) />
			
			<cfset getEventQueue().put(nextEvent) />
			
			<cfcatch  type="any">
				<cfset exception = getRequestHandler().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> 

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> 

executeSubroutine

public boolean executeSubroutine( string subroutineName, Event event )

Executes a subroutine.

Parameters:
string subroutineName
Event event

Code:

	<cffunction name="executeSubroutine" access="public" returntype="boolean" output="true"
		hint="Executes a subroutine.">
		<cfargument name="subroutineName" type="string" required="true" />
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var subroutineHandler = "" />
		<cfset var exception = "" />
		<cfset var continue = true />
	
		<cftry>
					
			<cfset subroutineHandler = getAppManager().getSubroutineManager().getSubroutineHandler(arguments.subroutineName) />
			
			<cfset continue = subroutineHandler.handleSubroutine(arguments.event, this) />
			
			<cfcatch type="any">
				<cfset exception = getRequestHandler().wrapException(cfcatch) />
				<cfset handleException(exception, true) />
			</cfcatch>
		</cftry>
		
		<cfreturn continue />
	</cffunction> 

getAppManager

public AppManager getAppManager( )

Sets the appManager that pertains to context of currently executing event.

Parameters:

Code:

	<cffunction name="getAppManager" access="public" returntype="MachII.framework.AppManager" output="false"
		hint="Sets the appManager that pertains to context of currently executing event.">
		<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> 

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 getRequestHandler().getEventCount() />
	</cffunction> 

getEventMapping

public struct getEventMapping( string eventName )

Gets an event mapping by the event name.

Parameters:
string eventName

Code:

	<cffunction name="getEventMapping" access="public" returntype="struct" output="false"
		hint="Gets an event mapping by the event name.">
		<cfargument name="eventName" type="string" required="true" />
		
		<cfset var mapping = StructNew() />
		
		
		<cfif StructKeyExists(variables.mappings, arguments.eventName)>
			<cfset mapping = variables.mappings[arguments.eventName] />
		<cfelse>
			<cfset mapping.eventName = arguments.eventName />
			<cfset mapping.moduleName = getAppManager().getModuleName() />
		</cfif>
		
		<cfreturn mapping />
	</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> 

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> 

getRequestHandler

private any getRequestHandler( )

Parameters:

Code:

	<cffunction name="getRequestHandler" access="private" type="MachII.framework.RequestHandler" output="false">
		<cfreturn variables.requestHandler />
	</cffunction> 

getViewContext

private any getViewContext( )

Parameters:

Code:

	<cffunction name="getViewContext" access="private" type="MachII.framework.ViewContext" output="false">
		<cfreturn variables.viewContext />
	</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 nextEvent = "" />
		<cfset var eventArgs = StructNew() />
		<cfset var appManager = getAppManager() />
		<cfset var result = StructNew() />
		
		<cfset result.eventName = getExceptionEventName() />
		
		<cftry>
						
			<cfset eventArgs.exception = arguments.exception />
			<cfif hasCurrentEvent()>
				<cfset eventArgs.exceptionEvent = getCurrentEvent() />
			</cfif>
			
			
			<cfset appManager.getPluginManager().handleException(this, arguments.exception) />
			
			
			<cfif arguments.clearEventQueue>
				<cfset variables.clearEventQueue() />
			</cfif>
			
			
			<cfif isEventMappingDefined(result.eventName)>
				<cfset result = getEventMapping(exceptionEventName) />
				<cfif NOT Len(result.moduleName)>
					<cfset appManager = appManager.getModuleManager().getModule(result.moduleName).getModuleAppManager() />
				<cfelse>
					<cfset appManager = appManager.getModuleManager().getAppManager() />
				</cfif>
			
			<cfelseif appManager.getPropertyManager().isPropertyDefined("exceptionEvent")>
				<cfset result.moduleName = appManager.getModuleName() />
			<cfelse>
				<cfset result.moduleName = "" />
			</cfif>
			
			
			<cfset nextEvent = appManager.getEventManager().createEvent(result.moduleName, result.eventName, eventArgs, getRequestHandler().getRequestEventName(), getRequestHandler().getRequestModuleName()) />
			<cfset getEventQueue().put(nextEvent) />
			
			<cfcatch type="any">
				<cfrethrow />
			</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> 

init

public EventContext init( RequestHandler requestHandler, SizedQueue eventQueue )

Initalizes the event-context.

Parameters:
RequestHandler requestHandler
SizedQueue eventQueue

Code:

	<cffunction name="init" access="public" returntype="EventContext" output="false"
		hint="Initalizes the event-context.">
		<cfargument name="requestHandler" type="MachII.framework.RequestHandler" required="true" />
		<cfargument name="eventQueue" type="MachII.util.SizedQueue" required="true" />
		
		<cfset setRequestHandler(arguments.requestHandler) />
		<cfset setEventQueue(arguments.eventQueue) />
		<cfset setViewContext(CreateObject("component", "MachII.framework.ViewContext")) />
		
		<cfreturn this />
	</cffunction> 

isEventMappingDefined

public boolean isEventMappingDefined( string eventName )

Checks if an event mapping is defined.

Parameters:
string eventName

Code:

	<cffunction name="isEventMappingDefined" type="public" returntype="boolean" output="false"
		hint="Checks if an event mapping is defined.">
		<cfargument name="eventName" type="string" required="true" />
		
		<cfset var result = false />
		
		<cfif StructKeyExists(variables.mappings, arguments.eventName)>
			<cfset result = true />
		</cfif>
		
		<cfreturn result />
	</cffunction> 

setAppManager

private void setAppManager( AppManager appManager )

Sets the appManager that pertains to context of currently executing event.

Parameters:
AppManager appManager

Code:

	<cffunction name="setAppManager" access="private" returntype="void" output="false"
		hint="Sets the appManager that pertains to context of currently executing event.">
		<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> 

setEventMapping

public void setEventMapping( string eventName, string mappingName, [string mappingModuleName="#getAppManager().getModuleName()#"] )

Sets an event mapping.

Parameters:
string eventName
string mappingName
[string mappingModuleName="#getAppManager().getModuleName()#"]

Code:

	<cffunction name="setEventMapping" access="public" returntype="void" output="false"
		hint="Sets an event mapping.">
		<cfargument name="eventName" type="string" required="true" />
		<cfargument name="mappingName" type="string" required="true" />
		<cfargument name="mappingModuleName" type="string" required="false"
			default="#getAppManager().getModuleName()#" />

		<cfset var mapping = StructNew() />

		<cfif Len(arguments.mappingModuleName)
			AND NOT getAppManager().getModuleManager().isModuleDefined(arguments.mappingModuleName)>
			<cfthrow type="MachII.framework.eventMappingModuleNotDefined"
				message="The module '#arguments.mappingModuleName#' cannot be found for this event-mapping." />	
		</cfif>
		
		
		<cfset mapping.eventName = arguments.mappingName />
		<cfset mapping.moduleName = arguments.mappingModuleName />
		
		<cfset variables.mappings[arguments.eventName] = mapping />
	</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> 

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> 

setRequestHandler

private void setRequestHandler( RequestHandler requestHandler )

Parameters:
RequestHandler requestHandler

Code:

	<cffunction name="setRequestHandler" access="private" returntype="void" output="false">
		<cfargument name="requestHandler" type="MachII.framework.RequestHandler" required="true" />
		<cfset variables.requestHandler = arguments.requestHandler />
	</cffunction> 

setup

public void setup( AppManager appManager, [any currentEvent=""] )

Sets up the event-context.

Parameters:
AppManager appManager
[any currentEvent=""]

Code:

	<cffunction name="setup" access="public" returntype="void" output="false"
		hint="Sets up the event-context.">
		<cfargument name="appManager" type="MachII.framework.AppManager" required="true" />
		<cfargument name="currentEvent" type="any" required="false" default="" />
		
		<cfset setAppManager(arguments.appManager) />
		<cfif hasCurrentEvent()>
			<cfset setPreviousEvent(getCurrentEvent()) />
		</cfif>
		<cfif IsObject(arguments.currentEvent)>
			<cfset setCurrentEvent(arguments.currentEvent) />
		</cfif>
		
		
		<cfset setExceptionEventName(getAppManager().getPropertyManager().getProperty("exceptionEvent")) />
		
		
		<cfset getViewContext().init(getAppManager()) />
		
		
		<cfset clearEventMappings() />
	</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>