BaseBean.java
001 package com.bea.medrec.beans;
002 
003 import com.bea.medrec.utils.MedRecLog4jFactory;
004 import java.lang.reflect.Array;
005 import java.lang.reflect.Constructor;
006 import java.text.ParseException;
007 import java.text.SimpleDateFormat;
008 import java.util.*;
009 import org.apache.log4j.Logger;
010 import org.apache.struts.validator.ValidatorForm;
011 
012 /**
013  <p>Base bean encapsulating common bean attributes and functionality.</p>
014  *
015  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
016  */
017 abstract public class BaseBean extends ValidatorForm {
018   private static Logger logger =
019       MedRecLog4jFactory.getLogger(BaseBean.class.getName());
020 
021   // Instance Variables
022   private String action = "";
023   private String id = "";
024 
025   public BaseBean() {
026   }
027 
028   // Getters
029   public String getAction() {
030     return this.action;
031   }
032 
033   public String getId() {
034     return this.id;
035   }
036 
037   // Setters
038   public void setAction(String action) {
039     this.action = action;
040   }
041 
042   public void setId(String id) {
043     this.id = id;
044   }
045 
046   public void setId(Integer id) {
047     this.id = toStr(id);
048   }
049 
050   // Utility
051   protected String setDateStr(String str) {
052     SimpleDateFormat sdf = null;
053     String dateStr = "";
054     try {
055       sdf = new SimpleDateFormat("MM/dd/yyyy");
056       Date d = sdf.parse(str);
057       dateStr = sdf.format(d);
058     catch (ParseException e) {
059     }
060     return dateStr;
061   }
062 
063   protected static String toStr(Object obj) {
064     if (obj != null)
065       return obj.toString();
066     else
067       return null;
068   }
069 
070   // Validation
071   /**
072    <p>String null check.</p>
073    *
074    @param str
075    */
076   protected boolean isNotEmpty(String str) {
077     return str != null && str.length() 0;
078   }
079 
080   /**
081    <p>String null check.</p>
082    *
083    @param str
084    */
085   protected boolean isEmpty(String str) {
086     return !(isNotEmpty(str));
087   }
088 
089   protected Integer str2Integer(String str) {
090     logger.debug("Converting "+str+" to Integer");
091     Integer tempInt = null;
092     try {
093       if (!isEmpty(str))
094         tempInt = Integer.valueOf(str);
095     catch (NumberFormatException e) {
096     }
097     return tempInt;
098   }
099 
100  /**
101   <p>Convert string representation of a date to calendar.</p>
102   *
103   @param str
104   @return Calendar
105   */
106   protected Calendar str2Calendar(String str) {
107    logger.debug("Converting "+str+" to Calendar");
108     Calendar cal = null;
109     if (!isEmpty(str)) {
110       try {
111         SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
112         java.util.Date d = sdf.parse(str);
113         cal = Calendar.getInstance();
114         cal.setTime(d);
115       }
116       catch(ParseException e) {   }
117     }
118     return cal;
119   }
120 
121  /**
122   <p>Check for valid email.  Format only.  Format: a@a.aaa</p>
123   *
124   @param email
125   @return boolean
126   */
127   protected boolean isValidEmail(String email) {
128     boolean valid = true;
129     if (email == null) {
130       valid = false;
131       logger.warn("Email: invalid");
132     }
133     if (valid) {
134       String invalid[] {" ","#","&",":",";","^","%","!","*","(",")"};
135       for (int i = 0; i < invalid.length; i++) {
136         if (email.indexOf(invalid[i]) >= 0) {
137           valid = false;
138           logger.warn("Email: invalid");
139         }
140       }
141     }
142     if (valid) {
143       int atFound  = email.indexOf("@");
144       int dotFound = email.indexOf(".", atFound);
145 
146       if (atFound < || dotFound < || atFound+== dotFound) {
147         valid = false;
148         logger.warn("Email: invalid");
149       }
150     }
151     return valid;
152   }
153 
154   /**
155    <p>Check for valid social security number. Format: 999999999</p>
156    *
157    @param ssn
158    @return boolean
159    */
160   protected boolean isValidSsn(String ssn) {
161     boolean valid = true;
162     if (isEmpty(ssn)) {
163       logger.warn("SSN: invalid");
164       valid = false;
165     else if (ssn.length() != 9) {
166       logger.warn("SSN: invalid");
167       valid = false;
168     }
169     return valid;
170   }
171 
172   /**
173    <p>Converts a array to array of given class.</p>
174    *
175    @param pObjArray Array of objects
176    @param pClazz Class of newly transformed array
177    @return Object Array of given Class objects
178    */
179   public Object toBeanArray(Object[] pObjArray, Class pClazz) {
180     if (pObjArray != null && pObjArray.length > 0) {
181       Class cl = pObjArray.getClass().getComponentType();
182       logger.debug("Converting incoming "+cl.getName()+" array (len="+
183           pObjArray.length+") to array of "+pClazz.getName());
184       Object newObjArray = Array.newInstance(pClazz, pObjArray.length);
185       for (int i=0; i<pObjArray.length; i++) {
186         try {
187           Constructor constr = pClazz.getConstructor(new Class[]{cl});
188           logger.debug("Calling: "+constr.getName()+"("+cl+")");
189           Array.set(newObjArray, i, constr.newInstance(new Object[]{pObjArray[i]}));
190         catch (Exception e) {
191           logger.error("Unable to transform value object array.", e);
192         }
193       }
194       return newObjArray;
195     else {
196       logger.debug("Incoming array null or len=0");
197       return Array.newInstance(pClazz, 0);
198     }
199   }
200 
201   /**
202    <p>Converts a array to array of given class.</p>
203    *
204    @param pObjArray Array of objects
205    @param pClazz Class of newly transformed array
206    @return Collection Collection of given Class objects
207    */
208   public Collection<Object> toCollectionBean(Object[] pObjArray, 
209                                              Class pClazz) {
210     if (pObjArray != null && pObjArray.length > 0) {
211       Class cl = pObjArray.getClass().getComponentType();
212       logger.debug("Converting incoming "+cl.getName()+" array (len="+
213           pObjArray.length+") to array of "+pClazz.getName());
214       Collection<Object> newCollection = new ArrayList<Object>();
215       for (int i=0; i<pObjArray.length; i++) {
216         try {
217           Constructor constr = pClazz.getConstructor(new Class[]{cl});
218           logger.debug("Calling: "+constr.getName()+"("+cl+")");
219           newCollection.add(constr.newInstance(new Object[]{pObjArray[i]}));
220         catch (Exception e) {
221           logger.error("Unable to transform value object array.", e);
222         }
223       }
224       return newCollection;
225     else {
226       logger.debug("Incoming array null or len=0");
227       return new ArrayList<Object>();
228     }
229   }
230 
231   /**
232    <p>Converts a Collection to array of given class.</p>
233    *
234    @param objCol Collection of objects
235    @param pClazz Class of newly transformed array
236    @return Object Array of given Class objects
237    */
238   protected Object toArray(Collection objCol, Class pClazz) {
239     if (objCol != null && objCol.size() 0) {
240       Object newObjArray = null;
241       Constructor constr = null;
242       try {
243         newObjArray = Array.newInstance(pClazz, objCol.size());
244         Iterator itr = objCol.iterator();
245         Object obj = (Objectitr.next();
246         Class cl = obj.getClass();
247         logger.debug("Converting incoming "+cl.getName()+" array (len="+
248           objCol.size()+") to array of "+pClazz.getName());
249         logger.debug("Calling: "+constr.getName()+"("+cl+")");
250         constr = pClazz.getConstructor(new Class[]{cl});
251         int i = 0;
252         while (itr.hasNext()) {
253           obj = (Objectitr.next();
254           Array.set(newObjArray, i, constr.newInstance(new Object[]{obj}));
255           i++;
256         }
257       catch (Exception e) {
258           logger.error("Unable to transform value object array", e);
259       }
260       return newObjArray;
261     else {
262       logger.debug("Incoming array null or len=0");
263       return Array.newInstance(pClazz, 0);
264     }
265   }
266 }