package net.sourceforge.fenixedu.domain.organizationalStructure; import java.util.ArrayList; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; import net.sourceforge.fenixedu.domain.CompetenceCourse; import net.sourceforge.fenixedu.domain.CurricularCourse; import net.sourceforge.fenixedu.domain.Degree; import net.sourceforge.fenixedu.domain.Department; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.ScientificAreaSite; import net.sourceforge.fenixedu.domain.UnitSite; import net.sourceforge.fenixedu.domain.accessControl.ScientificAreaMemberGroup; import net.sourceforge.fenixedu.domain.administrativeOffice.AdministrativeOffice; import net.sourceforge.fenixedu.domain.degreeStructure.Context; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.space.Campus; import net.sourceforge.fenixedu.injectionCode.AccessControl; import net.sourceforge.fenixedu.injectionCode.IGroup; import org.apache.commons.lang.StringUtils; import org.joda.time.YearMonthDay; import pt.iscte.ci.metadata.ISCTE; import pt.ist.fenixWebFramework.services.Service; import pt.utl.ist.fenix.tools.util.StringNormalizer; import pt.utl.ist.fenix.tools.util.i18n.MultiLanguageString; public class ScientificAreaUnit extends ScientificAreaUnit_Base { private ScientificAreaUnit() { super(); super.setType(PartyTypeEnum.SCIENTIFIC_AREA); } public static ScientificAreaUnit createNewInternalScientificArea(MultiLanguageString name, Integer costCenterCode, String acronym, YearMonthDay beginDate, YearMonthDay endDate, Unit parentUnit, AccountabilityType accountabilityType, String webAddress, UnitClassification classification, Boolean canBeResponsibleOfSpaces, Campus campus) { ScientificAreaUnit scientificAreaUnit = new ScientificAreaUnit(); scientificAreaUnit.init(name, costCenterCode, acronym, beginDate, endDate, webAddress, classification, canBeResponsibleOfSpaces, campus); Unit scientificAreaParentUnit = parentUnit; if (parentUnit.isDepartmentUnit()) { for (Unit unit : parentUnit.getDepartmentUnit().getSubUnits(PartyTypeEnum.AGGREGATE_UNIT)) { if (!unit.getSubUnits(PartyTypeEnum.SCIENTIFIC_AREA).isEmpty() || StringNormalizer.normalize(unit.getName()).equals("areas cientificas")) { scientificAreaParentUnit = unit; break; } } } scientificAreaUnit.addParentUnit(scientificAreaParentUnit, accountabilityType); checkIfAlreadyExistsOneScientificAreaUnitWithSameAcronymAndName(scientificAreaUnit); return scientificAreaUnit; } @Service public static ScientificAreaUnit createNewInternalScientificAreaWithCompetenceCourseGroupUnit(MultiLanguageString name, Integer costCenterCode, String acronym, YearMonthDay beginDate, YearMonthDay endDate, Unit parentUnit, AccountabilityType accountabilityType, String webAddress, UnitClassification classification, Boolean canBeResponsibleOfSpaces, Campus campus) { final ScientificAreaUnit scientificAreaUnit = createNewInternalScientificArea(name, costCenterCode, acronym, beginDate, endDate, parentUnit, accountabilityType, webAddress, classification, canBeResponsibleOfSpaces, campus); CompetenceCourseGroupUnit.createNewInternalCompetenceCourseGroupUnit(name, costCenterCode, acronym, beginDate, endDate, scientificAreaUnit, AccountabilityType.readAccountabilityTypeByType(AccountabilityTypeEnum.ACADEMIC_STRUCTURE), webAddress, classification, canBeResponsibleOfSpaces, campus); return scientificAreaUnit; } @Override public void edit(MultiLanguageString unitName, Integer unitCostCenter, String acronym, YearMonthDay beginDate, YearMonthDay endDate, String webAddress, UnitClassification classification, Department department, Degree degree, AdministrativeOffice administrativeOffice, Boolean canBeResponsibleOfSpaces, Campus campus) { super.edit(unitName, unitCostCenter, acronym, beginDate, endDate, webAddress, classification, department, degree, administrativeOffice, canBeResponsibleOfSpaces, campus); checkIfAlreadyExistsOneScientificAreaUnitWithSameAcronymAndName(this); } @Service public void edit(MultiLanguageString unitName, String acronym, YearMonthDay beginDate, YearMonthDay endDate, UnitClassification classification) { setPartyName(unitName); if (acronym != null) { setAcronym(acronym); } setBeginDateYearMonthDay(beginDate); setEndDateYearMonthDay(endDate); setClassification(classification); checkIfAlreadyExistsOneScientificAreaUnitWithSameAcronymAndName(this); } @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 Accountability addParentUnit(Unit parentUnit, AccountabilityType accountabilityType) { if (parentUnit != null && (!parentUnit.isInternal() || (!parentUnit.isDepartmentUnit() && !parentUnit.isAggregateUnit()))) { throw new DomainException("error.unit.invalid.parentUnit"); } return super.addParentUnit(parentUnit, accountabilityType); } @Override public boolean isScientificAreaUnit() { return true; } @ISCTE(author = "Nadir Amin", comment = "Changed to use ISCTE specific structure of unit<->competence course") @Override public boolean hasCompetenceCourses(final CompetenceCourse competenceCourse) { return competenceCourse.getScientificAreaUnit().isEqualOrHasParent(this); } public List getCompetenceCourseGroupUnits() { final SortedSet result = new TreeSet( CompetenceCourseGroupUnit.COMPARATOR_BY_NAME_AND_ID); for (Unit unit : getSubUnits()) { if (unit.isCompetenceCourseGroupUnit()) { result.add((CompetenceCourseGroupUnit) unit); } } return new ArrayList(result); } public Double getScientificAreaUnitEctsCredits() { double result = 0.0; for (CompetenceCourseGroupUnit competenceCourseGroupUnit : getCompetenceCourseGroupUnits()) { for (CompetenceCourse competenceCourse : competenceCourseGroupUnit.getCompetenceCourses()) { result += competenceCourse.getEctsCredits(); } } return result; } public Double getScientificAreaUnitEctsCredits(List contexts) { double result = 0.0; for (Context context : contexts) { if (context.getChildDegreeModule().isLeaf()) { CurricularCourse curricularCourse = (CurricularCourse) context.getChildDegreeModule(); if (!curricularCourse.isOptional() && curricularCourse.getCompetenceCourse().getScientificAreaUnit().equals(this)) { result += curricularCourse.getCompetenceCourse().getEctsCredits(); } } } return result; } private static void checkIfAlreadyExistsOneScientificAreaUnitWithSameAcronymAndName(ScientificAreaUnit scientificAreaUnit) { for (Unit parentUnit : scientificAreaUnit.getParentUnits()) { for (Unit unit : parentUnit.getAllSubUnits()) { if (unit.isScientificAreaUnit() && !unit.equals(scientificAreaUnit) && (scientificAreaUnit.getName().equalsIgnoreCase(unit.getName()) || scientificAreaUnit.getAcronym() .equalsIgnoreCase(unit.getAcronym()))) { throw new DomainException("error.unit.already.exists.unit.with.same.name.or.acronym"); } } } } @Override protected UnitSite createSite() { return new ScientificAreaSite(this); } public boolean isUserMemberOfScientificArea(Person person) { for (Contract contract : getWorkingContracts()) { if (contract.getPerson().equals(person)) { return true; } } return false; } public boolean isCurrentUserMemberOfScientificArea() { return isUserMemberOfScientificArea(AccessControl.getPerson()); } @Override protected List getDefaultGroups() { List groups = super.getDefaultGroups(); groups.add(new ScientificAreaMemberGroup(this)); return groups; } @ISCTE(author = "Nadir Amin", comment = "Added logic to delete associated curricular course rules") @Override public void delete() { while (!getAssociatedAnyCurricularCourseRules().isEmpty()) { getAssociatedAnyCurricularCourseRules().get(0).delete(); } super.delete(); } public boolean isHiddenClassification() { return getClassification() == UnitClassification.HIDDEN; } public String getDepartmentUnitName() { final DepartmentUnit departmentUnit = getDepartmentUnit(); return departmentUnit != null ? departmentUnit.getName() : StringUtils.EMPTY; } }