class urn_perun_user_attribute_def_def_uid_namespace extends UserAttributesModuleAbstract implements UserAttributesModuleImplApi {

	@Override
	public void checkAttributeSemantics(PerunSessionImpl sess, User user, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
		Integer uid = attribute.valueAsInteger();
		String uidNamespace = attribute.getFriendlyNameParameter();

		if (uid == null) {
			throw new WrongReferenceAttributeValueException(attribute, "Attribute was not filled, therefore there is nothing to be checked.");
		}

		Attribute minUidAttribute;
		Attribute maxUidAttribute;
		try {
			minUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_minUID);
			maxUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_maxUID);
		} catch (AttributeNotExistsException e) {
			throw new ConsistencyErrorException("minUid and maxUid attributes are required", e);
		}

		Integer min = minUidAttribute.valueAsInteger();
		Integer max = maxUidAttribute.valueAsInteger();
		if(min == null) {
			throw new WrongReferenceAttributeValueException(attribute, minUidAttribute);
		}
		if (max == null) {
			throw new WrongReferenceAttributeValueException(attribute, maxUidAttribute);
		}
		//uid is in proper range
		if (uid < min) {
			throw new WrongReferenceAttributeValueException(attribute, minUidAttribute, user, null, uidNamespace, null, "UID " + uid + " is lesser than min " + min);
		}

		if (uid > max) {
			throw new WrongReferenceAttributeValueException(attribute, maxUidAttribute, user, null, uidNamespace, null, "UID " + uid + " is higher than max " + max);
		}

		// Get all users who have set attribute urn:perun:member:attribute-def:def:uid-namespace:[uid-namespace], with the value.
		List<User> usersWithUid = sess.getPerunBl().getUsersManagerBl().getUsersByAttribute(sess, attribute);
		usersWithUid.remove(user); //remove self
		if (!usersWithUid.isEmpty()) {
			if(usersWithUid.size() > 1) throw new ConsistencyErrorException("FATAL ERROR: Duplicated UID detected." +  attribute + " " + usersWithUid);
			throw new WrongReferenceAttributeValueException(attribute, "UID " + attribute.getValue() + " is already occupied by " + usersWithUid.get(0)  + ". We can't set it for " + user + ".");
		}
	}

}