package net.sourceforge.fenixedu.domain.contacts; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.DomainObject; import net.sourceforge.fenixedu.domain.Login; import net.sourceforge.fenixedu.domain.LoginAlias; import net.sourceforge.fenixedu.domain.ModifyServiceEntriesMarker; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.organizationalStructure.Party; import net.sourceforge.fenixedu.domain.space.AllocatableSpace; import net.sourceforge.fenixedu.injectionCode.AccessControl; import pt.iscte.ci.metadata.ISCTE; import pt.ist.fenixframework.plugins.luceneIndexing.domain.interfaces.Indexable; import pt.ist.fenixframework.plugins.luceneIndexing.domain.interfaces.Searchable; public abstract class PartyContact extends PartyContact_Base implements ModifyServiceEntriesMarker, Searchable { public static Comparator COMPARATOR_BY_TYPE = new Comparator() { public int compare(PartyContact contact, PartyContact otherContact) { int result = contact.getType().compareTo(otherContact.getType()); return (result == 0) ? DomainObject.COMPARATOR_BY_ID.compare(contact, otherContact) : result; } }; protected PartyContact() { super(); super.setRootDomainObject(RootDomainObject.getInstance()); // ISCTE setIsValidated(false); } protected void init(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact) { checkParameters(party, type); super.setParty(party); super.setType(type); super.setVisible(Boolean.valueOf(visible)); setDefaultContactInformation(defaultContact); // ISCTE if (isInstitutionalType()) { validate(); } } private void setDefaultContactInformation(final boolean defaultContact) { if (defaultContact) { changeToDefault(); } else { final List partyContacts = (List) getParty().getPartyContacts(getClass()); if (partyContacts.isEmpty() || partyContacts.size() == 1) { super.setDefaultContact(Boolean.TRUE); } else { setAnotherContactAsDefault(); super.setDefaultContact(Boolean.FALSE); } } } public void changeToDefault() { final PartyContact defaultPartyContact = getParty().getDefaultPartyContact(getClass()); if (defaultPartyContact != null && defaultPartyContact != this) { defaultPartyContact.setDefaultContact(Boolean.FALSE); } super.setDefaultContact(Boolean.TRUE); } private void checkParameters(final Party party, final PartyContactType type) { if (party == null) { throw new DomainException("error.contacts.PartyContact.party.cannot.be.null"); } if (type == null) { throw new DomainException("error.contacts.PartyContact.contactType.cannot.be.null"); } } public void edit(final PartyContactType type, final boolean visible, final boolean defaultContact) { checkParameters(getParty(), type); super.setType(type); super.setVisible(Boolean.valueOf(visible)); setDefaultContactInformation(defaultContact); } public boolean isDefault() { return hasDefaultContactValue() && getDefaultContact().booleanValue(); } private boolean hasDefaultContactValue() { return getDefaultContact() != null; } public boolean isContactVisible() { return getVisible().booleanValue(); } public boolean isInstitutionalType() { return getType() == PartyContactType.INSTITUTIONAL; } public boolean isWebAddress() { return false; } public boolean isPhysicalAddress() { return false; } public boolean isEmailAddress() { return false; } public boolean isPhone() { return false; } public boolean isMobile() { return false; } public boolean isFax() { return false; } @ISCTE public boolean isPhysicalDeliveryOfficeContact() { return false; } @ISCTE public boolean isPostBoxContact() { return false; } @ISCTE public boolean isInternalPhone() { return false; } public void deleteWithoutCheckRules() { processDelete(); } public void delete() { checkRulesToDelete(); processDelete(); } private void processDelete() { setAnotherContactAsDefault(); removeParty(); if (hasPartyContactValidationRequest()) { getPartyContactValidationRequest().delete(); } removeRootDomainObject(); super.deleteDomainObject(); } protected void checkRulesToDelete() { // nothing to check } private void setAnotherContactAsDefault() { if (isDefault()) { final List contacts = (List) getParty().getPartyContacts(getClass()); if (!contacts.isEmpty() && contacts.size() > 1) { contacts.remove(this); for (PartyContact partyContact : contacts) { if (!partyContact.isInstitutionalType()) { partyContact.setDefaultContact(Boolean.TRUE); break; } } } } } static public WebAddress createDefaultPersonalWebAddress(final Party party) { return new WebAddress(party, PartyContactType.PERSONAL, true, true); } static public WebAddress createDefaultPersonalWebAddress(final Party party, final String url) { return new WebAddress(party, PartyContactType.PERSONAL, true, true, url); } static public WebAddress createWebAddress(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact, final String url) { return new WebAddress(party, type, visible, defaultContact, url); } static public PhysicalAddress createDefaultPersonalPhysicalAddress(final Party party) { return new PhysicalAddress(party, PartyContactType.PERSONAL, true, true); } static public PhysicalAddress createDefaultPersonalPhysicalAddress(final Party party, final PhysicalAddressData data) { return new PhysicalAddress(party, PartyContactType.PERSONAL, true, true, data); } static public PhysicalAddress createPhysicalAddress(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact, final PhysicalAddressData data) { return new PhysicalAddress(party, type, visible, defaultContact, data); } static public Phone createDefaultPersonalPhone(final Party party) { return new Phone(party, PartyContactType.PERSONAL, true, true); } static public Phone createDefaultPersonalPhone(final Party party, final String number) { return new Phone(party, PartyContactType.PERSONAL, true, true, number); } static public Phone createPhone(final Party party, final PartyContactType type, boolean visible, boolean defaultContact, final String number) { return new Phone(party, type, visible, defaultContact, number); } static public InternalPhone createInternalPhone(final Party party, final PartyContactType type, boolean visible, boolean defaultContact, final String number) { return new InternalPhone(party, type, visible, defaultContact, number); } static public MobilePhone createDefaultPersonalMobilePhone(final Party party) { return new MobilePhone(party, PartyContactType.PERSONAL, true, true); } static public InternalPhone createDefaultPersonalInternalPhone(final Party party) { return new InternalPhone(party, PartyContactType.PERSONAL, true, true); } static public InternalPhone createDefaultPersonalInternalPhone(final Party party, String number) { return new InternalPhone(party, PartyContactType.PERSONAL, true, true, number); } static public MobilePhone createDefaultPersonalMobilePhone(final Party party, final String number) { return new MobilePhone(party, PartyContactType.PERSONAL, true, true, number); } static public MobilePhone createMobilePhone(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact, final String number) { return new MobilePhone(party, type, visible, defaultContact, number); } static public EmailAddress createDefaultPersonalEmailAddress(final Party party) { return new EmailAddress(party, PartyContactType.PERSONAL, true, true); } static public EmailAddress createDefaultPersonalEmailAddress(final Party party, final String value) { return createEmailAddress(party, PartyContactType.PERSONAL, true, true, value); } static public EmailAddress createEmailAddress(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact, final String value) { return new EmailAddress(party, type, visible, defaultContact, value); } @ISCTE static public EmailAddress createDefaultInstitutionalEmailAddress(final Party party) { return new EmailAddress(party, PartyContactType.INSTITUTIONAL, true, true); } @ISCTE static public PhysicalDeliveryOfficeContact createDefaultPhysicalDeliveryOfficeContact(final Party party) { return new PhysicalDeliveryOfficeContact(party, PartyContactType.INSTITUTIONAL, true, true); } @ISCTE static public PhysicalDeliveryOfficeContact createDefaultPhysicalDeliveryOfficeContact(final Party party, final AllocatableSpace value) { return new PhysicalDeliveryOfficeContact(party, PartyContactType.INSTITUTIONAL, true, true, value); } @ISCTE static public PhysicalDeliveryOfficeContact createPhysicalDeliveryOfficeContact(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact, final AllocatableSpace space) { Iterator iterator = party.getPartyContacts(PhysicalDeliveryOfficeContact.class).iterator(); while (iterator.hasNext()) { final PartyContact partyContact = iterator.next(); partyContact.delete(); iterator.remove(); } return new PhysicalDeliveryOfficeContact(party, type, visible, defaultContact, space); } @ISCTE static public PostBoxContact createDefaultPostBoxContact(final Party party) { return new PostBoxContact(party, PartyContactType.INSTITUTIONAL, true, true); } @ISCTE static public PostBoxContact createDefaultPostBoxContact(final Party party, final String value) { return new PostBoxContact(party, PartyContactType.INSTITUTIONAL, true, true, value); } @ISCTE static public PostBoxContact createPostBoxContact(final Party party, final PartyContactType type, final boolean visible, final boolean defaultContact, final String value) { Iterator iterator = party.getPartyContacts(PostBoxContact.class).iterator(); while (iterator.hasNext()) { final PartyContact partyContact = iterator.next(); partyContact.delete(); iterator.remove(); } return new PostBoxContact(party, type, visible, defaultContact, value); } @ISCTE static public Phone createDefaultWorkPhone(final Party party) { return new Phone(party, PartyContactType.WORK, true, true); } @ISCTE static public Phone createDefaultWorkPhone(final Party party, final String number) { return new Phone(party, PartyContactType.WORK, true, true, number); } @ISCTE public static Fax createDefaultPersonalFax(Party party) { return new Fax(party, PartyContactType.PERSONAL, true, true); } @ISCTE public static Fax createDefaultPersonalFax(final Party party, final String number) { return new Fax(party, PartyContactType.PERSONAL, true, true, number); } @ISCTE public static InternalPhone createDefaultInternalPhone(final Party party, final String number) { return new InternalPhone(party, PartyContactType.PERSONAL, true, true, number); } @ISCTE public void validate() { setIsValidated(true); setValidatedBy(AccessControl.hasPerson() ? AccessControl.getPerson().getUsername() : null); } @ISCTE public void invalidate() { setIsValidated(false); setValidatedBy(null); } @ISCTE public Boolean getIsToBeValidated() { return getType() == PartyContactType.PERSONAL && !getIsValidated(); } public LoginAlias getInstitutionalLoginAlias() { Party party = getParty(); Person person = (party != null && party.isPerson()) ? (Person) party : null; Login loginIdentification = person != null ? person.getLoginIdentification() : null; return loginIdentification != null ? loginIdentification.getInstitutionalLoginAlias() : null; } public boolean isDisplayableInPortal() { return Boolean.TRUE.equals(getVisiblePublic()); } @Override public Set getObjectsToIndex() { return (Set) ((hasParty() && hasRootDomainObject()) ? Collections.singleton((Indexable) getParty()) : Collections.emptySet()); } @ISCTE public static Fax createFax(final Party party, final PartyContactType type, boolean visible, boolean defaultContact, final String number) { return new Fax(party, type, visible, defaultContact, number); } }