package net.sourceforge.fenixedu.domain.teacherServiceDistribution; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.CompetenceCourse; import net.sourceforge.fenixedu.domain.Department; import net.sourceforge.fenixedu.domain.ExecutionSemester; import net.sourceforge.fenixedu.domain.Teacher; import net.sourceforge.fenixedu.domain.organizationalStructure.Unit; public class TSDFactory { private static TSDFactory instance = new TSDFactory(); private TSDFactory() { } public static TSDFactory getInstance() { return instance; } public void createTSDTreeStructure(TSDProcessPhase tsdPhase) { createRootTSD(tsdPhase); createSubGroupings(tsdPhase); } private TeacherServiceDistribution createRootTSD(TSDProcessPhase tsdPhase) { TSDProcess tsdProcess = tsdPhase.getTSDProcess(); List tsdTeachers = createTSDTeacher(tsdProcess.getDepartment(), tsdProcess.getExecutionPeriods()); List tsdCourses = createTSDCompetenceCourses(tsdProcess); return new TeacherServiceDistribution(tsdPhase, tsdProcess.getDepartment().getAcronym(), null, tsdTeachers, tsdCourses, null, null, null, null); } private void createSubGroupings(TSDProcessPhase tsdPhase) { HashMap unitTeachersNewGrouping = new HashMap(); TeacherServiceDistribution rootTSD = tsdPhase.getRootTSD(); for (TSDTeacher tsdTeacher : rootTSD.getTSDTeachers()) { Teacher teacher = ((TSDRealTeacher) tsdTeacher).getTeacher(); if (teacher.getCurrentWorkingUnit() != null) { createChildUnitsGroupingsForTeachers(tsdPhase, tsdTeacher, teacher.getCurrentWorkingUnit(), null, rootTSD, unitTeachersNewGrouping); } } HashMap unitCoursesNewGrouping = new HashMap(); for (TSDCourse course : rootTSD.getTSDCourses()) { if (course.getCompetenceCourse().getCompetenceCourseGroupUnit() != null) { createChildCompetenceCourseGroupUnits(tsdPhase, course, rootTSD, unitCoursesNewGrouping); } } HashMap unitScientificAreaGrouping = new HashMap(); for (Unit courseUnit : unitCoursesNewGrouping.keySet()) { Iterator iter = courseUnit.getParentUnits().iterator(); Unit scientificAreaUnit = (!iter.hasNext()) ? null : iter.next(); TeacherServiceDistribution scientificAreaGrouping = unitScientificAreaGrouping.get(scientificAreaUnit); TeacherServiceDistribution teachersNewGrouping = unitTeachersNewGrouping.get(scientificAreaUnit); TeacherServiceDistribution coursesNewGrouping = unitCoursesNewGrouping.get(courseUnit); if (scientificAreaGrouping == null) { if (teachersNewGrouping == null) { scientificAreaGrouping = new TeacherServiceDistribution(tsdPhase, scientificAreaUnit.getName(), rootTSD, new ArrayList(), new ArrayList(), rootTSD.getCoursesValuationManagers(), rootTSD.getTeachersValuationManagers(), rootTSD.getCoursesManagementGroup(), rootTSD .getTeachersValuationManagers()); } else { scientificAreaGrouping = teachersNewGrouping; } unitScientificAreaGrouping.put(scientificAreaUnit, scientificAreaGrouping); } scientificAreaGrouping.getTSDCoursesSet().addAll(coursesNewGrouping.getTSDCourses()); coursesNewGrouping.setParent(scientificAreaGrouping); } } private void createChildUnitsGroupingsForTeachers(TSDProcessPhase tsdPhase, TSDTeacher tsdTeacher, Unit unit, TeacherServiceDistribution childTSD, TeacherServiceDistribution rootTSD, HashMap unitNewTSD) { if (unit.isDepartmentUnit()) { if (childTSD != null) { childTSD.setParent(rootTSD); } return; } TeacherServiceDistribution newTSD = unitNewTSD.get(unit); if (newTSD == null) { List teachersList = new ArrayList(); teachersList.add(tsdTeacher); List emptyCourseList = new ArrayList(); newTSD = new TeacherServiceDistribution(tsdPhase, unit.getName(), null, teachersList, emptyCourseList, null, null, null, null); unitNewTSD.put(unit, newTSD); if (childTSD != null) { childTSD.setParent(newTSD); } for (Unit parentUnit : unit.getParentUnitsByOrganizationalStructureAccountabilityType()) { for (Unit grandParentUnit : parentUnit.getParentUnitsByOrganizationalStructureAccountabilityType()) { if (!grandParentUnit.getParentUnitsByOrganizationalStructureAccountabilityType().isEmpty()) { createChildUnitsGroupingsForTeachers(tsdPhase, tsdTeacher, parentUnit, newTSD, rootTSD, unitNewTSD); } } } } else { newTSD.addTSDTeachers(tsdTeacher); for (TeacherServiceDistribution parentGrouping = newTSD.getParent(); parentGrouping != null && parentGrouping != rootTSD; parentGrouping = parentGrouping.getParent()) { parentGrouping.addTSDTeachers(tsdTeacher); } } } private void createChildCompetenceCourseGroupUnits(TSDProcessPhase tsdPhase, TSDCourse tsdCourse, TeacherServiceDistribution rootTSD, HashMap unitNewGrouping) { CompetenceCourse competenceCourse = tsdCourse.getCompetenceCourse(); TeacherServiceDistribution newGrouping = unitNewGrouping.get(competenceCourse.getCompetenceCourseGroupUnit()); if (newGrouping == null) { List courseList = new ArrayList(); courseList.add(new TSDCompetenceCourse(competenceCourse, tsdCourse.getExecutionPeriod())); List emptyTSDTeacherList = new ArrayList(); newGrouping = new TeacherServiceDistribution(tsdPhase, competenceCourse.getCompetenceCourseGroupUnit().getName(), rootTSD, emptyTSDTeacherList, courseList, null, null, null, null); unitNewGrouping.put(competenceCourse.getCompetenceCourseGroupUnit(), newGrouping); } else { newGrouping.addTSDCourses(tsdCourse); } } private List createTSDCompetenceCourses(TSDProcess tsdProcess) { List courseList = new ArrayList(); List periods = tsdProcess.getExecutionPeriods(); for (CompetenceCourse competenceCourse : tsdProcess.getAllCompetenceCourses()) { courseList.addAll(createTSDCompetenceCourses(competenceCourse, periods)); } return courseList; } private List createTSDCompetenceCourses(CompetenceCourse course, List periods) { List courseList = new ArrayList(); for (ExecutionSemester period : periods) { if (course.getCurricularCoursesWithActiveScopesInExecutionPeriod(period).size() > 0) courseList.add(new TSDCompetenceCourse(course, period)); } return courseList; } private List createTSDTeacher(Department department, List executionPeriodList) { List tsdTeacherList = new ArrayList(); Set teacherSet = new HashSet(); for (ExecutionSemester executionSemester : executionPeriodList) { teacherSet.addAll(department.getAllTeachers(executionSemester.getBeginDateYearMonthDay(), executionSemester .getEndDateYearMonthDay())); } for (Teacher teacher : teacherSet) { tsdTeacherList.add(new TSDRealTeacher(teacher)); } return tsdTeacherList; } }