Search in sources :

Example 1 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class Auditer method flush.

/**
	 * Imidiately flushes stored message for last top-level transaction into the log
	 *
	 */
public void flush() {
    List<List<List<AuditerMessage>>> topLevelTransactions = getTopLevelTransactions();
    if (topLevelTransactions.isEmpty()) {
        log.trace("No messages to flush");
        return;
    }
    List<List<AuditerMessage>> transactionChain = topLevelTransactions.get(topLevelTransactions.size() - 1);
    if (transactionChain.isEmpty()) {
        log.trace("No messages to flush");
        topLevelTransactions.remove(topLevelTransactions.size() - 1);
        return;
    }
    if (transactionChain.size() != 1) {
        log.error("There should be only one list of messages while flushing representing the most outer transaction.");
    }
    List<AuditerMessage> messages = transactionChain.get(0);
    topLevelTransactions.remove(topLevelTransactions.size() - 1);
    if (topLevelTransactions.isEmpty()) {
        TransactionSynchronizationManager.unbindResourceIfPossible(this);
    }
    log.trace("Audit messages was flushed for current transaction.");
    synchronized (LOCK_DB_TABLE_AUDITER_LOG) {
        storeMessagesToDb(messages);
    }
    for (AuditerMessage message : messages) {
        for (VirtualAttributesModuleImplApi virtAttrModuleImplApi : registeredAttributesModules) {
            List<String> resolvingMessages = new ArrayList<String>();
            try {
                resolvingMessages.addAll(virtAttrModuleImplApi.resolveVirtualAttributeValueChange((PerunSessionImpl) message.getOriginaterPerunSession(), message.getMessage()));
            } catch (InternalErrorException ex) {
                log.error("Error when auditer trying to resolve messages in modules.", ex);
            } catch (WrongAttributeAssignmentException ex) {
                log.error("Error when auditer trying to resolve messages in modules.", ex);
            } catch (WrongReferenceAttributeValueException ex) {
                log.error("Error when auditer trying to resolve messages in modules.", ex);
            } catch (AttributeNotExistsException ex) {
                log.error("Error when auditer trying to resolve messages in modules.", ex);
            }
            if (!resolvingMessages.isEmpty()) {
                List<AuditerMessage> resolvingAuditerMessages = new ArrayList<>();
                for (String msg : resolvingMessages) {
                    resolvingAuditerMessages.add(new AuditerMessage(message.getOriginaterPerunSession(), msg));
                }
                storeMessagesToDb(resolvingAuditerMessages);
            }
        }
    }
}
Also used : VirtualAttributesModuleImplApi(cz.metacentrum.perun.core.implApi.modules.attributes.VirtualAttributesModuleImplApi) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_group_attribute_def_def_unixGID_namespace method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl sess, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    try {
        String gidNamespace = attribute.getFriendlyNameParameter();
        //Special behaviour if gid is null
        if (attribute.getValue() == null) {
            List<Facility> groupFacilities = new ArrayList<Facility>();
            for (Resource r : sess.getPerunBl().getResourcesManagerBl().getAssignedResources(sess, group)) {
                groupFacilities.add(sess.getPerunBl().getResourcesManagerBl().getFacility(sess, r));
            }
            Set<String> namespacesWhereGroupMustHaveGIDifItHaveUnixNameThere = sess.getPerunBl().getModulesUtilsBl().getSetOfGroupNameNamespacesWhereFacilitiesHasTheSameGIDNamespace(sess, groupFacilities, attribute);
            for (String namespace : namespacesWhereGroupMustHaveGIDifItHaveUnixNameThere) {
                Attribute unixGroupName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, group, A_G_unixGroupName_namespace + ":" + namespace);
                if (unixGroupName.getValue() != null) {
                    throw new WrongAttributeValueException(attribute, group, "Group is propagated to the facility where it have set unix group name so it must have unix GID too.");
                }
            }
            //Group is not propagated to any facility in this GID namespace or it doesn't have set unix name there so it doesn't need to have unix GID.
            return;
        }
        //Special behaviour if gid is null
        Integer attrValue = null;
        if (attribute.getValue() == null) {
            throw new WrongAttributeValueException(attribute, group, "Unix GID must be set");
        } else {
            attrValue = (Integer) attribute.getValue();
        }
        //check if gid is not already depleted
        Attribute usedGids = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, gidNamespace, A_E_usedGids);
        //null in value means there is no depleted or used gids
        if (usedGids.getValue() != null) {
            Map<String, String> usedGidsValue = (Map<String, String>) usedGids.getValue();
            //Dx, where x is GID means depleted value for GID x
            if (usedGidsValue.containsKey("D" + attrValue.toString())) {
                throw new WrongReferenceAttributeValueException(attribute, usedGids, group, null, gidNamespace, null, "This GID is already depleted.");
            }
        }
        //Check if gid GID is within allowed range
        sess.getPerunBl().getModulesUtilsBl().checkIfGIDIsWithinRange(sess, attribute);
        //Prepare lists for all groups and resources with same GID in the same namespace
        List<Group> allGroupsWithSameGIDInSameNamespace = new ArrayList<Group>();
        List<Resource> allResourcesWithSameGIDInSameNamespace = new ArrayList<Resource>();
        //Prepare attributes for searching through groups and resources
        Attribute groupGIDAttribute = attribute;
        Attribute resourceGIDAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGID_namespace + ":" + gidNamespace));
        resourceGIDAttribute.setValue(groupGIDAttribute.getValue());
        //Fill lists of Groups and Resources by data
        allGroupsWithSameGIDInSameNamespace.addAll(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupGIDAttribute));
        allResourcesWithSameGIDInSameNamespace.addAll(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceGIDAttribute));
        //remove this group
        allGroupsWithSameGIDInSameNamespace.remove(group);
        //Prepare list of GroupName attributes of this group
        List<Attribute> groupNamesOfGroup = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, A_G_unixGroupName_namespace + ":");
        //Searching through groups
        if (!allGroupsWithSameGIDInSameNamespace.isEmpty()) {
            for (Group g : allGroupsWithSameGIDInSameNamespace) {
                for (Attribute a : groupNamesOfGroup) {
                    int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, a);
                    if (compare > 0) {
                        //This is problem, there is the same attribute but have other value
                        throw new WrongReferenceAttributeValueException(attribute, a, "There is a group with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + g + " " + group);
                    }
                //Other possibilities are not problem, less than 0 mean that same attribute not exists, and 0 mean that attribute exists but have same value
                }
            }
        }
        //Searching through resources
        if (!allResourcesWithSameGIDInSameNamespace.isEmpty()) {
            for (Resource r : allResourcesWithSameGIDInSameNamespace) {
                for (Attribute a : groupNamesOfGroup) {
                    //Prepare resource version of this group attribute
                    Attribute resourceGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGroupName_namespace + ":" + a.getFriendlyNameParameter()));
                    resourceGroupName.setValue(a.getValue());
                    int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, resourceGroupName);
                    if (compare > 0) {
                        //This is problem, there is the same attribute but have other value
                        throw new WrongReferenceAttributeValueException(attribute, a, "There is a resource with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + r + " " + group);
                    }
                //Other possibilities are not problem, less than 0 mean that same attribute not exists, and 0 mean that attribute exists but have same value
                }
            }
        }
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_group_attribute_def_def_unixGroupName_namespace method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl sess, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    //prepare namespace and groupName value variables
    String groupName = null;
    if (attribute.getValue() != null)
        groupName = (String) attribute.getValue();
    String groupNameNamespace = attribute.getFriendlyNameParameter();
    if (groupName == null) {
        // if this is group attribute, its ok
        return;
    }
    //Check attribute regex
    sess.getPerunBl().getModulesUtilsBl().checkAttributeRegex(attribute, "^[-._a-zA-Z0-9]+$");
    //Check reserved unix group names
    sess.getPerunBl().getModulesUtilsBl().checkReservedUnixGroupNames(attribute);
    try {
        //prepare attributes group and resource unixGroupName
        Attribute groupUnixGroupName = attribute;
        Attribute resourceUnixGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGroupName_namespace + ":" + groupNameNamespace));
        resourceUnixGroupName.setValue(attribute.getValue());
        //prepare lists of groups and resources with the same groupName value in the same namespace
        List<Group> groupsWithSameGroupNameInTheSameNamespace = new ArrayList<Group>();
        List<Resource> resourcesWithSameGroupNameInTheSameNamespace = new ArrayList<Resource>();
        //Fill lists of groups and resources
        groupsWithSameGroupNameInTheSameNamespace.addAll(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupUnixGroupName));
        resourcesWithSameGroupNameInTheSameNamespace.addAll(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceUnixGroupName));
        //If there is no group or resource with same GroupNameInTheSameNamespace, its ok
        if (groupsWithSameGroupNameInTheSameNamespace.isEmpty() && resourcesWithSameGroupNameInTheSameNamespace.isEmpty())
            return;
        //First need to know that i have right to write any of duplicit groupName-namespace attribute
        boolean haveRights = sess.getPerunBl().getModulesUtilsBl().haveRightToWriteAttributeInAnyGroupOrResource(sess, groupsWithSameGroupNameInTheSameNamespace, resourcesWithSameGroupNameInTheSameNamespace, groupUnixGroupName, resourceUnixGroupName);
        if (!haveRights)
            throw new WrongReferenceAttributeValueException(attribute, "This groupName is already used for other group or resource and user has no rights to use it.");
        //Now if rights are ok, prepare lists of UnixGIDs attributes of this group (also equivalent resource GID)
        List<Attribute> groupUnixGIDs = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, A_G_unixGID_namespace + ":");
        List<Attribute> resourceVersionOfUnixGIDs = sess.getPerunBl().getModulesUtilsBl().getListOfResourceGIDsFromListOfGroupGIDs(sess, groupUnixGIDs);
        //In list of duplicit groups looking for GID in same namespace but with different value, thats not correct
        if (!groupsWithSameGroupNameInTheSameNamespace.isEmpty()) {
            for (Group g : groupsWithSameGroupNameInTheSameNamespace) {
                for (Attribute a : groupUnixGIDs) {
                    int compare;
                    compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, a);
                    if (compare > 0) {
                        throw new WrongReferenceAttributeValueException(attribute, a, "One of the group GIDs is from the same namespace like other group GID but with different values.");
                    }
                }
            }
        }
        //In list of duplicit resources looking for GID in same namespace but with different value, thats not correct
        if (!resourcesWithSameGroupNameInTheSameNamespace.isEmpty()) {
            for (Resource r : resourcesWithSameGroupNameInTheSameNamespace) {
                for (Attribute a : resourceVersionOfUnixGIDs) {
                    int compare;
                    compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, a);
                    if (compare > 0) {
                        throw new WrongReferenceAttributeValueException(attribute, a, "One of the group GIDs is from the same namespace like other resource GIDs but with different values.");
                    }
                }
            }
        }
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)

