package net.sourceforge.fenixedu.presentationTier.Action.contactsManagement; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; 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.contacts.WebAddress; import net.sourceforge.fenixedu.domain.organizationalStructure.Party; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.domain.space.AllocatableSpace; import net.sourceforge.fenixedu.injectionCode.AccessControl; import net.sourceforge.fenixedu.util.LanguageUtils; import pt.ist.fenixWebFramework.rendererExtensions.util.IPresentableEnum; public class PartyContactBean implements Serializable { public static enum AvailableContact implements IPresentableEnum { PHONE, INTERNAL_PHONE, EMAIL, PHYSICAL_DELIVERY, POST_BOX, WEB_ADDRESS; @Override public String getLocalizedName() { return ResourceBundle.getBundle("resources.ManagerResources", LanguageUtils.getLocale()).getString( "label.AvailableContact." + name()); } public static List getContactsForUnit() { List list = new ArrayList(); list.add(INTERNAL_PHONE); list.add(PHONE); if (AccessControl.getPerson().hasRole(RoleType.MANAGER)) { list.add(EMAIL); } return list; } public static List getContactsForPerson() { List list = new ArrayList(); list.add(INTERNAL_PHONE); list.add(PHONE); list.add(PHYSICAL_DELIVERY); if (AccessControl.getPerson().hasRole(RoleType.MANAGER)) { list.add(EMAIL); } list.add(POST_BOX); return list; } } private String value; private String description; private PartyContact contact; private AllocatableSpace space; private AvailableContact availableContact; private boolean publicVisible; private Integer exportOrder; public PartyContactBean(AvailableContact availableContact, Integer order) { setAvailableContact(availableContact); setExportOrder(order + 1); } public PartyContactBean(PartyContact contact, Integer defaultOrder) { setContact(contact); setDescription(contact.getDescription()); setPublicVisible(Boolean.TRUE.equals(contact.getVisiblePublic())); Integer contactExportOrder = contact.getExportOrder(); setExportOrder(contactExportOrder == null ? defaultOrder + 1 : contactExportOrder); if (contact.isPhone()) { setValue(((Phone) contact).getNumber()); setAvailableContact(AvailableContact.PHONE); } else if (contact.isInternalPhone()) { setValue(((InternalPhone) contact).getNumber()); setAvailableContact(AvailableContact.INTERNAL_PHONE); } else if (contact.isEmailAddress()) { setValue(((EmailAddress) contact).getValue()); setAvailableContact(AvailableContact.EMAIL); } else if (contact.isPhysicalDeliveryOfficeContact()) { setSpace(((PhysicalDeliveryOfficeContact) contact).getSpace()); setAvailableContact(AvailableContact.PHYSICAL_DELIVERY); } else if (contact.isPostBoxContact()) { setValue(((PostBoxContact) contact).getValue()); setAvailableContact(AvailableContact.POST_BOX); } else { throw new IllegalArgumentException(contact.getClass() + " is unsupported"); } } public AllocatableSpace getSpace() { return space; } public void setSpace(AllocatableSpace space) { this.space = space; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public PartyContact getContact() { return contact; } public void setContact(PartyContact contact) { this.contact = contact; } public AvailableContact getAvailableContact() { return availableContact; } public void setAvailableContact(AvailableContact availableContact) { this.availableContact = availableContact; } public boolean isPublicVisible() { return publicVisible; } public void setPublicVisible(boolean publicVisible) { this.publicVisible = publicVisible; } public Integer getExportOrder() { return exportOrder; } public void setExportOrder(Integer exportOrder) { this.exportOrder = exportOrder; } public boolean isNew() { return contact == null; } public void persist(Party party) { if (isNew()) { switch (availableContact) { case PHONE: setContact(PartyContact.createPhone(party, PartyContactType.INSTITUTIONAL, true, false, getValue())); break; case INTERNAL_PHONE: setContact(PartyContact.createInternalPhone(party, PartyContactType.INSTITUTIONAL, true, false, getValue())); break; case EMAIL: setContact(PartyContact.createEmailAddress(party, PartyContactType.INSTITUTIONAL, true, false, getValue())); break; case PHYSICAL_DELIVERY: setContact(PartyContact.createPhysicalDeliveryOfficeContact(party, PartyContactType.INSTITUTIONAL, true, false, getSpace())); break; case POST_BOX: setContact(PartyContact.createPostBoxContact(party, PartyContactType.INSTITUTIONAL, true, false, getValue())); break; } } else { switch (availableContact) { case PHONE: ((Phone) contact).setNumber(getValue()); break; case INTERNAL_PHONE: ((InternalPhone) contact).setNumber(getValue()); break; case EMAIL: ((EmailAddress) contact).setValue(getValue()); break; case PHYSICAL_DELIVERY: ((PhysicalDeliveryOfficeContact) contact).setSpace(getSpace()); break; case POST_BOX: ((PostBoxContact) contact).setValue(getValue()); break; } } contact.setDescription(getDescription()); contact.setVisiblePublic(isPublicVisible()); contact.setExportOrder(getExportOrder()); } }