package net.sourceforge.fenixedu.domain.space; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import net.sourceforge.fenixedu.dataTransferObject.spaceManager.FindSpacesBean.SpacesSearchCriteriaType; import net.sourceforge.fenixedu.domain.DomainObject; import net.sourceforge.fenixedu.domain.DomainObjectActionLog; import net.sourceforge.fenixedu.domain.ExecutionCourse; import net.sourceforge.fenixedu.domain.ExecutionSemester; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.WrittenEvaluation; import net.sourceforge.fenixedu.domain.accessControl.Group; import net.sourceforge.fenixedu.domain.accessControl.GroupUnion; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.material.Extension; import net.sourceforge.fenixedu.domain.material.Material; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.domain.resource.Resource; import net.sourceforge.fenixedu.domain.resource.ResourceAllocation; import net.sourceforge.fenixedu.domain.resource.ResourceResponsibility; import net.sourceforge.fenixedu.injectionCode.AccessControl; import net.sourceforge.fenixedu.injectionCode.FenixDomainObjectActionLogAnnotation; import net.sourceforge.fenixedu.injectionCode.IGroup; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang.StringUtils; import org.joda.time.YearMonthDay; import pt.ist.fenixWebFramework.security.accessControl.Checked; import pt.utl.ist.fenix.tools.util.StringNormalizer; public abstract class Space extends Space_Base { public abstract Integer getExamCapacity(); public abstract Integer getNormalCapacity(); public final static Comparator COMPARATOR_BY_PRESENTATION_NAME = new Comparator() { public int compare(Space o1, Space o2) { if (o1.isFloor() && o2.isFloor()) { return compareFloors((Floor) o1, (Floor) o2); } return comparePresentationName(o1, o2); } }; public final static Comparator COMPARATOR_BY_NAME_FLOOR_BUILDING_AND_CAMPUS = new Comparator() { public int compare(Space o1, Space o2) { Integer buildingCheck = checkObjects(o1.getSpaceBuilding(), o2.getSpaceBuilding()); if (buildingCheck != null) { return buildingCheck.intValue(); } Integer campusCheck = checkObjects(o1.getSpaceCampus(), o2.getSpaceCampus()); if (campusCheck != null) { return campusCheck.intValue(); } Integer floorCheck = checkObjects(o1.getSpaceFloorWithIntermediary(), o2.getSpaceFloorWithIntermediary()); if (floorCheck != null) { return floorCheck.intValue(); } return comparePresentationName(o1, o2); } private Integer checkObjects(Space space1, Space space2) { if (space1 != null && space2 == null) { return Integer.valueOf(1); } else if (space1 == null && space2 != null) { return Integer.valueOf(-1); } else if (space1 == null && space2 == null) { return null; } else if (!space1.equals(space2)) { if (space1.isFloor() && space2.isFloor()) { return compareFloors((Floor) space1, (Floor) space2); } return comparePresentationName(space1, space2); } return null; } }; private static int comparePresentationName(Space space1, Space space2) { int compareTo = space1.getSpaceInformation().getPresentationName().compareTo( space2.getSpaceInformation().getPresentationName()); if (compareTo == 0) { return space1.getIdInternal().compareTo(space2.getIdInternal()); } return compareTo; } private static int compareFloors(Floor floor1, Floor floor2) { int compareTo = floor1.getSpaceInformation().getLevel().compareTo(floor2.getSpaceInformation().getLevel()); if (compareTo == 0) { return floor1.getIdInternal().compareTo(floor2.getIdInternal()); } return compareTo; } protected Space() { super(); setCreatedOn(new YearMonthDay()); } @Checked("SpacePredicates.checkPermissionsToManageSpace") @FenixDomainObjectActionLogAnnotation(actionName = "Set new Parent space", parameters = { "this", "newParentSpace" }) public void setNewPossibleParentSpace(Space newParentSpace) { if (newParentSpace != null) { setSuroundingSpace(newParentSpace); } } @Override public boolean isSpace() { return true; } public SpaceInformation getMostRecentSpaceInformation() { SpaceInformation selectedSpaceInformation = null; for (final SpaceInformation spaceInformation : getSpaceInformations()) { if (spaceInformation.getValidUntil() == null) { return spaceInformation; } else if (selectedSpaceInformation == null || spaceInformation.getValidUntil().isAfter(selectedSpaceInformation.getValidUntil())) { selectedSpaceInformation = spaceInformation; } } return selectedSpaceInformation; } public SpaceInformation getSpaceInformation() { return getSpaceInformation(null); } public SpaceInformation getSpaceInformation(YearMonthDay when) { when = (when == null) ? new YearMonthDay() : when; for (final SpaceInformation spaceInformation : getSpaceInformations()) { if (spaceInformation.isActive(when)) { return spaceInformation; } } return getMostRecentSpaceInformation(); } public SortedSet getOrderedSpaceInformations() { return new TreeSet(getSpaceInformations()); } public Blueprint getSuroundingSpaceMostRecentBlueprint() { Space suroundingSpace = getSuroundingSpace(); if (suroundingSpace != null) { return suroundingSpace.getMostRecentBlueprint(); } return null; } public Blueprint getMostRecentBlueprint() { SortedSet orderedBlueprints = getOrderedBlueprints(); return (!orderedBlueprints.isEmpty()) ? orderedBlueprints.last() : null; } public SortedSet getOrderedBlueprints() { return new TreeSet(getBlueprints()); } public List getWrittenEvaluationSpaceOccupations() { List occupations = new ArrayList(); for (ResourceAllocation allocation : getResourceAllocations()) { if (allocation.isWrittenEvaluationSpaceOccupation()) { occupations.add((WrittenEvaluationSpaceOccupation) allocation); } } return occupations; } public List getPersonSpaceOccupations() { List personSpaceOccupations = new ArrayList(); for (ResourceAllocation allocation : getResourceAllocations()) { if (allocation.isPersonSpaceOccupation()) { personSpaceOccupations.add((PersonSpaceOccupation) allocation); } } return personSpaceOccupations; } public int getSpaceResponsibilityCount() { return getSpaceResponsibility().size(); } public List getSpaceResponsibility() { List result = new ArrayList(); for (ResourceResponsibility responsibility : getResourceResponsibility()) { if (responsibility.isSpaceResponsibility()) { result.add((SpaceResponsibility) responsibility); } } return result; } public List getMaterialSpaceOccupations() { List materialSpaceOccupations = new ArrayList(); for (ResourceAllocation allocation : getResourceAllocations()) { if (allocation.isMaterialSpaceOccupation()) { materialSpaceOccupations.add((MaterialSpaceOccupation) allocation); } } return materialSpaceOccupations; } public List getUnitSpaceOccupations() { List unitSpaceOccupations = new ArrayList(); for (ResourceAllocation allocation : getResourceAllocations()) { if (allocation.isUnitSpaceOccupation()) { unitSpaceOccupations.add((UnitSpaceOccupation) allocation); } } return unitSpaceOccupations; } public Set getActiveContainedSpacesByType(Class clazz) { Set result = new TreeSet(Space.COMPARATOR_BY_PRESENTATION_NAME); for (Space space : getContainedSpaces()) { if (space.getClass().equals(clazz) && space.isActive()) { result.add(space); } } return result; } public Set getContainedSpacesByState(SpaceState spaceState) { Set result = new TreeSet(Space.COMPARATOR_BY_PRESENTATION_NAME); for (Space space : getContainedSpaces()) { if ((spaceState.equals(SpaceState.ACTIVE) && space.isActive()) || spaceState.equals(SpaceState.INACTIVE) && !space.isActive()) { result.add(space); } } return result; } public SortedSet getActivePersonSpaceOccupations() { return getPersonSpaceOccupationsByState(true); } public SortedSet getInactivePersonSpaceOccupations() { return getPersonSpaceOccupationsByState(false); } private SortedSet getPersonSpaceOccupationsByState(boolean state) { SortedSet personSpaceOccupations = new TreeSet( PersonSpaceOccupation.COMPARATOR_BY_PERSON_NAME_AND_OCCUPATION_INTERVAL); YearMonthDay current = new YearMonthDay(); for (PersonSpaceOccupation personSpaceOccupation : getPersonSpaceOccupations()) { if (personSpaceOccupation.contains(current) == state) { personSpaceOccupations.add(personSpaceOccupation); } } return personSpaceOccupations; } public SortedSet getActiveSpaceResponsibility() { return getSpaceResponsabilityByState(true); } public SortedSet getInactiveSpaceResponsibility() { return getSpaceResponsabilityByState(false); } private SortedSet getSpaceResponsabilityByState(boolean state) { SortedSet spaceResponsibility = new TreeSet( SpaceResponsibility.COMPARATOR_BY_UNIT_NAME_AND_RESPONSIBILITY_INTERVAL); YearMonthDay current = new YearMonthDay(); for (ResourceResponsibility responsibility : getResourceResponsibilitySet()) { if (responsibility.isSpaceResponsibility() && responsibility.isActive(current) == state) { spaceResponsibility.add((SpaceResponsibility) responsibility); } } return spaceResponsibility; } public SortedSet getActiveUnitSpaceOccupations() { return getUnitSpaceOccupationsByState(true); } public SortedSet getInactiveUnitSpaceOccupations() { return getUnitSpaceOccupationsByState(false); } private SortedSet getUnitSpaceOccupationsByState(boolean state) { SortedSet unitSpaceOccupations = new TreeSet( UnitSpaceOccupation.COMPARATOR_BY_OCCUPATION_INTERVAL_AND_UNIT); YearMonthDay current = new YearMonthDay(); for (UnitSpaceOccupation unitSpaceOccupation : getUnitSpaceOccupations()) { if (unitSpaceOccupation.isActive(current) == state) { unitSpaceOccupations.add(unitSpaceOccupation); } } return unitSpaceOccupations; } public SortedSet getActiveSpaceMaterial() { SortedSet spaceMaterial = new TreeSet(Material.COMPARATOR_BY_CLASS_NAME); YearMonthDay current = new YearMonthDay(); for (MaterialSpaceOccupation materialSpaceOccupation : getMaterialSpaceOccupations()) { if (materialSpaceOccupation.isActive(current)) { spaceMaterial.add(materialSpaceOccupation.getMaterial()); } } return spaceMaterial; } public SortedSet getActiveMaterialSpaceOccupationsToLoggedPerson() { return getMaterialSpaceOccupationsToLoggedPersonByState(true); } public SortedSet getInactiveMaterialSpaceOccupationsToLoggedPerson() { return getMaterialSpaceOccupationsToLoggedPersonByState(false); } private SortedSet getMaterialSpaceOccupationsToLoggedPersonByState(boolean state) { SortedSet materialOccupations = new TreeSet( MaterialSpaceOccupation.COMPARATOR_BY_CLASS_NAME); YearMonthDay current = new YearMonthDay(); Person loggedPerson = AccessControl.getPerson(); for (MaterialSpaceOccupation materialSpaceOccupation : getMaterialSpaceOccupations()) { if (materialSpaceOccupation.isActive(current) == state && (materialSpaceOccupation.getSpace().personHasPermissionsToManageSpace(loggedPerson) || (materialSpaceOccupation .getAccessGroup() != null && materialSpaceOccupation.getAccessGroup().isMember(loggedPerson)))) { materialOccupations.add(materialSpaceOccupation); } } return materialOccupations; } public Set getMaterialSpaceOccupationsByMaterialClass( Class clazz) { Set materialOccupations = new HashSet(); for (MaterialSpaceOccupation occupation : getMaterialSpaceOccupations()) { if (occupation.getClass().equals(clazz)) { materialOccupations.add(occupation); } } return materialOccupations; } public static Set getListOfChangesInSpacesOrderedByInstant() { Set> classs = new HashSet>(); Person loggedPerson = AccessControl.getPerson(); if (personIsSpacesAdministrator(loggedPerson)) { classs.add(Room.class); classs.add(Floor.class); classs.add(Campus.class); classs.add(Building.class); classs.add(Blueprint.class); classs.add(RoomSubdivision.class); classs.add(RoomInformation.class); classs.add(FloorInformation.class); classs.add(CampusInformation.class); classs.add(RoomClassification.class); classs.add(BuildingInformation.class); classs.add(UnitSpaceOccupation.class); classs.add(SpaceResponsibility.class); classs.add(PersonSpaceOccupation.class); classs.add(ExtensionSpaceOccupation.class); classs.add(RoomSubdivisionInformation.class); return DomainObjectActionLog.readDomainObjectActionLogsOrderedByInstant(classs); } return new HashSet(); } public Space readSubSpaceByBlueprintNumber(String blueprintNumber) { if (blueprintNumber != null && !StringUtils.isEmpty(blueprintNumber)) { for (Space space : getContainedSpaces()) { if (space.getSpaceInformation() != null) { String spaceBlueprint = space.getSpaceInformation().getBlueprintNumber(); if (spaceBlueprint != null && !StringUtils.isEmpty(spaceBlueprint) && spaceBlueprint.equals(blueprintNumber)) { return space; } } } } return null; } @Override public void delete() { if (!canBeDeleted()) { throw new DomainException("error.space.cannot.be.deleted"); } for (; !getBlueprints().isEmpty(); getBlueprints().get(0).delete()) ; for (; !getSpaceInformations().isEmpty(); getSpaceInformations().get(0).deleteWithoutCheckNumberOfSpaceInformations()) ; super.setSuroundingSpace(null); super.delete(); } private boolean canBeDeleted() { return !hasAnyContainedSpaces(); } public boolean isActive() { return getMostRecentSpaceInformation().isActive(new YearMonthDay()); } public Boolean getActiveFlag() { return Boolean.valueOf(isActive()); } public static List getAllSpacesByPresentationName(String name) { List result = new ArrayList(); String[] identificationWords = getIdentificationWords(name); for (Resource resource : RootDomainObject.getInstance().getResources()) { if (resource.isSpace() && ((Space) resource).verifyNameEquality(identificationWords)) { result.add((Space) resource); } } return result; } protected boolean verifyNameEquality(String[] nameWords) { if (nameWords != null) { String spacePresentationName = getSpaceInformation().getPresentationName(); if (spacePresentationName != null) { String[] spaceIdentificationWords = spacePresentationName.trim().split(" "); StringNormalizer.normalize(spaceIdentificationWords); int j, i; for (i = 0; i < nameWords.length; i++) { if (!nameWords[i].equals("")) { for (j = 0; j < spaceIdentificationWords.length; j++) { if (spaceIdentificationWords[j].equals(nameWords[i])) { break; } } if (j == spaceIdentificationWords.length) { return false; } } } if (i == nameWords.length) { return true; } } } return false; } public static String[] getIdentificationWords(String name) { String[] identificationWords = null; if (name != null && !StringUtils.isEmpty(name.trim())) { identificationWords = name.trim().split(" "); StringNormalizer.normalize(identificationWords); } return identificationWords; } public static List getAllCampus() { return (List) getAllSpacesByClass(Campus.class, null); } public static List getAllActiveCampus() { return (List) getAllSpacesByClass(Campus.class, Boolean.TRUE); } public static List getAllActiveBuildings() { return (List) getAllSpacesByClass(Building.class, Boolean.TRUE); } private static List getAllSpacesByClass(Class clazz, Boolean active) { List result = new ArrayList(); for (Resource space : RootDomainObject.getInstance().getResources()) { if (space.getClass().equals(clazz) && (active == null || ((Space) space).isActive() == active.booleanValue())) { result.add((Space) space); } } return result; } public List getAllActiveSubRoomsForEducation() { List result = new ArrayList(); List containedSpaces = getContainedSpaces(); for (Space space : containedSpaces) { if (space.isAllocatableSpace() && space.isActive() && ((AllocatableSpace) space).isForEducation()) { result.add((AllocatableSpace) space); } } for (Space subSpace : containedSpaces) { result.addAll(subSpace.getAllActiveSubRoomsForEducation()); } return result; } public static boolean personIsSpacesAdministrator(Person person) { return (person.hasRole(RoleType.MANAGER) || person.hasRole(RoleType.SPACE_MANAGER_SUPER_USER)) && person.hasRole(RoleType.SPACE_MANAGER); } public void checkIfLoggedPersonHasPermissionsToManageSpace(Person person) { if (personHasPermissionsToManageSpace(person)) { return; } throw new DomainException("error.logged.person.not.authorized.to.make.operation"); } public void checkIfLoggedPersonIsSpacesAdministrator(Person person) { if (personIsSpacesAdministrator(person)) { return; } throw new DomainException("error.logged.person.not.authorized.to.make.operation"); } public boolean personHasPermissionsToManageSpace(Person person) { return personIsSpacesAdministrator(person) || personHasSpecialPermissionToManageSpace(person); } public boolean personHasSpecialPermissionToManageSpace(Person person) { Group accessGroup = getSpaceManagementAccessGroupWithChainOfResponsibility(); return accessGroup != null && accessGroup.isMember(person); } public boolean personHasPermissionToManagePersonOccupations(Person person) { Group accessGroup = getPersonOccupationsAccessGroupWithChainOfResponsibility(); return accessGroup != null && accessGroup.isMember(person); } public boolean personHasPermissionToManageExtensionOccupations(Person person) { Group accessGroup = getExtensionOccupationsAccessGroupWithChainOfResponsibility(); return accessGroup != null && accessGroup.isMember(person); } public boolean personHasPermissionToManageUnitOccupations(Person person) { Group accessGroup = getUnitOccupationsAccessGroupWithChainOfResponsibility(); return accessGroup != null && accessGroup.isMember(person); } public boolean personHasPermissionToManageLessonOccupations(Person person) { Group accessGroup = getLessonOccupationsAccessGroupWithChainOfResponsibility(); return accessGroup != null && accessGroup.isMember(person); } public boolean personHasPermissionToManageWrittenEvaluationOccupations(Person person) { Group accessGroup = getWrittenEvaluationOccupationsAccessGroup(); return accessGroup != null && accessGroup.isMember(person); } public boolean personHasPermissionToGenericEventOccupations(Person person) { Group accessGroup = getGenericEventOccupationsAccessGroup(); return accessGroup != null && accessGroup.isMember(person); } public Group getPersonOccupationsAccessGroupWithChainOfResponsibility() { final Group accessGroup = getPersonOccupationsAccessGroup(); if (accessGroup != null && !accessGroup.getElements().isEmpty()) { return accessGroup; } final Space surroundingSpace = getSuroundingSpace(); if (surroundingSpace != null) { return surroundingSpace.getPersonOccupationsAccessGroupWithChainOfResponsibility(); } return null; } public Group getUnitOccupationsAccessGroupWithChainOfResponsibility() { final Group accessGroup = getUnitOccupationsAccessGroup(); if (accessGroup != null && !accessGroup.getElements().isEmpty()) { return accessGroup; } final Space surroundingSpace = getSuroundingSpace(); if (surroundingSpace != null) { return surroundingSpace.getUnitOccupationsAccessGroupWithChainOfResponsibility(); } return null; } public Group getExtensionOccupationsAccessGroupWithChainOfResponsibility() { final Group accessGroup = getExtensionOccupationsAccessGroup(); if (accessGroup != null && !accessGroup.getElements().isEmpty()) { return accessGroup; } final Space surroundingSpace = getSuroundingSpace(); if (surroundingSpace != null) { return surroundingSpace.getExtensionOccupationsAccessGroupWithChainOfResponsibility(); } return null; } public Group getSpaceManagementAccessGroupWithChainOfResponsibility() { final Group accessGroup = getSpaceManagementAccessGroup(); if (accessGroup != null && !accessGroup.getElements().isEmpty()) { return accessGroup; } final Space surroundingSpace = getSuroundingSpace(); if (surroundingSpace != null) { return surroundingSpace.getSpaceManagementAccessGroupWithChainOfResponsibility(); } return null; } public Group getLessonOccupationsAccessGroupWithChainOfResponsibility() { final Group thisGroup = getLessonOccupationsAccessGroup(); if (thisGroup != null && !thisGroup.getElements().isEmpty()) { return thisGroup; } final Space surroundingSpace = getSuroundingSpace(); if (surroundingSpace != null) { return surroundingSpace.getLessonOccupationsAccessGroupWithChainOfResponsibility(); } return null; } public Group getWrittenEvaluationOccupationsAccessGroupWithChainOfResponsibility() { final Group thisGroup = getWrittenEvaluationOccupationsAccessGroup(); if (thisGroup != null && !thisGroup.getElements().isEmpty()) { return thisGroup; } final Space surroundingSpace = getSuroundingSpace(); if (surroundingSpace != null) { return surroundingSpace.getWrittenEvaluationOccupationsAccessGroupWithChainOfResponsibility(); } return null; } public Group getGenericEventOccupationsAccessGroupWithChainOfResponsibility() { final Group thisGroup = getGenericEventOccupationsAccessGroup(); if (thisGroup != null && !thisGroup.getElements().isEmpty()) { return thisGroup; } final Space surroundingSpace = getSuroundingSpace(); if (surroundingSpace != null) { return surroundingSpace.getGenericEventOccupationsAccessGroupWithChainOfResponsibility(); } return null; } @Checked("SpacePredicates.checkIfLoggedPersonIsSpaceAdministrator") @FenixDomainObjectActionLogAnnotation(actionName = "Add or remove person from access group", parameters = { "this", "accessGroupType", "toAdd", "isToMaintainElements", "expression" }) public void addOrRemovePersonFromAccessGroup(SpaceAccessGroupType accessGroupType, Boolean toAdd, Boolean isToMaintainElements, String expression) throws DomainException { if (StringUtils.isEmpty(expression)) { throw new DomainException("error.space.access.groups.management.no.person"); } if (!toAdd) { byte[] encodeHex; try { encodeHex = Hex.decodeHex(expression.toCharArray()); } catch (DecoderException e) { throw new DomainException("error.space.access.groups.invalid.expression"); } expression = new String(encodeHex); } Group groupToAddOrRemove = Group.fromString(expression); Set elementsToAddOrRemove = null; Group existentGroup = null, newGroupUnion = null; switch (accessGroupType) { case PERSON_OCCUPATION_ACCESS_GROUP: elementsToAddOrRemove = groupToAddOrRemove.getElements(); checkIfPersonAlreadyHasPermissions(elementsToAddOrRemove, toAdd); if (isToMaintainElements) { existentGroup = getPersonOccupationsAccessGroupWithChainOfResponsibility(); } else { existentGroup = getPersonOccupationsAccessGroup(); } newGroupUnion = manageGroups(toAdd, groupToAddOrRemove, existentGroup); setPersonOccupationsAccessGroup(newGroupUnion); spaceManagerRoleManagement(elementsToAddOrRemove, toAdd); break; case EXTENSION_OCCUPATION_ACCESS_GROUP: elementsToAddOrRemove = groupToAddOrRemove.getElements(); checkIfPersonAlreadyHasPermissions(elementsToAddOrRemove, toAdd); if (isToMaintainElements) { existentGroup = getExtensionOccupationsAccessGroupWithChainOfResponsibility(); } else { existentGroup = getExtensionOccupationsAccessGroup(); } newGroupUnion = manageGroups(toAdd, groupToAddOrRemove, existentGroup); setExtensionOccupationsAccessGroup(newGroupUnion); spaceManagerRoleManagement(elementsToAddOrRemove, toAdd); break; case UNIT_OCCUPATION_ACCESS_GROUP: elementsToAddOrRemove = groupToAddOrRemove.getElements(); checkIfPersonAlreadyHasPermissions(elementsToAddOrRemove, toAdd); if (isToMaintainElements) { existentGroup = getUnitOccupationsAccessGroupWithChainOfResponsibility(); } else { existentGroup = getUnitOccupationsAccessGroup(); } newGroupUnion = manageGroups(toAdd, groupToAddOrRemove, existentGroup); setUnitOccupationsAccessGroup(newGroupUnion); spaceManagerRoleManagement(elementsToAddOrRemove, toAdd); break; case SPACE_MANAGEMENT_ACCESS_GROUP: elementsToAddOrRemove = groupToAddOrRemove.getElements(); checkIfPersonAlreadyHasPermissions(elementsToAddOrRemove, toAdd); if (isToMaintainElements) { existentGroup = getSpaceManagementAccessGroupWithChainOfResponsibility(); } else { existentGroup = getSpaceManagementAccessGroup(); } newGroupUnion = manageGroups(toAdd, groupToAddOrRemove, existentGroup); setSpaceManagementAccessGroup(newGroupUnion); spaceManagerRoleManagement(elementsToAddOrRemove, toAdd); break; case LESSON_OCCUPATION_ACCESS_GROUP: if (isToMaintainElements) { existentGroup = getLessonOccupationsAccessGroupWithChainOfResponsibility(); } else { existentGroup = getLessonOccupationsAccessGroup(); } newGroupUnion = manageGroups(toAdd, groupToAddOrRemove, existentGroup); setLessonOccupationsAccessGroup(newGroupUnion); break; case WRITTEN_EVALUATION_OCCUPATION_ACCESS_GROUP: if (isToMaintainElements) { existentGroup = getWrittenEvaluationOccupationsAccessGroupWithChainOfResponsibility(); } else { existentGroup = getWrittenEvaluationOccupationsAccessGroup(); } newGroupUnion = manageGroups(toAdd, groupToAddOrRemove, existentGroup); setWrittenEvaluationOccupationsAccessGroup(newGroupUnion); break; case GENERIC_EVENT_SPACE_OCCUPATION_ACCESS_GROUP: if (isToMaintainElements) { existentGroup = getGenericEventOccupationsAccessGroupWithChainOfResponsibility(); } else { existentGroup = getGenericEventOccupationsAccessGroup(); } newGroupUnion = manageGroups(toAdd, groupToAddOrRemove, existentGroup); setGenericEventOccupationsAccessGroup(newGroupUnion); break; default: break; } } private Group manageGroups(Boolean toAdd, Group groupToAddOrRemove, Group existentGroup) { List existentGroups = new ArrayList(); if (existentGroup != null) { if (existentGroup instanceof GroupUnion) { existentGroups.addAll(((GroupUnion) existentGroup).getChildren()); } else { existentGroups.add(existentGroup); } } if (toAdd) { for (Iterator iter = existentGroups.iterator(); iter.hasNext();) { IGroup existentGroup_ = (IGroup) iter.next(); if (existentGroup_.getElements().containsAll(groupToAddOrRemove.getElements())) { toAdd = false; break; } } if (toAdd) { existentGroups.add(groupToAddOrRemove); } } else { for (Iterator iter = existentGroups.iterator(); iter.hasNext();) { IGroup existentGroup_ = (IGroup) iter.next(); if (existentGroup_.getElementsCount() == groupToAddOrRemove.getElementsCount() && existentGroup_.getElements().containsAll(groupToAddOrRemove.getElements())) { iter.remove(); existentGroups.remove(existentGroup_); } } } return (Group) (existentGroups.isEmpty() ? null : existentGroups.size() == 1 ? existentGroups.get(0) : new GroupUnion( existentGroups)); } private void checkIfPersonAlreadyHasPermissions(Set persons, boolean toAdd) throws DomainException { if (toAdd) { for (Person person : persons) { if (personHasPermissionsToManageSpace(person)) { throw new DomainException("error.space.access.groups.management.person.already.have.permission"); } } } } private void spaceManagerRoleManagement(Set elementsToManage, boolean toAdd) { if (toAdd) { for (Person person : elementsToManage) { person.addPersonRoleByRoleType(RoleType.SPACE_MANAGER); } } else { for (Resource resource : RootDomainObject.getInstance().getResources()) { if (resource.isSpace()) { Space space = (Space) resource; for (Person person : elementsToManage) { if (!personIsSpacesAdministrator(person) && !space.personHasPermissionToManageExtensionOccupations(person) && !space.personHasPermissionToManagePersonOccupations(person) && !space.personHasPermissionToManageUnitOccupations(person) && !space.personHasSpecialPermissionToManageSpace(person)) { person.removeRoleByType(RoleType.SPACE_MANAGER); } } } } } } public static enum SpaceAccessGroupType { PERSON_OCCUPATION_ACCESS_GROUP("personOccupationsAccessGroup"), UNIT_OCCUPATION_ACCESS_GROUP("unitOccupationsAccessGroup"), EXTENSION_OCCUPATION_ACCESS_GROUP("extensionOccupationsAccessGroup"), SPACE_MANAGEMENT_ACCESS_GROUP("spaceManagementAccessGroup"), LESSON_OCCUPATION_ACCESS_GROUP("lessonOccupationsAccessGroup"), WRITTEN_EVALUATION_OCCUPATION_ACCESS_GROUP("writtenEvaluationOccupationsAccessGroup"), GENERIC_EVENT_SPACE_OCCUPATION_ACCESS_GROUP("genericEventOccupationsAccessGroup"); private String spaceAccessGroupSlotName; private SpaceAccessGroupType(String spaceAccessGroupSlotName) { this.spaceAccessGroupSlotName = spaceAccessGroupSlotName; } public String getName() { return name(); } public String getSpaceAccessGroupSlotName() { return spaceAccessGroupSlotName; } } public Building getSpaceBuilding() { if (isBuilding()) { return (Building) this; } if (getSuroundingSpace() == null) { return null; } return getSuroundingSpace().getSpaceBuilding(); } public Floor getSpaceFloor() { if (isFloor()) { if (getSuroundingSpace() == null) { return (Floor) this; } else if (getSuroundingSpace().isFloor()) { return getSuroundingSpace().getSpaceFloor(); } else { return (Floor) this; } } if (getSuroundingSpace() == null) { return null; } return getSuroundingSpace().getSpaceFloor(); } public Floor getSpaceFloorWithIntermediary() { if (isFloor()) { return (Floor) this; } if (getSuroundingSpace() == null) { return null; } return getSuroundingSpace().getSpaceFloorWithIntermediary(); } public Campus getSpaceCampus() { if (isCampus()) { return (Campus) this; } if (getSuroundingSpace() == null) { return null; } return getSuroundingSpace().getSpaceCampus(); } public List getSpaceFullPath() { List result = new ArrayList(); result.add(this); Space suroundingSpace = getSuroundingSpace(); while (suroundingSpace != null) { result.add(0, suroundingSpace); suroundingSpace = suroundingSpace.getSuroundingSpace(); } return result; } public Set getPossibleParentSpacesToMoveSpaceUp() { Set result = new HashSet(); if (!(isCampus())) { result = getPossibleParentSpacesToMoveSpaceUp(result); result.addAll(Space.getAllCampus()); if (getSuroundingSpace() != null) { result.remove(getSuroundingSpace()); } } return result; } private Set getPossibleParentSpacesToMoveSpaceUp(Set result) { if (getSuroundingSpace() != null) { result.add(getSuroundingSpace()); getSuroundingSpace().getPossibleParentSpacesToMoveSpaceUp(result); } return result; } public List getPossibleParentSpacesToMoveSpaceDown() { List result = new ArrayList(); if (!(isCampus())) { if (getSuroundingSpace() != null) { result.addAll(getSuroundingSpace().getContainedSpaces()); } result.remove(this); } return result; } public String getResourceAllocationsResume() { StringBuilder builder = new StringBuilder(); int eventOccupations = 0, personOccupations = 0, unitOccupations = 0, materialOccupations = 0; for (ResourceAllocation resourceAllocation : getResourceAllocations()) { if (resourceAllocation.isEventSpaceOccupation()) { eventOccupations++; } else if (resourceAllocation.isPersonSpaceOccupation()) { personOccupations++; } else if (resourceAllocation.isUnitSpaceOccupation()) { unitOccupations++; } else if (resourceAllocation.isMaterialSpaceOccupation()) { materialOccupations++; } } builder.append(eventOccupations).append(" (Events)").append(", "); builder.append(personOccupations).append(" (Persons)").append(", "); builder.append(unitOccupations).append(" (Units)").append(", "); builder.append(materialOccupations).append(" (Material)"); return builder.toString(); } public static Set findSpaces(String labelToSearch, Campus campus, Building building, SpacesSearchCriteriaType searchType) { Set result = new TreeSet(Space.COMPARATOR_BY_NAME_FLOOR_BUILDING_AND_CAMPUS); if (searchType != null && (campus != null || building != null || (labelToSearch != null && !StringUtils.isEmpty(labelToSearch.trim())))) { String[] labelWords = getIdentificationWords(labelToSearch); Set executionCoursesToTest = searchExecutionCoursesByName(searchType, labelWords); Collection personsToTest = searchPersonsByName(searchType, labelToSearch); for (Resource resource : RootDomainObject.getInstance().getResources()) { if (resource.isSpace() && ((Space) resource).isActive() && !resource.equals(campus) && !resource.equals(building)) { Space space = (Space) resource; if (labelWords != null) { boolean toAdd = false; switch (searchType) { case SPACE: toAdd = space.verifyNameEquality(labelWords); break; case PERSON: for (Person person : personsToTest) { if (person.getActivePersonSpaces().contains(resource)) { toAdd = true; break; } } break; case EXECUTION_COURSE: for (ExecutionCourse executionCourse : executionCoursesToTest) { if (executionCourse.getAllRooms().contains(resource)) { toAdd = true; break; } } break; case WRITTEN_EVALUATION: for (ExecutionCourse executionCourse : executionCoursesToTest) { SortedSet writtenEvaluations = executionCourse.getWrittenEvaluations(); for (WrittenEvaluation writtenEvaluation : writtenEvaluations) { if (writtenEvaluation.getAssociatedRooms().contains(resource)) { toAdd = true; break; } } if (toAdd) { break; } } break; default: break; } if (!toAdd) { continue; } } if (building != null) { Building spaceBuilding = space.getSpaceBuilding(); if (spaceBuilding == null || !spaceBuilding.equals(building)) { continue; } } else if (campus != null) { Campus spaceCampus = space.getSpaceCampus(); if (spaceCampus == null || !spaceCampus.equals(campus)) { continue; } } result.add(space); } } } return result; } private static Collection searchPersonsByName(SpacesSearchCriteriaType searchType, String labelToSearch) { if (labelToSearch != null && !StringUtils.isEmpty(labelToSearch) && searchType.equals(SpacesSearchCriteriaType.PERSON)) { return Person.findPerson(labelToSearch); } return Collections.EMPTY_LIST; } private static Set searchExecutionCoursesByName(SpacesSearchCriteriaType searchType, String[] labelWords) { Set executionCoursesToTest = null; if (labelWords != null && (searchType.equals(SpacesSearchCriteriaType.EXECUTION_COURSE) || searchType .equals(SpacesSearchCriteriaType.WRITTEN_EVALUATION))) { executionCoursesToTest = new HashSet(); for (ExecutionCourse executionCourse : ExecutionSemester.readActualExecutionSemester() .getAssociatedExecutionCoursesSet()) { if (executionCourse.verifyNameEquality(labelWords)) { executionCoursesToTest.add(executionCourse); } } } return executionCoursesToTest; } public Set getActiveSpaceExtensions() { Set result = new HashSet(); YearMonthDay current = new YearMonthDay(); for (MaterialSpaceOccupation materialSpaceOccupation : getMaterialSpaceOccupations()) { if (materialSpaceOccupation.getMaterial().isExtension() && materialSpaceOccupation.isActive(current)) { result.add((Extension) materialSpaceOccupation.getMaterial()); } } return result; } }