Utils.java
001 package com.bea.medrec.webservices.swing;
002 
003 import com.bea.medrec.value.Address;
004 import com.bea.medrec.value.Patient;
005 //import com.bea.medrec.webservices.Address;
006 //import com.bea.medrec.webservices.Patient;
007 import java.text.ParseException;
008 import java.text.SimpleDateFormat;
009 import java.util.Calendar;
010 import java.util.Date;
011 
012 /**
013  <p>Util Class for Swing Client</p>
014  *
015  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
016  */
017 public class Utils {
018 
019   /**
020    <p>Takes an address value object and return an Address</p>
021    *
022    @param pAddress Address to be converted to an Address
023    @return Address
024    */
025    public static Address toAddress(Address pAddress)
026    {
027      Address Address = null;
028      if (pAddress != null) {
029        Address = new Address();
030        Address.setId(pAddress.getId());
031        Address.setStreetName1(pAddress.getStreetName1());
032        Address.setStreetName2(pAddress.getStreetName2());
033        Address.setCity(pAddress.getCity());
034        Address.setState(pAddress.getState());
035        Address.setZipCode(pAddress.getZipCode());
036        Address.setCountry(pAddress.getCountry());
037      }
038      return Address;
039   }
040 
041   /**
042    <p>Take a Patient and return a Patient</p>
043    *
044    @param pPatient Patient Value Object
045    @return Patient
046    */
047    public static Patient toPatient(Patient pPatient)
048    {
049      Patient Patient = null;
050      if (pPatient != null) {
051        Patient = new Patient();
052        Patient.setId(pPatient.getId());
053        Patient.setFirstName(pPatient.getFirstName());
054        Patient.setMiddleName(pPatient.getMiddleName());
055        Patient.setLastName(pPatient.getLastName());
056        Patient.setDateOfBirth(pPatient.getDateOfBirth());
057        Patient.setGender(pPatient.getGender());
058        Patient.setSsn(pPatient.getSsn());
059        Patient.setPhone(pPatient.getPhone());
060        Patient.setEmail(pPatient.getEmail());
061        Patient.setAddress(toAddress(pPatient.getAddress()));
062      }
063      return Patient;
064   }
065 
066   /**
067    <p>Take a string and return a Calendar object</p>
068    *
069    @param pString A string representing a Calendar
070    @return Calendar
071    */
072    public static Calendar str2Calendar(String pString)
073    {
074      Calendar cal = null;
075      if (pString != null) {
076        try {
077          SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
078          Date d = sdf.parse(pString);
079          cal = Calendar.getInstance();
080          cal.setTime(d);
081        }
082        catch(ParseException e) {   }
083      }
084      return cal;
085   }
086 
087  /**
088   <p>Check for valid date, ignoring delimitator. Format: MM/DD/YYYY</p>
089   *
090   @param date
091   @return boolean
092   */
093   public static boolean isValidDate(String date)
094   {
095     boolean valid = true;
096     try {
097       if (date.length() != 10return false;
098       String m = date.substring(0,2);
099       String d = date.substring(3,5);
100       String y = date.substring(6,10);
101       Integer.parseInt(m);
102       Integer.parseInt(d);
103       Integer.parseInt(y);
104     catch(Exception e) {
105       return false;
106     }
107     return valid;
108   }
109 
110          //   D A T E   M A N I P U L A T I O N
111  /**
112   <p>Format: MM/DD/YYYY</p>
113   *
114   @param pCalendar
115   @return String
116   */
117   public static String getDisplayDate(Calendar pCalendar)
118   {
119     SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
120     if (pCalendar != null) {
121       return format.format(pCalendar.getTime());
122     }
123     else return "";
124   }
125 
126 }