package net.sourceforge.fenixedu.domain.candidacy.accounting; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import net.sourceforge.fenixedu.domain.DegreeCurricularPlan; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.candidacy.SecondCycleCandidacy; import net.sourceforge.fenixedu.domain.candidacy.accounting.paymentCodes.SecondCycleCandidacyPaymentCode; import net.sourceforge.fenixedu.domain.candidate.CandidateRegistrationEmailTemplates; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.transactions.PaymentType; import net.sourceforge.fenixedu.domain.util.Email; import net.sourceforge.fenixedu.util.LanguageUtils; import net.sourceforge.fenixedu.util.Money; import org.apache.commons.collections.Predicate; import org.apache.struts.util.MessageResources; import org.joda.time.DateTime; import org.joda.time.YearMonthDay; import pt.iscte.ci.metadata.ISCTE; import pt.utl.ist.fenix.tools.util.StringNormalizer; import pt.utl.ist.fenix.tools.util.i18n.Language; public abstract class CandidacyPaymentDetails extends CandidacyPaymentDetails_Base { public CandidacyPaymentDetails() { super(); setRootDomainObject(RootDomainObject.getInstance()); } public abstract boolean isCandidacyPayed(); public abstract Money getAmount(); public abstract DateTime getWhenRegistered(); public void pay() { flagAsPayed(); final SecondCycleCandidacy ownCandidacy = getSecondCycleCandidacy(); Person person = ownCandidacy.getPerson(); final String ownDocumentId = person.getDocumentIdNumber() != null ? person.getDocumentIdNumber().replaceAll("_", "") : null; final String ownSocialSecurityNumber = person.getSocialSecurityNumber() != null ? person.getSocialSecurityNumber() .replaceAll("_", "") : null; final String firstAndLastName = StringNormalizer.normalize(person.getFirstAndLastName()); final YearMonthDay dateOfBirth = person.getDateOfBirthYearMonthDay(); DegreeCurricularPlan dcp = ownCandidacy.getDegreeCurricularPlan(); Predicate removalPredicate = new Predicate() { @Override public boolean evaluate(Object arg0) { if (arg0 instanceof SecondCycleCandidacy && arg0 != ownCandidacy) { SecondCycleCandidacy otherCandidacy = (SecondCycleCandidacy) arg0; Person otherPerson = otherCandidacy.getPerson(); String otherDocumentId = otherPerson.getDocumentIdNumber() != null ? otherPerson.getDocumentIdNumber() .replaceAll("_", "") : null; String otherSocialSecurityNumber = otherPerson.getSocialSecurityNumber() != null ? otherPerson .getSocialSecurityNumber().replaceAll("_", "") : null; String otherFirstAndLastName = StringNormalizer.normalize(otherPerson.getFirstAndLastName()); YearMonthDay otherDateOfBirth = otherPerson.getDateOfBirthYearMonthDay(); return otherDocumentId != null && otherDocumentId.equals(ownDocumentId) && (otherSocialSecurityNumber == null || otherSocialSecurityNumber.equals(ownSocialSecurityNumber)) && otherFirstAndLastName.equals(firstAndLastName) && otherDateOfBirth != null && otherDateOfBirth.equals(dateOfBirth) && !otherCandidacy.isInscriptionPayed(); } return false; } }; for (SecondCycleCandidacy candidacy : dcp.getSecondCycleCandidacies()) { if (candidacy != ownCandidacy) { if (removalPredicate.evaluate(candidacy)) { candidacy.delete(); } } } } protected abstract void flagAsPayed(); public boolean isPaypal() { return false; } private String getLabel(String bundle, String key) { return ResourceBundle.getBundle(bundle, LanguageUtils.getLocale()).getString(key); } public String getDescription() { return getLabel("resources.CandidateResources", "label." + getClass().getName()); } public boolean isATM() { return false; } public boolean isBankTransfer() { return false; } public boolean isMoneyPaymentDetail() { return false; } public boolean isPayableDirectlyInInterface() { return !isATM() && !isCandidacyPayed(); } protected void notifyCandidateOfPayment() { SecondCycleCandidacy secondCycleCandidacy = getSecondCycleCandidacy(); String degreeName = secondCycleCandidacy.getDegreeCurricularPlan().getDegree() .getPresentationName(null, new Locale(Language.pt.name())); String degreeNameEnglish = secondCycleCandidacy.getDegreeCurricularPlan().getDegree() .getPresentationName(null, Locale.ENGLISH); CandidateRegistrationEmailTemplates templates = CandidateRegistrationEmailTemplates.getInstance(); final MessageResources messages = MessageResources.getMessageResources("resources/GlobalResources"); new Email("ISCTE-IUL", messages.getMessage(LanguageUtils.getLocale(), "2ndCicleCandidacies.email.support"), new String[] {}, Collections.singletonList(secondCycleCandidacy.getPerson().getEmail()), Collections.EMPTY_LIST, Collections.EMPTY_LIST, templates.getFormattedEmailSubjectPaymentReceived(degreeName, degreeNameEnglish), templates.getFormattedEmailBodyPaymentReceived(degreeName, degreeNameEnglish)); } @Override public void setSecondCycleCandidacy(SecondCycleCandidacy secondCycleCandidacy) { if (secondCycleCandidacy == null) { throw new DomainException("error.candidacy.cannotSetSecondCycleCandidacyToNullInPaymentDetail"); } super.setSecondCycleCandidacy(secondCycleCandidacy); } public void delete() { final SecondCycleCandidacyPaymentCode paymentCode = getPaymentCode(); removePaymentCode(); if (paymentCode != null) { paymentCode.delete(); } super.setSecondCycleCandidacy(null); removeRootDomainObject(); deleteDomainObject(); } @ISCTE public PaymentType getPaymentType() { return null; } public static Collection getPayedAfterForFinantialSystem(DateTime minimunDate) { List result = new ArrayList(); for (CandidacyPaymentDetails candidacyPaymentDetails : RootDomainObject.getInstance().getCandidacyPaymentDetails()) { if (candidacyPaymentDetails.isATM() && candidacyPaymentDetails.isCandidacyPayed()) { if (minimunDate == null || candidacyPaymentDetails.getWhenRegistered().isAfter(minimunDate)) { result.add(candidacyPaymentDetails); } } } return result; } }