|
| 1 | +package cz.metacentrum.perun.core.impl.modules.attributes; |
| 2 | + |
| 3 | +import cz.metacentrum.perun.core.api.Attribute; |
| 4 | +import cz.metacentrum.perun.core.api.AttributeDefinition; |
| 5 | +import cz.metacentrum.perun.core.api.AttributesManager; |
| 6 | +import cz.metacentrum.perun.core.api.User; |
| 7 | +import cz.metacentrum.perun.core.api.UserExtSource; |
| 8 | +import cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException; |
| 9 | +import cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException; |
| 10 | +import cz.metacentrum.perun.core.impl.PerunSessionImpl; |
| 11 | +import cz.metacentrum.perun.core.implApi.modules.attributes.SkipValueCheckDuringDependencyCheck; |
| 12 | +import cz.metacentrum.perun.core.implApi.modules.attributes.UserVirtualAttributeCollectedFromUserExtSource; |
| 13 | + |
| 14 | +import java.util.LinkedHashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +/** |
| 19 | + * Virtual attribute for user's eligibilities. |
| 20 | + * Attribute is calculated using all user's ext sources and their corresponding eligibilities |
| 21 | + * attribute so that for each unique key (name) only the highest obtained value is selected. |
| 22 | + * |
| 23 | + * @author Luboslav Halama |
| 24 | + */ |
| 25 | +@SuppressWarnings("unused") |
| 26 | +@SkipValueCheckDuringDependencyCheck |
| 27 | +public class urn_perun_user_attribute_def_virt_userEligibilities extends UserVirtualAttributeCollectedFromUserExtSource { |
| 28 | + |
| 29 | + private final static String USER_ELIGIBILITIES_FRIENDLY_NAME = "userEligibilities"; |
| 30 | + private final static String A_U_V_USER_ELIGIBILITIES = AttributesManager.NS_USER_ATTR_VIRT + ":" + USER_ELIGIBILITIES_FRIENDLY_NAME; |
| 31 | + |
| 32 | + @Override |
| 33 | + public String getSourceAttributeFriendlyName() { |
| 34 | + return "eligibilities"; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getDestinationAttributeFriendlyName() { |
| 39 | + return USER_ELIGIBILITIES_FRIENDLY_NAME; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) { |
| 44 | + List<UserExtSource> userExtSources = sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user); |
| 45 | + |
| 46 | + Map<String, String> collectedEligibilities = new LinkedHashMap<>(); |
| 47 | + |
| 48 | + for (UserExtSource extSource : userExtSources) { |
| 49 | + Attribute attribute; |
| 50 | + try { |
| 51 | + attribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, extSource, |
| 52 | + AttributesManager.NS_UES_ATTR_DEF + ":" + getSourceAttributeFriendlyName()); |
| 53 | + } catch (AttributeNotExistsException | WrongAttributeAssignmentException e) { |
| 54 | + throw new RuntimeException(e); |
| 55 | + } |
| 56 | + |
| 57 | + Map<String, String> eligibilities = attribute.valueAsMap(); |
| 58 | + |
| 59 | + // if no eligibilities were obtained, skip this ext source |
| 60 | + if (eligibilities == null) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + for (String key : eligibilities.keySet()) { |
| 65 | + |
| 66 | + // if given key is not present yet, store it |
| 67 | + if (!collectedEligibilities.containsKey(key)) { |
| 68 | + collectedEligibilities.put(key, eligibilities.get(key)); |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + // if given is present, check if value should be updated |
| 73 | + int timestamp = Integer.parseInt(eligibilities.get(key)); |
| 74 | + if (Integer.parseInt(collectedEligibilities.get(key)) < timestamp) { |
| 75 | + collectedEligibilities.replace(key, eligibilities.get(key)); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + Attribute attribute = new Attribute(attributeDefinition); |
| 81 | + attribute.setValue(collectedEligibilities); |
| 82 | + return attribute; |
| 83 | + } |
| 84 | + |
| 85 | +// @Override |
| 86 | +// public List<AttributeHandleIdentifier> getHandleIdentifiers() { |
| 87 | +// List<AttributeHandleIdentifier> handleIdentifiers = super.getHandleIdentifiers(); |
| 88 | +// handleIdentifiers.add(auditEvent -> { |
| 89 | +// if (auditEvent instanceof AttributeChangedForUser && |
| 90 | +// ((AttributeChangedForUser) auditEvent).getAttribute().getFriendlyName().equals(getDestinationAttributeFriendlyName())) { |
| 91 | +// return ((AttributeChangedForUser) auditEvent).getUser().getId(); |
| 92 | +// } else { |
| 93 | +// return null; |
| 94 | +// } |
| 95 | +// }); |
| 96 | +// return handleIdentifiers; |
| 97 | +// } |
| 98 | +// |
| 99 | +// @Override |
| 100 | +// public List<AuditEvent> resolveVirtualAttributeValueChange(PerunSessionImpl perunSession, AuditEvent message) throws AttributeNotExistsException, WrongAttributeAssignmentException, WrongReferenceAttributeValueException { |
| 101 | +// List<AuditEvent> resolvingMessages = super.resolveVirtualAttributeValueChange(perunSession, message); |
| 102 | +// if (message == null) return resolvingMessages; |
| 103 | +// |
| 104 | +// if (message instanceof AttributeSetForUser && |
| 105 | +// ((AttributeSetForUser) message).getAttribute().getFriendlyName().equals(getSecondarySourceAttributeFriendlyName())) { |
| 106 | +// AttributeDefinition attrVirtUserEligibilitiesDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, A_U_V_USER_ELIGIBILITIES); |
| 107 | +// resolvingMessages.add(new AttributeSetForUser(new Attribute(attrVirtUserEligibilitiesDefinition), ((AttributeSetForUser) message).getUser())); |
| 108 | +// } else if (message instanceof AttributeChangedForUser) { |
| 109 | +// AttributeDefinition attrVirtUserEligibilitiesDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, A_U_V_USER_ELIGIBILITIES); |
| 110 | +// resolvingMessages.add(new AttributeChangedForUser(new Attribute(attrVirtUserEligibilitiesDefinition), ((AttributeChangedForUser) message).getUser())); |
| 111 | +// } else if (message instanceof AttributeRemovedForUser) { |
| 112 | +// AttributeDefinition attrVirtUserEligibilitiesDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, A_U_V_USER_ELIGIBILITIES); |
| 113 | +// resolvingMessages.add(new AttributeRemovedForUser(new Attribute(attrVirtUserEligibilitiesDefinition), ((AttributeRemovedForUser) message).getUser())); |
| 114 | +// } |
| 115 | +// |
| 116 | +// return resolvingMessages; |
| 117 | +// } |
| 118 | + |
| 119 | +// private AuditEvent resolveEvent(PerunSessionImpl perunSession, User user) throws AttributeNotExistsException { |
| 120 | +// AttributeDefinition attrVirtUserEligibilitiesDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, A_U_V_USER_ELIGIBILITIES); |
| 121 | +// return new AttributeChangedForUser(new Attribute(attrVirtUserEligibilitiesDefinition), user); |
| 122 | +// } |
| 123 | + |
| 124 | + @Override |
| 125 | + public AttributeDefinition getAttributeDefinition() { |
| 126 | + AttributeDefinition attr = new AttributeDefinition(); |
| 127 | + attr.setNamespace(AttributesManager.NS_USER_ATTR_VIRT); |
| 128 | + attr.setFriendlyName(USER_ELIGIBILITIES_FRIENDLY_NAME); |
| 129 | + attr.setDisplayName("user eligibilities"); |
| 130 | + attr.setType(LinkedHashMap.class.getName()); |
| 131 | + attr.setDescription("Virtual attribute, which collects all eligibilities user ext source attributes " + |
| 132 | + "with keys and values (map). Only the highest value is selected for each key."); |
| 133 | + return attr; |
| 134 | + } |
| 135 | +} |
0 commit comments