@Override
  public void ExecuteCommand(UICommand command) {
    super.ExecuteCommand(command);

    if (StringHelper.stringsEqual(command.getName(), "AddHost")) {
      AddHost();
    }
    if (StringHelper.stringsEqual(command.getName(), "OnConfirmPMHost")) {
      OnConfirmPMHost();
    }
    if (StringHelper.stringsEqual(command.getName(), "OnAddHost")) {
      OnAddHost();
    }
    if (StringHelper.stringsEqual(command.getName(), "SelectHost")) {
      SelectHost();
    }
    if (StringHelper.stringsEqual(command.getName(), "OnSelectHost")) {
      OnSelectHost();
    }
    if (StringHelper.stringsEqual(command.getName(), "Cancel")) {
      Cancel();
    }
    if (StringHelper.stringsEqual(command.getName(), "CancelConfirm")) {
      CancelConfirm();
    }
    if (StringHelper.stringsEqual(command.getName(), "CancelConfirmWithFocus")) {
      CancelConfirmWithFocus();
    }
  }
  @Override
  public void ExecuteCommand(UICommand command) {
    super.ExecuteCommand(command);

    if (command == getNewCommand()) {
      New();
    } else if (command == getEditCommand()) {
      Edit();
    } else if (command == getRemoveCommand()) {
      remove();
    } else if (StringHelper.stringsEqual(command.getName(), "OnSave")) {
      OnSave();
    } else if (StringHelper.stringsEqual(command.getName(), "Cancel")) {
      Cancel();
    } else if (StringHelper.stringsEqual(command.getName(), "OnRemove")) {
      OnRemove();
    }
  }
  @Override
  public void ExecuteCommand(UICommand command) {
    super.ExecuteCommand(command);

    if (command == getManageCommand()) {
      Manage();
    } else if (command == getSetAsDisplayCommand()) {
      SetAsDisplay();
    } else if (StringHelper.stringsEqual(command.getName(), "OnManage")) {
      OnManage();
    } else if (StringHelper.stringsEqual(command.getName(), "New")) {
      New();
    } else if (StringHelper.stringsEqual(command.getName(), "OnSave")) {
      OnSave();
    } else if (StringHelper.stringsEqual(command.getName(), "Cancel")) {
      Cancel();
    }
  }
  private void remove() {
    if (getWindow() != null) {
      return;
    }

    ConfirmationModel model = new ConfirmationModel();
    setWindow(model);
    model.setTitle("Remove Network Interface(s)");
    model.setMessage("Network Interface(s)");

    java.util.ArrayList<String> items = new java.util.ArrayList<String>();
    for (Object item : getSelectedItems()) {
      VmNetworkInterface a = (VmNetworkInterface) item;
      items.add(a.getName());
    }
    model.setItems(items);

    UICommand tempVar = new UICommand("OnRemove", this);
    tempVar.setTitle("OK");
    tempVar.setIsDefault(true);
    model.getCommands().add(tempVar);
    UICommand tempVar2 = new UICommand("Cancel", this);
    tempVar2.setTitle("Cancel");
    tempVar2.setIsCancel(true);
    model.getCommands().add(tempVar2);
  }
  public void PostGetClusterNetworkList_New(java.util.ArrayList<network> network_list) {
    java.util.ArrayList<network> networks = new java.util.ArrayList<network>();
    for (network a : network_list) {
      if (a.getStatus() == NetworkStatus.Operational) {
        networks.add(a);
      }
    }

    java.util.ArrayList<VmNetworkInterface> nics = Linq.<VmNetworkInterface>Cast(getItems());
    int nicCount = nics.size();
    String newNicName = DataProvider.GetNewNicName(nics);

    VmInterfaceModel model = (VmInterfaceModel) getWindow();
    model.getNetwork().setItems(networks);
    model.getNetwork().setSelectedItem(networks.size() > 0 ? networks.get(0) : null);
    model
        .getNicType()
        .setItems(DataProvider.GetNicTypeList(getEntityStronglyTyped().getos(), false));
    model
        .getNicType()
        .setSelectedItem(DataProvider.GetDefaultNicType(getEntityStronglyTyped().getos()));
    model.getName().setEntity(newNicName);
    model.getMAC().setIsAvailable(false);

    UICommand tempVar = new UICommand("OnSave", this);
    tempVar.setTitle("OK");
    tempVar.setIsDefault(true);
    model.getCommands().add(tempVar);
    UICommand tempVar2 = new UICommand("Cancel", this);
    tempVar2.setTitle("Cancel");
    tempVar2.setIsCancel(true);
    model.getCommands().add(tempVar2);
  }
  public void OnConfirmPMHost() {
    HostModel model = (HostModel) getWindow();

    if (!model.Validate()) {
      return;
    }

    if (!((Boolean) model.getIsPm().getEntity())) {
      ConfirmationModel confirmModel = new ConfirmationModel();
      setConfirmWindow(confirmModel);
      confirmModel.setTitle("Power Management Configuration");
      confirmModel.setHashName("power_management_configuration");
      confirmModel.setMessage(
          "You haven't configured Power Management for this Host. Are you sure you want to continue?");

      UICommand tempVar = new UICommand("OnAddHost", this);
      tempVar.setTitle("OK");
      tempVar.setIsDefault(true);
      confirmModel.getCommands().add(tempVar);
      UICommand tempVar2 = new UICommand("CancelConfirmWithFocus", this);
      tempVar2.setTitle("Cancel");
      tempVar2.setIsCancel(true);
      confirmModel.getCommands().add(tempVar2);
    } else {
      OnAddHost();
    }
  }
  public void AddHost() {
    HostModel model = new HostModel();
    setWindow(model);
    model.setTitle("New Host");
    model.setHashName("new_host");
    model.getPort().setEntity(54321);
    model.getOverrideIpTables().setEntity(true);

    model.getCluster().setSelectedItem(getEntity());
    model.getCluster().setIsChangable(false);

    java.util.ArrayList<storage_pool> dataCenters = DataProvider.GetDataCenterList();
    model.getDataCenter().setItems(dataCenters);
    if (getEntity().getstorage_pool_id() != null) {
      model
          .getDataCenter()
          .setSelectedItem(
              Linq.FirstOrDefault(
                  dataCenters,
                  new Linq.DataCenterPredicate((Guid) getEntity().getstorage_pool_id())));
    }
    model.getDataCenter().setIsChangable(false);

    UICommand tempVar = new UICommand("OnConfirmPMHost", this);
    tempVar.setTitle("OK");
    tempVar.setIsDefault(true);
    model.getCommands().add(tempVar);
    UICommand tempVar2 = new UICommand("Cancel", this);
    tempVar2.setTitle("Cancel");
    tempVar2.setIsCancel(true);
    model.getCommands().add(tempVar2);
  }
  public void New() {
    if (getWindow() != null) {
      return;
    }

    ClusterNetworkModel clusterModel = new ClusterNetworkModel();
    setWindow(clusterModel);
    clusterModel.setTitle("New Logical Network");
    clusterModel.setHashName("new_logical_network");
    clusterModel.setIsNew(true);
    if (getEntity().getstorage_pool_id() != null) {
      AsyncQuery _asyncQuery = new AsyncQuery();
      _asyncQuery.setModel(clusterModel);
      _asyncQuery.asyncCallback =
          new INewAsyncCallback() {
            public void OnSuccess(Object model, Object result) {
              ClusterNetworkModel clusterNetworkModel = (ClusterNetworkModel) model;
              storage_pool dataCenter = (storage_pool) result;
              clusterNetworkModel.setDataCenterName(dataCenter.getname());
            }
          };
      AsyncDataProvider.GetDataCenterById(_asyncQuery, getEntity().getstorage_pool_id().getValue());
    }
    UICommand tempVar = new UICommand("OnSave", this);
    tempVar.setTitle("OK");
    tempVar.setIsDefault(true);
    clusterModel.getCommands().add(tempVar);

    UICommand tempVar2 = new UICommand("Cancel", this);
    tempVar2.setTitle("Cancel");
    tempVar2.setIsCancel(true);
    clusterModel.getCommands().add(tempVar2);
  }
  public void PostManage(java.util.ArrayList<network> networkList, ListModel model) {
    Collections.sort(networkList, new Linq.NetworkByNameComparer());

    java.util.ArrayList<EntityModel> items = new java.util.ArrayList<EntityModel>();
    for (network a : networkList) {
      if (!a.getname().equals("engine")) {
        EntityModel tempVar = new EntityModel();
        tempVar.setEntity(a);
        tempVar.setTitle(a.getname());
        items.add(tempVar);
      }
    }
    model.setItems(items);

    boolean noItems = items.isEmpty();

    java.util.ArrayList<network> networks = Linq.<network>Cast(getItems());
    java.util.ArrayList<EntityModel> selectedItems = new java.util.ArrayList<EntityModel>();
    for (EntityModel item : items) {
      network net = (network) item.getEntity();
      boolean value = false;
      for (network a : networks) {
        if (a.getId().equals(net.getId())) {
          value = true;
          break;
        }
      }
      item.setIsSelected(value);
      if (value) {
        selectedItems.add(item);
      }
    }

    model.setSelectedItems(selectedItems);

    UICommand tempVar2 = new UICommand("Cancel", this);
    tempVar2.setTitle("Cancel");
    tempVar2.setIsDefault(noItems);
    tempVar2.setIsCancel(true);
    model.getCommands().add(tempVar2);

    if (!noItems) {
      UICommand tempVar3 = new UICommand("OnManage", this);
      tempVar3.setTitle("OK");
      tempVar3.setIsDefault(true);
      model.getCommands().add(0, tempVar3);
    }
  }
  public void PostGetClusterNetworkList_Edit(java.util.ArrayList<network> network_list) {
    VmNetworkInterface nic = (VmNetworkInterface) getSelectedItem();
    int nicCount = Linq.<VmNetworkInterface>Cast(getItems()).size();
    java.util.ArrayList<network> networks = new java.util.ArrayList<network>();
    for (network a : network_list) {
      if (a.getStatus() == NetworkStatus.Operational) {
        networks.add(a);
      }
    }

    VmInterfaceModel model = (VmInterfaceModel) getWindow();
    model.getNetwork().setItems(networks);
    network network = null;
    for (network a : networks) {
      if (StringHelper.stringsEqual(a.getname(), nic.getNetworkName())) {
        network = a;
        break;
      }
    }
    model.getNetwork().setSelectedItem(network);

    Integer selectedNicType = nic.getType();
    java.util.ArrayList<VmInterfaceType> nicTypes =
        DataProvider.GetNicTypeList(
            getEntityStronglyTyped().getos(),
            VmInterfaceType.forValue(selectedNicType) == VmInterfaceType.rtl8139_pv);
    model.getNicType().setItems(nicTypes);

    if (selectedNicType == null || !nicTypes.contains(VmInterfaceType.forValue(selectedNicType))) {
      selectedNicType = DataProvider.GetDefaultNicType(getEntityStronglyTyped().getos()).getValue();
    }

    model.getNicType().setSelectedItem(VmInterfaceType.forValue(selectedNicType));

    model.getName().setEntity(nic.getName());
    model.getMAC().setIsAvailable(false);

    UICommand tempVar = new UICommand("OnSave", this);
    tempVar.setTitle("OK");
    tempVar.setIsDefault(true);
    model.getCommands().add(tempVar);
    UICommand tempVar2 = new UICommand("Cancel", this);
    tempVar2.setTitle("Cancel");
    tempVar2.setIsCancel(true);
    model.getCommands().add(tempVar2);
  }
  public void SelectHost() {
    java.util.ArrayList<VDSGroup> clusters = new java.util.ArrayList<VDSGroup>();
    clusters.add(getEntity());

    MoveHost model = new MoveHost();
    model.setTitle("Select Host");
    model.setHashName("select_host");
    setWindow(model);
    model.getCluster().setItems(clusters);
    model.getCluster().setSelectedItem(Linq.FirstOrDefault(clusters));
    model.getCluster().setIsAvailable(false);

    UICommand tempVar = new UICommand("OnSelectHost", this);
    tempVar.setTitle("OK");
    tempVar.setIsDefault(true);
    model.getCommands().add(tempVar);
    UICommand tempVar2 = new UICommand("Cancel", this);
    tempVar2.setTitle("Cancel");
    tempVar2.setIsCancel(true);
    model.getCommands().add(tempVar2);
  }
  private void UpdateOptions() {
    getCompulsoryActions().clear();
    getOptionalActions().clear();

    if (getEntity() != null) {
      storage_pool dataCenter = null;
      if (getEntity().getstorage_pool_id() != null) {
        dataCenter = DataProvider.GetDataCenterById(getEntity().getstorage_pool_id().getValue());
      }
      if (dataCenter == null || dataCenter.getstorage_pool_type() != StorageType.LOCALFS) {
        // Add host action.
        UICommand addHostAction = new UICommand("AddHost", this);

        // 				var hosts = DataProvider.GetHostListByCluster(Entity.name)
        // 					.Skip(1)
        // 					.ToList();
        java.util.ArrayList<VDS> hosts = DataProvider.GetHostListByCluster(getEntity().getname());
        if (hosts.size() > 1) {
          hosts.remove(0);
        }

        if (hosts.isEmpty()) {
          addHostAction.setTitle(ClusterConfigureHostsAction);
          getCompulsoryActions().add(addHostAction);
        } else {
          addHostAction.setTitle(ClusterAddAnotherHostAction);
          getOptionalActions().add(addHostAction);
        }
        if (getEntity().getstorage_pool_id() == null) {
          addHostAction.setIsExecutionAllowed(false);
          addHostAction
              .getExecuteProhibitionReasons()
              .add("The Cluster isn't attached to a Data Center");
          return;
        }
        java.util.ArrayList<VDSGroup> clusters =
            DataProvider.GetClusterList((Guid) getEntity().getstorage_pool_id());
        Version minimalClusterVersion = Linq.GetMinVersionByClusters(clusters);
        java.util.ArrayList<VDS> availableHosts = new java.util.ArrayList<VDS>();
        for (VDS vds : DataProvider.GetHostList()) {
          if ((!Linq.IsHostBelongsToAnyOfClusters(clusters, vds))
              && (vds.getstatus() == VDSStatus.Maintenance
                  || vds.getstatus() == VDSStatus.PendingApproval)
              && (vds.getVersion().getFullVersion() == null
                  || Extensions.GetFriendlyVersion(vds.getVersion().getFullVersion())
                          .compareTo(minimalClusterVersion)
                      >= 0)) {
            availableHosts.add(vds);
          }
        }
        // Select host action.
        UICommand selectHostAction = new UICommand("SelectHost", this);

        if (availableHosts.size() > 0 && clusters.size() > 0) {
          if (hosts.isEmpty()) {
            selectHostAction.setTitle(SelectHostsAction);
            getCompulsoryActions().add(selectHostAction);
          } else {
            selectHostAction.setTitle(SelectHostsAction);
            getOptionalActions().add(selectHostAction);
          }
        }
      } else {
        UICommand tempVar = new UICommand("AddHost", this);
        tempVar.setTitle(ClusterAddAnotherHostAction);
        UICommand addHostAction = tempVar;
        UICommand tempVar2 = new UICommand("SelectHost", this);
        tempVar2.setTitle(SelectHostsAction);
        UICommand selectHost = tempVar2;
        VDS host = DataProvider.GetLocalStorageHost(dataCenter.getname());
        if (host != null) {
          addHostAction.setIsExecutionAllowed(false);
          selectHost.setIsExecutionAllowed(false);
          String hasHostReason =
              "This Cluster belongs to a Local Data Center which already contain a Host";
          addHostAction.getExecuteProhibitionReasons().add(hasHostReason);
          selectHost.getExecuteProhibitionReasons().add(hasHostReason);
        }
        getCompulsoryActions().add(addHostAction);
        getOptionalActions().add(selectHost);
      }
    }
  }