001 package com.bea.medrec.utils;
002
003 import java.security.MessageDigest;
004 import java.security.NoSuchAlgorithmException;
005 import java.security.SecureRandom;
006 import java.sql.Date;
007 import java.text.SimpleDateFormat;
008 import java.util.Calendar;
009 import java.util.Collection;
010 import java.util.GregorianCalendar;
011 import java.util.Iterator;
012 import java.io.Serializable;
013 import org.apache.log4j.Logger;
014 import sun.misc.BASE64Encoder;
015
016 /**
017 * <p>Utility methods used through MedRec.</p>
018 *
019 * @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
020 */
021 public class MedRecUtils implements Serializable {
022 private static Logger logger =
023 MedRecLog4jFactory.getLogger(MedRecUtils.class.getName());
024
025 /**
026 * <p>String null check.</p>
027 *
028 * @param str
029 */
030 public static boolean isNotEmpty(String str) {
031 return str != null && str.length() > 0;
032 }
033
034 /**
035 * <p>String null check.</p>
036 *
037 * @param str
038 */
039 public static boolean isEmpty(String str) {
040 return !(isNotEmpty(str));
041 }
042
043 /**
044 * <p>Convert Date to Calendar.</p>
045 *
046 * @param date
047 * @return Calendar
048 */
049 public static Calendar convertSqlDate2Calendar(java.sql.Date date) {
050 Calendar cal = null;
051 if (date != null) {
052 cal = GregorianCalendar.getInstance();
053 cal.setTime(date);
054 }
055 return cal;
056 }
057
058 /**
059 * <p>String date representation of a converted long date.</p>
060 *
061 * @param pLong
062 * @return String
063 */
064 public static String long2DateStr(long pLong) {
065 Calendar cal = Calendar.getInstance();
066 cal.setTimeInMillis(pLong);
067 return getDisplayDate(cal);
068 }
069
070 /**
071 * <p>Get common display date from Calendar.</p>
072 *
073 * @param pCalendar
074 * @return String
075 */
076 public static String getDisplayDate(Calendar pCalendar) {
077 SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
078 if (pCalendar != null)
079 return format.format(pCalendar.getTime());
080 else
081 return "";
082 }
083
084 /**
085 * <p>Print contents of collection.</p>
086 *
087 * @param col
088 */
089 public static void printCollection(Collection<Object> col) {
090 if (col != null) {
091 Iterator itr = col.iterator();
092 logger.debug("#: " + col.size());
093 while (itr.hasNext()) {
094 logger.debug(itr.next().toString());
095 System.out.println(itr.next().toString());
096 }
097 }
098 }
099
100 /**
101 * <p>Get Date from Calendar.</p>
102 *
103 * @param pCal
104 */
105 public static Date getDate(Calendar pCal) {
106 java.util.Date date = pCal.getTime();
107 return new java.sql.Date(date.getTime());
108 }
109
110 /**
111 * <p>Group assignments.</p>
112 *
113 * @param groupType
114 * @return String[]
115 */
116 public static String[] getGroupArray(int groupType) {
117 String[] groupNames = null;
118
119 switch (groupType) {
120 case MedRecConstants.ADMIN_GROUP_TYPE:
121 groupNames = new String[1];
122 groupNames[0] = MedRecConstants.ADMIN_GROUP_NAME;
123 return groupNames;
124 case MedRecConstants.PATIENT_GROUP_TYPE:
125 groupNames = new String[1];
126 groupNames[0] = MedRecConstants.PATIENT_GROUP_NAME;
127 return groupNames;
128 case MedRecConstants.PHYSICIAN_GROUP_TYPE:
129 groupNames = new String[1];
130 groupNames[0] = MedRecConstants.PHYSICIAN_GROUP_NAME;
131 return groupNames;
132 default :
133 return groupNames;
134 }
135 }
136
137 /**
138 * <p></p>
139 *
140 * @param passToConvert
141 * @return
142 */
143 public static String encodePassword(String passToConvert) {
144 SecureRandom rnd = null;
145 try {
146 rnd = SecureRandom.getInstance("SHA1PRNG");
147 } catch (NoSuchAlgorithmException nsae) {
148 // If no SHA1 PRNG, try the old style.
149 rnd = new SecureRandom(
150 new Long(System.currentTimeMillis()).toString().getBytes());
151 }
152 String salt = "1234";
153 byte[] someBytes = salt.getBytes();
154 rnd.nextBytes(someBytes);
155 salt = new BASE64Encoder().encode(someBytes);
156 if (salt.length() > 4)
157 salt = salt.substring(0, 4);
158
159 String hashedValue = null;
160 try {
161 MessageDigest digest = MessageDigest.getInstance("sha-1");
162 digest.update(salt.getBytes());
163 digest.update(new String(passToConvert).getBytes());
164 byte[] pwdHashFromUser = digest.digest();
165 hashedValue = new BASE64Encoder().encode(pwdHashFromUser);
166 } catch (Exception e) {
167 e.printStackTrace();
168 return null;
169 }
170
171 String password = "{sha-1}" + salt + hashedValue;
172 return password;
173 }
174
175 // U T I L I T Y M E T H O D S
176 /**
177 * <p>Convert Calendar to String.</p>
178 */
179 public static String cal2Str(Calendar pCal) {
180 StringBuffer str = new StringBuffer();
181 if (pCal != null) {
182 int month = pCal.get(Calendar.MONTH) + 1;
183 int date = pCal.get(Calendar.DATE);
184 int year = pCal.get(Calendar.YEAR);
185 str.append(month);
186 str.append("/" + date);
187 str.append("/" + year);
188 }
189
190 return str.toString();
191 }
192 }
|