package net.sourceforge.fenixedu.domain.vigilancy; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.ExecutionCourse; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.Teacher; import net.sourceforge.fenixedu.domain.WrittenEvaluation; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.vigilancy.strategies.Strategy; import net.sourceforge.fenixedu.domain.vigilancy.strategies.StrategyFactory; import net.sourceforge.fenixedu.domain.vigilancy.strategies.StrategySugestion; import org.joda.time.DateTime; import pt.ist.fenixWebFramework.services.Service; public class VigilantGroup extends VigilantGroup_Base { public VigilantGroup() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public double getPointsAverage() { double sumOfPoints = 0; for (VigilantWrapper v : this.getVigilantWrappersThatCanBeConvoked()) { sumOfPoints += v.getPoints(); } return ((this.getVigilantWrappersThatCanBeConvoked().isEmpty()) ? 0 : sumOfPoints / this.getVigilantWrappersThatCanBeConvoked().size()); } public VigilantWrapper hasPerson(Person person) { for (VigilantWrapper vigilantWrapper : getVigilantWrappers()) { if (vigilantWrapper.getPerson().equals(person)) { return vigilantWrapper; } } return null; } public StrategySugestion sugestVigilantsToConvoke(WrittenEvaluation writtenEvaluation) { String strategyName = this.getConvokeStrategy(); StrategyFactory factory = StrategyFactory.getInstance(); Strategy strategy = factory.getStrategy(strategyName); List possibleVigilants = new ArrayList(this.getVigilantWrappersThatCanBeConvoked()); possibleVigilants.addAll(findTeachersThatAreInGroupFor(writtenEvaluation.getAssociatedExecutionCourses())); return (strategy != null) ? strategy.sugest(possibleVigilants, writtenEvaluation) : null; } private List findTeachersThatAreInGroupFor(List executionCourses) { List teachers = new ArrayList(); for (VigilantWrapper vigilantWrapper : this.getVigilantWrappersThatCantBeConvoked()) { Teacher teacher = vigilantWrapper.getTeacher(); if (teacher != null && teacher.teachesAny(executionCourses, this.getExecutionYear())) { teachers.add(vigilantWrapper); } } return teachers; } public void convokeVigilants(List vigilants, WrittenEvaluation writtenEvaluation) { for (VigilantWrapper vigilant : vigilants) { if (!vigilant.hasBeenConvokedForEvaluation(writtenEvaluation)) { Teacher teacher = vigilant.getTeacher(); if (teacher != null && teacher.teachesAny(writtenEvaluation.getAssociatedExecutionCourses())) { vigilant.addVigilancies(new OwnCourseVigilancy(writtenEvaluation)); } else { vigilant.addVigilancies(new OtherCourseVigilancy(writtenEvaluation)); } } else { Vigilancy vigilancy = vigilant.getVigilancyFor(writtenEvaluation); if (!vigilancy.isActive()) { vigilancy.setActive(true); } } } } public List getVigilants() { return this.getVigilantWrappers(); } public int getVigilantsCount() { return this.getVigilants().size(); } public List getVigilantWrappersThatCanBeConvoked() { ArrayList vigilantWrappersThatCanBeConvoked = new ArrayList(); for (VigilantWrapper vigilantWrapper : this.getVigilantWrappers()) { if (vigilantWrapper.getConvokable()) { vigilantWrappersThatCanBeConvoked.add(vigilantWrapper); } } return vigilantWrappersThatCanBeConvoked; } public List getVigilantWrappersThatCantBeConvoked() { ArrayList vigilantWrappersThatCantBeConvoked = new ArrayList(); for (VigilantWrapper vigilantWrapper : this.getVigilantWrappers()) { if (!vigilantWrapper.getConvokable()) { vigilantWrappersThatCantBeConvoked.add(vigilantWrapper); } } return vigilantWrappersThatCantBeConvoked; } public List getPersons() { List persons = new ArrayList(); List vigilantWrappers = this.getVigilantWrappers(); for (VigilantWrapper vigilantWrapper : vigilantWrappers) { persons.add(vigilantWrapper.getPerson()); } return persons; } public List getVigilancies() { List vigilancies = new ArrayList(); for (ExecutionCourse course : this.getExecutionCourses()) { for (WrittenEvaluation evaluation : course.getWrittenEvaluations()) { vigilancies.addAll(evaluation.getVigilancies()); } } return vigilancies; } public List getConvokesAfterDate(DateTime date) { List convokesToReturn = new ArrayList(); for (Vigilancy convoke : this.getVigilancies()) { if (convoke.getBeginDateTime().isAfter(date) || convoke.getBeginDateTime().isEqual(date)) { convokesToReturn.add(convoke); } } return convokesToReturn; } public List getConvokesBeforeDate(DateTime date) { List convokesToReturn = new ArrayList(); for (Vigilancy convoke : this.getVigilancies()) { if (convoke.getBeginDateTime().isBefore(date)) { convokesToReturn.add(convoke); } } return convokesToReturn; } public List getVigilancies(VigilantWrapper vigilantWrapper) { List vigilantConvokes = new ArrayList(); for (Vigilancy convoke : this.getVigilancies()) { if (vigilantWrapper == convoke.getVigilantWrapper()) { vigilantConvokes.add(convoke); } } return vigilantConvokes; } public List getUnavailablePeriodsOfVigilantsInGroup() { List unavailablePeriods = new ArrayList(); for (VigilantWrapper vigilantWrapper : this.getVigilantWrappers()) { unavailablePeriods.addAll(vigilantWrapper.getUnavailablePeriods()); } return unavailablePeriods; } public boolean canSpecifyUnavailablePeriodIn(DateTime date) { return (date.isAfter(this.getBeginOfFirstPeriodForUnavailablePeriods()) && date.isBefore(this.getEndOfFirstPeriodForUnavailablePeriods()) || (date.isAfter(this .getBeginOfSecondPeriodForUnavailablePeriods()) && date .isBefore(this.getEndOfSecondPeriodForUnavailablePeriods()))); } public List getVigilantWrappersWithIncompatiblePerson() { List vigilantWrappers = new ArrayList(); for (VigilantWrapper vigilantWrapper : this.getVigilantWrappers()) { Person incompatiblePerson = vigilantWrapper.getPerson().getIncompatibleVigilantPerson(); if (incompatiblePerson != null && !vigilantWrappers.contains(incompatiblePerson.getVigilantWrappers())) { vigilantWrappers.add(vigilantWrapper); } } return vigilantWrappers; } @Override public void addExecutionCourses(ExecutionCourse course) { if (course.hasVigilantGroup()) { throw new DomainException("vigilancy.error.executionCourseAlreadyInAGroup"); } else { super.addExecutionCourses(course); } } public void delete() { removeExecutionYear(); getExecutionCourses().clear(); getExamCoordinators().clear(); removeUnit(); removeRootDomainObject(); for (VigilantWrapper vigilant : this.getVigilantWrappers()) { List vigilancies = vigilant.getVigilancies(); for (Vigilancy vigilancy : vigilancies) { if (vigilancy.isActive()) { throw new DomainException("label.vigilancy.error.cannotDeleteGroupWithVigilants"); } } removeVigilantWrappers(vigilant); } super.deleteDomainObject(); } public List getAllAssociatedWrittenEvaluations() { List courses = this.getExecutionCourses(); Set evaluations = new HashSet(); for (ExecutionCourse course : courses) { evaluations.addAll(course.getWrittenEvaluations()); } return new ArrayList(evaluations); } public List getWrittenEvaluationsAfterDate(DateTime date) { List convokes = this.getConvokesAfterDate(date); return getEvaluationsFromConvokes(convokes); } public List getWrittenEvaluationsBeforeDate(DateTime date) { List convokes = this.getConvokesBeforeDate(date); return getEvaluationsFromConvokes(convokes); } private List getEvaluationsFromConvokes(List convokes) { Set evaluations = new HashSet(); for (Vigilancy convoke : convokes) { evaluations.add(convoke.getWrittenEvaluation()); } return new ArrayList(evaluations); } @Service public void copyPointsFromVigilantGroup(VigilantGroup previousGroup) { this.setPointsForTeacher(previousGroup.getPointsForTeacher()); this.setPointsForConvoked(previousGroup.getPointsForConvoked()); this.setPointsForDisconvoked(previousGroup.getPointsForDisconvoked()); this.setPointsForDismissed(previousGroup.getPointsForDismissed()); this.setPointsForDismissedTeacher(previousGroup.getPointsForDismissedTeacher()); this.setPointsForMissing(previousGroup.getPointsForMissing()); this.setPointsForMissingTeacher(previousGroup.getPointsForMissingTeacher()); } public VigilantWrapper getVigilantWrapperFor(Person person) { for (VigilantWrapper wrapper : getVigilantWrappers()) { if (wrapper.getPerson() == person) { return wrapper; } } return null; } }