package net.sourceforge.fenixedu.domain.contacts; 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 net.sourceforge.zas.metainfo.AccessControlled; import pt.iscte.ci.metadata.ISCTE; import pt.iscte.ci.zas.OwnerPersonOrAdministratorOrAcademicAdministrativeOfficeDecider; import pt.ist.utl.fenix.utils.EmailSender; public class EmailAddress extends EmailAddress_Base { @ISCTE(author="ajsco", comment="See http://www.regular-expressions.info/email.html") private static final String EMAIL_REX = "\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,3}\\b"; 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; } }; protected EmailAddress() { super(); } protected EmailAddress(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact) { this(); super.init(party, type, visible, defaultContact); } public EmailAddress(final Party party, final PartyContactType type, final Boolean defaultContact, final String value) { this(party, type, true, defaultContact.booleanValue(), value); } public EmailAddress(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact, final String value) { this(); init(party, type, visible, defaultContact, value); } protected void init(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact, final String value) { super.init(party, type, visible, 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; } @AccessControlled(value = "*", deciderClasses = OwnerPersonOrAdministratorOrAcademicAdministrativeOfficeDecider.class) public void edit(final String value) { super.setValue(value); invalidate(); } public void edit(final PartyContactType type, final Boolean defaultContact, final String value) { super.edit(type, true, defaultContact); edit(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; } @ISCTE(author = "ajsco") public static boolean isValid(String email) { return email.toUpperCase().matches(EMAIL_REX); } }