use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.
the class urn_perun_group_attribute_def_def_unixGID_namespace method changedAttributeHook.
@Override
public void changedAttributeHook(PerunSessionImpl session, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
String gidNamespace = attribute.getFriendlyNameParameter();
//get attribute with usedGids for update
//IMPORTANT: for update lock row in table of attr values, be careful when using
Attribute usedGids;
try {
usedGids = session.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(session, gidNamespace, A_E_usedGids);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
//Get Map of gids (if there is no value, use empty map
Map<String, String> usedGidsValue = new LinkedHashMap<>();
if (usedGids.getValue() != null)
usedGidsValue = (Map<String, String>) usedGids.getValue();
//initial settings
String key = "G" + group.getId();
String oldGid = usedGidsValue.get(key);
//for removing gid
if (attribute.getValue() == null) {
//remove record from map
if (oldGid != null) {
usedGidsValue.remove(key);
//looking for another oldGid value, if not exists, add depleted record
if (!usedGidsValue.containsValue(oldGid)) {
usedGidsValue.put("D" + oldGid, oldGid);
}
}
//for setting gid
} else {
String newUnixGid = ((Integer) attribute.getValue()).toString();
//add new record to map
usedGidsValue.put(key, newUnixGid);
//looking for another oldGid value, if not exists, add depleted record
if (oldGid != null && !usedGidsValue.containsValue(oldGid)) {
usedGidsValue.put("D" + oldGid, oldGid);
}
}
//set new attribute value for usedGids
usedGids.setValue(usedGidsValue);
try {
session.getPerunBl().getAttributesManagerBl().setAttribute(session, gidNamespace, usedGids);
} catch (WrongAttributeValueException ex) {
throw new WrongReferenceAttributeValueException(attribute, usedGids, ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.
the class urn_perun_group_attribute_def_def_unixGroupName_namespace method changedAttributeHook.
@Override
public void changedAttributeHook(PerunSessionImpl session, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
//Need to know if this is remove or set, if value is null, its remove, otherway it is set
String groupNameNamespace = attribute.getFriendlyNameParameter();
try {
if (attribute.getValue() == null) {
//This is ok, for now no changes for removing some GroupName of this Group
} else {
//First need to find all facilities for the group
Set<Facility> facilitiesOfGroup = new HashSet<Facility>();
List<Resource> resourcesOfGroup = session.getPerunBl().getResourcesManagerBl().getAssignedResources(session, group);
for (Resource r : resourcesOfGroup) {
facilitiesOfGroup.add(session.getPerunBl().getResourcesManagerBl().getFacility(session, r));
}
//Prepare list of gid namespaces of all facilities which have the same groupName namespace like this unixGroupName namespace
Set<String> gidNamespaces;
gidNamespaces = session.getPerunBl().getModulesUtilsBl().getSetOfGIDNamespacesWhereFacilitiesHasTheSameGroupNameNamespace(session, new ArrayList<Facility>(facilitiesOfGroup), attribute);
//If there is any gidNamespace which is need to be set, do it there
if (!gidNamespaces.isEmpty()) {
List<Attribute> gidsToSet = new ArrayList<>();
for (String s : gidNamespaces) {
Attribute groupUnixGIDNamespace = session.getPerunBl().getAttributesManagerBl().getAttribute(session, group, A_G_unixGID_namespace + ":" + s);
//If attribute is not set, then set it (first fill, then set)
if (groupUnixGIDNamespace.getValue() == null) {
groupUnixGIDNamespace = session.getPerunBl().getAttributesManagerBl().fillAttribute(session, group, groupUnixGIDNamespace);
if (groupUnixGIDNamespace.getValue() == null)
throw new WrongReferenceAttributeValueException(attribute, groupUnixGIDNamespace);
//Set after fill (without check because all namespaces must be set before check (there can be relation between namespaces)
gidsToSet.add(groupUnixGIDNamespace);
}
}
//set and check if there is some gid to set
if (!gidsToSet.isEmpty()) {
try {
session.getPerunBl().getAttributesManagerBl().setAttributes(session, group, gidsToSet);
} catch (WrongAttributeValueException e) {
throw new WrongReferenceAttributeValueException(attribute, e.getAttribute(), group, null, e.getAttributeHolder(), e.getAttributeHolderSecondary(), "Problem when setting all needed GIDs in hook.", e);
}
}
}
}
} catch (WrongAttributeAssignmentException ex) {
//TODO: need to add WrongAttributeAssignmentException to header of modules methods
throw new InternalErrorException(ex);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException 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.InternalErrorException in project perun by CESNET.
the class urn_perun_group_resource_attribute_def_virt_unixGID method setAttributeValue.
@Override
public boolean setAttributeValue(PerunSessionImpl sess, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
Attribute unixGIDNamespaceAttribute = sess.getPerunBl().getModulesUtilsBl().getUnixGIDNamespaceAttributeWithNotNullValue(sess, resource);
try {
Attribute gidAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGID-namespace:" + unixGIDNamespaceAttribute.getValue()));
gidAttribute.setValue(attribute.getValue());
return sess.getPerunBl().getAttributesManagerBl().setAttributeWithoutCheck(sess, group, gidAttribute);
} catch (WrongAttributeValueException ex) {
throw new InternalErrorException(ex);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.
the class urn_perun_group_resource_attribute_def_virt_unixGroupName method getAttributeValue.
@Override
public Attribute getAttributeValue(PerunSessionImpl sess, Resource resource, Group group, AttributeDefinition attributeDefinition) throws InternalErrorException {
Attribute attribute = new Attribute(attributeDefinition);
Attribute unixGroupNameNamespaceAttribute;
try {
unixGroupNameNamespaceAttribute = sess.getPerunBl().getModulesUtilsBl().getUnixGroupNameNamespaceAttributeWithNotNullValue(sess, resource);
} catch (WrongReferenceAttributeValueException ex) {
return attribute;
}
try {
Attribute groupNameAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, group, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGroupName-namespace:" + unixGroupNameNamespaceAttribute.getValue());
attribute = Utils.copyAttributeToVirtualAttributeWithValue(groupNameAttribute, attribute);
return attribute;
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
}
Aggregations