SearchResultsAction.java
001 package com.bea.medrec.actions;
002 
003 import com.bea.medrec.beans.SearchBean;
004 import com.bea.medrec.utils.ClientException;
005 import com.bea.medrec.utils.MedRecLog4jFactory;
006 import com.bea.medrec.value.Patient;
007 import java.util.HashMap;
008 import java.util.Map;
009 import javax.servlet.http.HttpServletRequest;
010 import javax.servlet.http.HttpServletResponse;
011 import org.apache.log4j.Logger;
012 import org.apache.struts.action.ActionErrors;
013 import org.apache.struts.action.ActionForm;
014 import org.apache.struts.action.ActionForward;
015 import org.apache.struts.action.ActionMapping;
016 
017 /**
018  <p>Search controller.  Handles all search requests.</p>
019  *
020  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
021  */
022 public class SearchResultsAction extends PhysBaseLookupDispatchAction {
023 
024   private static Logger logger =
025       MedRecLog4jFactory.getLogger(SearchResultsAction.class.getName());
026 
027   /**
028    <p>Mapping of action to method.</p>
029    *
030    @return Map
031    */
032   protected Map getKeyMethodMap() {
033     Map<String,String> map = new HashMap<String,String>();
034     map.put("button.Search""search");
035     return map;
036   }
037 
038   /**
039    <p>Default behavior.</p>
040    *
041    @return ActionForward
042    */
043   public ActionForward defaultMethod(ActionMapping mapping,
044                                      ActionForm form,
045                                      HttpServletRequest request,
046                                      HttpServletResponse response)
047       throws Exception {
048     logger.info("Default action method.");
049     SearchBean search = (SearchBeansuper.getSessionAttribute(request,
050         PREVIOUS_SEARCH);
051 
052     // If search null, then new search.
053     if (search == nullreturn mapping.findForward("search.home");
054 
055     // Process search.
056     try {
057       processSearch(request, search);
058     catch (Exception e) {
059       throwClientException(e, mapping, "search.home");
060     }
061     return mapping.findForward("search.results");
062   }
063 
064   /**
065    <p>Searches based on user's criteria.</p>
066    *
067    @return ActionForward
068    */
069   public ActionForward search(ActionMapping mapping,
070                               ActionForm form,
071                               HttpServletRequest request,
072                               HttpServletResponse response)
073       throws ClientException, Exception {
074     logger.info("New search.");
075     SearchBean search = (SearchBeanform;
076 
077     // Validate search.
078     // Report any errors we have discovered back to the original form.
079     ActionErrors errors = search.validate();
080     if (!errors.isEmpty()) {
081       saveErrors(request, errors);
082       return mapping.findForward("search.invalid");
083     }
084 
085     // Process search.
086     try {
087       processSearch(request, search);
088     catch (Exception e) {
089       throwClientException(e, mapping, "search.home");
090     }
091     return mapping.findForward("search.results");
092   }
093 
094   /**
095    <p>Search for patients.  Once retrieved, store the results to the
096    * session.  Also save search criteria to handle when user returns to
097    * search results page.</p>
098    */
099   private void processSearch(HttpServletRequest request, SearchBean search)
100       throws Exception {
101     logger.info("Process search.");
102 
103     // pass transformed search value object.
104     Patient[] patients = getPhysicianSession().searchPatients(
105         search.toSearch());
106 
107     logger.debug("Removing old patient.");
108     super.removeSessionAttribute(request, PATIENT_BEAN);
109 
110     logger.debug("Setting patient result collection.");
111     super.setSessionAttribute(request, PATIENT_BEANS,
112         toObjectBeanArray(patients, com.bea.medrec.beans.PatientBean.class));
113 
114     logger.debug("Setting search criteria.");
115     super.setSessionAttribute(request, PREVIOUS_SEARCH, search);
116 
117   }
118 }