<?xml version="1.0" encoding="utf-8"?>
<!--
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  - JaDOrT: JASMINe Deployment Orchestration Tool
  - Copyright (C) 2008-2009 Bull S.A.S.
  - Copyright (C) 2008-2009 France Telecom R&D
  - Contact: jasmine@ow2.org
  -
  - This library is free software; you can redistribute it and/or
  - modify it under the terms of the GNU Lesser General Public
  - License as published by the Free Software Foundation; either
  - version 2.1 of the License, or any later version.
  -
  - This library is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  - Lesser General Public License for more details.
  -
  - You should have received a copy of the GNU Lesser General Public
  - License along with this library; if not, write to the Free Software
  - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  - USA
  -
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  - $Id: SelectServers.mxml 3601 2009-04-30 13:58:52Z alitokmen $
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-->
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
           xmlns:view="org.ow2.jasmine.jadort.client.flex.view.*"
           horizontalScrollPolicy="off"
           width="100%"
           height="100%"
           updateComplete="clearSelectionBoxes()">

    <mx:Script>
        <![CDATA[
            import mx.states.*;
            import mx.collections.*;
            import mx.controls.*;
            import mx.core.*;
            import mx.managers.*;
            import mx.events.*;
            import flash.events.*;
            import flash.net.*;
            import org.ow2.jasmine.jadort.client.flex.model.*;
            import org.ow2.jasmine.jadort.client.flex.event.*
            import org.ow2.jasmine.jadort.client.flex.vo.*
            import com.adobe.cairngorm.control.CairngormEventDispatcher;

            public function clearSelectionBoxes() : void {
                if (col != null) {
                    col.selected = false;
                }
            }

            [Bindable]
            public var model : JadortModelLocator = JadortModelLocator.getInstance();

            public function selectCapacity() : void {
                // Create a list of all non-maintained servers and calculate
                // the total capacity as well as the current number of sessions
                var server : ServerVO;
                var capacity : Number = 0;
                var sessions : Number = 0;
                var servers : ArrayCollection = new ArrayCollection();
                for each (server in model.servers) {
                    if (!server.maintained) {
                        servers.addItem(server);
                        capacity += server.capacity;
                        sessions += server.activeSessions;
                    }
                }

                if (servers.length < 1) {
                    // This should not be happening
                } else if (servers.length == 1) {
                    Alert.show("JaDOrT will select the only server left to maintain...");
                    model.selectedServers.removeAll();
                    model.selectedServers = servers;
                } else {
                    var sortServers : Array = new Array();
                    for (var i : Number = 0; i < servers.length; i++) {
                        sortServers[i] = servers[i];
                    }
                    sortServers.sortOn(["capacity", "activeSessions"], [Array.NUMERIC]);
                    servers.removeAll();
                    for each(server in sortServers) {
                        servers.addItem(server);
                    }

                    var index : Number = 0;
                    var autoSelect : ArrayCollection = new ArrayCollection();
                    while (servers.length > 1) {
                        server = servers[index];
                        if (capacity > 0 && capacity - server.capacity < sessions) {
                            if (index == servers.length - 1) {
                                break;
                            } else {
                                index++;
                            }
                        } else {
                            autoSelect.addItem(server);
                            servers.removeItemAt(index);
                            capacity -= server.capacity;
                            index = 0;
                        }
                    }

                    model.selectedServers.removeAll();
                    model.selectedServers = autoSelect;

                    if (autoSelect.length == 0) {
                        Alert.show("JaDOrT could not automatically select any server!\n\n"
                            + "Make sure the current user traffic can be handled by the remaining servers.");
                    } else {
                        Alert.show("JaDOrT automatically selected " + autoSelect.length + " server(s) to maintain.");
                    }
                }

                CairngormEventDispatcher.getInstance().dispatchEvent(new GetCurrentOperationEvent());
            }

            ]]>
    </mx:Script>

    <mx:CheckBox id="callcheckbox" includeInLayout="false" visible="false" />
    <mx:Grid x="0" y="0" width="100%" height="100%">
         <mx:GridRow width="100%" height="30">
            <mx:GridItem width="100%" height="100%">
                <mx:Label width="100%" color="blue" text="Select the server(s) on which to launch the maintenance operation:" height="100%" fontSize="11" />
            </mx:GridItem>
        </mx:GridRow>
        <mx:GridRow width="100%" height="100%">
            <mx:GridItem width="100%" height="100%">
                <mx:DataGrid id="dg" width="100%" height="100%" dataProvider="{model.servers}" textAlign="center">
                    <mx:columns>
                        <view:CheckBoxHeaderColumn id ="col" width="50"
                            headerRenderer="org.ow2.jasmine.jadort.client.flex.view.CheckBoxHeaderRenderer"
                            itemRenderer="org.ow2.jasmine.jadort.client.flex.view.CheckBoxItemRenderer"
                            dataField="name" textAlign="center" />
                        <mx:DataGridColumn dataField="name" headerText="Server name" width="118" />
                        <mx:DataGridColumn dataField="capacity" headerText="Capacity" width="118" />
                        <mx:DataGridColumn dataField="activeSessions" headerText="Active sessions" width="118" />
                        <mx:DataGridColumn dataField="status" headerText="Maintenance status" />
                    </mx:columns>
                </mx:DataGrid>
            </mx:GridItem>
        </mx:GridRow>
        <mx:GridRow width="100%" height="30">
            <mx:GridItem width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
                <mx:Button label="Click here" click="selectCapacity()" />
                <mx:Label text="to select all servers that can be migrated right now (based on capacity check)." />
            </mx:GridItem>
        </mx:GridRow>
    </mx:Grid>
</mx:Canvas>
