class urn_perun_group_resource_attribute_def_def_freeipaGroupName extends GroupResourceAttributesModuleAbstract implements GroupResourceAttributesModuleImplApi {

	@Override
	public void checkAttributeSemantics(PerunSessionImpl sess, Group group, Resource resource, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException {

		//prepare group name and check its format
		String groupName = attribute.valueAsString();
		if (groupName == null) {
			throw new WrongReferenceAttributeValueException(attribute, null, group, resource, "Attribute cannot be null.");
		}

		//Get facility for the resource
		Facility facility = sess.getPerunBl().getResourcesManagerBl().getFacility(sess, resource);

		// Get all resources from the facility
		List<Resource> facilityResources =  sess.getPerunBl().getFacilitiesManagerBl().getAssignedResources(sess, facility);

		//For each resource get all groups
		for(Resource rs : facilityResources){
			List<Group> resourceGroups =  sess.getPerunBl().getResourcesManagerBl().getAssignedGroups(sess, rs);

			//Remove our group from list of groups
			if (rs.getId() == resource.getId()){
				resourceGroups.remove(group);
			}

			//For all groups get name and check uniqueness
			for(Group gr : resourceGroups){
				Attribute freeipaGroupNameAttribute;

				try{
					freeipaGroupNameAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, rs, gr, A_GR_freeipaGroupName);
				} catch(AttributeNotExistsException ex) {
					throw new ConsistencyErrorException("Attribute "+ A_GR_freeipaGroupName +" does not exists for group " + gr + " and resource " + rs ,ex);
				} catch (GroupResourceMismatchException ex) {
					throw new InternalErrorException(ex);
				}

				if (freeipaGroupNameAttribute.getValue() != null){
					String name = freeipaGroupNameAttribute.valueAsString();

					if (name.toLowerCase().equals(groupName.toLowerCase())){
						throw new WrongReferenceAttributeValueException(attribute, freeipaGroupNameAttribute, group, resource, rs, gr, "Attribute has to be unique within one facility (case insensitive).");
					}
				}
			}
		}
	}

}