CreatePrescriptionAction.java
001 package com.bea.medrec.actions;
002 
003 import com.bea.medrec.beans.PatientBean;
004 import com.bea.medrec.beans.PhysicianBean;
005 import com.bea.medrec.beans.PrescriptionBean;
006 import com.bea.medrec.beans.RecordBean;
007 import com.bea.medrec.utils.MedRecLog4jFactory;
008 import com.bea.medrec.utils.MedRecWebAppUtils;
009 import com.bea.medrec.value.Prescription;
010 import java.util.HashMap;
011 import java.util.Map;
012 import javax.servlet.http.HttpServletRequest;
013 import javax.servlet.http.HttpServletResponse;
014 import org.apache.log4j.Logger;
015 import org.apache.struts.action.ActionForm;
016 import org.apache.struts.action.ActionForward;
017 import org.apache.struts.action.ActionMapping;
018 
019 /**
020  <p>Controller to handle the creation of a prescriptions.</p>
021  *
022  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
023  */
024 public class CreatePrescriptionAction extends PhysBaseLookupDispatchAction {
025 
026   private static Logger logger =
027       MedRecLog4jFactory.getLogger(CreatePrescriptionAction.class.getName());
028 
029  /**
030   <p>Mapping of action to method.</p>
031   *
032   @return Map
033   */
034   protected Map getKeyMethodMap() {
035     Map<String,String> map = new HashMap<String,String>();
036     map.put("button.Save""save");
037     map.put("button.Cancel""cancel");
038     return map;
039   }
040 
041  /**
042   <p>Default behavior.</p>
043   *
044   @return ActionForward
045   */
046   public ActionForward defaultMethod(ActionMapping mapping,
047                                      ActionForm form,
048                                      HttpServletRequest request,
049                                      HttpServletResponse response)
050     throws Exception
051   {
052     // First timers
053     // Set new token for dup form submission check.
054     logger.info("Default action method.");
055     logger.debug("setting token.");
056     saveToken(request);
057     return mapping.findForward("create.prescription");
058   }
059 
060 
061  /**
062   <p>Cancel prescribe medication.</p>
063   *
064   @return ActionForward
065   */
066   public ActionForward cancel(ActionMapping mapping,
067                               ActionForm form,
068                               HttpServletRequest request,
069                               HttpServletResponse response)
070     throws Exception
071   {
072     logger.info("Cancel create prescription.");
073     ActionForward forward = null;
074     try {
075       forward = mapping.findForward("cancel.prescription");
076     }
077     catch(Exception e) {
078       return handleException(e, request, mapping);
079     }
080     return forward;
081   }
082 
083  /**
084   <p>Save prescription to be recorded.</p>
085   *
086   @return ActionForward
087   */
088   public ActionForward save(ActionMapping mapping,
089                             ActionForm form,
090                             HttpServletRequest request,
091                             HttpServletResponse response)
092     throws Exception
093   {
094     logger.info("Save prescription.");
095     PrescriptionBean prescriptionBean = (PrescriptionBean)form;
096     RecordBean recordBean = null;
097     Prescription[] prescriptionVOs = null;
098 
099     // Set patient and physician ids and current date.
100     setPrescriptionValues(request, prescriptionBean);
101 
102     // Every's okay.  Get record from session,
103     // add prescription, then reattach to session.
104     recordBean = (RecordBean)getSessionAttribute(request, RECORD_BEAN);
105 
106     // Add new prescription to record.
107     logger.debug("Adding : "+prescriptionBean.toString());
108     recordBean.addPrescription(prescriptionBean);
109     logger.debug("Num of prescriptions: "+recordBean.getNumOfPrescriptions());
110 
111     // Set record to session.
112     setSessionAttribute(request, RECORD_BEAN, recordBean);
113     return mapping.findForward("save.prescription.success");
114   }
115 
116  /**
117   * Sets known values from the session.
118   */
119   private void setPrescriptionValues(HttpServletRequest request,
120                                      PrescriptionBean prescriptionBean) {
121     logger.info("Setting patient and physician ids from session.");
122 
123     PatientBean patient =
124       (PatientBean)getSessionAttribute(request, PATIENT_BEAN);
125     PhysicianBean physician =
126       (PhysicianBean)getSessionAttribute(request, PHYSICIAN_BEAN);
127 
128     prescriptionBean.setPatientId(patient.getId());
129     prescriptionBean.setDatePrescribed(MedRecWebAppUtils.getCurrentDate());
130   }
131 }