/**
   * Organizes items according to the current groupping flag. When new items provided takes them in
   * account and add to the Items collection.
   */
  private void InitializeItems(
      java.util.List<LunModel> newLuns, java.util.List<SanTargetModel> newTargets) {
    if (getIsGrouppedByTarget()) {
      if (getItems() == null) {
        setItems(new ObservableCollection<SanTargetModel>());
      } else {
        // Convert to list of another type as neccessary.
        if (TypeUtil.<LunModel>IsListOf(getItems())) {
          setItems(ToTargetModelList((java.util.List<LunModel>) getItems()));
        }
      }

      java.util.List<SanTargetModel> items = (java.util.List<SanTargetModel>) getItems();

      // Add new targets.
      if (newTargets != null) {
        for (SanTargetModel newItem : newTargets) {
          if (Linq.FirstOrDefault(items, new Linq.TargetPredicate(newItem)) == null) {
            items.add(newItem);
          }
        }
      }

      // Merge luns into targets.
      if (newLuns != null) {
        MergeLunsToTargets(newLuns, items);
      }

      UpdateLoginAllAvailability();
    } else {
      if (getItems() == null) {
        setItems(new ObservableCollection<LunModel>());
      } else {
        // Convert to list of another type as neccessary.
        if (TypeUtil.<SanTargetModel>IsListOf(getItems())) {
          setItems(ToLunModelList((java.util.List<SanTargetModel>) getItems()));
        }
      }

      java.util.List<LunModel> items = (java.util.List<LunModel>) getItems();

      // Add new LUNs.
      if (newLuns != null) {
        for (LunModel newItem : newLuns) {
          if (Linq.FirstOrDefault(items, new Linq.LunPredicate(newItem)) == null) {
            items.add(newItem);
          }
        }
      }
    }
  }
  private void ClearItems() {
    if (getItems() == null) {
      return;
    }

    if (getIsGrouppedByTarget()) {
      java.util.List<SanTargetModel> items = (java.util.List<SanTargetModel>) getItems();

      for (SanTargetModel target : Linq.ToList(items)) {
        boolean found = false;

        // Ensure remove targets that are not in last dicovered targets list.
        if (Linq.FirstOrDefault(lastDiscoveredTargets, new Linq.TargetPredicate(target)) != null) {
          found = true;
        } else {
          // Ensure remove targets that are not contain already included LUNs.
          for (LunModel lun : target.getLuns()) {
            LunModel foundItem = Linq.FirstOrDefault(includedLUNs, new Linq.LunPredicate(lun));
            if (foundItem == null) {
              found = true;
              break;
            }
          }
        }

        if (!found) {
          items.remove(target);
        }
      }
    } else {
      java.util.List<LunModel> items = (java.util.List<LunModel>) getItems();

      // Ensure remove targets that are not contain already included LUNs.
      for (LunModel lun : Linq.ToList(items)) {
        LunModel foundItem = Linq.FirstOrDefault(includedLUNs, new Linq.LunPredicate(lun));
        if (foundItem == null) {
          items.remove(lun);
        }
      }
    }
  }
  private void MergeLunsToTargets(
      java.util.List<LunModel> newLuns, java.util.List<SanTargetModel> targets) {
    for (LunModel lun : newLuns) {
      for (SanTargetModel target : lun.getTargets()) {
        SanTargetModel item = Linq.FirstOrDefault(targets, new Linq.TargetPredicate(target));
        if (item == null) {
          item = target;
          targets.add(item);
        }

        if (Linq.FirstOrDefault(item.getLuns(), new Linq.LunPredicate(lun)) == null) {
          item.getLuns().add(lun);
        }
      }
    }
  }
  /** Creates model items from the provided list of business entities. */
  public void ApplyData(java.util.List<LUNs> source, boolean isIncluded) {
    java.util.ArrayList<LunModel> newItems = new java.util.ArrayList<LunModel>();

    for (LUNs a : source) {
      if (a.getLunType() == getType() || a.getLunType() == StorageType.UNKNOWN) {
        java.util.ArrayList<SanTargetModel> targets = new java.util.ArrayList<SanTargetModel>();
        for (storage_server_connections b : a.getLunConnections()) {
          SanTargetModel tempVar = new SanTargetModel();
          tempVar.setAddress(b.getconnection());
          tempVar.setPort(b.getport());
          tempVar.setName(b.getiqn());
          tempVar.setIsSelected(true);
          tempVar.setIsLoggedIn(true);
          tempVar.setLuns(new ObservableCollection<LunModel>());
          SanTargetModel model = tempVar;
          model.getLoginCommand().setIsExecutionAllowed(false);

          targets.add(model);
        }

        LunModel tempVar2 = new LunModel();
        tempVar2.setLunId(a.getLUN_id());
        tempVar2.setVendorId(a.getVendorId());
        tempVar2.setProductId(a.getProductId());
        tempVar2.setSerial(a.getSerial());
        tempVar2.setMultipathing(a.getPathCount());
        tempVar2.setTargets(targets);
        tempVar2.setSize(a.getDeviceSize());
        tempVar2.setIsAccessible(a.getAccessible());
        tempVar2.setIsIncluded(isIncluded);
        tempVar2.setIsSelected(isIncluded);
        LunModel lun = tempVar2;
        newItems.add(lun);

        // Remember included LUNs to prevent their removal while updating items.
        if (isIncluded) {
          includedLUNs.add(lun);
        }
      }
    }

    InitializeItems(newItems, null);
    ProposeDiscover();
  }
  @Override
  public boolean Validate() {
    setIsValid(getAddedLuns().size() > 0 || includedLUNs.size() > 0);

    return super.Validate() && getIsValid();
  }