Created
October 18, 2010 14:55
-
-
Save Stray/632333 to your computer and use it in GitHub Desktop.
Interface for loading services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountDataLoadingService extends AdminDataLoadingService { | |
public function AccountDataLoadingService() | |
{ | |
_loadingDataVOType = AccountDataVO; | |
super(); | |
} | |
override protected function processLoadedData(urlVars:URLVariables):void | |
{ | |
var dataXML:XML = new XML(urlVars.dataXML); | |
adminXMLToModelProcessor.processAccountData(dataXML); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AdminDataLoadingService extends Actor implements IAdminDataLoadingService { | |
[Inject] | |
public var loadingURLVarsFactory:ILoadingURLVarsFactory; | |
[Inject(name='loadingScriptPath')] | |
public var loadingScriptPath:String; | |
[Inject] | |
public var adminXMLToModelProcessor:IAdminXMLToModelProcessor; | |
protected var _loadingDataVOType:Class; | |
//-------------------------------------------------------------------------- | |
// | |
// Initialization | |
// | |
//-------------------------------------------------------------------------- | |
public function AdminDataLoadingService() | |
{ | |
super(); | |
} | |
//-------------------------------------------------------------------------- | |
// | |
// API | |
// | |
//-------------------------------------------------------------------------- | |
//--------------------------------------- | |
// IAdminDataLoadingService Implementation | |
//--------------------------------------- | |
public function loadData():void | |
{ | |
var urlLoader:URLLoader = new URLLoader(); | |
var urlRequest:URLRequest = createLoadRequestForDataType(_loadingDataVOType) | |
addCommonEventListeners(urlLoader); | |
urlLoader.load(urlRequest); | |
} | |
protected function createLoadRequestForDataType(dataClass:Class):URLRequest | |
{ | |
var urlReq:URLRequest = new URLRequest(); | |
urlReq.url = loadingScriptPath; | |
urlReq.data = loadingURLVarsFactory.createLoadingURLVarsForDataType(dataClass); | |
urlReq.method = URLRequestMethod.POST; | |
return urlReq; | |
} | |
protected function addCommonEventListeners(urlLoader:URLLoader):void | |
{ | |
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); | |
urlLoader.addEventListener(Event.COMPLETE, completeHandler); | |
} | |
protected function removeCommonEventListeners(urlLoader:URLLoader):void | |
{ | |
urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); | |
urlLoader.removeEventListener(Event.COMPLETE, completeHandler); | |
} | |
protected function ioErrorHandler(e:IOErrorEvent):void | |
{ | |
//trace("AdminDataLoadingService::ioErrorHandler()"); | |
removeCommonEventListeners(e.target as URLLoader); | |
var evt:AdminDataServiceUpdateEvent = new AdminDataServiceUpdateEvent(AdminDataServiceUpdateEvent.DATA_LOADING_ERROR); | |
dispatch(evt); | |
} | |
protected function completeHandler(e:Event):void | |
{ | |
var urlLoader:URLLoader = e.target as URLLoader; | |
//trace("urlLoader.data: " + urlLoader.data); | |
var urlVars:URLVariables = new URLVariables(urlLoader.data); | |
processLoadedData(urlVars); | |
dispatchServiceCompleteEvent(); | |
} | |
protected function processLoadedData(urlVars:URLVariables):void | |
{ | |
trace("AdminDataLoadingService::processLoadedData() should be overwritten in the extending class."); | |
} | |
private function dispatchServiceCompleteEvent():void | |
{ | |
var evt:AdminDataServiceUpdateEvent = new AdminDataServiceUpdateEvent(AdminDataServiceUpdateEvent.DATA_LOADING_COMPLETED); | |
dispatch(evt); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AdminDataLoadingURLVarsFactory implements ILoadingURLVarsFactory { | |
[Inject] | |
public var fingerprintKey:IFingerprint; | |
[Inject] | |
public var queryLookup:IAdminDataQueryKeyLookup; | |
public function AdminDataLoadingURLVarsFactory() { | |
} | |
public function createLoadingURLVarsForDataType(dataClass:Class):URLVariables | |
{ | |
var urlVars:URLVariables = new URLVariables(); | |
urlVars.fingerprint = fingerprintKey.fingerprint; | |
urlVars.queryType = queryLookup.getQueryKeyForDataType(dataClass); | |
return urlVars; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AdminDataQueryKeyLookup implements IAdminDataQueryKeyLookup { | |
private var _queriesByType:Dictionary; | |
public function AdminDataQueryKeyLookup () { | |
configure(); | |
} | |
//--------------------------------------- | |
// IAdminDataQueryKeyLookup Implementation | |
//--------------------------------------- | |
public function getQueryKeyForDataType(dataType:Class):String | |
{ | |
var query:String = _queriesByType[dataType]; | |
if(query == null) | |
{ | |
var errorMessage:String = 'No query string was found for the dataType ' + dataType; | |
throw(new ReferenceError(errorMessage)); | |
} | |
return query; | |
} | |
private function configure():void | |
{ | |
_queriesByType = new Dictionary(); | |
_queriesByType[AccountDataVO] = 'GET_ACCOUNT_ADMIN_DATA'; | |
_queriesByType[ServerDataVO] = 'GET_SERVER_ADMIN_DATA'; | |
_queriesByType[PluginDataVO] = 'GET_PLUGIN_ADMIN_DATA'; | |
_queriesByType[AdminPluginDataVO] = 'GET_ADMIN_PLUGIN_ADMIN_DATA'; | |
_queriesByType[UserDataVO] = 'LOCAL_GET_USER_ADMIN_DATA'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataLoadingCommandLookup implements IDataLoadingCommandLookup { | |
private var _commandsByDataType:Dictionary; | |
public function DataLoadingCommandLookup() { | |
} | |
//--------------------------------------- | |
// IDataLoadingCommandLookup Implementation | |
//--------------------------------------- | |
public function mapCommandToClass(commandClazz:Class, dataClazz:Class):void | |
{ | |
if(commandsByDataType[dataClazz] != null) | |
{ | |
throw(new IllegalOperationError('Ambiguous mapping: This data class has already been mapped to a command class.')); | |
} | |
if(isNotACommand(commandClazz)) | |
{ | |
throw(new IllegalOperationError('The command class you passed is missing an execute method.')); | |
} | |
commandsByDataType[dataClazz] = commandClazz; | |
} | |
public function getCommandForClass(dataClazz:Class):Class | |
{ | |
return commandsByDataType[dataClazz]; | |
} | |
private function get commandsByDataType():Dictionary | |
{ | |
return _commandsByDataType || (_commandsByDataType = new Dictionary()); | |
} | |
private function isNotACommand(commandClazz:Class):Boolean | |
{ | |
if(describeType(commandClazz).factory.method.(@name == "execute").length() > 0) | |
{ | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IAdminDataLoadingService { | |
function loadData():void; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IAdminDataQueryKeyLookup { | |
function getQueryKeyForDataType(dataType:Class):String; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ILoadingURLVarsFactory { | |
function createLoadingURLVarsForDataType(dataClass:Class):URLVariables; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProcessDataLoadRequestCommand extends Command | |
{ | |
[Inject] | |
public var dataLoadingCommandLookup:IDataLoadingCommandLookup; | |
[Inject] | |
public var deferredCommandQueue:IDeferredCommandQueue; | |
[Inject] | |
public var adminDataRequestEvent:AdminDataRequestEvent; | |
override public function execute():void | |
{ | |
var classesRequiredVector:Vector.<Class> = adminDataRequestEvent.dataClassesRequestedVector; | |
for each(var requiredDataType:Class in classesRequiredVector) | |
{ | |
var commandClassToLoadData:Class = dataLoadingCommandLookup.getCommandForClass(requiredDataType); | |
deferredCommandQueue.addCommandToQueue(commandClassToLoadData); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment