package net.sourceforge.fenixedu.domain.serviceRequests.documentRequests; import java.util.Collection; import java.util.HashSet; import net.sourceforge.fenixedu.dataTransferObject.serviceRequests.AcademicServiceRequestBean; import net.sourceforge.fenixedu.dataTransferObject.serviceRequests.DocumentRequestCreateBean; import net.sourceforge.fenixedu.domain.Enrolment; import net.sourceforge.fenixedu.domain.IEnrolment; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.accounting.EventType; import net.sourceforge.fenixedu.domain.degree.DegreeType; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.student.MobilityProgram; import net.sourceforge.fenixedu.domain.student.Registration; import net.sourceforge.fenixedu.domain.student.curriculum.ICurriculum; import net.sourceforge.fenixedu.domain.student.curriculum.ICurriculumEntry; import net.sourceforge.fenixedu.domain.studentCurriculum.CurriculumLine; import net.sourceforge.fenixedu.domain.studentCurriculum.CycleCurriculumGroup; import net.sourceforge.fenixedu.domain.studentCurriculum.Dismissal; import net.sourceforge.fenixedu.domain.studentCurriculum.ExternalCurriculumGroup; import net.sourceforge.fenixedu.domain.studentCurriculum.ExternalEnrolment; import net.sourceforge.fenixedu.domain.studentCurriculum.InternalEnrolmentWrapper; import net.sourceforge.fenixedu.injectionCode.AccessControl; import org.joda.time.DateTime; import com.linkare.commons.metainfo.Linkare; public class ApprovementCertificateRequest extends ApprovementCertificateRequest_Base { protected ApprovementCertificateRequest() { super(); } public ApprovementCertificateRequest(final DocumentRequestCreateBean bean) { this(); super.init(bean); checkParameters(bean); super.setMobilityProgram(bean.getMobilityProgram()); super.setIgnoreExternalEntries(bean.isIgnoreExternalEntries()); if (isEmployee()) { // TODO: remove this after DEA diplomas and certificates if (!isDEARegistration()) { if (getRegistration().isConcluded()) { throw new DomainException("ApprovementCertificateRequest.registration.is.concluded"); } if (getRegistration().isRegistrationConclusionProcessed()) { throw new DomainException("ApprovementCertificateRequest.registration.has.conclusion.processed"); } } if (getEntriesToReport(isDEARegistration()).isEmpty()) { throw new DomainException("ApprovementCertificateRequest.registration.without.approvements"); } } } // TODO: remove this after DEA diplomas and certificates private boolean isDEARegistration() { return getRegistration().getDegreeType() == DegreeType.BOLONHA_ADVANCED_SPECIALIZATION_DIPLOMA; } private boolean isEmployee() { final Person person = AccessControl.getPerson(); return person != null && person.hasEmployee(); } @Override final protected void checkParameters(final DocumentRequestCreateBean bean) { if (bean.getMobilityProgram() != null && bean.isIgnoreExternalEntries()) { throw new DomainException("ApprovementCertificateRequest.cannot.ignore.external.entries.within.a.mobility.program"); } } @Linkare(author = "Paulo Zenida", comments = "Delegated the invocation of getYears() on degree") @Override protected void internalChangeState(AcademicServiceRequestBean academicServiceRequestBean) { super.internalChangeState(academicServiceRequestBean); if (academicServiceRequestBean.isToProcess()) { // TODO: remove this after DEA diplomas and certificate if (!isDEARegistration()) { if (getRegistration().isConcluded()) { throw new DomainException("ApprovementCertificateRequest.registration.is.concluded"); } if (getRegistration().isRegistrationConclusionProcessed()) { throw new DomainException("ApprovementCertificateRequest.registration.has.conclusion.processed"); } } if (getEntriesToReport(isDEARegistration()).isEmpty()) { throw new DomainException("ApprovementCertificateRequest.registration.without.approvements"); } // FIXME For now, the following conditions are only valid for 5year // degrees if (getRegistration().getDegree().getYears() == 5 && getDocumentPurposeType() == DocumentPurposeType.PROFESSIONAL) { int curricularYear = getRegistration().getCurricularYear(); if (curricularYear <= 3) { throw new DomainException("ApprovementCertificateRequest.registration.hasnt.finished.third.year"); } } } if (academicServiceRequestBean.isToConclude()) { super.setNumberOfUnits(calculateNumberOfUnits()); } } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") @Override public final DocumentRequestType getDocumentRequestType() { return DocumentRequestType.APPROVEMENT_CERTIFICATE; } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") @Override public final String getDocumentTemplateKey() { return getClass().getName(); } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") @Override public final EventType getEventType() { return EventType.APPROVEMENT_CERTIFICATE_REQUEST; } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") @Override public final Integer getNumberOfUnits() { final Integer res = super.getNumberOfUnits(); return res == null ? calculateNumberOfUnits() : res; } private int calculateNumberOfUnits() { return getEntriesToReport(isDEARegistration()).size() + getExtraCurricularEntriesToReport().size() + getPropaedeuticEntriesToReport().size(); } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") @Override public final void setNumberOfUnits(final Integer numberOfUnits) { throw new DomainException("error.ApprovementCertificateRequest.cannot.modify.numberOfUnits"); } @Override public void setMobilityProgram(MobilityProgram mobilityProgram) { throw new DomainException("error.ApprovementCertificateRequest.cannot.modify"); } @Override public void setIgnoreExternalEntries(Boolean ignoreExternalEntries) { throw new DomainException("error.ApprovementCertificateRequest.cannot.modify"); } @Override public boolean isToPrint() { final Integer units = super.getNumberOfUnits(); return !hasConcluded() || (units != null && units.intValue() == calculateNumberOfUnits()); } final private Collection getEntriesToReport(final boolean useConcluded) { final HashSet result = new HashSet(); final Registration registration = getRegistration(); ICurriculum curriculum; if (registration.isBolonha()) { for (final CycleCurriculumGroup cycle : registration.getLastStudentCurricularPlan().getInternalCycleCurriculumGrops()) { if (cycle.hasAnyApprovedCurriculumLines() && (useConcluded || !cycle.isConclusionProcessed())) { curriculum = cycle.getCurriculum(getFilteringDate()); filterEntries(result, this, curriculum); } } } else { curriculum = getRegistration().getCurriculum(getFilteringDate()); filterEntries(result, this, curriculum); } return result; } public DateTime getFilteringDate() { return hasConcluded() ? getRequestConclusionDate() : new DateTime(); } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") public static final void filterEntries(final Collection result, final ApprovementCertificateRequest request, final ICurriculum curriculum) { for (final ICurriculumEntry entry : curriculum.getCurriculumEntries()) { if (entry instanceof Dismissal) { final Dismissal dismissal = (Dismissal) entry; if (dismissal.getCredits().isEquivalence() || (dismissal.isCreditsDismissal() && !dismissal.getCredits().isSubstitution())) { continue; } } else if (entry instanceof ExternalEnrolment && request.getIgnoreExternalEntries()) { continue; } result.add(entry); } } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") public final Collection getExtraCurricularEntriesToReport() { final Collection result = new HashSet(); reportApprovedCurriculumLines(result, calculateExtraCurriculumLines()); reportExternalGroups(result); return result; } private Collection calculateExtraCurriculumLines() { final Collection result = new HashSet(); for (final CurriculumLine line : getRegistration().getExtraCurricularCurriculumLines()) { if (line.isEnrolment()) { if (!((Enrolment) line).isSourceOfAnyCreditsInCurriculum()) { result.add(line); } } else { result.add(line); } } return result; } private void reportApprovedCurriculumLines(final Collection result, final Collection lines) { for (final CurriculumLine line : lines) { if (line.isApproved()) { if (line.isEnrolment()) { result.add((IEnrolment) line); } else if (line.isDismissal() && ((Dismissal) line).getCredits().isSubstitution()) { result.addAll(((Dismissal) line).getSourceIEnrolments()); } } } } private void reportExternalGroups(final Collection result) { for (final ExternalCurriculumGroup group : getRegistration().getLastStudentCurricularPlan().getExternalCurriculumGroups()) { filterEntries(result, this, group.getCurriculumInAdvance(getFilteringDate())); } } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") public final Collection getPropaedeuticEntriesToReport() { final Collection result = new HashSet(); reportApprovedCurriculumLines(result, getRegistration().getPropaedeuticCurriculumLines()); return result; } @Override public boolean hasPersonalInfo() { return true; } }