SearchBean.java
001 package com.bea.medrec.beans;
002 
003 import com.bea.medrec.utils.MedRecLog4jFactory;
004 import com.bea.medrec.value.Search;
005 import org.apache.log4j.Logger;
006 import org.apache.struts.action.ActionError;
007 import org.apache.struts.action.ActionErrors;
008 
009 /**
010  * Form bean for the patient search page.
011  * This form has the following fields:
012  <ul>
013  <li><b>last name</b> - Last name search option
014  <li><b>ssn</b> - Social security number search option
015  </ul>
016  *
017  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
018  */
019 public final class SearchBean extends BaseBean {
020   private static Logger logger = MedRecLog4jFactory.getLogger(SearchBean.class.getName());
021 
022   // Instance Variables
023   private String lastName = "";
024   private String ssn = "";
025 
026   // Constuctors
027   public SearchBean() {
028   }
029 
030   public SearchBean(String lastName, String ssn) {
031     this.lastName = lastName;
032     this.ssn = ssn;
033   }
034 
035   // Getters
036   public String getLastName() {
037     return this.lastName;
038   }
039 
040   public String getSsn() {
041     return this.ssn;
042   }
043 
044   // Setters
045   public void setLastName(String lastName) {
046     this.lastName = lastName;
047   }
048 
049   public void setSsn(String ssn) {
050     this.ssn = ssn;
051   }
052 
053   /**
054    <p>Validate search.  Due to the complexity of the input fields, this
055    * bean requires manual validation verus Struts' validation.</p>
056    *
057    @return ActionErrors
058    */
059   public ActionErrors validate() {
060     logger.debug("Validating search bean.");
061     logger.debug(toString());
062 
063     ActionErrors errors = new ActionErrors();
064 
065     if ((isEmpty(lastName&& isEmpty(ssn))
066         || (isNotEmpty(lastName&& isNotEmpty(ssn))) {
067       logger.warn("Entires emtpy or all populated.");
068       errors.add("multipleEntries"new ActionError("enter.one.option.only"));
069     else {
070       if (isEmpty(lastName&& !isValidSsn(ssn)) {
071         errors.add("ssn"new ActionError("invalid.ssn"));
072       }
073     }
074 
075     if (!errors.isEmpty()) {
076       logger.warn("SearchBean invalid. " + errors.size() " errors found.");
077     }
078 
079     return errors;
080   }
081 
082   /**
083    <p>Converts search presentation bean to search value object.</p>
084    *
085    @return RecordWS
086    */
087   public Search toSearch() {
088     return new Search(getLastName(), getSsn());
089   }
090 
091   public String toString() {
092     StringBuffer str = new StringBuffer();
093     str.append("Search [");
094     str.append("LastName: " + lastName);
095     str.append(" | SSN: " + ssn);
096     str.append("]");
097 
098     return str.toString();
099   }
100 
101 }