package net.sourceforge.fenixedu.domain.candidacy; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.ResourceBundle; import java.util.Set; import net.sourceforge.fenixedu.domain.DegreeCurricularPlan; import net.sourceforge.fenixedu.domain.DegreeCurricularPlanCandidacyPeriod; import net.sourceforge.fenixedu.domain.ExecutionDegree; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.PhotoType; import net.sourceforge.fenixedu.domain.Photograph; import net.sourceforge.fenixedu.domain.candidacy.CandidacyDocument.CandidacyDocumentType; import net.sourceforge.fenixedu.domain.candidacy.accounting.CandidacyPaymentDetails; import net.sourceforge.fenixedu.domain.candidacy.accounting.CandidacyPaymentDetailsWithInfo; import net.sourceforge.fenixedu.domain.candidacy.accounting.paymentCodes.SecondCycleCandidacyPaymentCodePool; import net.sourceforge.fenixedu.domain.candidate.SelectionReport; import net.sourceforge.fenixedu.domain.contacts.PhysicalAddress; import net.sourceforge.fenixedu.domain.degreeStructure.CourseGroup; import net.sourceforge.fenixedu.domain.degreeStructure.CycleType; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.organizationalStructure.Accountability; import net.sourceforge.fenixedu.domain.organizationalStructure.DegreeUnit; import net.sourceforge.fenixedu.domain.organizationalStructure.FunctionType; import net.sourceforge.fenixedu.domain.organizationalStructure.PartyTypeEnum; import net.sourceforge.fenixedu.domain.organizationalStructure.PersonFunction; import net.sourceforge.fenixedu.domain.organizationalStructure.SchoolUnit; import net.sourceforge.fenixedu.domain.organizationalStructure.Unit; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.domain.student.Registration; import net.sourceforge.fenixedu.domain.student.RegistrationDataByExecutionYear; import net.sourceforge.fenixedu.domain.student.Student; import net.sourceforge.fenixedu.domain.transactions.PaymentType; import net.sourceforge.fenixedu.domain.util.workflow.Operation; import net.sourceforge.fenixedu.injectionCode.AccessControl; import net.sourceforge.fenixedu.util.ByteArray; import net.sourceforge.fenixedu.util.ContentType; import net.sourceforge.fenixedu.util.LanguageUtils; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.lang.StringUtils; import org.joda.time.DateTime; import pt.ist.fenixWebFramework.rendererExtensions.util.IPresentableEnum; import pt.ist.fenixWebFramework.services.Service; public class SecondCycleCandidacy extends SecondCycleCandidacy_Base { public static enum InformationSourceType implements IPresentableEnum { PRESS_RELEASE, NEWSPAPER_ARTICLE, PERSONAL_RECOMMENDATION, WEBSITE, INTERNET_SEARCH, EXPO, SCHOOL_CONTACT, COMPANY_RECOMMENDATION, SOCIAL_MEDIA, THROUGH_TEACHERS, OUTDOORS, FRIENDS, PARENTS, EMAIL, OTHERS, ADVERTISEMENTS, GOOGLE, YOU_TUBE; public String getLocalizedName() { return ResourceBundle.getBundle("resources.CandidateResources", LanguageUtils.getLocale()).getString( "label.InformationSourceType." + name()); } } public SecondCycleCandidacy(ExecutionYear executionYear, DegreeCurricularPlan dcp, Person person) { super(); setExecutionYear(executionYear); setDegreeCurricularPlan(dcp); setPerson(person); } @Override public String getDescription() { return ResourceBundle.getBundle("resources.CandidateResources", LanguageUtils.getLocale()).getString( "label.secondCycleCandidacy") + " - " + getDegreeCurricularPlan().getName() + " - " + getExecutionYear().getYear(); } @Override Set getOperations(CandidacySituation candidacySituation) { return new HashSet(); } @Override void moveToNextState(CandidacyOperationType candidacyOperationType, Person person) { } @Override public Map> getStateMapping() { return null; } @Override public String getDefaultState() { return null; } public SecondCycleCandidacyReference getReference(Integer order) { for (SecondCycleCandidacyReference reference : getReferences()) { if (reference.getReferenceOrder().equals(order)) { return reference; } } return null; } public List getOrderedReferences() { List references = new ArrayList(); references.addAll(getReferences()); Collections.sort(references); return references; } public List getAdHocDocuments() { return getListOfCandidacyDocuments(CandidacyDocumentType.ADHOC); } public List getListOfCandidacyDocuments(CandidacyDocumentType type) { List list = new ArrayList(); for (CandidacyDocument document : getCandidacyDocuments()) { if (document.getDocumentType() == type) { list.add(document); } } return list; } public CandidacyDocument getCandidacyDocument(CandidacyDocumentType type) { for (CandidacyDocument document : getCandidacyDocuments()) { if (document.getDocumentType() == type) { return document; } } return null; } public boolean isEditAllowed() { Person currentUser = AccessControl.getPerson(); final SchoolUnit schoolUnitWithSecretariatFunction = currentUser.getSchoolUnitWithSecretariatFunction(); if (schoolUnitWithSecretariatFunction != null && schoolUnitWithSecretariatFunction == getSchoolUnit() && currentUser.hasRole(RoleType.DEPARTMENT_ADMINISTRATIVE_OFFICE)) { return true; } CandidacySituationType type = getActiveCandidacySituationType(); return currentUser.hasRole(RoleType.MANAGER) || currentUser.hasRole(RoleType.SCIENTIFIC_COUNCIL) || (currentUser == getPerson() && getDegreeCurricularPlan().hasCandidacyPeriodOpen(getExecutionYear()) && (type == CandidacySituationType.PRE_CANDIDACY || type == CandidacySituationType.STAND_BY_FILLED_DATA)); } public boolean isLoggedUserOwnCandidacy() { return getPerson() == AccessControl.getPerson(); } public boolean isSubmitted() { return getCandidacySituationByType(CandidacySituationType.STAND_BY_CONFIRMED_DATA) != null; } public boolean isAfterSubmition() { return isSubmitted() || isInscriptionPayed(); } public boolean isAdmitted() { return getActiveCandidacySituationType() == CandidacySituationType.ADMITTED; } public boolean isConditionallyAdmitted() { return getActiveCandidacySituationType() == CandidacySituationType.CONDITIONALLY_ADMITTED; } public boolean isAbleToSubmitCandidacy() { final PhysicalAddress defaultPhysicalAddress = getPerson().getDefaultPhysicalAddress(); if (defaultPhysicalAddress != null && defaultPhysicalAddress.hasCountryOfResidence() && !defaultPhysicalAddress.getCountryOfResidence().isPortugal()) { return isReferenceAvailableForCandidacy() || isPayPalAvailableForPeriod(); } if (getPaymentType() == PaymentType.ATM) { return isReferenceAvailableForCandidacy(); } else if (getPaymentType() == PaymentType.PAYPAL) { return isPayPalAvailableForPeriod(); } return false; } public boolean isPayPalAvailableForPeriod() { DegreeCurricularPlanCandidacyPeriod period = (DegreeCurricularPlanCandidacyPeriod) getDegreeCurricularPlan() .getActiveCandidacyPeriod(getExecutionYear()); return period.hasCandidacyCostDetail() && !StringUtils.isEmpty(period.getCandidacyCostDetail().getPaypalCode()); } public boolean isReferenceAvailableForCandidacy() { DegreeCurricularPlanCandidacyPeriod period = (DegreeCurricularPlanCandidacyPeriod) getDegreeCurricularPlan() .getActiveCandidacyPeriod(getExecutionYear()); return period == null ? false : !SecondCycleCandidacyPaymentCodePool.getPool().getCodes(period).isEmpty(); } public String getAvailablePaymentOptionsSerialized() { final StringBuilder result = new StringBuilder(); if (isPayPalAvailableForPeriod()) { result.append(PaymentType.PAYPAL.name()); } if (isReferenceAvailableForCandidacy()) { if (result.length() > 0) { result.append(","); } result.append(PaymentType.ATM.name()); } return result.toString(); } public boolean isInscriptionPayed() { return getCandidacySituationByType(CandidacySituationType.SUBMITTED_DATA) != null; } public boolean isCancelled() { return getActiveCandidacySituationType() == CandidacySituationType.CANCELLED; } public boolean isAbleToRemoveCancellation() { return isCancelled() && getDegreeCurricularPlan().hasCandidacyPeriodOpen(getExecutionYear()); } public void removeCancellation() { if (!isCancelled()) { throw new DomainException("error.secondCycleCandidacy.canOnlyRemoveCancellationWhenCancelled"); } getActiveCandidacySituation().delete(); } @Override public void delete() { removeExecutionYear(); removeDegreeCurricularPlan(); while (hasAnyReferences()) { getReferencesIterator().next().delete(); } if (hasPaymentCode()) { getPaymentCode().delete(); } while (hasAnyBranchSelections()) { getBranchSelectionsIterator().next().delete(); } if (hasContributor()) { getContributor().delete(); } if (hasCandidacyPaymentDetails()) { if (getCandidacyPaymentDetails().isCandidacyPayed()) { throw new DomainException("error.SecondCycleCandidacy.delete.payedCandidacy"); } getCandidacyPaymentDetails().delete(); } super.delete(); } public void forceDelete() { final Person person = getPerson(); if (hasCandidacyPaymentDetails()) { getCandidacyPaymentDetails().delete(); } delete(); if (person.getPersonRolesCount() == 1 && person.hasRole(RoleType.CANDIDATE) && !person.hasAnyCandidacies()) { person.delete(); } } public DegreeCurricularPlanCandidacyPeriod getCandidacyPeriod() { if (isAfterSubmition()) { final CandidacySituation standByConfirmedSituation = getCandidacySituationByType(CandidacySituationType.STAND_BY_CONFIRMED_DATA); if (standByConfirmedSituation != null) { DateTime dateToLookUp = standByConfirmedSituation.getSituationDate(); final DegreeCurricularPlanCandidacyPeriod candidacyPeriod = (DegreeCurricularPlanCandidacyPeriod) getDegreeCurricularPlan() .getCandidacyPeriod(dateToLookUp, getExecutionYear()); final CandidacySituation payedSituation = getCandidacySituationByType(CandidacySituationType.SUBMITTED_DATA); if (candidacyPeriod != null && payedSituation != null && candidacyPeriod.getFeePaymentEndDate() != null && payedSituation.getSituationDate().isAfter(candidacyPeriod.getFeePaymentEndDate())) { final DegreeCurricularPlanCandidacyPeriod paymentCandidacyPeriod = (DegreeCurricularPlanCandidacyPeriod) getDegreeCurricularPlan() .getCandidacyPeriod(candidacyPeriod.getFeePaymentEndDate(), getExecutionYear()); return paymentCandidacyPeriod != null ? paymentCandidacyPeriod : getDegreeCurricularPlan() .getLastDegreeCurricularPlanCandidacyPeriod(getExecutionYear()); } return candidacyPeriod; } } return (DegreeCurricularPlanCandidacyPeriod) getDegreeCurricularPlan().getCandidacyPeriod( getCreationEntityDate().toDateTimeAtMidnight(), getExecutionYear()); } public boolean isRegistrationAfterCandidacyFinalizationPeriod() { final CandidacySituation registeredSituation = getCandidacySituationByType(CandidacySituationType.REGISTERED); if (registeredSituation == null) { return false; } final DateTime candidacyFinalizationPeriodEndDate = getCandidacyPeriod().getCandidacyFinalizationPeriodEndDate(); return candidacyFinalizationPeriodEndDate != null && registeredSituation.getSituationDate().isAfter(candidacyFinalizationPeriodEndDate); } public SchoolUnit getSchoolUnit() { Collection allParentUnits = getDegreeCurricularPlan().getDegree().getUnit().getAllParentUnits(PartyTypeEnum.SCHOOL); return (SchoolUnit) (allParentUnits.isEmpty() ? null : allParentUnits.iterator().next()); } public boolean isAbleToChangeSituationBySchool() { final CandidacySituationType activeSituationType = getActiveCandidacySituationType(); return activeSituationType != CandidacySituationType.PRE_CANDIDACY && activeSituationType != CandidacySituationType.STAND_BY && activeSituationType != CandidacySituationType.STAND_BY_FILLED_DATA && activeSituationType != CandidacySituationType.REGISTERED; } public Integer getCandidacyPeriodOrder() { final DegreeCurricularPlanCandidacyPeriod candidacyPeriod = getCandidacyPeriod(); return candidacyPeriod != null ? candidacyPeriod.getPeriodOrderForDegreeCurricularPlan(getDegreeCurricularPlan()) : null; } public String getPaymentOrSubmissionDateWithDescription() { final ResourceBundle bundle = ResourceBundle.getBundle("resources.CandidateResources", LanguageUtils.getLocale()); final CandidacySituation payedSituation = getCandidacySituationByType(CandidacySituationType.SUBMITTED_DATA); if (payedSituation != null) { return payedSituation.getSituationDate().toString("dd-MM-YYYY") + " (" + bundle.getString("label.candidaciesAnalysis.dateOfPayment") + ")"; } final CandidacySituation submittedSituation = getCandidacySituationByType(CandidacySituationType.STAND_BY_CONFIRMED_DATA); if (submittedSituation != null) { return submittedSituation.getSituationDate().toString("dd-MM-YYYY") + " (" + bundle.getString("label.candidaciesAnalysis.dateOfSubmission") + ")"; } final CandidacySituation filledSituation = getCandidacySituationByType(CandidacySituationType.STAND_BY_FILLED_DATA); if (filledSituation != null) { return filledSituation.getSituationDate().toString("dd-MM-YYYY") + " (" + bundle.getString("label.candidaciesAnalysis.dateOfFill") + ")"; } return ""; } public boolean isCandidatePhotoAvailable() { return getCandidacyDocument(CandidacyDocumentType.PHOTOGRAPH) != null; } public SelectionReport getSelectionReport() { return getDegreeCurricularPlan().getSelectionReportForCandidacyPeriod(getCandidacyPeriod()); } public boolean isLoggedUserCoordinator() { final Person person = AccessControl.getPerson(); if (person == null) { return false; } final ExecutionYear executionYear = getExecutionYear(); final DegreeUnit degreeUnit = getDegreeCurricularPlan().getDegree().getUnit(); if (degreeUnit != null) { final Collection functions = degreeUnit .getChildAccountabilitiesByAccountabilityClass(PersonFunction.class); for (final Accountability function : functions) { final PersonFunction personFunction = (PersonFunction) function; if (personFunction.getFunction().getFunctionType() == FunctionType.COORDINATOR && personFunction.belongsToPeriod(executionYear.getBeginDateYearMonthDay(), executionYear.getEndDateYearMonthDay())) { return true; } } } final ExecutionDegree executionDegree = getDegreeCurricularPlan().getExecutionDegreeByYear(executionYear); return executionDegree != null && executionDegree.getCoordinatorByTeacher(person) != null; } @Service public void editPaymentDate(final DateTime paymentDate) { if (paymentDate == null) { return; } final CandidacyPaymentDetails paymentDetails = getCandidacyPaymentDetails(); if (paymentDetails != null && paymentDetails instanceof CandidacyPaymentDetailsWithInfo) { final CandidacyPaymentDetailsWithInfo paymentDetailsWithInfo = (CandidacyPaymentDetailsWithInfo) paymentDetails; if (paymentDetailsWithInfo.isCandidacyPayed()) { paymentDetailsWithInfo.setWhenRegistered(paymentDate); } } final CandidacySituation submittedSituation = getCandidacySituationByType(CandidacySituationType.SUBMITTED_DATA); if (submittedSituation != null) { submittedSituation.setSituationDate(paymentDate); } } public Integer getOrderForBranchSelection(CourseGroup courseGroup) { for (CandidateBranchSelection groupSelection : getBranchSelections()) { if (groupSelection.getCourseGroup() == courseGroup) { return groupSelection.getSelectionOrder(); } } return null; } public CourseGroup getFirstBranchIfExists() { List selectedOnes = new ArrayList(getBranchSelections()); if (selectedOnes.isEmpty()) { return null; } Collections.sort(selectedOnes, new BeanComparator("selectionOrder")); return selectedOnes.get(0).getCourseGroup(); } public boolean finalizeCandidacy() { if (isMergeRequiredForCandidate()) { return false; } final Person person = getPerson(); person.addPersonRoleByRoleType(RoleType.PERSON); person.addPersonRoleByRoleType(RoleType.STUDENT); final CandidacyDocument photoDocument = getCandidacyDocument(CandidacyDocumentType.PHOTOGRAPH); if (photoDocument != null) { ContentType contentType = ContentType.getContentType(photoDocument.getFile().getContentType()); if (contentType == null) { contentType = ContentType.getContentTypeByFileExtension(photoDocument.getFile().getFilename()); } if (contentType != null) { try { person.setPersonalPhotograph(new Photograph(contentType, new ByteArray(photoDocument.getFile().getContent()), PhotoType.USER)); } catch (DomainException e) { // if some problem while compressing/storing image, we don't // want to abort the candidacy finalization } } } if (!person.hasStudent()) { new Student(person, getNumber()); } final Registration registration = new Registration(person, getDegreeCurricularPlan(), this, null, CycleType.SECOND_CYCLE, getExecutionYear()); final RegistrationDataByExecutionYear registrationDataByExecutionYear = registration .getRegistrationDataByExecutionYear(getExecutionYear()); registrationDataByExecutionYear.setRecalculateTuition(Boolean.TRUE); return true; } public boolean isMergeRequiredForCandidate() { return getPerson().isMergeRequired(); } public boolean isFinalizeAvailable() { final CandidacySituationType activeCandidacySituationType = getActiveCandidacySituationType(); final DegreeCurricularPlanCandidacyPeriod candidacyPeriod = getCandidacyPeriod(); return (activeCandidacySituationType == CandidacySituationType.ADMITTED || activeCandidacySituationType == CandidacySituationType.CONDITIONALLY_ADMITTED) && !hasRegistration() && candidacyPeriod.getCandidacyFinalizationPeriodStartDate() != null && !candidacyPeriod.getCandidacyFinalizationPeriodStartDate().isAfterNow() && DegreeCurricularPlanCandidacyPeriod.isAnyCandidacyFinalizationPeriodOpen(getDegreeCurricularPlan()); } public boolean isFinalizationNeeded() { return getActiveCandidacySituationType() == CandidacySituationType.REGISTERED && !hasRegistration(); } public boolean isFinalized() { return getActiveCandidacySituationType() == CandidacySituationType.REGISTERED && hasRegistration(); } public CandidacyDocument getCandidateIdentificationDocument() { return getCandidacyDocument(CandidacyDocumentType.IDENTIFICATION_DOCUMENT); } public CandidacyDocument getCandidateSocialSecurityDocument() { return getCandidacyDocument(CandidacyDocumentType.SOCIAL_SECURITY_NUMBER_DOCUMENT); } @Override public Boolean getLivesNowInInstitutionCountry() { return super.getLivesNowInInstitutionCountry() == null || super.getLivesNowInInstitutionCountry(); } }