package net.sourceforge.fenixedu.domain.organizationalStructure; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.Department; import net.sourceforge.fenixedu.domain.ExternalCurricularCourse; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.degree.DegreeType; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.domain.space.Campus; import net.sourceforge.fenixedu.injectionCode.AccessControl; import org.apache.commons.lang.StringUtils; import org.joda.time.YearMonthDay; import pt.iscte.ci.metadata.ISCTE; import pt.utl.ist.fenix.tools.util.i18n.MultiLanguageString; public class SchoolUnit extends SchoolUnit_Base { private SchoolUnit() { super(); super.setType(PartyTypeEnum.SCHOOL); } public static SchoolUnit createNewSchoolUnit(MultiLanguageString schoolName, Integer costCenterCode, String schoolAcronym, YearMonthDay beginDate, YearMonthDay endDate, Unit parentUnit, String webAddress, UnitClassification classification, Boolean canBeResponsibleOfSpaces, Campus campus) { SchoolUnit schoolUnit = new SchoolUnit(); schoolUnit.init(schoolName, costCenterCode, schoolAcronym, beginDate, endDate, webAddress, classification, canBeResponsibleOfSpaces, campus); if (parentUnit.isCountryUnit()) { schoolUnit.addParentUnit(parentUnit, AccountabilityType .readAccountabilityTypeByType(AccountabilityTypeEnum.GEOGRAPHIC)); } else if (parentUnit.isUniversityUnit() || parentUnit.isAggregateUnit()) { schoolUnit.addParentUnit(parentUnit, AccountabilityType .readAccountabilityTypeByType(AccountabilityTypeEnum.ORGANIZATIONAL_STRUCTURE)); } checkIfAlreadyExistsOneSchoolWithSameAcronymAndName(schoolUnit); return schoolUnit; } @Override public Accountability addParentUnit(Unit parentUnit, AccountabilityType accountabilityType) { // ISCTE - António Casqueiro - 2010-06-24 // "Disable is '!parentUnit.isOfficialExternal()' check for manager // role. // This was required to create the school in ISCTE-IUL // new organizational structure. if (parentUnit != null) { final Person person = AccessControl.getPerson(); if ((!parentUnit.isOfficialExternal() && person != null && !person.hasRole(RoleType.MANAGER)) // HACK || (!parentUnit.isPlanetUnit() && !parentUnit.isCountryUnit() && !parentUnit.isUniversityUnit() && !parentUnit .isAggregateUnit())) { throw new DomainException("error.unit.invalid.parentUnit"); } } return super.addParentUnit(parentUnit, accountabilityType); } @Override public void setAcronym(String acronym) { if (StringUtils.isEmpty(acronym)) { throw new DomainException("error.unit.empty.acronym"); } super.setAcronym(acronym); } @Override public void setType(PartyTypeEnum partyTypeEnum) { throw new DomainException("unit.impossible.set.type"); } @Override public boolean isSchoolUnit() { return true; } @Override public List getAllExternalCurricularCourses() { final List result = new ArrayList(getExternalCurricularCourses()); final Set departments = new HashSet(); appendDepartmentSubUnits(this, departments); for (final DepartmentUnit subUnit : departments) { result.addAll(subUnit.getExternalCurricularCourses()); } return result; } private void appendDepartmentSubUnits(final Unit unit, Set departments) { for (final Unit subUnit : unit.getSubUnits()) { if (subUnit.isDepartmentUnit()) { departments.add((DepartmentUnit) subUnit); continue; } else { appendDepartmentSubUnits(unit, departments); } } } private static void checkIfAlreadyExistsOneSchoolWithSameAcronymAndName(SchoolUnit schoolUnit) { for (Unit parentUnit : schoolUnit.getParentUnits()) { for (Unit unit : parentUnit.getAllSubUnits()) { if (!unit.equals(schoolUnit) && unit.isSchoolUnit() && (schoolUnit.getAcronym().equalsIgnoreCase(unit.getAcronym()) || schoolUnit.getName().equalsIgnoreCase( unit.getName()))) { throw new DomainException("error.unit.already.exists.unit.with.same.name.or.acronym"); } } } } @ISCTE(author="António Casqueiro") public boolean isMemberOfSecretariat(Person person) { Function function = getSecretariat(); if (function != null) { return isMemberOfSecretariat(person, function); } return false; } @ISCTE(author="António Casqueiro") private boolean isMemberOfSecretariat(Person person, Function function) { for (PersonFunction personFunction : function.getPersonFunctions()) { if (personFunction.getPerson() == person) { return true; } } return false; } @ISCTE(author="António Casqueiro") private Function getSecretariat() { for (Function function : getFunctions()) { if (function.getFunctionType() == FunctionType.SECRETARY) { return function; } } return null; } @ISCTE(author="António Casqueiro") public List getDepartments() { List departments = new ArrayList(); for (Unit unit : getSubUnits(getSubUnits())) { if (unit instanceof DepartmentUnit) { DepartmentUnit departmentUnit = (DepartmentUnit)unit; departments.add(departmentUnit.getDepartment()); } } return departments; } @ISCTE(author="António Casqueiro") private List getSubUnits(final Collection subUnits) { List list = new ArrayList(); for (final Unit unit : subUnits) { if (unit instanceof AggregateUnit) { list.addAll(getSubUnits(unit.getSubUnits())); } else { list.add((Unit)unit); } } return list; } @ISCTE public AggregateUnit findDegreeAggregatorByDegreeType(final DegreeType degreeType) { for (Unit unit : getSubUnits()) { if (unit.isAggregateUnit()) { final AggregateUnit aggregateUnit = ((AggregateUnit) unit).getDegreeAggregatorOfDegreeType(degreeType); if (aggregateUnit != null) { return aggregateUnit; } } } return null; } @ISCTE static public List readActiveInternalSchools() { final List result = new ArrayList(); for (final Unit schoolUnit : SchoolUnit.readUnitsByType(PartyTypeEnum.SCHOOL)) { if (schoolUnit.isInternal() && schoolUnit.isActive(new YearMonthDay())) { result.add((SchoolUnit) schoolUnit); } } return result; } }