EditProfileAction.java
001 package com.bea.medrec.actions;
002 
003 import com.bea.medrec.beans.PatientBean;
004 import com.bea.medrec.utils.ClientException;
005 import com.bea.medrec.utils.MedRecLog4jFactory;
006 import java.util.HashMap;
007 import java.util.Map;
008 import javax.servlet.http.HttpServletRequest;
009 import javax.servlet.http.HttpServletResponse;
010 import org.apache.log4j.Logger;
011 import org.apache.struts.action.ActionErrors;
012 import org.apache.struts.action.ActionForm;
013 import org.apache.struts.action.ActionForward;
014 import org.apache.struts.action.ActionMapping;
015 
016 /**
017  <p>Controller to update user profile requests.</p>
018  *
019  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
020  */
021 public class EditProfileAction extends PatientBaseLookupDispatchAction {
022   private static Logger logger =
023       MedRecLog4jFactory.getLogger(EditProfileAction.class.getName());
024 
025   /**
026    <p>Mapping of action to method.</p>
027    *
028    @return Map
029    */
030   protected Map getKeyMethodMap() {
031     Map<String,String> map = new HashMap<String,String>();
032     map.put("button.Save""save");
033     map.put("button.Cancel""cancel");
034     return map;
035   }
036 
037   /**
038    <p>Default behavior.</p>
039    *
040    @return ActionForward
041    */
042   public ActionForward defaultMethod(ActionMapping mapping,
043                                      ActionForm form,
044                                      HttpServletRequest request,
045                                      HttpServletResponse response)
046       throws Exception {
047     logger.debug("Default method execution.");
048     // First time thru.
049     return mapping.findForward("edit.profile");
050   }
051 
052   /**
053    <p>Cancel edit patient profile.</p>
054    *
055    @return ActionForward
056    */
057   public ActionForward cancel(ActionMapping mapping,
058                               ActionForm form,
059                               HttpServletRequest request,
060                               HttpServletResponse response)
061       throws Exception {
062     logger.info("Cancel edit profile.");
063     ActionForward forward = null;
064     try {
065       form.reset(mapping, request);
066       forward = (mapping.findForward("view.profile"));
067     catch (Exception e) {
068       return handleException(e, request, mapping);
069     }
070     return forward;
071   }
072 
073   /**
074    <p>Save updated patient profile.</p>
075    *
076    @return ActionForward
077    */
078   public ActionForward save(ActionMapping mapping,
079                             ActionForm form,
080                             HttpServletRequest request,
081                             HttpServletResponse response)
082       throws ClientException, Exception {
083     logger.debug("Save updated profile.");
084     PatientBean origPatient = null;
085     PatientBean newPatient = null;
086     ActionErrors errors = null;
087 
088     // Get original patient from session.
089     origPatient = (PatientBean)getSessionAttribute(request, PATIENT_BEAN);
090     // Populate new patient from form, set original id values.
091     newPatient = (PatientBeanform;
092     newPatient.setId(origPatient.getId());
093     newPatient.getAddress().setId(origPatient.getAddress().getId());
094 
095     // Set new patient on session.  View presentation comes from session.
096     setSessionAttribute(request, PATIENT_BEAN, newPatient);
097 
098     try {
099       // Update presist patient information with new data.
100       getPatientSession().updatePatient(newPatient.toPatient());
101     catch (Exception e) {
102       throwClientException(e, mapping, "view.profile");
103     }
104 
105     return mapping.findForward("view.profile");
106   }
107 }