package net.sourceforge.fenixedu.presentationTier.Action.contactsManagement; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import net.sourceforge.fenixedu.domain.contacts.EmailAddress; import net.sourceforge.fenixedu.domain.contacts.InternalPhone; import net.sourceforge.fenixedu.domain.contacts.PartyContact; import net.sourceforge.fenixedu.domain.contacts.PartyContactType; import net.sourceforge.fenixedu.domain.contacts.Phone; import net.sourceforge.fenixedu.domain.contacts.PhysicalDeliveryOfficeContact; import net.sourceforge.fenixedu.domain.contacts.PostBoxContact; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.organizationalStructure.Party; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.injectionCode.AccessControl; import net.sourceforge.fenixedu.presentationTier.Action.contactsManagement.PartyContactBean.AvailableContact; import org.apache.commons.beanutils.BeanComparator; public class PartyContactsHolder implements Serializable { private Party party; private List unitContacts; private List toDelete; public PartyContactsHolder(Party party) { setParty(party); List contacts = new ArrayList(); for (PartyContact partyContact : party.getPartyContacts(Phone.class, PartyContactType.INSTITUTIONAL)) { contacts.add(new PartyContactBean(partyContact, contacts.size())); } for (PartyContact partyContact : party.getPartyContacts(InternalPhone.class, PartyContactType.INSTITUTIONAL)) { contacts.add(new PartyContactBean(partyContact, contacts.size())); } for (PartyContact partyContact : party.getPartyContacts(PhysicalDeliveryOfficeContact.class, PartyContactType.INSTITUTIONAL)) { contacts.add(new PartyContactBean(partyContact, contacts.size())); } for (PartyContact partyContact : party.getPartyContacts(PostBoxContact.class, PartyContactType.INSTITUTIONAL)) { contacts.add(new PartyContactBean(partyContact, contacts.size())); } for (PartyContact partyContact : party.getPartyContacts(EmailAddress.class, PartyContactType.INSTITUTIONAL)) { contacts.add(new PartyContactBean(partyContact, contacts.size())); } setUnitContacts(contacts); Collections.sort(unitContacts, new BeanComparator("exportOrder")); toDelete = new ArrayList(); } public Party getParty() { return party; } public void setParty(Party party) { this.party = party; } public List getUnitContacts() { return unitContacts; } public void setUnitContacts(List unitContacts) { this.unitContacts = unitContacts; } public void addNewContact(AvailableContact availableContact) { this.unitContacts.add(new PartyContactBean(availableContact, unitContacts.size() + 1)); Collections.sort(unitContacts, new BeanComparator("exportOrder")); } public void removeContact(int index) { PartyContactBean removed = this.unitContacts.remove(index); if (!removed.isNew()) { toDelete.add(removed); } } public List getContactsToRemove() { return toDelete; } public void persist() { Integer lastOrder = -1; for (PartyContactBean bean : unitContacts) { if (lastOrder.equals(bean.getExportOrder())) { throw new DomainException("error.domain.contacts.cannotRepeatOrder"); } lastOrder = bean.getExportOrder(); bean.persist(getParty()); } for (PartyContactBean bean : toDelete) { bean.getContact().delete(); } toDelete.clear(); } public boolean isAllowedToAddPhone() { for (PartyContactBean bean : unitContacts) { if (bean.getAvailableContact() == AvailableContact.INTERNAL_PHONE) { return true; } } return false; } public boolean isAllowedToAddPhysicalDeliveryAddress() { for (PartyContactBean bean : unitContacts) { if (bean.getAvailableContact() == AvailableContact.PHYSICAL_DELIVERY) { return false; } } return true; } public boolean isAllowedToAddPostBox() { for (PartyContactBean bean : unitContacts) { if (bean.getAvailableContact() == AvailableContact.POST_BOX) { return false; } } return true; } }