Example 4 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_group_resource_attribute_def_def_projectDataLimit method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Attribute attrProjectDataQuota = null;
    String projectDataQuota = null;
    String projectDataLimit = null;
    String projectDataQuotaNumber = null;
    String projectDataQuotaLetter = null;
    String projectDataLimitNumber = null;
    String projectDataLimitLetter = null;
    //Check if attribute value has the right exp pattern (can be null)
    if (attribute.getValue() != null) {
        Matcher testMatcher = testingPattern.matcher((String) attribute.getValue());
        if (!testMatcher.find())
            throw new WrongAttributeValueException(attribute, resource, group, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
    } else
        return;
    //Get ProjectDataQuota attribute
    try {
        attrProjectDataQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, group, A_GR_projectDataQuota);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute with projectDataQuota from resource " + resource.getId() + " and group + " + group.getId() + " could not obtain.", ex);
    }
    //Get ProjectDataLimit value
    if (attribute.getValue() != null) {
        projectDataLimit = (String) attribute.getValue();
        Matcher numberMatcher = numberPattern.matcher(projectDataLimit);
        Matcher letterMatcher = letterPattern.matcher(projectDataLimit);
        numberMatcher.find();
        letterMatcher.find();
        try {
            projectDataLimitNumber = projectDataLimit.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataLimitNumber = null;
        }
        try {
            projectDataLimitLetter = projectDataLimit.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataLimitLetter = "G";
        }
    }
    BigDecimal limitNumber;
    if (projectDataLimitNumber != null)
        limitNumber = new BigDecimal(projectDataLimitNumber.replace(',', '.'));
    else
        limitNumber = new BigDecimal("0");
    if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongAttributeValueException(attribute, attribute + " can't be less than 0.");
    }
    //Get ProjectDataQuota value
    if (attrProjectDataQuota != null && attrProjectDataQuota.getValue() != null) {
        projectDataQuota = (String) attrProjectDataQuota.getValue();
        Matcher numberMatcher = numberPattern.matcher(projectDataQuota);
        Matcher letterMatcher = letterPattern.matcher(projectDataQuota);
        numberMatcher.find();
        letterMatcher.find();
        try {
            projectDataQuotaNumber = projectDataQuota.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataQuotaNumber = null;
        }
        try {
            projectDataQuotaLetter = projectDataQuota.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataQuotaLetter = "G";
        }
    }
    BigDecimal quotaNumber;
    if (projectDataQuotaNumber != null)
        quotaNumber = new BigDecimal(projectDataQuotaNumber.replace(',', '.'));
    else
        quotaNumber = new BigDecimal("0");
    if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, attrProjectDataQuota + " cant be less than 0.");
    }
    //Compare ProjectDataLimit with ProjectDataQuota
    if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
        if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, "Try to set limited limit, but there is still set unlimited Quota.");
        }
    } else if ((quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) != 0) && (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0)) {
        if (projectDataLimitLetter.equals("K"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
        else if (projectDataLimitLetter.equals("M"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
        else if (projectDataLimitLetter.equals("T"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
        else if (projectDataLimitLetter.equals("P"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
        else if (projectDataLimitLetter.equals("E"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
        else
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
        if (projectDataQuotaLetter.equals("K"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
        else if (projectDataQuotaLetter.equals("M"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
        else if (projectDataQuotaLetter.equals("T"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
        else if (projectDataQuotaLetter.equals("P"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
        else if (projectDataQuotaLetter.equals("E"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
        else
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
        if (limitNumber.compareTo(quotaNumber) < 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, attribute + " must be more than or equals to " + attrProjectDataQuota);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BigDecimal(java.math.BigDecimal)

Example 5 with WrongReferenceAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.

the class urn_perun_group_resource_attribute_def_def_projectDataQuota method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Attribute attrProjectDataLimit = null;
    String projectDataQuota = null;
    String projectDataLimit = null;
    String projectDataQuotaNumber = null;
    String projectDataQuotaLetter = null;
    String projectDataLimitNumber = null;
    String projectDataLimitLetter = null;
    //Check if attribute value has the right exp pattern (can be null)
    if (attribute.getValue() != null) {
        Matcher testMatcher = testingPattern.matcher((String) attribute.getValue());
        if (!testMatcher.find())
            throw new WrongAttributeValueException(attribute, resource, group, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
    }
    //Get ProjectDataLimit attribute
    try {
        attrProjectDataLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, group, A_GR_projectDataLimit);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute with projectDataLimit from resource " + resource.getId() + " and group " + group.getId() + " could not obtain.", ex);
    }
    //Get ProjectDataQuota value
    if (attribute.getValue() != null) {
        projectDataQuota = (String) attribute.getValue();
        Matcher numberMatcher = numberPattern.matcher(projectDataQuota);
        Matcher letterMatcher = letterPattern.matcher(projectDataQuota);
        numberMatcher.find();
        letterMatcher.find();
        try {
            projectDataQuotaNumber = projectDataQuota.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataQuotaNumber = null;
        }
        try {
            projectDataQuotaLetter = projectDataQuota.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataQuotaLetter = "G";
        }
    }
    BigDecimal quotaNumber;
    if (projectDataQuotaNumber != null)
        quotaNumber = new BigDecimal(projectDataQuotaNumber.replace(',', '.'));
    else
        quotaNumber = new BigDecimal("0");
    if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongAttributeValueException(attribute, attribute + " can't be less than 0.");
    }
    //Get ProjectDataLimit value
    if (attrProjectDataLimit != null && attrProjectDataLimit.getValue() != null) {
        projectDataLimit = (String) attrProjectDataLimit.getValue();
        Matcher numberMatcher = numberPattern.matcher(projectDataLimit);
        Matcher letterMatcher = letterPattern.matcher(projectDataLimit);
        numberMatcher.find();
        letterMatcher.find();
        try {
            projectDataLimitNumber = projectDataLimit.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataLimitNumber = null;
        }
        try {
            projectDataLimitLetter = projectDataLimit.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataLimitLetter = "G";
        }
    }
    BigDecimal limitNumber;
    if (projectDataLimitNumber != null)
        limitNumber = new BigDecimal(projectDataLimitNumber.replace(',', '.'));
    else
        limitNumber = new BigDecimal("0");
    if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, attrProjectDataLimit + " cant be less than 0.");
    }
    //Compare ProjectDataQuota with ProjectDataLimit
    if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
        if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, "Try to set unlimited quota, but limit is still " + projectDataLimitNumber + projectDataLimitLetter);
        }
    } else if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
        if (projectDataLimitLetter.equals("K"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
        else if (projectDataLimitLetter.equals("M"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
        else if (projectDataLimitLetter.equals("T"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
        else if (projectDataLimitLetter.equals("P"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
        else if (projectDataLimitLetter.equals("E"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
        else
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
        if (projectDataQuotaLetter.equals("K"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
        else if (projectDataQuotaLetter.equals("M"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
        else if (projectDataQuotaLetter.equals("T"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
        else if (projectDataQuotaLetter.equals("P"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
        else if (projectDataQuotaLetter.equals("E"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
        else
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
        if (limitNumber.compareTo(quotaNumber) < 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, attribute + " must be less than or equals to " + projectDataLimit);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BigDecimal(java.math.BigDecimal)

Aggregations

WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)224 Attribute (cz.metacentrum.perun.core.api.Attribute)188 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)155 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)128 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)120 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)116 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)87 ArrayList (java.util.ArrayList)36 Group (cz.metacentrum.perun.core.api.Group)35 Resource (cz.metacentrum.perun.core.api.Resource)33 Facility (cz.metacentrum.perun.core.api.Facility)26 User (cz.metacentrum.perun.core.api.User)26 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)20 GroupResourceMismatchException (cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException)17 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)15 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)14 Vo (cz.metacentrum.perun.core.api.Vo)13 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)13 VoNotExistsException (cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)12 LinkedHashMap (java.util.LinkedHashMap)12