package com.afi.estimator.commands
{
    import com.afi.autoquote.api.PopulateVo;
    import com.afi.autoquote.api.XMLConverter;
    import com.afi.autoquote.log.Logger;
    import com.afi.autoquote.net.IResponder;
    import com.afi.autoquote.util.ReflectionUtil;
    
    public class AsyncCommand
        extends Command
        implements IResponder
    {
        private var _result:Function;
        private var _fault:Function;
        
        /**
         * Base class for handling asynchronous work. When using a Delegate that requires
         * a IResponder instance, this provides an easy way to pass the command instance
         * itself (using 'this') to the Delegate.
         * 
         * When building subclasses, use the result and fault handlers to bubble the
         * respective objects up to the original caller.
         * 
         * The default behavior on result or fault is to log that the event happened.
         * 
         * If you do not need to process the result, you may choose to not define it
         * in the subclass. For example, pinging the server to keep the session alive.
         * 
         * See example class commands.Login
         */
        public function AsyncCommand(result:Function = null, fault:Function = null)
        {
            _result = result;
            _fault = fault;
        }
        
        /**
         * Will eventually be called by a URLLoader, override in AsyncCommmand
         * subclass for extra result processing.
         */
        public function result(data:Object):void
        {
            Logger.debug(ReflectionUtil.getClassName(this) + ": result");
            if(_result != null) {
                _result(data);
            }
        }
        
        /**
         * Will eventually be called by a URLLoader, override in AsyncCommmand
         * subclass for extra fault processing.
         */
        public function fault(info:String):void
        {
            Logger.error(ReflectionUtil.getClassName(this) + ": fault: " + info);
            if(_fault != null) {
                _fault(info);
            }
        }
        
        
        //populates one value obj to obher value obj
        public function populateValueBean(src:Object,dest:Object):void
        {
            populateVOObject=new PopulateVo();
            
            populateVOObject.populateValueBean(src,dest);
            
        }
        //converts vo to request xml for httpservice
        public function voToXML(vo:Object, qName:String):XML
        {
            xmlConverter=new XMLConverter();
            return xmlConverter.voToXml(vo,qName);
        }
        //converts response xml obj to vo
        public function xmlToVO(xml:XML):Object
        {
            xmlConverter=new XMLConverter();
            return xmlConverter.xmlToVo(xml);
        }
    }
}