class urn_perun_group_attribute_def_def_adName_o365mu extends GroupAttributesModuleAbstract implements GroupAttributesModuleImplApi {

	@Override
	public void checkAttributeSemantics(PerunSessionImpl sess, Group group, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
		//Attribute can be null
		if(attribute.getValue() == null) {
			return;
		}

		//Prepare map where keys are facilityIds (to be able to group them together) and values are all resources under facilityId
		Map<Integer, Set<Resource>> mapOfResourcesByFacility = sess.getPerunBl().getResourcesManagerBl()
			.getAssignedResources(sess, group).stream()
			.collect(groupingBy(Resource::getFacilityId, toSet()));

		//For every facility id in the Map check all resources where group was set
		for(Set<Resource> setOfResourcesWithSameFacilityId : mapOfResourcesByFacility.values()) {
			boolean resourceWithOuSet = false;
			for (Resource resource : setOfResourcesWithSameFacilityId) {
				try {
					Attribute resourceAdOuName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, A_R_D_AD_OU_NAME);
					if (resourceAdOuName.getValue() != null) {
						if(resourceWithOuSet) {
							throw new WrongReferenceAttributeValueException(attribute, resourceAdOuName, group, null, resource, null,
								"There is more than one resource where group is assigned and OU is also set there (on the same facility)!");
						} else {
							resourceWithOuSet = true;
						}
					}
				} catch (AttributeNotExistsException ex) {
					throw new ConsistencyErrorException(ex);
				}
			}
		}
	}

}