protected boolean checkRemoveEditValidations() {
    Guid clusterPolicyId = getParameters().getClusterPolicyId();
    if (clusterPolicyId == null) {
      return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_PARAMETERS_INVALID);
    }
    ClusterPolicy clusterPolicy = schedulingManager.getClusterPolicy(clusterPolicyId);
    if (clusterPolicy == null) {
      return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_PARAMETERS_INVALID);
    }
    if (clusterPolicy.isLocked()) {
      return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_PARAMETERS_INVALID);
    }

    return true;
  }
 @Override
 public List<ClusterPolicy> getAll() {
   List<ClusterPolicy> clusterPolicies = super.getAll();
   Map<Guid, ClusterPolicy> map = new HashMap<>();
   for (ClusterPolicy clusterPolicy : clusterPolicies) {
     map.put(clusterPolicy.getId(), clusterPolicy);
   }
   List<ClusterPolicyUnit> clusterPolicyUnits =
       getCallsHandler()
           .executeReadList(
               "GetAllFromClusterPolicyUnits",
               createClusterPolicyUnitRowMapper(),
               getCustomMapSqlParameterSource());
   fillClusterPolicy(map, clusterPolicyUnits);
   return clusterPolicies;
 }
 @Override
 public ClusterPolicy get(Guid id) {
   ClusterPolicy clusterPolicy = super.get(id);
   if (clusterPolicy == null) {
     return null;
   }
   List<ClusterPolicyUnit> clusterPolicyUnits =
       getCallsHandler()
           .executeReadList(
               "GetClusterPolicyUnitsByClusterPolicyId",
               createClusterPolicyUnitRowMapper(),
               createIdParameterMapper(id));
   Map<Guid, ClusterPolicy> map = new HashMap<Guid, ClusterPolicy>();
   map.put(clusterPolicy.getId(), clusterPolicy);
   fillClusterPolicy(map, clusterPolicyUnits);
   return clusterPolicy;
 }
 private ClusterPolicyUnit getClusterPolicyUnit(
     ClusterPolicy clusterPolicy, Guid policyUnitId, Map<Guid, ClusterPolicyUnit> map) {
   ClusterPolicyUnit clusterPolicyUnit = map.get(policyUnitId);
   if (clusterPolicyUnit == null) {
     clusterPolicyUnit = new ClusterPolicyUnit(clusterPolicy.getId(), policyUnitId);
     map.put(policyUnitId, clusterPolicyUnit);
   }
   return clusterPolicyUnit;
 }
 @Override
 public void update(ClusterPolicy clusterPolicy) {
   super.update(clusterPolicy);
   getCallsHandler()
       .executeModification(
           "DeleteClusterPolicyUnitsByClusterPolicyId",
           getCustomMapSqlParameterSource().addValue("id", clusterPolicy.getId()));
   List<ClusterPolicyUnit> clusterPolicyUnits = getclusterPolicyUnit(clusterPolicy);
   if (clusterPolicyUnits != null) {
     for (ClusterPolicyUnit clusterPolicyUnit : clusterPolicyUnits) {
       saveClusterPolicyUnit(clusterPolicyUnit);
     }
   }
 }
 @Override
 protected MapSqlParameterSource createFullParametersMapper(ClusterPolicy entity) {
   return createIdParameterMapper(entity.getId())
       .addValue("name", entity.getName())
       .addValue("description", entity.getDescription())
       .addValue("is_locked", entity.isLocked())
       .addValue("is_default", entity.isDefaultPolicy())
       .addValue(
           "custom_properties",
           SerializationFactory.getSerializer().serialize(entity.getParameterMap()));
 }
 private List<ClusterPolicyUnit> getclusterPolicyUnit(ClusterPolicy entity) {
   Map<Guid, ClusterPolicyUnit> map = new HashMap<Guid, ClusterPolicyUnit>();
   ClusterPolicyUnit unit;
   if (entity.getFilters() != null) {
     for (Guid policyUnitId : entity.getFilters()) {
       unit = getClusterPolicyUnit(entity, policyUnitId, map);
       if (entity.getFilterPositionMap() != null) {
         Integer position = entity.getFilterPositionMap().get(policyUnitId);
         unit.setFilterSequence(position != null ? position : 0);
       }
     }
   }
   if (entity.getFunctions() != null) {
     for (Pair<Guid, Integer> pair : entity.getFunctions()) {
       unit = getClusterPolicyUnit(entity, pair.getFirst(), map);
       unit.setFactor(pair.getSecond());
     }
   }
   if (entity.getBalance() != null) {
     getClusterPolicyUnit(entity, entity.getBalance(), map);
   }
   return new ArrayList<ClusterPolicyUnit>(map.values());
 }
  protected boolean checkAddEditValidations() {
    List<ClusterPolicy> clusterPolicies = schedulingManager.getClusterPolicies();
    if (getClusterPolicy() == null) {
      return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_PARAMETERS_INVALID);
    }
    for (ClusterPolicy clusterPolicy : clusterPolicies) {
      if (!clusterPolicy.getId().equals(getClusterPolicy().getId())
          && clusterPolicy.getName().equals(getClusterPolicy().getName())) {
        return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_NAME_INUSE);
      }
    }
    Map<Guid, PolicyUnitImpl> map = schedulingManager.getPolicyUnitsMap();
    Set<Guid> existingPolicyUnits = new HashSet<>();
    // check filter policy units
    if (getClusterPolicy().getFilters() != null) {
      for (Guid filterId : getClusterPolicy().getFilters()) {
        if (isPolicyUnitExists(filterId, existingPolicyUnits)) {
          return failValidation(
              EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_DUPLICATE_POLICY_UNIT);
        }
        PolicyUnitImpl policyUnitImpl = map.get(filterId);
        if (policyUnitImpl == null) {
          return failValidation(
              EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_UNKNOWN_POLICY_UNIT);
        }
        if (policyUnitImpl.getPolicyUnit().getPolicyUnitType() != PolicyUnitType.FILTER) {
          return failValidation(
              EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_FILTER_NOT_IMPLEMENTED);
        }
      }
    }
    // check filters positions (there could be only one filter attached to first (-1) and last (-1)
    if (getClusterPolicy().getFilterPositionMap() != null) {
      boolean hasFirst = false;
      boolean hasLast = false;
      for (Integer position : getClusterPolicy().getFilterPositionMap().values()) {
        if (position == -1) {
          if (!hasFirst) {
            hasFirst = true;
          } else {
            return failValidation(
                EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_ONLY_ONE_FILTER_CAN_BE_FIRST);
          }
        } else if (position == 1) {
          if (!hasLast) {
            hasLast = true;
          } else {
            return failValidation(
                EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_ONLY_ONE_FILTER_CAN_BE_LAST);
          }
        }
      }
    }
    // check function policy units
    if (getClusterPolicy().getFunctions() != null) {
      for (Pair<Guid, Integer> functionPair : getClusterPolicy().getFunctions()) {
        if (isPolicyUnitExists(functionPair.getFirst(), existingPolicyUnits)) {
          return failValidation(
              EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_DUPLICATE_POLICY_UNIT);
        }
        PolicyUnitImpl policyUnitImpl = map.get(functionPair.getFirst());
        if (policyUnitImpl == null) {
          return failValidation(
              EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_UNKNOWN_POLICY_UNIT);
        }
        if (policyUnitImpl.getPolicyUnit().getPolicyUnitType() != PolicyUnitType.WEIGHT) {
          return failValidation(
              EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_FUNCTION_NOT_IMPLEMENTED);
        }
        if (functionPair.getSecond() < 0) {
          return failValidation(
              EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_FUNCTION_FACTOR_NEGATIVE);
        }
      }
    }
    // check balance policy unit
    if (getClusterPolicy().getBalance() != null) {
      PolicyUnitImpl policyUnitImpl = map.get(getClusterPolicy().getBalance());
      if (policyUnitImpl == null) {
        return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_UNKNOWN_POLICY_UNIT);
      }
      if (policyUnitImpl.getPolicyUnit().getPolicyUnitType() != PolicyUnitType.LOAD_BALANCING) {
        return failValidation(
            EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_BALANCE_NOT_IMPLEMENTED);
      }
    }

    List<ValidationError> validationErrors =
        SimpleCustomPropertiesUtil.getInstance()
            .validateProperties(
                schedulingManager.getCustomPropertiesRegexMap(getClusterPolicy()),
                getClusterPolicy().getParameterMap());
    if (!validationErrors.isEmpty()) {
      SimpleCustomPropertiesUtil.getInstance()
          .handleCustomPropertiesError(validationErrors, getReturnValue().getValidationMessages());
      return false;
    }
    return true;
  }
 private void fillClusterPolicy(
     Map<Guid, ClusterPolicy> map, List<ClusterPolicyUnit> clusterPolicyUnits) {
   Map<Guid, PolicyUnit> policyUnitMap = new HashMap<Guid, PolicyUnit>();
   for (PolicyUnit policyUnit : policyUnitDao.getAll()) {
     policyUnitMap.put(policyUnit.getId(), policyUnit);
   }
   for (ClusterPolicyUnit clusterPolicyUnit : clusterPolicyUnits) {
     ClusterPolicy clusterPolicy = map.get(clusterPolicyUnit.getClusterPolicyId());
     if (policyUnitMap.get(clusterPolicyUnit.getPolicyUnitId()).getPolicyUnitType()
         == PolicyUnitType.FILTER) {
       if (clusterPolicy.getFilters() == null) {
         clusterPolicy.setFilters(new ArrayList<Guid>());
       }
       clusterPolicy.getFilters().add(clusterPolicyUnit.getPolicyUnitId());
       if (clusterPolicyUnit.getFilterSequence() != 0) {
         if (clusterPolicy.getFilterPositionMap() == null) {
           clusterPolicy.setFilterPositionMap(new HashMap<Guid, Integer>());
         }
         clusterPolicy
             .getFilterPositionMap()
             .put(clusterPolicyUnit.getPolicyUnitId(), clusterPolicyUnit.getFilterSequence());
       }
     }
     if (policyUnitMap.get(clusterPolicyUnit.getPolicyUnitId()).getPolicyUnitType()
         == PolicyUnitType.WEIGHT) {
       if (clusterPolicy.getFunctions() == null) {
         clusterPolicy.setFunctions(new ArrayList<Pair<Guid, Integer>>());
       }
       clusterPolicy
           .getFunctions()
           .add(
               new Pair<Guid, Integer>(
                   clusterPolicyUnit.getPolicyUnitId(), clusterPolicyUnit.getFactor()));
     }
     if (policyUnitMap.get(clusterPolicyUnit.getPolicyUnitId()).getPolicyUnitType()
         == PolicyUnitType.LOAD_BALANCING) {
       clusterPolicy.setBalance(clusterPolicyUnit.getPolicyUnitId());
     }
   }
 }