/* * Created on 08/Mar/2005 * */ package net.sourceforge.fenixedu.domain; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.student.Registration; import net.sourceforge.fenixedu.util.EnrolmentGroupPolicyType; import net.sourceforge.fenixedu.util.ProposalState; import com.linkare.commons.metainfo.Linkare; /** * @author joaosa & rmalo * */ public class Grouping extends Grouping_Base { public static Comparator COMPARATOR_BY_ENROLMENT_BEGIN_DATE = new Comparator() { @Override public int compare(Grouping g1, Grouping g2) { return g1.getEnrolmentBeginDayDateDateTime().compareTo(g2.getEnrolmentBeginDayDateDateTime()); } }; public Grouping() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public Calendar getEnrolmentBeginDay() { if (this.getEnrolmentBeginDayDate() != null) { Calendar result = Calendar.getInstance(); result.setTime(this.getEnrolmentBeginDayDate()); return result; } return null; } public void setEnrolmentBeginDay(Calendar enrolmentBeginDay) { if (enrolmentBeginDay != null) { this.setEnrolmentBeginDayDate(enrolmentBeginDay.getTime()); } else { this.setEnrolmentBeginDayDate(null); } } public Calendar getEnrolmentEndDay() { if (this.getEnrolmentEndDayDate() != null) { Calendar result = Calendar.getInstance(); result.setTime(this.getEnrolmentEndDayDate()); return result; } return null; } public void setEnrolmentEndDay(Calendar enrolmentEndDay) { if (enrolmentEndDay != null) { this.setEnrolmentEndDayDate(enrolmentEndDay.getTime()); } else { this.setEnrolmentEndDayDate(null); } } public List getExecutionCourses() { final List result = new ArrayList(); for (final ExportGrouping exportGrouping : this.getExportGroupings()) { if (exportGrouping.getProposalState().getState() == ProposalState.ACEITE || exportGrouping.getProposalState().getState() == ProposalState.CRIADOR) { result.add(exportGrouping.getExecutionCourse()); } } return result; } public List getStudentGroupsWithoutShift() { final List result = new ArrayList(); for (final StudentGroup studentGroup : this.getStudentGroups()) { if (studentGroup.getShift() == null) { result.add(studentGroup); } } return result; } public List getStudentGroupsWithShift() { final List result = new ArrayList(); for (final StudentGroup studentGroup : this.getStudentGroups()) { if (studentGroup.getShift() != null) { result.add(studentGroup); } } return result; } public Integer getNumberOfStudentsNotInGrouping() { int numberOfStudents = 0; for (final ExportGrouping exportGrouping : this.getExportGroupings()) { if (exportGrouping.getProposalState().getState() == ProposalState.ACEITE || exportGrouping.getProposalState().getState() == ProposalState.CRIADOR) { for (final Attends attend : exportGrouping.getExecutionCourse().getAttends()) { if (!this.getAttends().contains(attend)) { numberOfStudents++; } } } } return Integer.valueOf(numberOfStudents); } public void checkShiftCapacity(Shift shift) { List shiftStudentGroups = this.readAllStudentGroupsBy(shift); Integer groupMaximumNumber = this.getGroupMaximumNumber(); if (shiftStudentGroups != null && groupMaximumNumber != null && shiftStudentGroups.size() == groupMaximumNumber) throw new DomainException(this.getClass().getName(), "error.shift.with.max.number.of.studentGroups"); } public Integer getNumberOfStudentsInGrouping() { return this.getAttends().size(); } public Attends getStudentAttend(final Registration registration) { for (final Attends attend : this.getAttends()) { if (attend.getRegistration().getStudent() == registration.getStudent()) { return attend; } } return null; } public Attends getStudentAttend(String studentUsername) { for (final Attends attend : this.getAttends()) { if (attend.getRegistration().getPerson().hasUsername(studentUsername)) { return attend; } } return null; } public StudentGroup readStudentGroupBy(Integer studentGroupNumber) { for (final StudentGroup studentGroup : this.getStudentGroups()) { if (studentGroup.getGroupNumber().equals(studentGroupNumber)) { return studentGroup; } } return null; } public List readAllStudentGroupsBy(Shift shift) { final List result = new ArrayList(); for (final StudentGroup studentGroup : this.getStudentGroups()) { if (studentGroup.getShift() == shift) { result.add(studentGroup); } } return result; } public static Grouping create(String goupingName, Date enrolmentBeginDay, Date enrolmentEndDay, EnrolmentGroupPolicyType enrolmentGroupPolicyType, Integer groupMaximumNumber, Integer idealCapacity, Integer maximumCapacity, Integer minimumCapacity, String projectDescription, ShiftType shiftType, ExecutionCourse executionCourse) { if (goupingName == null || enrolmentBeginDay == null || enrolmentEndDay == null || enrolmentGroupPolicyType == null) { throw new NullPointerException(); } checkIfGroupingAlreadyExistInExecutionCourse(goupingName, executionCourse); Grouping grouping = new Grouping(); grouping.setName(goupingName); grouping.setEnrolmentBeginDayDate(enrolmentBeginDay); grouping.setEnrolmentEndDayDate(enrolmentEndDay); grouping.setEnrolmentPolicy(enrolmentGroupPolicyType); grouping.setGroupMaximumNumber(groupMaximumNumber); grouping.setIdealCapacity(idealCapacity); grouping.setMaximumCapacity(maximumCapacity); grouping.setMinimumCapacity(minimumCapacity); grouping.setProjectDescription(projectDescription); grouping.setShiftType(shiftType); ExportGrouping exportGrouping = new ExportGrouping(grouping, executionCourse); exportGrouping.setProposalState(new ProposalState(ProposalState.CRIADOR)); addGroupingToAttends(grouping, executionCourse.getAttends()); return grouping; } private static void addGroupingToAttends(final Grouping grouping, final List attends) { for (final Attends attend : attends) { attend.addGroupings(grouping); } } private static void checkIfGroupingAlreadyExistInExecutionCourse(final String goupingName, final ExecutionCourse executionCourse) { if (executionCourse.getGroupingByName(goupingName) != null) { throw new DomainException("error.exception.existing.groupProperties"); } } public void edit(String goupingName, Date enrolmentBeginDay, Date enrolmentEndDay, EnrolmentGroupPolicyType enrolmentGroupPolicyType, Integer groupMaximumNumber, Integer idealCapacity, Integer maximumCapacity, Integer minimumCapacity, String projectDescription, ShiftType shiftType) { if (goupingName == null || enrolmentBeginDay == null || enrolmentEndDay == null || enrolmentGroupPolicyType == null) { throw new NullPointerException(); } checkIfGroupingAlreadyExists(goupingName); setName(goupingName); setEnrolmentBeginDayDate(enrolmentBeginDay); setEnrolmentEndDayDate(enrolmentEndDay); setEnrolmentPolicy(enrolmentGroupPolicyType); setGroupMaximumNumber(groupMaximumNumber); setIdealCapacity(idealCapacity); setMaximumCapacity(maximumCapacity); setMinimumCapacity(minimumCapacity); setProjectDescription(projectDescription); setShiftType(shiftType); if (shiftType == null) { unEnrollStudentGroups(this.getStudentGroups()); } } private void checkIfGroupingAlreadyExists(String groupingName) { if (!this.getName().equals(groupingName)) { for (final ExecutionCourse executionCourse : this.getExecutionCourses()) { if (executionCourse.getGroupingByName(groupingName) != null) { throw new DomainException(this.getClass().getName(), "error.exception.existing.groupProperties"); } } } } private void unEnrollStudentGroups(List studentGroups) { for (final StudentGroup studentGroup : studentGroups) { studentGroup.setShift(null); } } public void createStudentGroup(Shift shift, Integer groupNumber, List students) { if (groupNumber == null || students == null) throw new NullPointerException(); if (readStudentGroupBy(groupNumber) != null) { throw new DomainException(this.getClass().getName(), "error.invalidGroupNumber"); } checkForStudentsInStudentGroupsAndGrouping(students); StudentGroup newStudentGroup = null; if (shift != null) newStudentGroup = new StudentGroup(groupNumber, this, shift); else newStudentGroup = new StudentGroup(groupNumber, this); for (Registration registration : students) { Attends attend = getStudentAttend(registration); newStudentGroup.addAttends(attend); } } private void checkForStudentsInStudentGroupsAndGrouping(List students) { for (Registration registration : students) { Attends attend = getStudentAttend(registration); for (final StudentGroup studentGroup : this.getStudentGroups()) { if (studentGroup.getAttends().contains(attend)) throw new DomainException(this.getClass().getName(), "errors.existing.studentEnrolment"); else if (!this.getAttends().contains(attend)) throw new DomainException(this.getClass().getName(), "errors.notExisting.studentInGrouping"); } } } public void delete() { if (!super.getStudentGroups().isEmpty()) { throw new DomainException(this.getClass().getName(), ""); } List attends = this.getAttends(); List attendsAux = new ArrayList(); attendsAux.addAll(attends); for (Attends attend : attendsAux) { attend.removeGroupings(this); } List exportGroupings = this.getExportGroupings(); List exportGroupingsAux = new ArrayList(); exportGroupingsAux.addAll(exportGroupings); for (ExportGrouping exportGrouping : exportGroupingsAux) { ExecutionCourse executionCourse = exportGrouping.getExecutionCourse(); executionCourse.removeExportGroupings(exportGrouping); exportGrouping.delete(); } removeRootDomainObject(); super.deleteDomainObject(); } public int findMaxGroupNumber() { int max = 0; for (final StudentGroup studentGroup : super.getStudentGroups()) { max = Math.max(max, studentGroup.getGroupNumber().intValue()); } return max; } public ExportGrouping getExportGrouping(final ExecutionCourse executionCourse) { for (final ExportGrouping exportGrouping : this.getExportGroupingsSet()) { if (exportGrouping.getExecutionCourse() == executionCourse) { return exportGrouping; } } return null; } public boolean hasExportGrouping(final ExecutionCourse executionCourse) { return getExportGrouping(executionCourse) != null; } public StudentGroup getStudentGroupByAttends(Attends attends) { for (StudentGroup studentGroup : getStudentGroups()) { if (studentGroup.getAttends().contains(attends)) { return studentGroup; } } return null; } public Map> getStudentGroupsIndexedByShift() { final Map> map = new TreeMap>( Shift.SHIFT_COMPARATOR_BY_TYPE_AND_ORDERED_LESSONS); for (final StudentGroup studentGroup : getStudentGroups()) { if (studentGroup.hasShift()) { final Shift shift = studentGroup.getShift(); final SortedSet studentGroups; if (map.containsKey(shift)) { studentGroups = map.get(shift); } else { studentGroups = new TreeSet(StudentGroup.COMPARATOR_BY_GROUP_NUMBER); map.put(shift, studentGroups); } studentGroups.add(studentGroup); } } return map; } public SortedSet getStudentGroupsOrderedByGroupNumber() { final SortedSet studentGroups = new TreeSet(StudentGroup.COMPARATOR_BY_GROUP_NUMBER); studentGroups.addAll(getStudentGroups()); return studentGroups; } public boolean isPersonTeacher(Person person) { for (ExecutionCourse ec : getExecutionCourses()) { for (Professorship professorship : ec.getProfessorships()) { if (professorship.getPerson() == person) { return true; } } } return false; } @Override public List getStudentGroups() { List result = new ArrayList(); for (StudentGroup sg : super.getStudentGroups()) { if (!sg.wasDeleted()) { result.add(sg); } } return Collections.unmodifiableList(result); } public List getDeletedStudentGroups() { List result = new ArrayList(); for (StudentGroup sg : super.getStudentGroups()) { if (!sg.getValid()) { result.add(sg); } } return result; } @Override public int getStudentGroupsCount() { return this.getStudentGroups().size(); } @Linkare(author = "Paulo Zenida", comments = "Removed the TODO statement") @Override public Iterator getStudentGroupsIterator() { return this.getStudentGroups().iterator(); } @Linkare(author = "Paulo Zenida", comments = "Removed the TODO statement") @Override public Set getStudentGroupsSet() { return new TreeSet(this.getStudentGroups()); } @Linkare(author = "Paulo Zenida", comments = "Removed the TODO statement") @Override public boolean hasStudentGroups(StudentGroup studentGroups) { return this.getStudentGroups().contains(studentGroups); } }