class urn_perun_user_attribute_def_def_vsupMailAlias extends UserAttributesModuleAbstract implements UserAttributesModuleImplApi {

	@Override
	public void checkAttributeSemantics(PerunSessionImpl sess, User user, Attribute attribute) throws WrongReferenceAttributeValueException {

		// can be empty
		if (attribute.getValue() == null) return;

		// We must check uniqueness since vsupMailAlias is filled by itself and filling function iterates until value is correct.

		try {
			Attribute reservedMailsAttribute = sess.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(sess, usedMailsKeyVsup, usedMailsUrn);
			if (reservedMailsAttribute.getValue() != null) {
				Map<String,String> reservedMailsAttributeValue = reservedMailsAttribute.valueAsMap();
				String ownersUserId = reservedMailsAttributeValue.get(attribute.valueAsString());
				if (ownersUserId != null && !Objects.equals(ownersUserId, String.valueOf(user.getId()))) {
					User ownersUser = sess.getPerunBl().getUsersManagerBl().getUserById(sess, Integer.parseInt(ownersUserId));
					throw new WrongReferenceAttributeValueException(attribute, reservedMailsAttribute, user, null, ownersUser, null, "VŠUP mail alias: '"+attribute.getValue()+"' is already in use by User ID: " + ownersUserId + ".");
				}
			}
		} catch (AttributeNotExistsException ex) {
			throw new ConsistencyErrorException("Attribute doesn't exists.", ex);
		} catch (UserNotExistsException e) {
			throw new ConsistencyErrorException("User doesn't exists.", e);
		}

	}

}