CreateVisitAction.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.RecordBean;
006 import com.bea.medrec.utils.ClientException;
007 import com.bea.medrec.utils.MedRecLog4jFactory;
008 import com.bea.medrec.utils.MedRecWebAppUtils;
009 import java.io.IOException;
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.*;
016 
017 /**
018  <p>Controller that handles the creation of a visit
019  * containing a new record and, if applicable, prescriptions.</p>
020  *
021  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
022  */
023 public class CreateVisitAction extends PhysBaseLookupDispatchAction {
024   private static Logger logger =
025       MedRecLog4jFactory.getLogger(CreateVisitAction.class.getName());
026 
027   /**
028    <p>The following is done because this Controller has three functions:
029    <ul>
030    <li>new visit (fresh form)
031    <li>delete existing prescription
032    <li>submitting visit
033    </ul>
034    * New visit and deleting an existing prescription actions are triggered
035    * by query params within a link, not through a form submit button,
036    * thus need manual method redirection. Submitting
037    * a new visit follows the standard dispatch action.</p>
038    *
039    @param mapping  The ActionMapping used to select this instance
040    @param form  The optional ActionForm bean for this request (if any)
041    @param request  The HTTP request we are processing
042    @param response  The HTTP response we are creating
043    */
044   public ActionForward execute(ActionMapping mapping,
045                                ActionForm form,
046                                HttpServletRequest request,
047                                HttpServletResponse response)
048       throws ClientException, Exception {
049     logger.debug("Create visit execute.");
050     // Check action param.
051     String action = request.getParameter(mapping.getParameter());
052     // If action is new visit or delete prescription, redirect appropriately.
053     if (NEW_VISIT.equals(action))
054       return newVisit(mapping, form, request, response);
055     else if (DELETE_PRESCRIPTION.equals(action))
056       return deletePrescription(mapping, form, request, response);
057     else
058       return super.execute(mapping, form, request, response);
059   }
060 
061   /**
062    <p>Mapping of action to method.</p>
063    *
064    @return Map
065    */
066   protected Map getKeyMethodMap() {
067     Map<String,String> map = new HashMap<String,String>();
068     map.put("button.Prescribe.Medication""prescribePrescription");
069     map.put("button.Save""save");
070     map.put("button.Reset""reset");
071     map.put("button.Cancel""cancel");
072     return map;
073   }
074 
075   /**
076    <p>Default behavior.</p>
077    *
078    @return ActionForward
079    */
080   public ActionForward defaultMethod(ActionMapping mapping,
081                                      ActionForm form,
082                                      HttpServletRequest request,
083                                      HttpServletResponse response)
084       throws Exception {
085     // Set new token for dup form submission check.
086     logger.info("Default action method.");
087     logger.debug("Setting token.");
088     saveToken(request);
089     return mapping.findForward("create.visit");
090   }
091 
092   /**
093    <p>Cancel new visit creation.</p>
094    *
095    @return ActionForward
096    */
097   public ActionForward cancel(ActionMapping mapping,
098                               ActionForm form,
099                               HttpServletRequest request,
100                               HttpServletResponse response)
101       throws Exception {
102     logger.info("Cancel create visit.");
103     ActionForward forward = null;
104     try {
105       // Remove record from session.
106       logger.info("Cleaning session of recordBean.");
107       super.removeSessionAttribute(request, RECORD_BEAN);
108       forward = mapping.findForward("cancel.record.success");
109     catch (Exception e) {
110       return handleException(e, request, mapping);
111     }
112     return forward;
113   }
114 
115   /**
116    <p>Delete prescribed prescription.</p>
117    *
118    @return ActionForward
119    */
120   public ActionForward deletePrescription(ActionMapping mapping,
121                                 ActionForm form,
122                                 HttpServletRequest request,
123                                 HttpServletResponse response)
124       throws Exception {
125     logger.info("Remove specified prescription.");
126 
127     // Declare local variables
128     String id = null;
129     RecordBean recordBean = null;
130 
131     // Remove prescribe prescription from record.
132     id = (Stringrequest.getParameter(PRESCRIPTION_ID);
133     if (isNotEmpty(id)) {
134       // Get record from session.
135       recordBean = (RecordBeangetSessionAttribute(request,
136           RECORD_BEAN);
137       // Remove prescriptions from record.
138       recordBean.removePrescription(id);
139     }
140     return mapping.findForward("create.visit");
141   }
142 
143   /**
144    <p>Start creating new visit.</p>
145    *
146    @return ActionForward
147    */
148   public ActionForward newVisit(ActionMapping mapping,
149                                 ActionForm form,
150                                 HttpServletRequest request,
151                                 HttpServletResponse response)
152       throws Exception {
153     logger.info("Create new visit.");
154 
155     // Make sure session is clean.
156     super.removeSessionAttribute(request, RECORD_BEAN);
157 
158     // Set new token for dup form submission check.
159     logger.debug("Setting token.");
160     saveToken(request);
161 
162     return mapping.findForward("create.visit");
163   }
164 
165   /**
166    <p>Prescribe medication during this visit.</p>
167    *
168    @return ActionForward
169    */
170   public ActionForward prescribePrescription(ActionMapping mapping,
171                                    ActionForm form,
172                                    HttpServletRequest request,
173                                    HttpServletResponse response)
174       throws IOException {
175     ActionForward forward = null;
176     try {
177       forward = prescribePrescriptionMethod(mapping, request);
178     catch (Exception e) {
179       return handleException(e, request, mapping);
180     }
181     return forward;
182   }
183 
184   /**
185    <p>Prescribe medication during this visit implementation.</p>
186    *
187    @return ActionForward
188    */
189   private ActionForward prescribePrescriptionMethod(ActionMapping mapping,
190                                           HttpServletRequest request)
191       throws Exception {
192     // Declare and initialize local variables
193     RecordBean recordBean = (RecordBeangetSessionAttribute(request,
194         RECORD_BEAN);
195 
196     // Prescribe prescription saves visit to session.
197     logger.info("Setting recordBean to session then redirect to create "+
198         "prescription.");
199     logger.debug(recordBean.toString());
200 
201     // Save to session.
202     setSessionAttribute(request, RECORD_BEAN, recordBean);
203     return mapping.findForward("create.prescription");
204   }
205 
206   /**
207    <p>Reset visit information.</p>
208    *
209    @return ActionForward
210    */
211   public ActionForward reset(ActionMapping mapping,
212                              ActionForm form,
213                              HttpServletRequest request,
214                              HttpServletResponse response)
215       throws Exception {
216     logger.info("Reset visit page.");
217     // Reset record.
218     super.removeSessionAttribute(request, RECORD_BEAN);
219     return mapping.findForward("create.visit");
220   }
221 
222   /**
223    <p>Saves visit.</p>
224    *
225    @return ActionForward
226    */
227   public ActionForward save(ActionMapping mapping,
228                             ActionForm form,
229                             HttpServletRequest request,
230                             HttpServletResponse response)
231       throws ClientException, Exception {
232     logger.info("Saving record.");
233 
234     // Declare and initialize local variables
235     RecordBean recordBean = (RecordBeangetSessionAttribute(request,
236         RECORD_BEAN);
237     ActionErrors errors = new ActionErrors();
238 
239     if (!isTokenValid(request)) {
240       logger.warn("Token not valid.");
241       errors.add("duplicate.submit"new ActionError("duplicate.submit"));
242     }
243 
244     resetToken(request);
245     if (!errors.isEmpty()) {
246       saveErrors(request, errors);
247       saveToken(request);
248       return mapping.findForward("validate.record.failure");
249     }
250 
251     // Save record to MedRec.
252     try {
253       // Set date, and patient and physician ids.
254       PatientBean patient =
255           (PatientBeangetSessionAttribute(request, PATIENT_BEAN);
256       PhysicianBean physician =
257           (PhysicianBeangetSessionAttribute(request, PHYSICIAN_BEAN);
258 
259       recordBean.setPatientId(patient.getId());
260       recordBean.setDate(MedRecWebAppUtils.getCurrentDate());
261 
262       // Let's see the record.
263       logger.debug(recordBean.toString());
264 
265       // Make sure session is clean.
266       super.removeSessionAttribute(request, RECORD_BEAN);
267 
268       // Pass transformed record value object.
269       getPhysicianSession().addRecord(physician.toPhysician(),
270           recordBean.toRecord());
271 
272       // Clean session of record.
273       super.removeSessionAttribute(request, RECORD_BEAN);
274     catch (Exception e) {
275       throwClientException(e, mapping, "validate.record.failure");
276     }
277     return mapping.findForward("save.record.success");
278   }
279 }