package net.sourceforge.fenixedu.presentationTier.Action.contactsManagement; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixDispatchAction; import net.sourceforge.fenixedu.presentationTier.Action.contactsManagement.PartyContactBean.AvailableContact; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import pt.ist.fenixWebFramework.renderers.utils.RenderUtils; import pt.ist.fenixWebFramework.services.Service; public abstract class ContactsConfiguration extends FenixDispatchAction { protected static final String ERROR_OPERATOR_CONTACTS_MANAGEMENT_TO_ADD_A_PHONE_FIRST_ADD_AN_INTERNAL_PHONE = "error.operator.contactsManagement.toAddAPhoneFirstAddAnInternalPhone"; protected static final String ERROR_OPERATOR_CONTACTS_MANAGEMENT_ONLY_ONE_POSTBOX = "error.operator.contactsManagement.onlyOnePostBox"; protected static final String ERROR_OPERATOR_CONTACTS_MANAGEMENT_ONLY_ONE_PHYSICAL_DELIVERY_ADDRESS = "error.operator.contactsManagement.onlyOnePhysicalDeliveryAddress"; public ActionForward addContact(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { PartyContactsHolder holder = (PartyContactsHolder) getRenderedObject("bean"); AvailableContact availableContact = AvailableContact.valueOf(request.getParameter("type")); if (availableContact == AvailableContact.PHONE && !holder.isAllowedToAddPhone()) { addActionMessage(request, ERROR_OPERATOR_CONTACTS_MANAGEMENT_TO_ADD_A_PHONE_FIRST_ADD_AN_INTERNAL_PHONE); } else if (availableContact == AvailableContact.PHYSICAL_DELIVERY && !holder.isAllowedToAddPhysicalDeliveryAddress()) { addActionMessage(request, ERROR_OPERATOR_CONTACTS_MANAGEMENT_ONLY_ONE_PHYSICAL_DELIVERY_ADDRESS); } else if (availableContact == availableContact.POST_BOX && !holder.isAllowedToAddPostBox()) { addActionMessage(request, ERROR_OPERATOR_CONTACTS_MANAGEMENT_ONLY_ONE_POSTBOX); } else { addNewContact(holder, availableContact); } request.setAttribute("editMode", Boolean.TRUE); request.setAttribute("bean", holder); RenderUtils.invalidateViewState(); return forwardToDetails(mapping, request); } private void addNewContact(PartyContactsHolder holder, AvailableContact availableContact) { holder.addNewContact(availableContact); } public ActionForward removeContact(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { PartyContactsHolder holder = (PartyContactsHolder) getRenderedObject("bean"); removeIndex(holder, Integer.valueOf(request.getParameter("index"))); request.setAttribute("editMode", Boolean.TRUE); request.setAttribute("bean", holder); RenderUtils.invalidateViewState(); return forwardToDetails(mapping, request); } private void removeIndex(PartyContactsHolder holder, int index) { holder.removeContact(index); } public ActionForward saveContacts(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { PartyContactsHolder holder = (PartyContactsHolder) getRenderedObject("bean"); try { persist(holder); } catch (DomainException e) { request.setAttribute("editMode", Boolean.TRUE); addActionMessage(request, e.getMessage()); holder = new PartyContactsHolder(holder.getParty()); RenderUtils.invalidateViewState(); } request.setAttribute("bean", holder); return forwardToDetails(mapping, request); } @Service private void persist(PartyContactsHolder holder) { holder.persist(); } protected abstract ActionForward forwardToDetails(ActionMapping mapping, HttpServletRequest request); }