/* * Created on Feb 6, 2006 * by mrsp */ package net.sourceforge.fenixedu.domain.organizationalStructure; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import org.joda.time.YearMonthDay; public class UnitUtils { public static List readAllExternalInstitutionUnits() { List allExternalUnits = new ArrayList(); allExternalUnits.addAll(readExternalInstitutionUnit().getAllSubUnits()); return allExternalUnits; } public static Unit readExternalInstitutionUnitByName(String name) { for (Unit unit : readAllExternalInstitutionUnits()) { if (unit.getName().equals(name)) { return unit; } } return null; } public static List readAllUnitsWithoutParents() { List allUnitsWithoutParent = new ArrayList(); for (Party party : RootDomainObject.getInstance().getPartys()) { if (party.isUnit()) { Unit unit = (Unit) party; if (unit.getParentUnits().isEmpty()) { allUnitsWithoutParent.add(unit); } } } return allUnitsWithoutParent; } public static List readAllInternalActiveUnitsThatCanBeResponsibleOfSpaces() { List result = new ArrayList(); final YearMonthDay now = new YearMonthDay(); Unit institutionUnit = readInstitutionUnit(); if (institutionUnit != null) { if (institutionUnit.getCanBeResponsibleOfSpaces() && institutionUnit.isActive(now)) { result.add(institutionUnit); } for (Unit subUnit : institutionUnit.getSubUnits()) { if (subUnit.getCanBeResponsibleOfSpaces() && subUnit.isActive(now)) { result.add(subUnit); } readAllInternalActiveSubUnitsThatCanBeResponsibleOfSpaces(result, subUnit, now); } } return result; } private static void readAllInternalActiveSubUnitsThatCanBeResponsibleOfSpaces(List result, Unit subUnit, YearMonthDay now) { for (Unit unit : subUnit.getSubUnits()) { if (unit.getCanBeResponsibleOfSpaces() && unit.isActive(now)) { result.add(unit); } readAllInternalActiveSubUnitsThatCanBeResponsibleOfSpaces(result, unit, now); } } public static List readAllActiveUnitsByType(PartyTypeEnum type) { final List result = new ArrayList(); final YearMonthDay now = new YearMonthDay(); PartyType partyType = PartyType.readPartyTypeByType(type); if (partyType != null) { List parties = partyType.getParties(); for (Party party : parties) { if (party.isUnit()) { Unit unit = (Unit) party; if (unit.isActive(now)) { result.add(unit); } } } } return result; } public static List readAllActiveUnitsByClassification(UnitClassification unitClassification) { final List result = new ArrayList(); final YearMonthDay now = new YearMonthDay(); if (unitClassification != null) { for (Party party : RootDomainObject.getInstance().getPartys()) { if (party.isUnit()) { Unit unit = (Unit) party; if (unit.getClassification() != null && unit.getClassification().equals(unitClassification) && unit.isActive(now)) { result.add(unit); } } } } return result; } public static List readAllDepartmentUnits() { List result = new ArrayList(); List readAllActiveUnitsByType = readAllActiveUnitsByType(PartyTypeEnum.DEPARTMENT); for (Unit unit : readAllActiveUnitsByType) { result.add((DepartmentUnit) unit); } return result; } public static Unit readUnitWithoutParentstByAcronym(String acronym) { for (Unit topUnit : readAllUnitsWithoutParents()) { if (topUnit.getAcronym() != null && topUnit.getAcronym().equals(acronym)) { return topUnit; } } return null; } public static Unit readExternalInstitutionUnit() { return RootDomainObject.getInstance().getExternalInstitutionUnit(); } public static Unit readInstitutionUnit() { return RootDomainObject.getInstance().getInstitutionUnit(); } public static Unit readEarthUnit() { return RootDomainObject.getInstance().getEarthUnit(); } public static Set readExternalUnitsByNameAndTypes(final String unitName, List types) { if (unitName == null) { return Collections.emptySet(); } final Collection units = UnitName.findExternalUnit(unitName.replace('%', ' '), Integer.MAX_VALUE); final Set result = new HashSet(); for (final UnitName un : units) { final Unit unit = un.getUnit(); if (types.contains(unit.getType())) { result.add(unit); } } return result; } public static List getUnitFullPath(final Unit unit, final List validAccountabilityTypes) { final Collection parentUnits = unit.getParentUnits(validAccountabilityTypes); if (parentUnits.isEmpty()) { return Collections.emptyList(); } if (parentUnits.size() == 1) { final List result = new ArrayList(); result.add(unit); result.addAll(0, getUnitFullPath(parentUnits.iterator().next(), validAccountabilityTypes)); return result; } throw new DomainException("error.unitUtils.unit.full.path.has.more.than.one.parent"); } public static StringBuilder getUnitFullPathName(final Unit unit, final List validAccountabilityTypes) { if (unit == readEarthUnit()) { return new StringBuilder(0); } final Collection parentUnits = unit.getParentUnits(validAccountabilityTypes); if (parentUnits.isEmpty()) { return new StringBuilder(unit.getName()); } if (parentUnits.size() == 1) { final StringBuilder builder = new StringBuilder(); builder.append(parentUnits.iterator().next() == readEarthUnit() ? "" : " > ").append(unit.getName()); builder.insert(0, getUnitFullPathName(parentUnits.iterator().next(), validAccountabilityTypes)); return builder; } throw new DomainException("error.unitUtils.unit.full.path.has.more.than.one.parent"); } public static List readExternalUnitsByNameAndTypesStartingAtEarth(final String unitName, final List types) { if (unitName == null) { return Collections.emptyList(); } final String nameToSearch = unitName.replaceAll("%", ".*").toLowerCase(); final List result = new ArrayList(); for (final UnitName name : RootDomainObject.getInstance().getUnitNameSet()) { if (name.getName().toLowerCase().matches(nameToSearch) && name.getIsExternalUnit() && types.contains(name.getUnit().getType())) { result.add(name.getUnit()); } } return result; } public static Collection readAllUnitsWithClassification(UnitClassification classification) { List result = new ArrayList(); for (Party party : RootDomainObject.getInstance().getPartys()) { if (party.isUnit()) { Unit unit = (Unit) party; UnitClassification unitClassification = unit.getClassification(); if (unitClassification != null && unitClassification.equals(classification)) { result.add(unit); } } } return result; } }