package net.sourceforge.fenixedu.domain; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; import net.sourceforge.fenixedu.util.LanguageUtils; import net.sourceforge.fenixedu.util.StringUtils; import pt.utl.ist.fenix.tools.util.StringNormalizer; public class FutureTeacher extends FutureTeacher_Base implements ITeacher { public static final Comparator FUTURE_TEACHER_COMPARATOR_BY_NAME = new Comparator() { public int compare(FutureTeacher futureTeacher1, FutureTeacher futureTeacher2) { return futureTeacher1.getName().compareTo(futureTeacher2.getName()); } }; public static final Comparator FUTURE_TEACHER_COMPARATOR_BY_NUMBER = new Comparator() { public int compare(FutureTeacher futureTeacher1, FutureTeacher futureTeacher2) { return futureTeacher1.getTeacherNumber().compareTo(futureTeacher2.getTeacherNumber()); } }; public FutureTeacher() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public FutureTeacher(String teacherName) { this(); setName(teacherName); } public FutureTeacher(final Integer teacherNumber, final String teacherName) { this(teacherName); setTeacherNumber(teacherNumber); } public static boolean existsTeacherWithNumber(final Integer number) { for (final FutureTeacher teacher : getAllTeachers()) { if (teacher.getTeacherNumber().equals(number)) { return true; } } return false; } public static List getAllTeachers() { return RootDomainObject.getInstance().getFutureTeachers(); } public void delete() { removeRootDomainObject(); removeTeacher(); removeDepartment(); deleteDomainObject(); } public Boolean getIsAssociatedToTeacher() { return hasTeacher(); } public static FutureTeacher getByNumber(Integer number) { for (FutureTeacher ft : getAllTeachers()) { if (ft.getTeacherNumber().equals(number)) { return ft; } } return null; } public static List getByName(String teacherName) { List result = new ArrayList(); for (FutureTeacher ft : getAllTeachers()) { verifyContainsName(teacherName, result, ft); } return result.size() > 0 ? result : null; } public static List getByNameAndDepartment(String teacherName, Integer teacherDepartmentId) { List result = new ArrayList(); for (FutureTeacher ft : getAllTeachers()) { if (ft.getDepartment().getIdInternal().equals(teacherDepartmentId)) { verifyContainsName(teacherName, result, ft); } } return result.size() > 0 ? result : null; } public static List getByDepartment(Integer teacherDepartmentId) { List result = new ArrayList(); for (FutureTeacher ft : getAllTeachers()) { if (ft.getDepartment().getIdInternal().equals(teacherDepartmentId)) { result.add(ft); } } return result.size() > 0 ? result : null; } private static void verifyContainsName(final String teacherName, List result, final FutureTeacher ft) { final String nameToVerify = StringNormalizer.normalize(ft.getName()); for (String part : StringUtils.getStrings(teacherName)) { if (!nameToVerify.contains(part)) { return; } } result.add(ft); } public String getNumberAndName() { return "(" + super.getTeacherNumber() + ") " + super.getName(); } /** * Validates if teacher is valid for listing in professorship creation. This * teacher is valid if: has no associated real teacher has associated real * teacher and its creation date is between the begining and the end of the * previous execution year * * @param executionYear * @return */ public boolean isValidForEdition(ExecutionYear executionYear) { if (executionYear == null) { return true; } else if (this.hasTeacher()) { ExecutionYear previous = executionYear.getPreviousExecutionYear(); if (previous != null && this.getCreationEntityDate().isBefore(previous.getEndDateYearMonthDay())) { return true; } else { return false; } } else { return true; } } public String getDepartmentAcronym() { return getDepartment().getAcronym(); } public String getTeacherName() { return getName(); } public String getTeacherTypeAbbreviation() { return "[F] "; } public Teacher getRealTeacher() { return getTeacher(); } public String getTeacherType() { return "(" + ResourceBundle.getBundle("resources.SoftplanningResources", LanguageUtils.getLocale()).getString( "label.futureTeacher") + ")"; } public boolean isExternal() { return false; } public static int getNextTeacherNumber() { final FutureTeacher minNumberTeacher = Collections.min(RootDomainObject.getInstance().getFutureTeachers(), FutureTeacher.FUTURE_TEACHER_COMPARATOR_BY_NUMBER); return minNumberTeacher == null ? -1 : minNumberTeacher.getTeacherNumber() - 1; } }