hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/operations/view/filetransfer/UploadActionBar.mxml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<actionbar:ActionBar xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx"
                     xmlns:actionbar="it.ht.rcs.console.main.actionbar.*">
  
  <s:Button label="{R.get('NEW_UPLOAD')}" click="onUpload(event)" icon="@Embed('/img/NEW/upload.png')" enabled="{section.stateManager.selectedAgent.status=='open' &amp;&amp; Console.currentSession.user.is_tech_upload() &amp;&amp; section.stateManager.selectedAgent.level!='soldier'}" visible="{LicenseManager.instance.modify}" includeInLayout="{LicenseManager.instance.modify}"/>
  <s:Button label="{R.get('UPLOAD_AND_EXECUTE')}" click="onUploadAndExecute(event)" icon="@Embed('/img/NEW/uploadexecute.png')" enabled="{section.stateManager.selectedAgent.status=='open' &amp;&amp; Console.currentSession.user.is_tech_exec() &amp;&amp; section.stateManager.selectedAgent.level!='soldier'}" visible="{LicenseManager.instance.modify}" includeInLayout="{LicenseManager.instance.modify}"/>
  <s:Button label="{R.get('EXPORT_FILE')}" click="onDownload(event)" enabled="{selectedObject != null}"
            icon="@Embed('/img/NEW/export.png')"/>
  <s:Button label="{R.get('DELETE')}" click="onDelete(event)" icon="@Embed('/img/NEW/delete_32.png')" enabled="{selectedObject}"/>
  
  <s:Spacer width="100%"/>
  
  <fx:Script>
    <![CDATA[
      import it.ht.rcs.console.agent.controller.AgentManager;
      import it.ht.rcs.console.agent.model.Agent;
      import it.ht.rcs.console.agent.model.Upload;
      import it.ht.rcs.console.events.RefreshEvent;
      import it.ht.rcs.console.operations.view.OperationsSection;
      import it.ht.rcs.console.operations.view.agents.build.UploadPopup;
      import it.ht.rcs.console.utils.AlertPopUp;
      import it.ht.rcs.console.utils.FileDownloader;
      import it.ht.rcs.console.monitor.controller.LicenseManager;
      
      import locale.R;
      
      import mx.core.FlexGlobals;
      import mx.managers.PopUpManager;
      import mx.rpc.events.ResultEvent;
      
      [Bindable]
      public var section:OperationsSection;
      
      private var execute:Boolean=false;
      
      private function onUpload(e:MouseEvent):void
      {
        execute=false;
        browse();
      }
      
      private function onUploadAndExecute(e:MouseEvent):void
      {
        execute=true;
        browse();
      }
      
      private var fileReference:FileReference;
      
      private function browse():void
      {
        fileReference = new FileReference();
        fileReference.addEventListener(Event.SELECT, fileSelection);
        fileReference.browse();
      }
      
      private function fileSelection(event:Event):void
      {
        manageUpload();
      }
      
      private var uploadPopup:UploadPopup;
      private function manageUpload():void
      {
        fileReference.addEventListener(HTTPStatusEvent.HTTP_STATUS, onUploadError);
        fileReference.addEventListener(IOErrorEvent.IO_ERROR, onUploadError);
        fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadError);
        fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadComplete);
        
        uploadPopup = PopUpManager.createPopUp(root, UploadPopup, true) as UploadPopup;
        uploadPopup.fileReference = fileReference;
        PopUpManager.centerPopUp(uploadPopup);
      }
      
      private function onUploadComplete(event:DataEvent):void
      {
        PopUpManager.removePopUp(uploadPopup);
        AgentManager.instance.createUpload(section.stateManager.selectedAgent, fileReference.name, event.data, onCreate);
      }
      
      private function onUploadError(event:Event):void
      {
        PopUpManager.removePopUp(uploadPopup);
        AlertPopUp.show(event.type);
      }
      
      private function onCreate(e:ResultEvent):void
      {
        if(execute)
        {
          var agent:Agent=section.stateManager.selectedAgent
          var command:String=fileReference.name;
          if(agent.platform=="windows")
          {
            command="$dir$\\"+command;
          }
          else
          {
            command="$dir$/"+command;
          }
          AgentManager.instance.createCommand(agent,command, onCommandComplete)
        }
        else{
          FlexGlobals.topLevelApplication.dispatchEvent(new RefreshEvent(RefreshEvent.REFRESH));
        }
       
      }
      
      private function onCommandComplete(e:ResultEvent):void
      {
        trace("upload and execution completed")
        
        FlexGlobals.topLevelApplication.dispatchEvent(new RefreshEvent(RefreshEvent.REFRESH));
      }
      
      
      
      private function onDownload(e:MouseEvent):void
      {
        var upload:Upload = selectedObject as Upload;
        var remotePath:String = "grid/" + upload._grid.getItemAt(0); // _grid is an array of ids
        var localPath:String = File.desktopDirectory.nativePath + '/RCS Downloads';
        new File(localPath).createDirectory();
        
        localPath = localPath + '/' + upload.filename;
        
        var fd:FileDownloader = new FileDownloader(remotePath, localPath);
        fd.onComplete = onComplete;
        fd.download();
      }
      
      private function onComplete():void
      {
        AlertPopUp.show("Download completed");
      }
      
      private function onDelete(e:MouseEvent):void
      {
        AgentManager.instance.deleteUpload(section.stateManager.selectedAgent, selectedObject._id, onDeleteSuccess);
      }
      
      private function onDeleteSuccess(e:ResultEvent):void
      {
        FlexGlobals.topLevelApplication.dispatchEvent(new RefreshEvent(RefreshEvent.REFRESH));
      }
    ]]>
  </fx:Script>
  
</actionbar:ActionBar>