package net.sourceforge.fenixedu.domain; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; import java.util.UUID; import net.sourceforge.fenixedu.dataTransferObject.alumni.AlumniSearchBean; import net.sourceforge.fenixedu.domain.contacts.EmailAddress; import net.sourceforge.fenixedu.domain.contacts.MobilePhone; import net.sourceforge.fenixedu.domain.contacts.PartyContact; import net.sourceforge.fenixedu.domain.contacts.PartyContactType; import net.sourceforge.fenixedu.domain.contacts.Phone; import net.sourceforge.fenixedu.domain.contacts.PhysicalAddress; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.domain.student.Registration; import net.sourceforge.fenixedu.domain.student.Student; import org.apache.commons.lang.StringUtils; import com.linkare.commons.metainfo.Linkare; public class Alumni extends Alumni_Base { public Alumni(Student student) { this(student, null, Boolean.FALSE); } public Alumni(Student student, UUID uuid) { this(student, uuid, Boolean.FALSE); } public Alumni(Student student, UUID uuid, Boolean registered) { super(); if (student == null) { throw new DomainException("alumni.creation.student.null"); } setStudent(student); if (uuid != null) { setUrlRequestToken(uuid); } setRegistered(registered); setRootDomainObject(RootDomainObject.getInstance()); } public Alumni readByStudentNumber(Integer studentNumber) { return Student.readStudentByNumber(studentNumber).getAlumni(); } public Boolean hasDocumentIdNumber(String documentIdNumber) { return getDocumentIdNumber().equals(documentIdNumber); } public Boolean hasStudentNumber(Integer studentNumber) { return getStudentNumber().equals(studentNumber); } public static Alumni readAlumniByStudentNumber(Integer studentNumber) { for (Alumni alumni : RootDomainObject.getInstance().getAlumnis()) { if (alumni.getStudent().getNumber().equals(studentNumber)) { return alumni; } } return null; } public Integer getStudentNumber() { return getStudent().getNumber(); } public String getDocumentIdNumber() { return getStudent().getPerson().getDocumentIdNumber(); } public MobilePhone getPersonalMobilePhone() { for (MobilePhone phone : getStudent().getPerson().getMobilePhones()) { if (phone.isPersonalType()) { return phone; } } return null; } public Boolean hasPersonalMobilePhone() { return getPersonalMobilePhone() != null; } public EmailAddress getPersonalEmail() { for (EmailAddress email : getStudent().getPerson().getEmailAddresses()) { if (email.isPersonalType()) { return email; } } return null; } public Boolean hasPersonalEmail() { return getPersonalEmail() != null; } public Phone getPersonalPhone() { for (Phone phone : getStudent().getPerson().getPhones()) { if (phone.isPersonalType()) { return phone; } } return null; } public Boolean hasPersonalPhone() { return getPersonalPhone() != null; } public PhysicalAddress getLastPersonalAddress() { SortedSet addressSet = new TreeSet(PartyContact.COMPARATOR_BY_ID); addressSet.addAll(getStudent().getPerson().getPhysicalAddresses()); return addressSet.last() != null ? addressSet.last() : null; } public PhysicalAddress getPersonalAddress() { if (getStudent().getPerson().hasDefaultPhysicalAddress()) { return getStudent().getPerson().getDefaultPhysicalAddress(); } return null; } public Boolean hasAnyJobs() { return getStudent().getPerson().hasAnyJobs(); } public List getJobs() { ArrayList jobs = new ArrayList(getStudent().getPerson().getJobs()); Collections.sort(jobs, Job.REVERSE_COMPARATOR_BY_BEGIN_DATE); return jobs; } public Job getLastJob() { List jobs = getJobs(); return jobs.get(jobs.size() - 1); } @Linkare(author = "Paulo Zenida", comments = "Decreased code size") public Boolean canChangePersonalAddress() { Person p = getStudent().getPerson(); return !p.hasRole(RoleType.EMPLOYEE) && !p.hasRole(RoleType.TEACHER) && !p.hasRole(RoleType.STUDENT); } public List getFormations() { ArrayList formations = new ArrayList(getStudent().getPerson().getFormations()); Collections.sort(formations, Formation.COMPARATOR_BY_BEGIN_YEAR); return formations; } public PhysicalAddress getUpdatableAddress() { List partyContacts = getStudent().getPerson().getPartyContacts(PhysicalAddress.class); for (int i = 0; i < partyContacts.size(); i++) { PhysicalAddress address = (PhysicalAddress) partyContacts.get(i); if (!address.isDefault()) return address; } return null; } public PartyContact getUpdatablePartyContact(final Class clazz) { List partyContacts = getStudent().getPerson().getPartyContacts(clazz); for (int i = 0; i < partyContacts.size(); i++) { if (!partyContacts.get(i).isDefault()) return partyContacts.get(i); } return null; } public EmailAddress getUpdatableMobilePhone() { List partyContacts = getStudent().getPerson().getPartyContacts(EmailAddress.class); for (int i = 0; i < partyContacts.size(); i++) { EmailAddress email = (EmailAddress) partyContacts.get(i); if (!email.isDefault()) return email; } return null; } public static List readAlumniRegistrations(AlumniSearchBean bean) { if (StringUtils.isEmpty(bean.getName())) { return new ArrayList(); } ExecutionYear firstYear = bean.getFirstExecutionYear() == null ? ExecutionYear.readFirstExecutionYear() : bean .getFirstExecutionYear(); ExecutionYear finalYear = bean.getFinalExecutionYear() == null ? ExecutionYear.readLastExecutionYear() : bean .getFinalExecutionYear(); List resultRegistrations = new ArrayList(); for (Person person : Person.readPersonsByNameAndRoleType(bean.getName(), RoleType.ALUMNI)) { if (person.hasStudent()) { if (bean.getStudentNumber() == null || person.getStudent().getNumber().equals(bean.getStudentNumber())) { for (Registration registration : (bean.getDegreeType() == null ? person.getStudent().getRegistrations() : person.getStudent().getRegistrationsByDegreeType(bean.getDegreeType()))) { if (registration.isConcluded()) { if (bean.getDegree() != null) { if (registration.hasStartedBetween(firstYear, finalYear) && registration.getDegree().equals(bean.getDegree())) { resultRegistrations.add(registration); } } else { if (registration.hasStartedBetween(firstYear, finalYear)) { resultRegistrations.add(registration); } } } } } } } return resultRegistrations; } public static List readRegistrations(String personName, Integer studentNumber, String documentIdNumber) { List resultRegistrations = new ArrayList(); if (!StringUtils.isEmpty(personName)) { for (Person person : Person.readPersonsByNameAndRoleType(personName, RoleType.ALUMNI)) { if (matchStudentNumber(person, studentNumber) && matchDocumentIdNumber(person, documentIdNumber)) { for (Registration registration : person.getStudent().getRegistrations()) { if (registration.isConcluded()) { resultRegistrations.add(registration); } } } } } else { if (studentNumber != null) { Student student = Student.readStudentByNumber(studentNumber); if (student != null && matchDocumentIdNumber(student.getPerson(), documentIdNumber)) { for (Registration registration : student.getRegistrations()) { if (registration.isConcluded()) { resultRegistrations.add(registration); } } } } else if (documentIdNumber != null) { Collection persons = Person.readByDocumentIdNumber(documentIdNumber); if (!persons.isEmpty()) { Person person = persons.iterator().next(); if (matchStudentNumber(person, studentNumber)) { for (Registration registration : person.getStudent().getRegistrations()) { if (registration.isConcluded()) { resultRegistrations.add(registration); } } } } } } return resultRegistrations; } private static boolean matchDocumentIdNumber(Person person, String documentIdNumber) { return StringUtils.isEmpty(documentIdNumber) || (!StringUtils.isEmpty(person.getDocumentIdNumber()) && person.getDocumentIdNumber().equals(documentIdNumber)); } private static boolean matchStudentNumber(Person person, Integer studentNumber) { return studentNumber == null || (person.hasStudent() && person.getStudent().getNumber().equals(studentNumber)); } public Boolean hasEmailAddress(String emailAddress) { for (EmailAddress personEmail : (List) getStudent().getPerson().getPartyContacts(EmailAddress.class)) { if (personEmail.getValue().equals(emailAddress)) { return Boolean.TRUE; } } return Boolean.FALSE; } public void addIfNotExistsEmail(String email) { EmailAddress.createEmailAddress(getStudent().getPerson(), email, PartyContactType.PERSONAL, false); } public boolean isRegistered() { return getRegistered(); } public boolean hasPastLogin() { return getStudent().getPerson().getUser().getLastLoginDateTimeDateTime() != null; } public boolean hasAnyPendingIdentityRequests() { for (AlumniIdentityCheckRequest request : getIdentityRequests()) { if (request.getApproved() == null) { return true; } } return false; } public String getLoginUsername() { Person person = getStudent().getPerson(); if (person.getIstUsername() == null) { return person.getLoginAlias().iterator().next().getAlias(); } return person.getIstUsername(); } public boolean hasStartedPublicRegistry() { return getUrlRequestToken() != null; } public boolean hasFinishedPublicRegistry() { return hasStartedPublicRegistry() && isRegistered(); } public String getName() { return getStudent().getPerson().getName(); } }