package net.sourceforge.fenixedu.domain.teacherServiceDistribution; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.commons.CollectionUtils; import net.sourceforge.fenixedu.domain.CompetenceCourse; import net.sourceforge.fenixedu.domain.Department; import net.sourceforge.fenixedu.domain.ExecutionSemester; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.Teacher; import net.sourceforge.fenixedu.domain.accessControl.Group; import net.sourceforge.fenixedu.domain.accessControl.GroupDifference; import net.sourceforge.fenixedu.domain.accessControl.GroupUnion; import net.sourceforge.fenixedu.domain.accessControl.PersonGroup; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import org.apache.commons.collections.Predicate; public class TeacherServiceDistribution extends TeacherServiceDistribution_Base { private TeacherServiceDistribution() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public TeacherServiceDistribution(TSDProcessPhase tsdProcessPhase, String name, TeacherServiceDistribution parent, List tsdTeacherList, List courseList, Group courseValuationGroup, Group teacherValuationGroup, Group courseManagementGroup, Group teacherManagementGroup) { this(); if (tsdProcessPhase == null) { throw new DomainException("TSDProcessPhase.required"); } this.setTSDProcessPhase(tsdProcessPhase); this.setName(name); this.setParent(parent); this.getTSDTeachers().addAll(tsdTeacherList); this.getTSDCourses().addAll(courseList); this.setTeachersManagementGroup(teacherManagementGroup); this.setCoursesManagementGroup(courseManagementGroup); this.setCoursesValuationManagers(courseValuationGroup); this.setTeachersValuationManagers(teacherValuationGroup); } public Boolean getIsRoot() { return getParent() == null; } public TeacherServiceDistribution getRootTSD() { if (getIsRoot()) { return this; } else { return getParent().getRootTSD(); } } public void delete() { for (TSDCourse tsdCourse : getTSDCourses()) { if (tsdCourse.getTeacherServiceDistributionsCount() == 1) { tsdCourse.delete(); } else { removeTSDCourses(tsdCourse); } } for (TSDTeacher teacher : getTSDTeachers()) { if (teacher.getTeacherServiceDistributionsCount() == 1) { teacher.delete(); } else { removeTSDTeachers(teacher); } } removeParent(); removeTSDProcessPhase(); for (TeacherServiceDistribution childTSD : getChilds()) { removeChilds(childTSD); childTSD.delete(); } removeRootDomainObject(); super.deleteDomainObject(); } public Set getCompetenceCourses() { Set courseList = new HashSet(); CompetenceCourse course = null; for (TSDCourse tsdCourse : getTSDCourses()) { if ((course = tsdCourse.getCompetenceCourse()) != null) { courseList.add(course); } } return courseList; } public List getTSDCompetenceAndVirtualCourses() { return getTSDCompetenceAndVirtualCoursesByExecutionPeriod(getTSDProcessPhase().getTSDProcess().getExecutionPeriods()); } public List getTSDCompetenceAndVirtualCoursesByExecutionPeriod(ExecutionSemester period) { List periods = new ArrayList(); periods.add(period); return getTSDCompetenceAndVirtualCoursesByExecutionPeriod(periods); } private List getTSDCompetenceAndVirtualCoursesByExecutionPeriod(List periods) { Set courseList = new HashSet(); Set competenceCourseSet = new HashSet(); for (ExecutionSemester period : periods) { for (TSDCourse tsdCourse : getTSDCoursesByExecutionPeriod(period)) { if (tsdCourse instanceof TSDCompetenceCourse && tsdCourse.getCompetenceCourse().isBolonha()) { courseList.add(tsdCourse); competenceCourseSet.add(tsdCourse.getCompetenceCourse()); } if (tsdCourse instanceof TSDVirtualCourseGroup) { courseList.add(tsdCourse); } } } return new ArrayList(courseList); } public List getTSDCoursesByCompetenceCourse(CompetenceCourse course) { List courseList = new ArrayList(); for (TSDCourse tsdCourse : getTSDCourses()) { CompetenceCourse competenceCourse = tsdCourse.getCompetenceCourse(); if (competenceCourse != null && competenceCourse.equals(course)) { courseList.add(tsdCourse); } } return courseList; } public List getTSDCoursesByExecutionPeriod(ExecutionSemester period) { List courseList = new ArrayList(); for (TSDCourse tsdCourse : getTSDCourses()) { if (tsdCourse.getExecutionPeriod().equals(period)) { courseList.add(tsdCourse); } } return courseList; } @SuppressWarnings("unchecked") public List getCompetenceCoursesByExecutionPeriod(final ExecutionSemester executionSemester) { List competenceCourseList = new ArrayList(); competenceCourseList.addAll(CollectionUtils.select(getCompetenceCourses(), new Predicate() { public boolean evaluate(Object arg0) { CompetenceCourse competenceCourse = (CompetenceCourse) arg0; return competenceCourse.getCurricularCoursesWithActiveScopesInExecutionPeriod(executionSemester).size() > 0; } })); return competenceCourseList; } public TSDTeacher getTSDTeacherByTeacher(final Teacher teacher) { return (TSDTeacher) CollectionUtils.find(getTSDTeachers(), new Predicate() { public boolean evaluate(Object arg0) { if (arg0 instanceof TSDRealTeacher) { TSDRealTeacher tsdTeacher = (TSDRealTeacher) arg0; return tsdTeacher.getTeacher() == teacher; } else { return false; } } }); } public void removeTSDTeacherFromAllChilds(TSDTeacher tsdTeacher) { removeTSDTeachers(tsdTeacher); for (TeacherServiceDistribution childGrouping : getChilds()) { childGrouping.removeTSDTeacherFromAllChilds(tsdTeacher); } } public void removeTSDCourseFromAllChilds(TSDCourse course) { removeTSDCourses(course); for (TeacherServiceDistribution childGrouping : getChilds()) { childGrouping.removeTSDCourseFromAllChilds(course); } } public void addTSDCourseToTSDTree(TSDCourse course) { getRootTSD().addTSDCourseToChildsTree(course); } private void addTSDCourseToChildsTree(TSDCourse course) { if (getCompetenceCourses().contains(course.getCompetenceCourse())) { addTSDCourses(course); for (TeacherServiceDistribution childTSD : getChilds()) { childTSD.addTSDCourseToChildsTree(course); } } } protected void addTSDCourseToParentTree(TSDCourse course) { addTSDCourses(course); if (getParent() != null) { getParent().addTSDCourseToParentTree(course); } } public void mergeWithGrouping(TeacherServiceDistribution peerGrouping) { if (peerGrouping == this) { return; } Set mergedTSDTeachers = new HashSet(this.getTSDTeachers()); mergedTSDTeachers.addAll(peerGrouping.getTSDTeachers()); Set mergedCourses = new HashSet(this.getTSDCourses()); mergedCourses.addAll(peerGrouping.getTSDCourses()); this.getTSDCoursesSet().addAll(mergedCourses); this.getTSDTeachersSet().addAll(mergedTSDTeachers); if (!(getParent().getTSDTeachers().containsAll(this.getTSDTeachers()) && getParent().getTSDCourses().containsAll( this.getTSDCourses()))) { this.setParent(getRootTSD()); } } public List getDepartmentTeachersNotInGrouping(Department department) { TSDProcess distribution = this.getTSDProcessPhase().getTSDProcess(); List departmentTeachers = department.getAllTeachers(distribution.getFirstExecutionPeriod() .getBeginDateYearMonthDay(), distribution.getLastExecutionPeriod().getEndDateYearMonthDay()); List teachersList = new ArrayList(); for (Teacher departmentTeacher : departmentTeachers) { if (this.getTSDTeacherByTeacher(departmentTeacher) == null) { teachersList.add(departmentTeacher); } } return teachersList; } public List getDepartmentCompetenceCoursesNotInGrouping(Department department) { TSDProcess distribution = this.getTSDProcessPhase().getTSDProcess(); Set departmentCourses = distribution.getCompetenceCoursesByDepartment(department); Set groupCourses = getCompetenceCourses(); List availableCoursesList = new ArrayList(); for (CompetenceCourse course : departmentCourses) { if (!groupCourses.contains(course)) { availableCoursesList.add(course); } } return availableCoursesList; } @SuppressWarnings("unchecked") public List getCompetenceCoursesWithoutActiveTSDCourses(List executionPeriodList) { Set tsdCoursesList = getCompetenceCourses(); Set activeCoursesSet = new HashSet(); List returnList = new ArrayList(); for (ExecutionSemester period : executionPeriodList) { for (CompetenceCourse course : tsdCoursesList) { for (TSDCourse tsdCourse : getTSDCoursesByCompetenceCourse(course)) { if (tsdCourse.getExecutionPeriod().equals(period) && tsdCourse.getIsActive()) { activeCoursesSet.add(course); } } } } for (ExecutionSemester period : executionPeriodList) { for (TSDCourse tsdCourse : getTSDCoursesByExecutionPeriod(period)) { if (tsdCourse instanceof TSDCompetenceCourse) { if (!activeCoursesSet.contains(tsdCourse.getCompetenceCourse())) { returnList.add(tsdCourse); activeCoursesSet.add(tsdCourse.getCompetenceCourse()); } } } } return returnList; } private boolean isUserMemberOfGroup(Person person, Group group) { return group == null ? false : group.isMember(person); } public boolean isMemberOfCoursesValuationManagers(Person person) { return isUserMemberOfGroup(person, getCoursesValuationManagers()); } public boolean isMemberOfTeachersValuationManagers(Person person) { return isUserMemberOfGroup(person, getTeachersValuationManagers()); } public boolean isMemberOfCoursesManagementGroup(Person person) { return isUserMemberOfGroup(person, getCoursesManagementGroup()); } public boolean isMemberOfTeachersManagementGroup(Person person) { return isUserMemberOfGroup(person, getTeachersManagementGroup()); } public boolean hasCourseValuationPermission(Person person) { return getTSDProcessPhase().getTSDProcess().getHasSuperUserPermission(person) || isMemberOfCoursesValuationManagers(person) || ((getParent() != null) ? getParent().isMemberOfCoursesValuationManagers(person) : false); } public boolean hasTeachersValuationPermission(Person person) { return getTSDProcessPhase().getTSDProcess().getHasSuperUserPermission(person) || isMemberOfTeachersValuationManagers(person) || ((getParent() != null) ? getParent().isMemberOfTeachersValuationManagers(person) : false); } public boolean hasCourseManagementPermission(Person person) { return getTSDProcessPhase().getTSDProcess().getHasSuperUserPermission(person) || isMemberOfCoursesManagementGroup(person) || ((getParent() != null) ? getParent().hasCourseManagementPermission(person) : false); } public boolean hasTeachersManagementPermission(Person person) { return getTSDProcessPhase().getTSDProcess().getHasSuperUserPermission(person) || isMemberOfTeachersManagementGroup(person) || ((getParent() != null) ? getParent().hasTeachersManagementPermission(person) : false); } private Group addPersonToGroup(Group group, Person person) { return group != null ? new GroupUnion(group, new PersonGroup(person)) : new PersonGroup(person); } private Group removePersonFromGroup(Group group, Person person) { return group != null ? new GroupDifference(group, new PersonGroup(person)) : null; } public void addCourseValuationPermission(Person person) { setCoursesValuationManagers(addPersonToGroup(getCoursesValuationManagers(), person)); } public void removeCourseValuationPermission(Person person) { setCoursesValuationManagers(removePersonFromGroup(getCoursesValuationManagers(), person)); } public void addTeacherValuationPermission(Person person) { setTeachersValuationManagers(addPersonToGroup(getTeachersValuationManagers(), person)); } public void removeTeacherValuationPermission(Person person) { setTeachersValuationManagers(removePersonFromGroup(getTeachersValuationManagers(), person)); } public void addCourseManagersPermission(Person person) { setCoursesManagementGroup(addPersonToGroup(getCoursesManagementGroup(), person)); } public void removeCourseManagersPermission(Person person) { setCoursesManagementGroup(removePersonFromGroup(getCoursesManagementGroup(), person)); } public void addTeachersManagersPermission(Person person) { setTeachersManagementGroup(addPersonToGroup(getTeachersManagementGroup(), person)); } public void removeTeachersManagersPermission(Person person) { setTeachersManagementGroup(removePersonFromGroup(getTeachersManagementGroup(), person)); } public Double getAllActiveTSDCourseTotalHoursByExecutionPeriods(List executionPeriodList) { double totalHours = 0d; for (TSDCourse tsdCourse : getActiveTSDCourseByExecutionPeriods(executionPeriodList)) { totalHours += tsdCourse.getTotalHours(); } return totalHours; } public List getActiveTSDCourses() { List tsdCourseList = new ArrayList(); for (TSDCourse course : getTSDCourses()) { if (course.getIsActive()) { tsdCourseList.add(course); } } return tsdCourseList; } public List getActiveTSDCourseByExecutionPeriods(List executionPeriodList) { List tsdCourseList = new ArrayList(); for (ExecutionSemester executionSemester : executionPeriodList) { for (TSDCourse course : getTSDCourses()) { if (course.getExecutionPeriod().equals(executionSemester) && course.getIsActive()) { tsdCourseList.add(course); } } } return tsdCourseList; } public Double getAllActiveTSDCourseTotalStudentsByExecutionPeriods(List executionPeriodList) { double totalStudents = 0d; for (TSDCourse tsdCourse : getActiveTSDCourseByExecutionPeriods(executionPeriodList)) { totalStudents += tsdCourse.getFirstTimeEnrolledStudents() + tsdCourse.getSecondTimeEnrolledStudents(); } return totalStudents; } public Integer getAllActiveTSDCourseFirstTimeEnrolledStudentsByExecutionPeriods(List executionPeriodList) { int totalStudents = 0; for (TSDCourse tsdCourse : getActiveTSDCourseByExecutionPeriods(executionPeriodList)) { totalStudents += tsdCourse.getFirstTimeEnrolledStudents(); } return totalStudents; } public Integer getAllActiveTSDCourseSecondTimeEnrolledStudentsByExecutionPeriods(List executionPeriodList) { int totalStudents = 0; for (TSDCourse tsdCourse : getActiveTSDCourseByExecutionPeriods(executionPeriodList)) { totalStudents += tsdCourse.getSecondTimeEnrolledStudents(); } return totalStudents; } public List getTSDCurricularCoursesWithoutTSDCourseGroup(CompetenceCourse course, ExecutionSemester executionSemester) { List tsdCurricularCourseList = new ArrayList(); for (TSDCourse tsdCourse : getTSDCoursesByCompetenceCourse(course)) { if (tsdCourse instanceof TSDCurricularCourse && ((TSDCurricularCourse) tsdCourse).getTSDCurricularCourseGroup() == null && tsdCourse.getExecutionPeriod().equals(executionSemester)) tsdCurricularCourseList.add((TSDCurricularCourse) tsdCourse); } return tsdCurricularCourseList; } public TSDCourseType getTSDCourseType(TSDCourse course, ExecutionSemester executionSemester) { if (course instanceof TSDVirtualCourseGroup) { return TSDCourseType.COMPETENCE_COURSE_VALUATION; } for (TSDCourse tsdCourse : getTSDCoursesByCompetenceCourse(course.getCompetenceCourse())) { if (tsdCourse.getIsActive()) { if (tsdCourse instanceof TSDCurricularCourse) { return TSDCourseType.CURRICULAR_COURSE_VALUATION; } if (tsdCourse instanceof TSDCurricularCourseGroup) { return TSDCourseType.CURRICULAR_COURSE_VALUATION_GROUP; } if (tsdCourse instanceof TSDCompetenceCourse) { return TSDCourseType.COMPETENCE_COURSE_VALUATION; } } } return TSDCourseType.NOT_DETERMINED; } public void setTSDCourseType(CompetenceCourse course, ExecutionSemester executionSemester, TSDCourseType courseType) { List tsdCoursesList = getTSDCoursesByCompetenceCourse(course); for (TSDCourse tsdCourse : tsdCoursesList) { tsdCourse.setIsActive(Boolean.FALSE); if (courseType == TSDCourseType.CURRICULAR_COURSE_VALUATION && tsdCourse instanceof TSDCurricularCourse) { tsdCourse.setIsActive(Boolean.TRUE); } if (courseType == TSDCourseType.CURRICULAR_COURSE_VALUATION_GROUP && tsdCourse instanceof TSDCurricularCourseGroup) { tsdCourse.setIsActive(Boolean.TRUE); } if (courseType == TSDCourseType.COMPETENCE_COURSE_VALUATION && tsdCourse instanceof TSDCompetenceCourse) { tsdCourse.setIsActive(Boolean.TRUE); } } } public TSDCompetenceCourse getTSDCompetenceCourse(CompetenceCourse course, ExecutionSemester executionSemester) { for (TSDCourse tsdCourse : getTSDCoursesByCompetenceCourse(course)) { if (tsdCourse instanceof TSDCompetenceCourse && tsdCourse.getExecutionPeriod().equals(executionSemester)) return (TSDCompetenceCourse) tsdCourse; } return null; } public List getTSDCurricularCourses(CompetenceCourse course, ExecutionSemester executionSemester) { List tsdCoursesList = new ArrayList(); for (TSDCourse tsdCourse : getTSDCoursesByCompetenceCourse(course)) { if (tsdCourse instanceof TSDCurricularCourse && tsdCourse.getExecutionPeriod().equals(executionSemester)) tsdCoursesList.add((TSDCurricularCourse) tsdCourse); } return tsdCoursesList; } public List getTSDCurricularCourseGroups(CompetenceCourse course, ExecutionSemester executionSemester) { List tsdCoursesList = new ArrayList(); for (TSDCourse tsdCourse : getTSDCoursesByCompetenceCourse(course)) { if (tsdCourse instanceof TSDCurricularCourseGroup && tsdCourse.getExecutionPeriod().equals(executionSemester)) tsdCoursesList.add((TSDCurricularCourseGroup) tsdCourse); } return tsdCoursesList; } }