package org.puremvc.as3.demos.flex.weborb.login.model
{
import mx.managers.CursorManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
import org.puremvc.as3.demos.flex.weborb.login.model.vo.LoginVO;
import org.puremvc.as3.interfaces.IProxy;
import org.puremvc.as3.patterns.proxy.Proxy;
/**
* A Proxy for storing users data and sending and retrieving data to/from its remoting service
*
*/
public class LoginProxy extends Proxy implements IProxy
{
public static const NAME:String = 'loginProxy';
public static const LOGIN_FAILED: String = "loginFailed";
public static const LOGIN_SUCCESS: String = "loginSucces";
private var _loginService: RemoteObject;
public var faultMessage: String;
private var _appProxy: ApplicationProxy;
/**
* Constructor
*
* @param Proxy's data if necessary
*/
public function LoginProxy (data:Object=null)
{
super(NAME, new LoginVO() );
_appProxy = facade.retrieveProxy( ApplicationProxy.NAME ) as ApplicationProxy;
_loginService = new RemoteObject();
_loginService.destination = "GenericDestination";
_loginService.source = "org.puremvc.as3.demos.flex.weborb.login.LoginFacade";
_loginService.getUser.addEventListener(ResultEvent.RESULT, getUserResult);
_loginService.addEventListener(FaultEvent.FAULT, getUserFailed);
}
/**
* Calls the remoting service passing users data
*
* @param users data as LoginVO
*/
public function getUser( vo: LoginVO ):void
{
_loginService.getUser( vo );
CursorManager.setBusyCursor();
}
/**
* Receives the remoting service result users data
*
* @param result event object
*/
private function getUserResult(event: ResultEvent):void
{
CursorManager.removeBusyCursor();
var loginVO: LoginVO = event.result as LoginVO
loginVO.loginDate = new Date();
setData( loginVO );
_appProxy.viewState = ApplicationProxy.LOGGED_IN_STATE;
sendNotification( LoginProxy.LOGIN_SUCCESS );
}
/**
* Receives the remoting service fault event
*
* @param fault event object
*/
private function getUserFailed(event: FaultEvent): void
{
faultMessage = event.fault.faultString;
CursorManager.removeBusyCursor();
_appProxy.viewState = ApplicationProxy.LOGIN_ERROR_STATE;
sendNotification( LoginProxy.LOGIN_FAILED );
}
/**
* Getter for its data object casted as LoginVO
*
* @return LoginVO
*/
public function get loginVO(): LoginVO
{
return data as LoginVO;
}
}
}