001 package com.bea.medrec.utils;
002
003 import java.text.ParseException;
004 import java.text.SimpleDateFormat;
005 import java.util.*;
006 import javax.servlet.http.Cookie;
007 import javax.servlet.http.HttpServletRequest;
008 import org.apache.log4j.Logger;
009 import org.apache.struts.action.ActionForward;
010 import org.apache.struts.action.ActionMapping;
011
012 /**
013 * <p>Utility methods used by web applications.</p>
014 *
015 * @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
016 */
017 public class MedRecWebAppUtils {
018
019 private static Logger logger =
020 MedRecLog4jFactory.getLogger(MedRecWebAppUtils.class.getName());
021
022 private static MedRecWebAppUtils instance;
023
024 private MedRecWebAppUtils() { }
025
026 /**
027 * <p>Get instance of ServiceLocator.</p>
028 *
029 * @return ServiceLocator
030 */
031 public static MedRecWebAppUtils getInstance() {
032 if (instance == null) instance = new MedRecWebAppUtils();
033 return instance;
034 }
035
036 // L O C A L I Z A T I O N
037 /**
038 * <p>Get locale cookie.</p>
039 *
040 * @param request
041 * @return Locale
042 */
043 public static Locale getLocaleFromCookie(HttpServletRequest request) {
044 Locale locale = null;
045 Cookie cookie = null;
046 String country = null;
047 String language = null;
048 Cookie[] cookies = request.getCookies();
049
050 // Start locale processing.
051 if (cookies != null) {
052 for (int i=0; i < cookies.length; i++) {
053 cookie = cookies[i];
054 if ("Language".equals(cookie.getName())) {
055 language = cookie.getValue();
056 logger.debug("Found language cookie with value = "+language);
057 } else if (cookie.getName().equals("Country")) {
058 country = cookie.getValue();
059 logger.debug("Found country cookie with value = "+country);
060 }
061 }
062 }
063
064 if (country != null && language != null) {
065 locale = new Locale(language, country);
066 }
067
068 return locale;
069 }
070
071 /**
072 * <p>String null check.</p>
073 *
074 * @param locale
075 * @return boolean
076 */
077 public static boolean isValidLocale(Locale locale) {
078 return (locale.getLanguage().equals("ja")
079 || locale.getLanguage().equals("en")
080 || locale.getLanguage().equals("es"));
081 }
082
083 // S T R I N G M A N I P U L A T I O N
084 /**
085 * <p>String null check.</p>
086 *
087 * @param str
088 */
089 public static boolean isNotEmpty(String str) {
090 return str != null && str.length() > 0;
091 }
092
093 /**
094 * <p>String null check.</p>
095 *
096 * @param str
097 */
098 public static boolean isEmpty(String str) { return !(isNotEmpty(str)); }
099
100 /**
101 * <p>Return trim, non-null representation of object.</p>
102 *
103 * @param obj
104 * @return String
105 */
106 public static String cleanParam(Object obj) {
107 String str = String.valueOf(obj);
108 if (str == null || str.equals("null")) str = "";
109 return str.trim();
110 }
111
112 /**
113 * <p>Check for valid string, no special chars including:
114 * "#","&","^","%","*","/","\\","(",")"</p>
115 *
116 * @param str
117 * @return boolean
118 */
119 public static boolean isValidString(String str) {
120 boolean valid = true;
121 if (valid) {
122 String invalid[] = {"#","&","^","%","*","/","\\","(",")"};
123 for(int i = 0; i < invalid.length; i++) {
124 if(str.indexOf(invalid[i]) >= 0) { valid = false; }
125 }
126 }
127 return valid;
128 }
129
130 /**
131 * <p>Trim textbox to 40 chars. Remaining display with elipse.</p>
132 *
133 * @param str
134 * @return String
135 */
136 public static String trimToSummaryStr(String str) {
137 int endLength = 40;
138
139 if (isEmpty(str)) return str;
140
141 if (str.length() > endLength) {
142 String tmpStr = str.substring(0, str.indexOf(" ", endLength));
143 tmpStr += "...";
144 return tmpStr;
145 }
146 else {
147 return str;
148 }
149 }
150
151 // D A T E M A N I P U L A T I O N
152 /**
153 * <p>String representation of a calendar. Format: MM/DD/YYYY</p>
154 *
155 * @param pCalendar
156 * @return String
157 */
158 public static String getDisplayDate(Calendar pCalendar) {
159 SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
160 if (pCalendar != null) return format.format(pCalendar.getTime());
161 else return "";
162 }
163
164 /**
165 * <p>Convert string representation of a date to calendar.</p>
166 *
167 * @param str
168 * @return Calendar
169 */
170 public static Calendar str2Calendar(String str) {
171 Calendar cal = null;
172 if (!isEmpty(str)) {
173 try {
174 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
175 java.util.Date d = sdf.parse(str);
176 cal = Calendar.getInstance();
177 cal.setTime(d);
178 }
179 catch(ParseException e) { }
180 }
181 return cal;
182 }
183
184 /**
185 * <p>String representation of current date. Format: MM/DD/YYYY</p>
186 *
187 * @return String
188 */
189 public static String getCurrentDate() {
190 return getDisplayDate(GregorianCalendar.getInstance());
191 }
192
193 // M I S C E L L A N E O U S
194 /**
195 * <p>Sets user's locale.</p>
196 *
197 * @param request The HTTP request we are processing
198 */
199 protected String getLocaleCode(HttpServletRequest request) {
200 logger.debug("Setup locale.");
201 Locale locale = MedRecWebAppUtils.getLocaleFromCookie(request);
202 if (locale.getCountry() == null)
203 return locale.getDefault().getCountry();
204 else
205 return locale.getCountry();
206 }
207
208 /**
209 * <p>Print collection contents.</p>
210 *
211 * @param col
212 * @return String
213 */
214 public static String col2Str(Collection col) {
215 StringBuffer str = new StringBuffer();
216 if (col != null && !col.isEmpty()) {
217 str.append(" Num of "+col.getClass().getName()+": "+col.size()+" |");
218 Iterator itr = col.iterator();
219 while(itr.hasNext()) {
220 Object obj = itr.next();
221 str.append(" "+obj.toString());
222 }
223 }
224 else {
225 Class cl = col.getClass();
226 str.append(cl.getName()+": [null]");
227 }
228 return str.toString();
229 }
230
231 /**
232 * <p>Print collection contents.</p>
233 *
234 * @param pObjArray Array
235 * @return String
236 */
237 public static String array2Str(Object[] pObjArray) {
238 StringBuffer str = new StringBuffer();
239 if (pObjArray != null && pObjArray.length > 0) {
240 str.append(" Num of "+pObjArray.getClass().getName()+
241 ": "+pObjArray.length+" |");
242 for (int i=0; i<pObjArray.length; i++) {
243 Object obj = pObjArray[i];
244 str.append(" "+obj.toString());
245 }
246 }
247 else {
248 str.append(pObjArray.getClass().getName()+": [null]");
249 }
250 return str.toString();
251 }
252
253 /**
254 * <p>Get HTPP method- POST or GET. This is used to debug the query string.</p>
255 *
256 * @return String
257 */
258 public static String getHttpMethod() {
259 if (logger.isDebugEnabled()) return "GET";
260 else return "POST";
261 }
262
263 /**
264 * <p>Get full url path,
265 * i.e. http://localhost:port/<webapp context>
266 * </p>
267 *
268 * @param request
269 * @return String
270 */
271 public static String getUrlPath(HttpServletRequest request) {
272 return getUrlRoot(request)+request.getContextPath();
273 }
274
275 /**
276 * <p>Get localhost and port url root, i.e. http://localhost:port</p>
277 *
278 * @param request
279 * @return String
280 */
281 public static String getUrlRoot(HttpServletRequest request) {
282 String serverName = request.getServerName();
283 int serverPort = request.getServerPort();
284 String nextPage = "http://"+serverName+":"+serverPort;
285 logger.debug("URL root: "+nextPage);
286 return nextPage;
287 }
288
289 /**
290 * <p>Get servlet name from given mapping.</p>
291 *
292 * @param mapping
293 * @param name
294 * @return String
295 */
296 public static String getFullUrlPath(HttpServletRequest request,
297 ActionMapping mapping, String name)
298 {
299 return getUrlPath(request)+"/"+getServletName(mapping, name);
300 }
301
302 /**
303 * <p>Get servlet name from given mapping.</p>
304 *
305 * @param mapping
306 * @param name
307 * @return String
308 */
309 public static String getServletName(ActionMapping mapping, String name) {
310 ActionForward forward = mapping.findForward(name);
311 String path = forward.getPath();
312 return path.substring(path.indexOf("/")+1);
313 }
314
315 // E X C E P T I O N H A N D L I N G
316 /**
317 * <p>Parse exception stack finding the orignial exception message.</p>
318 *
319 * @param th
320 * @return String
321 */
322 public static String getRootErrMsg(Throwable th) {
323 if (th.getCause() != null)
324 return getRootError(th.getCause()).getLocalizedMessage();
325 else
326 return th.getLocalizedMessage();
327 }
328
329 /**
330 * <p>Parse exception stack finding the orignial exception.</p>
331 *
332 * @param th
333 * @return Throwable
334 */
335 public static Throwable getRootError(Throwable th) {
336 if (th instanceof javax.ejb.EJBException) {
337 Throwable nested = ((javax.ejb.EJBException)th).getCausedByException();
338 if (nested != null) return getRootError(nested);
339 else return th;
340 } else if (th instanceof java.rmi.RemoteException
341 || th instanceof java.rmi.AccessException
342 || th instanceof java.sql.SQLException
343 || th instanceof com.bea.medrec.exceptions.MedRecException) {
344 Throwable nested = th.getCause();
345 if (nested != null) return getRootError(nested);
346 else return th;
347 }
348 else return th;
349 }
350 }
|