class urn_perun_member_attribute_def_def_o365EmailAddresses_mu extends MemberAttributesModuleAbstract {

	@Override
	public void checkAttributeSemantics(PerunSessionImpl sess, Member member, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
		log.trace("checkAttributeSemantics(member={},attribute={})", member, attribute);
		List<String> emails;

		//get values
		if (attribute.getValue() == null) {
			throw new WrongReferenceAttributeValueException(attribute, "can't be null.");
		} else {
			emails = attribute.valueAsList();
		}

		//check for presence of uco@muni.cz
		Attribute attrUCO = getUserUco(sess, member);
		String UCO = attrUCO.valueAsString();
		//Throw an exception if UCO is null (we need to have this value not-null to correctly check value of this attribute)
		if(UCO == null) {
			throw new WrongReferenceAttributeValueException(attribute, attrUCO, member, null, UCO_ATTRIBUTE + " has null value!");
		}
		String ucoEmail = UCO + "@muni.cz";
		if (!emails.contains(ucoEmail)) {
			throw new WrongReferenceAttributeValueException(attribute, attrUCO, member, null, member, null, "does not contain " + ucoEmail);
		}

		//No need to check duplicities between other members, cause attribute is unique
		//Check uniqueness between this attribute and groups
		try {
			Attribute groupO365EmailAddresses = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, G_D_O365_EMAIL_ADDRESSES_O365MU_ATTR));
			groupO365EmailAddresses.setValue(emails);
			Set<Integer> groupsWithSameAttributeValueSet = sess.getPerunBl().getAttributesManagerBl().getPerunBeanIdsForUniqueAttributeValue(sess, groupO365EmailAddresses).stream()
				.map(Pair::getLeft)
				.collect(Collectors.toSet());
			if (!groupsWithSameAttributeValueSet.isEmpty()) {
				try {
					Group groupWithDuplicateEmail = sess.getPerunBl().getGroupsManagerBl().getGroupById(sess, groupsWithSameAttributeValueSet.iterator().next());
					throw new WrongReferenceAttributeValueException(attribute, groupO365EmailAddresses, member, null, groupWithDuplicateEmail, null, "some of the email addresses are already assigned.");
				} catch (GroupNotExistsException e) {
					throw new ConsistencyErrorException(e);
				}
			}
		} catch (AttributeNotExistsException ex) {
			//If attribute not exists, we can log it and skip it, because there are no duplicates in not existing attributes
			log.debug("Attribute {} not exists to check duplicities in it while checkAttributeSemantics for {}.", G_D_O365_EMAIL_ADDRESSES_O365MU_ATTR, attribute);
		}
	}

}