use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_member_resource_attribute_def_def_filesQuota method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Member member, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrFilesLimit = null;
Integer filesQuota = null;
Integer filesLimit = null;
//Get FilesLimit attribute
try {
attrFilesLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, member, A_MR_filesLimit);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(attribute + " from member " + member.getId() + " and resource " + resource.getId() + " could not obtained.", ex);
}
//Get FilesQuota value
if (attribute.getValue() != null) {
filesQuota = (Integer) attribute.getValue();
} else {
try {
attribute = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultFilesQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultFilesQuota from resource " + resource.getId() + " could not obtained.", ex);
}
if (attribute != null && attribute.getValue() != null) {
filesQuota = (Integer) attribute.getValue();
}
}
if (filesQuota != null && filesQuota < 0)
throw new WrongAttributeValueException(attribute, resource, member, attribute + " cannot be less than 0.");
//Get FilesLimit value
if (attrFilesLimit != null && attrFilesLimit.getValue() != null) {
filesLimit = (Integer) attrFilesLimit.getValue();
} else if (attrFilesLimit == null || attrFilesLimit.getValue() == null) {
try {
attrFilesLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultFilesLimit);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultFilesLimit from resource " + resource.getId() + " could not obtained.", ex);
}
if (attrFilesLimit != null || attrFilesLimit.getValue() != null) {
filesLimit = (Integer) attrFilesLimit.getValue();
}
}
if (filesLimit != null && filesLimit < 0)
throw new ConsistencyErrorException(attrFilesLimit + " cannot be less than 0.");
//Compare filesQuota with filesLimit
if (filesQuota == null || filesQuota == 0) {
if (filesLimit != null && filesLimit != 0)
throw new WrongReferenceAttributeValueException(attribute, attrFilesLimit, resource, member, resource, null, "Try to set unlimited quota, but limit is still " + filesLimit);
} else if (filesLimit != null && filesLimit != 0) {
if (filesLimit < filesQuota)
throw new WrongReferenceAttributeValueException(attribute, attrFilesLimit, resource, member, resource, null, attribute + " must be less than or equal to " + attrFilesLimit);
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_member_resource_attribute_def_virt_isBanned method resolveVirtualAttributeValueChange.
@Override
public List<String> resolveVirtualAttributeValueChange(PerunSessionImpl perunSession, String message) throws InternalErrorException, WrongReferenceAttributeValueException, AttributeNotExistsException, WrongAttributeAssignmentException {
List<String> resolvingMessages = new ArrayList<>();
if (message == null)
return resolvingMessages;
Matcher banModificationMatcher = banModification.matcher(message);
List<Pair<Resource, Member>> listOfAffectedObjects = new ArrayList<>();
String operationType = "";
if (banModificationMatcher.find()) {
try {
String banType = banModificationMatcher.group(1);
operationType = banModificationMatcher.group(2);
int firstHolderId = Integer.valueOf(banModificationMatcher.group(3));
int secondHolderId = Integer.valueOf(banModificationMatcher.group(4));
if (operationType.equals(OPERATION_UPDATED)) {
operationType = OPERATION_SET;
} else if (!operationType.equals(OPERATION_SET) && !operationType.equals(OPERATION_REMOVED)) {
throw new InternalErrorException("Type of operation '" + operationType + "' is unknown by module.");
}
if (banType.equals(BanOnResource.class.getSimpleName())) {
Member member = perunSession.getPerunBl().getMembersManagerBl().getMemberById(perunSession, firstHolderId);
Resource resource = perunSession.getPerunBl().getResourcesManagerBl().getResourceById(perunSession, secondHolderId);
listOfAffectedObjects.add(new Pair(resource, member));
} else if (banType.equals(BanOnFacility.class.getSimpleName())) {
User user = perunSession.getPerunBl().getUsersManagerBl().getUserById(perunSession, firstHolderId);
Facility facility = perunSession.getPerunBl().getFacilitiesManagerBl().getFacilityById(perunSession, secondHolderId);
listOfAffectedObjects = getAffectedMemberResourceObjects(perunSession, user, facility);
} else {
throw new InternalErrorException("Type of ban '" + banType + "' is unkown by module.");
}
} catch (Exception e) {
log.error("Can't resolve virtual attribute value change for " + this.getClass().getSimpleName() + " module because of exception.", e);
//return empty array, do not throw exception because it can create problems
return new ArrayList<>();
}
}
for (Pair<Resource, Member> affectedObjects : listOfAffectedObjects) {
try {
Attribute attrVirtMemberResourceIsBanned = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, affectedObjects.getLeft(), affectedObjects.getRight(), AttributesManager.NS_MEMBER_RESOURCE_ATTR_VIRT + ":isBanned");
resolvingMessages.add(attrVirtMemberResourceIsBanned.serializeToString() + " " + operationType + " for " + affectedObjects.getLeft().serializeToString() + " and " + affectedObjects.getRight().serializeToString());
} catch (AttributeNotExistsException ex) {
//This means that attribute isBanned not exists at all so we can skip this process
log.info("Virtual attribute {} not exists.", this.getClass().getSimpleName());
break;
}
}
return resolvingMessages;
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_member_resource_attribute_def_def_dataLimit method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Member member, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrDataQuota = null;
String dataQuota = null;
String dataLimit = null;
String dataQuotaNumber = null;
String dataQuotaLetter = null;
String dataLimitNumber = null;
String dataLimitLetter = null;
//Get attrDataQuota attribute
try {
attrDataQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, member, A_MR_dataQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with dataQuota from member " + member.getId() + " and resource " + resource.getId() + " could not obtained.", ex);
}
//Get dataLimit value
if (attribute.getValue() == null) {
try {
attribute = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultDataLimit);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultDataLimit from resource " + resource.getId() + " could not obtained.", ex);
}
}
if (attribute.getValue() != null) {
dataLimit = (String) attribute.getValue();
Matcher numberMatcher = numberPattern.matcher(dataLimit);
Matcher letterMatcher = letterPattern.matcher(dataLimit);
numberMatcher.find();
letterMatcher.find();
try {
dataLimitNumber = dataLimit.substring(numberMatcher.start(), numberMatcher.end());
} catch (IllegalStateException ex) {
dataLimitNumber = null;
}
try {
dataLimitLetter = dataLimit.substring(letterMatcher.start(), letterMatcher.end());
} catch (IllegalStateException ex) {
dataLimitLetter = "G";
}
}
BigDecimal limitNumber;
if (dataLimitNumber != null)
limitNumber = new BigDecimal(dataLimitNumber.replace(',', '.').toString());
else
limitNumber = new BigDecimal("0");
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
throw new WrongAttributeValueException(attribute, resource, member, attribute + " cant be less than 0.");
}
//Get dataQuota value
if (attrDataQuota == null || attrDataQuota.getValue() == null) {
try {
attrDataQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultDataQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultDataQuota from resource " + resource.getId() + " could not obtained.", ex);
}
}
if (attrDataQuota != null && attrDataQuota.getValue() != null) {
dataQuota = (String) attrDataQuota.getValue();
Matcher numberMatcher = numberPattern.matcher(dataQuota);
Matcher letterMatcher = letterPattern.matcher(dataQuota);
numberMatcher.find();
letterMatcher.find();
try {
dataQuotaNumber = dataQuota.substring(numberMatcher.start(), numberMatcher.end());
} catch (IllegalStateException ex) {
dataQuotaNumber = null;
}
try {
dataQuotaLetter = dataQuota.substring(letterMatcher.start(), letterMatcher.end());
} catch (IllegalStateException ex) {
dataQuotaLetter = "G";
}
}
BigDecimal quotaNumber;
if (dataQuotaNumber != null)
quotaNumber = new BigDecimal(dataQuotaNumber.replace(',', '.'));
else
quotaNumber = new BigDecimal("0");
if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
throw new WrongReferenceAttributeValueException(attribute, attrDataQuota, resource, member, resource, null, attrDataQuota + " cant be less than 0.");
}
//Compare dataLimit with dataQuota
if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
throw new WrongReferenceAttributeValueException(attribute, attrDataQuota, resource, member, resource, null, "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 (dataLimitLetter.equals("K"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
else if (dataLimitLetter.equals("M"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
else if (dataLimitLetter.equals("T"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
else if (dataLimitLetter.equals("P"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
else if (dataLimitLetter.equals("E"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
else
limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
if (dataQuotaLetter.equals("K"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
else if (dataQuotaLetter.equals("M"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
else if (dataQuotaLetter.equals("T"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
else if (dataQuotaLetter.equals("P"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
else if (dataQuotaLetter.equals("E"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
else
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
if (limitNumber.compareTo(quotaNumber) < 0) {
throw new WrongReferenceAttributeValueException(attribute, attrDataQuota, resource, member, resource, null, attribute + " must be more than or equals to " + attrDataQuota);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_member_resource_attribute_def_def_dataQuota method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Member member, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrDataLimit = null;
String dataQuota = null;
String dataLimit = null;
String dataQuotaNumber = null;
String dataQuotaLetter = null;
String dataLimitNumber = null;
String dataLimitLetter = null;
//Get attrDataLimit attribute
try {
attrDataLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, member, A_MR_dataLimit);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with dataLimit from member " + member.getId() + " and resource " + resource.getId() + " could not obtained.", ex);
}
//Get dataQuota value
if (attribute.getValue() == null) {
try {
attribute = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultDataQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultDataQuota from resource " + resource.getId() + " could not obtained.", ex);
}
}
if (attribute.getValue() != null) {
dataQuota = (String) attribute.getValue();
Matcher numberMatcher = numberPattern.matcher(dataQuota);
Matcher letterMatcher = letterPattern.matcher(dataQuota);
numberMatcher.find();
letterMatcher.find();
try {
dataQuotaNumber = dataQuota.substring(numberMatcher.start(), numberMatcher.end());
} catch (IllegalStateException ex) {
dataQuotaNumber = null;
}
try {
dataQuotaLetter = dataQuota.substring(letterMatcher.start(), letterMatcher.end());
} catch (IllegalStateException ex) {
dataQuotaLetter = "G";
}
}
BigDecimal quotaNumber;
if (dataQuotaNumber != null)
quotaNumber = new BigDecimal(dataQuotaNumber.replace(',', '.').toString());
else
quotaNumber = new BigDecimal("0");
if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
throw new WrongAttributeValueException(attribute, resource, member, attribute + " cant be less than 0.");
}
//Get dataLimit value
if (attrDataLimit == null || attrDataLimit.getValue() == null) {
try {
attrDataLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultDataLimit);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultDataLimit from resource " + resource.getId() + " could not obtained.", ex);
}
}
if (attrDataLimit != null && attrDataLimit.getValue() != null) {
dataLimit = (String) attrDataLimit.getValue();
Matcher numberMatcher = numberPattern.matcher(dataLimit);
Matcher letterMatcher = letterPattern.matcher(dataLimit);
numberMatcher.find();
letterMatcher.find();
try {
dataLimitNumber = dataLimit.substring(numberMatcher.start(), numberMatcher.end());
} catch (IllegalStateException ex) {
dataLimitNumber = null;
}
try {
dataLimitLetter = dataLimit.substring(letterMatcher.start(), letterMatcher.end());
} catch (IllegalStateException ex) {
dataLimitLetter = "G";
}
}
BigDecimal limitNumber;
if (dataLimitNumber != null)
limitNumber = new BigDecimal(dataLimitNumber.replace(',', '.'));
else
limitNumber = new BigDecimal("0");
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
throw new WrongReferenceAttributeValueException(attribute, attrDataLimit, resource, member, resource, null, attrDataLimit + " cant be less than 0.");
}
//Compare dataQuota with dataLimit
if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
throw new WrongReferenceAttributeValueException(attribute, attrDataLimit, resource, member, resource, null, "Try to set unlimited quota, but limit is still " + dataLimitNumber + dataLimitLetter);
}
} else if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
if (dataLimitLetter.equals("K"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
else if (dataLimitLetter.equals("M"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
else if (dataLimitLetter.equals("T"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
else if (dataLimitLetter.equals("P"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
else if (dataLimitLetter.equals("E"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
else
limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
if (dataQuotaLetter.equals("K"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
else if (dataQuotaLetter.equals("M"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
else if (dataQuotaLetter.equals("T"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
else if (dataQuotaLetter.equals("P"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
else if (dataQuotaLetter.equals("E"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
else
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
if (limitNumber.compareTo(quotaNumber) < 0) {
throw new WrongReferenceAttributeValueException(attribute, attrDataLimit, resource, member, resource, null, attribute + " must be less than or equals to " + attrDataLimit);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_resource_attribute_def_def_defaultFilesLimit method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrDefaultFilesQuota = null;
Integer defaultFilesQuota = null;
Integer defaultFilesLimit = null;
//get defaultFilesQuota attribute
try {
attrDefaultFilesQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultFilesQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultFilesQuota from resource " + resource.getId() + " could not obtained.", ex);
}
//get defaultFilesLimit value
if (attribute.getValue() != null) {
defaultFilesLimit = (Integer) attribute.getValue();
}
if (defaultFilesLimit != null && defaultFilesLimit < 0)
throw new WrongAttributeValueException(attribute, resource, attribute + " cannot be less than 0.");
//get defaultFilesQuota value
if (attrDefaultFilesQuota != null && attrDefaultFilesQuota.getValue() != null) {
defaultFilesQuota = (Integer) attrDefaultFilesQuota.getValue();
}
if (defaultFilesQuota != null && defaultFilesQuota < 0)
throw new ConsistencyErrorException(attrDefaultFilesQuota + " cannot be less than 0.");
//Compare defaultFilesLimit with defaultFilesQuota
if (defaultFilesQuota == null || defaultFilesQuota == 0) {
if (defaultFilesLimit != null && defaultFilesLimit != 0)
throw new WrongReferenceAttributeValueException(attribute, attrDefaultFilesQuota, resource, null, resource, null, "Try to set limited limit, but there is still set unlimited Quota.");
} else if ((defaultFilesQuota != null && defaultFilesQuota != 0) && (defaultFilesLimit != null && defaultFilesLimit != 0)) {
if (defaultFilesLimit < defaultFilesQuota)
throw new WrongReferenceAttributeValueException(attribute, attrDefaultFilesQuota, resource, null, resource, null, attribute + " must be more than or equals to " + attrDefaultFilesQuota);
}
}
Aggregations