class urn_perun_group_resource_attribute_def_def_adName extends GroupResourceAttributesModuleAbstract implements GroupResourceAttributesModuleImplApi {

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

		Attribute resourceAdOuName;
		try {
			resourceAdOuName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, A_R_D_AD_OU_NAME);
		} catch (AttributeNotExistsException ex) {
			throw new ConsistencyErrorException(ex);
		}

		//if assigned
		if(resourceAdOuName.getValue() == null) {
			return;
		}

		List<Group> groupsWithSameADNameInSameOu = sess.getPerunBl().getSearcherBl().getGroupsByGroupResourceSetting(sess, attribute, resourceAdOuName);
		//Remove itself from the list of groups with the same ad name in same ou
		groupsWithSameADNameInSameOu.remove(group);
		if(!groupsWithSameADNameInSameOu.isEmpty()) {
			throw new WrongReferenceAttributeValueException(attribute, resourceAdOuName, group, resource, resource, null,
					"Attribute AD Name can't be set for group and resource in this OU, because this value is already " +
					"set for different group and resource in the same OU!");
		}
	}

}