hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/operations/view/agents/AgentsActionBar.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.*"
                                         xmlns:view="it.ht.rcs.console.operations.view.*">

    <s:HGroup visible="{section.currentState == 'singleTarget' || section.currentState == 'allAgents'}"
                        gap="0"
                        includeInLayout="{section.currentState == 'singleTarget' || section.currentState == 'allAgents'}">

        <s:Button label="{R.get('NEW_FACTORY')}"
                            click="onNewClick()"
                            enabled="{Console.currentSession.user.is_tech_factories() &amp;&amp; section.stateManager.selectedTarget.status == 'open' &amp;&amp; LicenseManager.instance.archive == 0}"
                            icon="@Embed('/img/NEW/newFactory.png')"
                            visible="{section.currentState == 'singleTarget'}"
                            includeInLayout="{section.currentState == 'singleTarget'}"/>

        <s:Button label="{R.get('EDIT')}"
                            click="onEditClick()"
                            enabled="{(Console.currentSession.user.is_tech() &amp;&amp; selectedObject &amp;&amp; selectedObject is Agent &amp;&amp; selectedObject._kind=='agent') 
              || (Console.currentSession.user.is_tech_factories() &amp;&amp; selectedObject &amp;&amp; selectedObject is Agent &amp;&amp; selectedObject._kind=='factory')}"
                            icon="@Embed('/img/NEW/edit.png')"/>

        <s:Button label="{R.get('DELETE')}"
                            click="onDeleteClick(event)"
                            enabled="{(Console.currentSession.user.is_tech() &amp;&amp; selectedObject &amp;&amp; selectedObject is Agent &amp;&amp; selectedObject._kind=='agent') 
              || (Console.currentSession.user.is_tech_factories() &amp;&amp; selectedObject &amp;&amp; selectedObject is Agent &amp;&amp; selectedObject._kind=='factory')}"
                            icon="@Embed('/img/NEW/delete_32.png')"/>


        <s:Button label="{R.get('CLOSE')}"
                            click="onClose()"
                            enabled="{(Console.currentSession.user.is_tech() &amp;&amp; selectedObject &amp;&amp; selectedObject.status == 'open' &amp;&amp; selectedObject is Agent &amp;&amp; selectedObject._kind=='agent')
              || (Console.currentSession.user.is_tech_factories () &amp;&amp; selectedObject &amp;&amp; selectedObject.status == 'open' &amp;&amp; selectedObject is Agent &amp;&amp; selectedObject._kind=='factory')}"
                            icon="@Embed('/img/NEW/lock32.png')"/>
    
    <s:Button label="{R.get('MOVE')}"
              icon="@Embed('/img/NEW/move.png')"
              click="moveAgent(event)"
              enabled="{(Console.currentSession.user.is_tech() &amp;&amp; selectedObject != null &amp;&amp; selectedObject is Agent
              &amp;&amp; selectedObject.status == 'open' &amp;&amp; section.stateManager.selectedTarget.status == 'open' &amp;&amp; selectedObject._kind=='agent' &amp;&amp; LicenseManager.instance.archive==0)
              
              }"/>

        <actionbar:Separator/>

        <s:Button label="{R.get('ADD_TO_DASHBOARD')}"
                            icon="@Embed('/img/NEW/newDashboard.png')"
                            click="addToDashboard(event)"
                            enabled="{Console.currentSession.user.is_view() &amp;&amp; selectedObject &amp;&amp; selectedObject is Agent &amp;&amp; (selectedObject as Agent)._kind == 'agent' &amp;&amp; selectedObject.status == 'open'}"
                            visible="{Console.currentSession.user.is_view()}"
                            includeInLayout="{Console.currentSession.user.is_view()}"/>

        <s:Button label="{R.get('ADD_TO_ALERT')}"
                            icon="@Embed('/img/NEW/newAlert.png')"
                            click="addToAlert(event)"
                            enabled="{Console.currentSession.user.is_view_alerts() &amp;&amp; selectedObject &amp;&amp; selectedObject is Agent &amp;&amp; selectedObject.status == 'open' }"
                            visible="{LicenseManager.instance.archive == 0}"
                            includeInLayout="{LicenseManager.instance.archive == 0}"/>

        <actionbar:Separator visible="{LicenseManager.instance.archive == 0}"
                         includeInLayout="{LicenseManager.instance.archive == 0}"/>

        <s:Button label="{R.get('UPDATE_AGENT')}"
                            click="updateAgent(event)"
                            icon="@Embed('/img/NEW/update.png')"
                            enabled="{Console.currentSession.user.is_tech_build() &amp;&amp;
                                                             selectedObject &amp;&amp; 
                                                            (selectedObject as Agent)._kind  == 'agent' &amp;&amp; 
                                                            (selectedObject as Agent).status == 'open'}"
                            visible="{LicenseManager.instance.archive == 0}"
                            includeInLayout="{LicenseManager.instance.archive == 0}"/>
    </s:HGroup>


    <s:HGroup visible="{section.currentState == 'singleTarget'}"
                        gap="0"
                        includeInLayout="{section.currentState == 'singleTarget'}">

        <actionbar:Separator/>

        <s:Button label="{R.get('EVIDENCE_IMPORT')}"
                            click="importEvidence(event)"
                            icon="@Embed('/img/NEW/import.png')"
                            enabled="{Console.currentSession.user.is_tech_import()}"/>

        <s:Button label="{R.get('EVIDENCE_EXPORT')}"
                            icon="@Embed('/img/NEW/export.png')"
                            click="exportEvidence(event)"
                            enabled="{Console.currentSession.user.is_view_export()}"/>
    
    <actionbar:Separator visible="{Console.currentSession.user.is_view()}" includeInLayout="{Console.currentSession.user.is_view()}"/>
    <s:Button label="{R.get('JUMP_TO_ENTITY')}"
              icon="@Embed('/img/NEW/jumpToEntity.png')" 
              enabled="{Console.currentSession.user.is_view_profiles()  &amp;&amp; section.stateManager.selectedTarget.status=='open'}" click="onJump()" />

    </s:HGroup>

    <s:HGroup visible="{section.currentState == 'singleAgent'}"
                        gap="0"
                        includeInLayout="{section.currentState == 'singleAgent'}">


        <s:Button label="{R.get('EVIDENCE_EXPORT')}"
                            icon="@Embed('/img/NEW/export.png')"
                            click="exportEvidence(event)"
                            enabled="{Console.currentSession.user.is_view_export()}"/>

        <actionbar:Separator visible="{Console.currentSession.user.is_tech_build()}" includeInLayout="{Console.currentSession.user.is_tech_build()}"/>

        <s:Button label="{R.get('UPDATE_AGENT')}"
                            click="updateAgent(event)"
                            visible="{Console.currentSession.user.is_tech_build()}" includeInLayout="{Console.currentSession.user.is_tech_build()}"
                            enabled="{section.stateManager.selectedAgent.status=='open'}"
                            icon="@Embed('/img/NEW/update.png')"/>

        <actionbar:Separator visible="{Console.currentSession.user.is_tech()}" includeInLayout="{Console.currentSession.user.is_tech()}"/>

        <s:Button label="{R.get('PURGE')}"
                            click="purgeAgent(event)"
                            visible="{Console.currentSession.user.is_tech()}" includeInLayout="{Console.currentSession.user.is_tech()}"
                            icon="@Embed('/img/NEW/trash.png')"/>

        <actionbar:Separator visible="{Console.currentSession.user.is_tech()}" includeInLayout="{Console.currentSession.user.is_tech()}"/>

        <s:Button label="{R.get('ADD_TO_DASHBOARD')}"
                            icon="@Embed('/img/NEW/newDashboard.png')"
                            click="addToDashboard(event)"
                            enabled="{Console.currentSession.user.is_view() &amp;&amp; section.stateManager.selectedAgent.status=='open'}"
                            visible="{LicenseManager.instance.archive == 0}"
                            includeInLayout="{LicenseManager.instance.archive == 0}"/>

        <s:Button label="{R.get('ADD_TO_ALERT')}"
                            icon="@Embed('/img/NEW/alert.png')"
                            click="addToAlert(event)"
                            enabled="{Console.currentSession.user.is_view_alerts() &amp;&amp; section.stateManager.selectedAgent.status=='open'}"
                            visible="{LicenseManager.instance.archive == 0}"
                            includeInLayout="{LicenseManager.instance.archive == 0}"/>

        <!--    <s:Button label="{R.get('GHOST_ACTIVATE')}"
                                click="activateGhost(event)"
                                visible="{Console.currentSession.user.is_tech_build() &amp;&amp; section.stateManager.selectedAgent.platform=='windows'}"
                                icon="@Embed('/img/NEW/ghost.png')"
                                enabled="{!section.stateManager.selectedAgent.scout}"/>-->

    </s:HGroup>


    <s:Spacer width="100%"/>

    <s:ButtonBar dataProvider="{viewStack}"
                             requireSelection="true"
                             includeInLayout="{section.currentState != 'singleAgent'}"
                             visible="{section.currentState != 'singleAgent'}"/>

    <s:TextInput id="searchInput"
                             keyUp="onKeyUp(event)"
                             removedFromStage="onRemovedFromStage()"/>


    <fx:Script>
        <![CDATA[
      import it.ht.rcs.console.ObjectUtils;
      import it.ht.rcs.console.agent.controller.AgentManager;
      import it.ht.rcs.console.agent.model.Agent;
      import it.ht.rcs.console.alert.controller.AlertManager;
      import it.ht.rcs.console.alert.model.Alert;
      import it.ht.rcs.console.dashboard.controller.DashboardController;
      import it.ht.rcs.console.events.SectionEvent;
      import it.ht.rcs.console.monitor.controller.LicenseManager;
      import it.ht.rcs.console.network.controller.CollectorManager;
      import it.ht.rcs.console.notifications.NotificationPopup;
      import it.ht.rcs.console.operations.view.MoveItemPopup;
      import it.ht.rcs.console.operations.view.OperationsSection;
      import it.ht.rcs.console.operations.view.evidences.EvidenceExport;
      import it.ht.rcs.console.operations.view.evidences.EvidenceImport;
      import it.ht.rcs.console.search.controller.SearchManager;
      import it.ht.rcs.console.target.model.Target;
      import it.ht.rcs.console.utils.AlertPopUp;
      
      import locale.R;
      
      import mx.collections.ArrayCollection;
      import mx.collections.ListCollectionView;
      import mx.controls.Alert;
      import mx.core.FlexGlobals;
      import mx.events.CloseEvent;
      import mx.managers.PopUpManager;
      import mx.rpc.events.ResultEvent;

            [Bindable]
            public var section:OperationsSection;

            private function onNewClick():void
            {
                var popup:FactoriesForm=PopUpManager.createPopUp(root, FactoriesForm, true) as FactoriesForm;
                popup.operation=section.stateManager.selectedOperation;
                popup.target=section.stateManager.selectedTarget;
                popup.factory=new Agent(Agent.defaultAgent());
                popup.currentState=FactoriesForm.CREATE;
                PopUpManager.centerPopUp(popup);
            }

            private function onEditClick():void
            {
                if (selectedObject._kind == 'factory')
                {
                    var popup:FactoriesForm=PopUpManager.createPopUp(root, FactoriesForm, true) as FactoriesForm;
                    popup.operation=section.stateManager.selectedOperation;
                    popup.target=section.stateManager.selectedTarget;
                    popup.factory=selectedObject as Agent;
                    popup.currentState=FactoriesForm.EDIT;
                    PopUpManager.centerPopUp(popup);
                }
                else if (selectedObject._kind == 'agent')
                {
                    var popup2:AgentsForm=PopUpManager.createPopUp(root, AgentsForm, true) as AgentsForm;
                    popup2.operation=section.stateManager.selectedOperation;
                    popup2.target=section.stateManager.selectedTarget;
                    popup2.agent=selectedObject as Agent;
                    popup2.currentState=AgentsForm.EDIT;
                    PopUpManager.centerPopUp(popup2);
                }
            }

            private function activateGhost(event:MouseEvent):void
            {
                var agent:Agent=section.stateManager.selectedAgent ? section.stateManager.selectedAgent : selectedObject as Agent;
                var collectors:ListCollectionView=CollectorManager.instance.getConnected();
                var popup:GhostForm=PopUpManager.createPopUp(root, GhostForm, true) as GhostForm;
                popup.agent=agent;
                popup.collectors=collectors;
                PopUpManager.centerPopUp(popup);
            }

            private function onClose():void
            {
                var message:String=selectedObject._kind == 'agent' ? R.get('CONFIRM_AGENT_CLOSE', [selectedObject.name]) : R.get('CONFIRM_FACTORY_CLOSE', [selectedObject.name]);
                AlertPopUp.show(message, R.get('CONFIRM'), AlertPopUp.YES | AlertPopUp.NO, null, function(e:CloseEvent):void
                {
                    if (e.detail == AlertPopUp.YES)
            AlertPopUp.getData().status='closed';
                        //selectedObject.status='closed';
                }, null, AlertPopUp.NO,null,false,selectedObject);
            }

            private function onDeleteClick(event:MouseEvent):void
            {
                if (selectedObject._kind && selectedObject._kind == "factory")
                {
                    AlertPopUp.show(R.get('CONFIRM_FACTORY_DELETION', [selectedObject.name]), R.get('CONFIRM'), AlertPopUp.YES | AlertPopUp.NO, null, function(e:CloseEvent):void
                    {
                        if (e.detail == AlertPopUp.YES)
                            AgentManager.instance.delFactory( AlertPopUp.getData(), event.shiftKey);
                    }, null, AlertPopUp.NO,null,false,selectedObject);
                }
                else
                {
                    AlertPopUp.show(R.get('CONFIRM_AGENT_DELETION', [selectedObject.name]), R.get('CONFIRM'), AlertPopUp.YES | AlertPopUp.NO, null, function(e:CloseEvent):void
                    {
                        if (e.detail == AlertPopUp.YES)
                            AgentManager.instance.delAgent(AlertPopUp.getData(), event.shiftKey);
                    }, null, AlertPopUp.NO,null,false,selectedObject);

                }

            }

            private function onRemovedFromStage():void
            {
                searchInput.text='';
                section.stateManager.searchField=searchInput;
                if (section.stateManager.view)
                    section.stateManager.view.refresh();
                if (section.stateManager.tableView)
                    section.stateManager.tableView.refresh();
            }

            private function onKeyUp(event:KeyboardEvent):void
            {
                section.stateManager.searchField=searchInput;
                section.stateManager.view.refresh();
                section.stateManager.tableView.refresh()
            }

            private function importEvidence(event:MouseEvent):void
            {
                var popup:EvidenceImport=PopUpManager.createPopUp(root, EvidenceImport, true) as EvidenceImport;
                PopUpManager.centerPopUp(popup);
            }

            private function updateAgent(event:MouseEvent):void
            {
            /*  */
        var agent:Agent=section.stateManager.selectedAgent ? section.stateManager.selectedAgent : selectedObject as Agent
        AgentManager.instance.canUpgrade(agent, onCheckResult)
            }
      
      private function onCheckResult(e:ResultEvent):void
      {
        var level:String=String(e.result)
        var popup:AgentUpgrade=PopUpManager.createPopUp(root, AgentUpgrade, true) as AgentUpgrade;
        popup.agent=section.stateManager.selectedAgent ? section.stateManager.selectedAgent : selectedObject as Agent;
        popup.level=level
        PopUpManager.centerPopUp(popup);
      }

            private function purgeAgent(event:MouseEvent):void
            {
                var popup:PurgeForm=PopUpManager.createPopUp(root, PurgeForm, true) as PurgeForm;
                popup.agent=section.stateManager.selectedAgent ? section.stateManager.selectedAgent : selectedObject as Agent;
                PopUpManager.centerPopUp(popup);
            }

            private function exportEvidence(event:MouseEvent):void
            {
                var popup:EvidenceExport=PopUpManager.createPopUp(root, EvidenceExport, true) as EvidenceExport;
                popup.target=section.stateManager.selectedTarget;
                popup.agent=section.stateManager.selectedAgent;
                PopUpManager.centerPopUp(popup);
            }

            private function addToAlert(event:MouseEvent):void
            {
                var a:it.ht.rcs.console.alert.model.Alert=new it.ht.rcs.console.alert.model.Alert(it.ht.rcs.console.alert.model.Alert.defaultAlert());
                a.type='LOG';
                if (section.currentState == 'singleAgent')
                {
                    a.action='SYNC';
                    a.evidence='*';
                    a.path=new ArrayCollection();
                    a.path.addAll(section.stateManager.selectedAgent.path);
                    a.path.addItem(section.stateManager.selectedAgent._id);

                    AlertManager.instance.addAlert(ObjectUtils.toHash(a), function(a:it.ht.rcs.console.alert.model.Alert):void
                    {
                        AlertPopUp.show(R.get('ADDED_TO_ALERT', [section.stateManager.selectedAgent.name]));
                    });
                }
                else
                {
                    a.action=selectedObject._kind == 'factory' ? 'INSTANCE' : 'SYNC';
                    a.evidence=selectedObject._kind == 'factory' ? '' : '*';
                    a.path=new ArrayCollection();
                    a.path.addAll(selectedObject.path);
                    a.path.addItem(selectedObject._id);

                    AlertManager.instance.addAlert(ObjectUtils.toHash(a), function(a:it.ht.rcs.console.alert.model.Alert):void
                    {
                        AlertPopUp.show(R.get('ADDED_TO_ALERT', [selectedObject.name]));
                    });
                }

            }

            private function addToDashboard(event:MouseEvent):void
            {
        var popup:mx.controls.Alert
                if (section.currentState == 'singleAgent')
                {
                    DashboardController.instance.newDashboardItem(section.stateManager.selectedAgent._id);       
          NotificationPopup.showNotification(R.get('ADDED_TO_DASHBOARD', [section.stateManager.selectedAgent.name]), 2)
                }
                else
                {
                    DashboardController.instance.newDashboardItem(selectedObject._id);
          NotificationPopup.showNotification(R.get('ADDED_TO_DASHBOARD', [selectedObject.name]), 2)
                }

            }

            private function moveAgent(event:MouseEvent):void
            {
                var popup:MoveItemPopup=PopUpManager.createPopUp(root, MoveItemPopup, true) as MoveItemPopup;
                popup.itemToMove=selectedObject;
                PopUpManager.centerPopUp(popup);
            }
      
      protected function onJump():void
      {
        var event:SectionEvent=new SectionEvent(SectionEvent.CHANGE_SECTION);
        event.section=SectionEvent.INTELLIGENCE;
        event.subsection='entity';
        var target:Target=section.stateManager.selectedTarget
        event.item=SearchManager.instance.getItem(target._id);
        FlexGlobals.topLevelApplication.dispatchEvent(event); 
      }
        ]]>
    </fx:Script>

</actionbar:ActionBar>