package net.sourceforge.fenixedu.domain.contacts; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.organizationalStructure.Party; import org.apache.commons.lang.StringUtils; import pt.utl.ist.fenix.tools.smtp.EmailSender; import com.linkare.commons.metainfo.Linkare; public class EmailAddress extends EmailAddress_Base { public static Comparator COMPARATOR_BY_EMAIL = new Comparator() { public int compare(EmailAddress contact, EmailAddress otherContact) { final String value = contact.getValue(); final String otherValue = otherContact.getValue(); int result = 0; if (value != null && otherValue != null) { result = value.compareTo(otherValue); } else if (value != null) { result = 1; } else if (otherValue != null) { result = -1; } return (result == 0) ? COMPARATOR_BY_TYPE.compare(contact, otherContact) : result; } }; public static EmailAddress createEmailAddress(Party party, String email, PartyContactType type, Boolean isDefault, Boolean visibleToPublic, Boolean visibleToStudents, Boolean visibleToTeachers, Boolean visibleToEmployees, Boolean visibleToAlumni) { for (EmailAddress emailAddress : party.getEmailAddresses()) { if (emailAddress.getValue().equals(email)) return emailAddress; } return (!StringUtils.isEmpty(email)) ? new EmailAddress(party, type, visibleToPublic, visibleToStudents, visibleToTeachers, visibleToEmployees, visibleToAlumni, isDefault, email) : null; } public static EmailAddress createEmailAddress(Party party, String email, PartyContactType type, boolean isDefault) { for (EmailAddress emailAddress : party.getEmailAddresses()) { if (emailAddress.getValue().equals(email)) return emailAddress; } return (!StringUtils.isEmpty(email)) ? new EmailAddress(party, type, isDefault, email) : null; } protected EmailAddress() { super(); } protected EmailAddress(final Party party, final PartyContactType type, final boolean defaultContact, final String value) { this(); super.init(party, type, defaultContact); checkParameters(value); super.setValue(value); } protected EmailAddress(final Party party, final PartyContactType type, final boolean visibleToPublic, final boolean visibleToStudents, final boolean visibleToTeachers, final boolean visibleToEmployees, final boolean visibleToAlumni, final boolean defaultContact, final String value) { this(); super.init(party, type, visibleToPublic, visibleToStudents, visibleToTeachers, visibleToEmployees, visibleToAlumni, defaultContact); checkParameters(value); super.setValue(value); } private void checkParameters(final String value) { if (!EmailSender.emailAddressFormatIsValid(value)) { throw new DomainException("error.domain.contacts.EmailAddress.invalid.format", value); } } @Override public void setType(PartyContactType type) { checkEmailType(type); super.setType(type); } private void checkEmailType(PartyContactType type) { if (type == PartyContactType.INSTITUTIONAL) { final List contacts = (List) getParty().getPartyContacts(getClass(), type); contacts.remove(this); if (!contacts.isEmpty()) { throw new DomainException("error.domain.contacts.EmailAddress.can.only.have.one.institutional.emailAddress"); } } } public boolean hasValue() { return getValue() != null; } public boolean hasValue(final String emailAddressString) { return hasValue() && getValue().equalsIgnoreCase(emailAddressString); } @Override public boolean isEmailAddress() { return true; } public void edit(final String value) { if (!isInstitutionalType()) { super.setValue(value); } } @Override protected void checkRulesToDelete() { if (isInstitutionalType()) { throw new DomainException("error.domain.contacts.EmailAddress.cannot.delete.institution.emailAddress", getValue()); } if (getParty().getPartyContacts(getClass()).size() == 1) { throw new DomainException("error.domain.contacts.EmailAddress.cannot.remove.last.emailAddress"); } } static public EmailAddress find(final String emailAddressString) { for (final PartyContact contact : RootDomainObject.getInstance().getPartyContactsSet()) { if (contact.isEmailAddress()) { final EmailAddress emailAddress = (EmailAddress) contact; if (emailAddress.hasValue(emailAddressString)) { return emailAddress; } } } return null; } @Override public String getPresentationValue() { return getValue(); } @Linkare(author = "Paulo Zenida") public static List readAll() { final List result = new ArrayList(); for (final PartyContact partyContact : RootDomainObject.getInstance().getPartyContacts()) { if (partyContact.isEmailAddress()) { result.add((EmailAddress) partyContact); } } return result; } }