001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.util;
018:
019: import java.io.PrintWriter;
020: import java.io.StringWriter;
021: import java.net.InetAddress;
022: import java.net.UnknownHostException;
023: import java.sql.Timestamp;
024: import java.util.ArrayList;
025: import java.util.Calendar;
026: import java.util.Collection;
027: import java.util.Collections;
028: import java.util.Comparator;
029: import java.util.Date;
030: import java.util.HashSet;
031: import java.util.List;
032: import java.util.Set;
033:
034: import org.apache.commons.lang.text.StrSubstitutor;
035:
036: import edu.iu.uis.eden.EdenConstants;
037: import edu.iu.uis.eden.KEWServiceLocator;
038: import edu.iu.uis.eden.actionrequests.ActionRequestValue;
039: import edu.iu.uis.eden.applicationconstants.ApplicationConstant;
040: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
041: import edu.iu.uis.eden.user.WorkflowUser;
042:
043: /**
044: * Various static utility methods.
045: *
046: * @author bmcgough
047: * @author rkirkend
048: * @author ewestfal
049: * @author ahamid
050: */
051: public class Utilities {
052: /**
053: * Commons-Lang StrSubstitor which substitutes variables specified like ${name} in strings,
054: * using a lookup implementation that pulls variables from the core config
055: */
056: private static StrSubstitutor substitutor = new StrSubstitutor(
057: new ConfigStringLookup());
058:
059: /**
060: * Performs variable substitution on the specified string, replacing variables specified like ${name}
061: * with the value of the corresponding config parameter obtained from the current context Config object
062: * @param string the string on which to perform variable substitution
063: * @return a string with any variables substituted with configuration parameter values
064: */
065: public static String substituteConfigParameters(String string) {
066: return substitutor.replace(string);
067: }
068:
069: public static String getApplicationConstant(String name) {
070: ApplicationConstant constant = KEWServiceLocator
071: .getApplicationConstantsService().findByName(name);
072: if (constant == null) {
073: return null;
074: }
075: return constant.getApplicationConstantValue();
076: }
077:
078: public static boolean getBooleanConstant(String name,
079: boolean defaultValue) {
080: String value = getApplicationConstant(name);
081: if (value == null) {
082: return defaultValue;
083: }
084: return Boolean.valueOf(value);
085: }
086:
087: public static boolean isEmpty(String value) {
088: return value == null || value.trim().equals("");
089: }
090:
091: public static boolean isEmpty(Collection collection) {
092: return collection == null || collection.isEmpty();
093: }
094:
095: public static boolean isEmpty(Object[] array) {
096: return array == null || array.length == 0;
097: }
098:
099: public static boolean equals(Object a, Object b) {
100: return ((a == null && b == null) || (a != null && a.equals(b)));
101: }
102:
103: public static String collectStackTrace(Throwable t) {
104: StringWriter sw = new StringWriter();
105: PrintWriter pw = new PrintWriter(sw);
106: t.printStackTrace(pw);
107: pw.close();
108: return sw.toString();
109: }
110:
111: public static boolean isAdmin(WorkflowUser user) {
112: String adminUserString = getApplicationConstant("Config.Application.AdminUserList");
113: String[] users = adminUserString.split(" ");
114: for (int i = 0; i < users.length; i++) {
115: if (users[i].equalsIgnoreCase(user
116: .getAuthenticationUserId().getAuthenticationId())) {
117: return true;
118: }
119: }
120: return false;
121: }
122:
123: public static Calendar convertTimestamp(Timestamp timestamp) {
124: if (timestamp == null) {
125: return null;
126: }
127: Calendar calendar = Calendar.getInstance();
128: calendar.setTime(timestamp);
129: return calendar;
130: }
131:
132: public static Timestamp convertCalendar(Calendar calendar) {
133: if (calendar == null) {
134: return null;
135: }
136: return new Timestamp(calendar.getTime().getTime());
137: }
138:
139: public static Set asSet(Object[] objects) {
140: Set set = new HashSet();
141: if (objects != null) {
142: for (int index = 0; index < objects.length; index++) {
143: Object object = objects[index];
144: set.add(object);
145: }
146: }
147: return set;
148: }
149:
150: public static Set asSet(Object object) {
151: Set set = new HashSet();
152: if (object != null) {
153: set.add(object);
154: }
155: return set;
156: }
157:
158: public static List asList(Object object) {
159: List<Object> list = new ArrayList<Object>(1);
160: if (object != null) {
161: list.add(object);
162: }
163: return list;
164: }
165:
166: /**
167: *
168: * Consider moving out of this class if this bugs
169: */
170: public class PrioritySorter implements Comparator {
171: public int compare(Object arg0, Object arg1) {
172: ActionRequestValue ar1 = (ActionRequestValue) arg0;
173: ActionRequestValue ar2 = (ActionRequestValue) arg1;
174: int value = ar1.getPriority().compareTo(ar2.getPriority());
175: if (value == 0) {
176: value = ActionRequestValue
177: .compareActionCode(ar1.getActionRequested(),
178: ar2.getActionRequested());
179: if (value == 0) {
180: value = ar1.getActionRequestId().compareTo(
181: ar2.getActionRequestId());
182: }
183: }
184: return value;
185: }
186: }
187:
188: /**
189: *
190: * Consider moving out of this class if this bugs
191: */
192: public class RouteLogActionRequestSorter implements Comparator {
193: public int compare(Object arg0, Object arg1) {
194: ActionRequestValue ar1 = (ActionRequestValue) arg0;
195: ActionRequestValue ar2 = (ActionRequestValue) arg1;
196:
197: if (!ar1.getChildrenRequests().isEmpty()) {
198: Collections.sort(ar1.getChildrenRequests(),
199: new RouteLogActionRequestSorter());
200: }
201: if (!ar2.getChildrenRequests().isEmpty()) {
202: Collections.sort(ar2.getChildrenRequests(),
203: new RouteLogActionRequestSorter());
204: }
205:
206: int routeLevelCompareVal = ar1.getRouteLevel().compareTo(
207: ar2.getRouteLevel());
208: if (routeLevelCompareVal != 0) {
209: return routeLevelCompareVal;
210: }
211:
212: if (ar1.isActive() && ar2.isPending()) {
213: return -1;
214: } else if (ar2.isActive() && ar1.isPending()) {
215: return 1;
216: }
217:
218: return new Utilities().new PrioritySorter().compare(arg0,
219: arg1);
220: }
221: }
222:
223: public static boolean validateDate(String date, boolean dateOptional) {
224: if ((date == null) || date.trim().equals("")) {
225: if (dateOptional) {
226: return true;
227: } else {
228: return false;
229: }
230: }
231:
232: try {
233: Date parsedDate = EdenConstants.getDefaultDateFormat()
234: .parse(date.trim());
235: if (!EdenConstants.getDefaultDateFormat()
236: .format(parsedDate).equals(date))
237: return false;
238: Calendar calendar = Calendar.getInstance();
239: calendar.setTime(parsedDate);
240: int yearInt = calendar.get(Calendar.YEAR);
241: if (yearInt <= 0 || yearInt > 2999) {
242: return false;
243: }
244: return true;
245: } catch (Exception ex) {
246: return false;
247: }
248: }
249:
250: public static boolean checkDateRanges(String fromDate, String toDate) {
251: try {
252: Date parsedDate = EdenConstants.getDefaultDateFormat()
253: .parse(fromDate.trim());
254: Calendar fromCalendar = Calendar.getInstance();
255: fromCalendar.setTime(parsedDate);
256: fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
257: fromCalendar.set(Calendar.MINUTE, 0);
258: fromCalendar.set(Calendar.SECOND, 0);
259: fromCalendar.set(Calendar.MILLISECOND, 0);
260: parsedDate = EdenConstants.getDefaultDateFormat().parse(
261: toDate.trim());
262: Calendar toCalendar = Calendar.getInstance();
263: toCalendar.setTime(parsedDate);
264: toCalendar.set(Calendar.HOUR_OF_DAY, 0);
265: toCalendar.set(Calendar.MINUTE, 0);
266: toCalendar.set(Calendar.SECOND, 0);
267: toCalendar.set(Calendar.MILLISECOND, 0);
268: if (fromCalendar.after(toCalendar)) {
269: return false;
270: }
271: return true;
272: } catch (Exception ex) {
273: return false;
274: }
275: }
276:
277: /**
278: * Performs a "brute force" comparison of collections by testing whether the collections contain each other.
279: * This circuments any particular uniqueness or ordering constraints on the collections
280: * (for instance, lists that are unordered but contain the same elements, where a hashset would not suffice
281: * for comparison purposes because it enforces element uniqueness)
282: */
283: public static boolean collectionsEquivalent(Collection a,
284: Collection b) {
285: if (a == null && b == null)
286: return true;
287: if (a == null ^ b == null)
288: return false;
289: return a.containsAll(b) && b.containsAll(a);
290: }
291:
292: public static String getIpNumber() {
293: try {
294: return InetAddress.getLocalHost().getHostAddress();
295: } catch (UnknownHostException e) {
296: throw new WorkflowRuntimeException(
297: "Error retrieving ip number.", e);
298: }
299: }
300:
301: public static String getHostName() {
302: try {
303: return InetAddress.getLocalHost().getHostName();
304: } catch (UnknownHostException e) {
305: throw new WorkflowRuntimeException(
306: "Error retrieving host name.", e);
307: }
308: }
309: }
|