use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.
the class urn_perun_group_resource_attribute_def_def_drupalGroupType method fillAttribute.
@Override
public Attribute fillAttribute(PerunSessionImpl session, Resource resource, Group group, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
Attribute filledAttribute = new Attribute(attribute);
String attributeValue = (String) filledAttribute.getValue();
if ((attributeValue == null) || ("".equals(attributeValue))) {
filledAttribute.setValue("public");
} else {
try {
checkAttributeValue(session, resource, group, filledAttribute);
} catch (WrongAttributeValueException ex) {
log.error("Type of drupal group can be either 'public' or 'private'.", ex);
} catch (WrongReferenceAttributeValueException ex) {
log.error("Reference attribute for drupal group type can be either 'public' or 'private'.", ex);
}
}
return filledAttribute;
}
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);
}
}
}
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);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.
the class urn_perun_member_attribute_def_def_mail method changedAttributeHook.
@Override
public void changedAttributeHook(PerunSessionImpl session, Member member, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
User user = session.getPerunBl().getUsersManagerBl().getUserByMember(session, member);
if (attribute.getValue() != null) {
Attribute userPreferredMail = null;
try {
userPreferredMail = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, A_U_preferredMail);
if (userPreferredMail.getValue() == null) {
userPreferredMail.setValue(attribute.getValue());
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredMail);
}
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new WrongReferenceAttributeValueException(attribute, userPreferredMail, "Mismatch in checking of member mail and user preferredMail (different checking rules).", ex);
}
}
/* This funcionality is not needed now
//if this mail has been removed, check user preffered mail if the value is still correct, if not set a new one or remove it if no other exists
User user = session.getPerunBl().getUsersManagerBl().getUserByMember(session, member);
try {
Attribute userPreferredMail = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, A_U_preferredMail);
//TODO: if userPreferredMail is null when memberMail has been removed, its error in consistency, but exception is not solution, need to log it
//If member mail has been removed
if(attribute.getValue() == null) {
try {
session.getPerunBl().getAttributesManagerBl().checkAttributeValue(session, user, userPreferredMail);
} catch (WrongAttributeValueException ex) {
List<Member> membersOfUser = session.getPerunBl().getMembersManagerBl().getMembersByUser(session, user);
for(Member m: membersOfUser) {
Attribute memberMail = session.getPerunBl().getAttributesManagerBl().getAttribute(session, member, A_M_mail);
if(memberMail.getValue() != null) {
userPreferredMail.setValue(memberMail.getValue());
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredMail);
break;
}
}
userPreferredMail.setValue(null);
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredMail);
}
//if member mail was new set or set to another value
} else {
//if userPreferredMail is null, so can save this new value there
if(userPreferredMail.getValue() == null) {
userPreferredMail.setValue(attribute.getValue());
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredMail);
//if userPreferredMail is not null, need to try if the old value is still correct, if not, save new value there
} else {
try {
session.getPerunBl().getAttributesManagerBl().checkAttributeValue(session, user, userPreferredMail);
} catch (WrongAttributeValueException ex) {
//old value of userPreferredMail is not correct now, save the new value from member mail there
userPreferredMail.setValue(attribute.getValue());
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, attribute);
}
}
}
} catch(WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch(AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch(WrongAttributeValueException ex) {
throw new WrongReferenceAttributeValueException("There is mismatch between possible format of member mail and userPreferredMail", ex);
}*/
}
use of cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException in project perun by CESNET.
the class urn_perun_group_resource_attribute_def_virt_googleGroupName method fillAttribute.
@Override
public Attribute fillAttribute(PerunSessionImpl sess, Resource resource, Group group, AttributeDefinition attributeDefinition) throws InternalErrorException, WrongAttributeAssignmentException {
Attribute attribute = new Attribute(attributeDefinition);
Attribute googleGroupNameNamespaceAttribute;
try {
googleGroupNameNamespaceAttribute = sess.getPerunBl().getModulesUtilsBl().getGoogleGroupNameNamespaceAttributeWithNotNullValue(sess, resource);
} catch (WrongReferenceAttributeValueException ex) {
return attribute;
}
Attribute groupNameAttribute;
try {
groupNameAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, group, AttributesManager.NS_GROUP_ATTR_DEF + ":googleGroupName-namespace:" + googleGroupNameNamespaceAttribute.getValue());
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
try {
sess.getPerunBl().getAttributesManagerBl().checkAttributeValue(sess, group, groupNameAttribute);
//check passed, we can use value from this physical attribute
attribute.setValue(groupNameAttribute.getValue());
return attribute;
} catch (WrongAttributeValueException ex) {
//Physical attribute have wrong value, let's find a new one
groupNameAttribute.setValue(null);
groupNameAttribute = sess.getPerunBl().getAttributesManagerBl().fillAttribute(sess, group, groupNameAttribute);
attribute.setValue(groupNameAttribute.getValue());
return attribute;
} catch (WrongReferenceAttributeValueException ex) {
return attribute;
}
}
Aggregations