class urn_perun_user_attribute_def_def_userPreferredCertDN extends UserAttributesModuleAbstract implements UserAttributesModuleImplApi {

	@Override
	public void checkAttributeSemantics(PerunSessionImpl sess, User user, Attribute attribute) throws WrongAttributeAssignmentException, WrongReferenceAttributeValueException {
		Attribute userCertDNs;
		try {
			userCertDNs = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":userCertDNs");
		} catch (AttributeNotExistsException ex) {
			throw new ConsistencyErrorException(ex);
		}
		Map<String, String> certDNsValue;
		if(userCertDNs.getValue() != null) {
			certDNsValue = userCertDNs.valueAsMap();
		} else {
			if(attribute.getValue() != null) throw new WrongReferenceAttributeValueException(attribute, userCertDNs, user, null, user, null, "There is no certificates for this user so preferred certificate can't be choose.");
			else return;
		}
		if(attribute.getValue() == null) {
			if(certDNsValue != null && !certDNsValue.isEmpty()) throw new WrongReferenceAttributeValueException(attribute, userCertDNs, user, null, user, null, "This attribute value can't be null because of notNull attribute userCertDNs");
		} else {
			String preferredCertDNValue = attribute.valueAsString();
			if(!certDNsValue.containsKey(preferredCertDNValue)) throw new WrongReferenceAttributeValueException(attribute, userCertDNs, user, null, user, null, "This attribute value must be one of exsiting keys in userCertDNs.");
		}
	}

}