| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.util; |
| 18 | |
|
| 19 | |
import java.io.PrintWriter; |
| 20 | |
import java.io.StringWriter; |
| 21 | |
import java.net.InetAddress; |
| 22 | |
import java.net.UnknownHostException; |
| 23 | |
import java.sql.Timestamp; |
| 24 | |
import java.util.ArrayList; |
| 25 | |
import java.util.Calendar; |
| 26 | |
import java.util.Collection; |
| 27 | |
import java.util.Collections; |
| 28 | |
import java.util.Comparator; |
| 29 | |
import java.util.Date; |
| 30 | |
import java.util.HashMap; |
| 31 | |
import java.util.HashSet; |
| 32 | |
import java.util.List; |
| 33 | |
import java.util.Map; |
| 34 | |
import java.util.Set; |
| 35 | |
|
| 36 | |
import org.apache.commons.collections.KeyValue; |
| 37 | |
import org.apache.commons.lang.StringUtils; |
| 38 | |
import org.apache.commons.lang.text.StrSubstitutor; |
| 39 | |
import org.kuali.rice.core.util.RiceConstants; |
| 40 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
| 41 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
| 42 | |
import org.kuali.rice.kim.util.KimConstants; |
| 43 | |
import org.kuali.rice.kns.bo.Parameter; |
| 44 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 0 | public class Utilities { |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | private static StrSubstitutor substitutor = new StrSubstitutor(new ConfigStringLookup()); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public static String substituteConfigParameters(String namespace, String string) { |
| 68 | 0 | StrSubstitutor sub = new StrSubstitutor(new ConfigStringLookup(namespace)); |
| 69 | 0 | return sub.replace(string); |
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public static String substituteConfigParameters(String string) { |
| 79 | 0 | return substitutor.replace(string); |
| 80 | |
} |
| 81 | |
|
| 82 | |
public static String getKNSParameterValue(String nameSpace, String detailType, String name) { |
| 83 | 0 | Parameter parameter = KNSServiceLocator.getParameterService().retrieveParameter(nameSpace, detailType, name); |
| 84 | 0 | if (parameter == null) { |
| 85 | 0 | return null; |
| 86 | |
} |
| 87 | 0 | return parameter.getParameterValue(); |
| 88 | |
} |
| 89 | |
|
| 90 | |
public static boolean getKNSParameterBooleanValue(String nameSpace, String detailType, String name) { |
| 91 | 0 | return getKNSParameterBooleanValue(nameSpace, detailType, name, false); |
| 92 | |
} |
| 93 | |
|
| 94 | |
public static boolean getKNSParameterBooleanValue(String nameSpace, String detailType, String name, boolean defaultValue) { |
| 95 | 0 | Parameter parameter = KNSServiceLocator.getParameterService().retrieveParameter(nameSpace, detailType, name); |
| 96 | 0 | if (parameter == null || StringUtils.isBlank(parameter.getParameterValue())) { |
| 97 | 0 | return defaultValue; |
| 98 | |
} |
| 99 | 0 | if (parameter.getParameterValue().trim().equals("Y")) { |
| 100 | 0 | return true; |
| 101 | |
} else { |
| 102 | 0 | return false; |
| 103 | |
} |
| 104 | |
} |
| 105 | |
|
| 106 | |
public static String parseGroupNamespaceCode(String namespaceAndNameCombo) { |
| 107 | 0 | if (namespaceAndNameCombo == null) { |
| 108 | 0 | return null; |
| 109 | |
} |
| 110 | 0 | String[] groupData = namespaceAndNameCombo.split(KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER); |
| 111 | 0 | if (groupData.length == 1) { |
| 112 | 0 | return KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE; |
| 113 | 0 | } else if (groupData.length == 2) { |
| 114 | 0 | return groupData[0].trim(); |
| 115 | |
} else { |
| 116 | 0 | return null; |
| 117 | |
} |
| 118 | |
} |
| 119 | |
|
| 120 | |
public static String parseGroupName(String namespaceAndNameCombo) { |
| 121 | 0 | if (namespaceAndNameCombo == null) { |
| 122 | 0 | return null; |
| 123 | |
} |
| 124 | 0 | String[] groupData = namespaceAndNameCombo.split(KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER); |
| 125 | 0 | if (groupData.length == 1) { |
| 126 | 0 | return groupData[0].trim(); |
| 127 | 0 | } else if (groupData.length == 2) { |
| 128 | 0 | return groupData[1].trim(); |
| 129 | |
} else { |
| 130 | 0 | return null; |
| 131 | |
} |
| 132 | |
} |
| 133 | |
|
| 134 | |
public static boolean isEmpty(String value) { |
| 135 | 0 | return value == null || value.trim().equals(""); |
| 136 | |
} |
| 137 | |
|
| 138 | |
public static boolean isEmpty(Collection collection) { |
| 139 | 0 | return collection == null || collection.isEmpty(); |
| 140 | |
} |
| 141 | |
|
| 142 | |
public static boolean isEmpty(Object[] array) { |
| 143 | 0 | return array == null || array.length == 0; |
| 144 | |
} |
| 145 | |
|
| 146 | |
public static boolean equals (Object a, Object b) { |
| 147 | 0 | return ((a == null && b == null) || (a != null && a.equals(b))); |
| 148 | |
} |
| 149 | |
|
| 150 | |
public static String collectStackTrace(Throwable t) { |
| 151 | 0 | StringWriter sw = new StringWriter(); |
| 152 | 0 | PrintWriter pw = new PrintWriter(sw); |
| 153 | 0 | t.printStackTrace(pw); |
| 154 | 0 | pw.close(); |
| 155 | 0 | return sw.toString(); |
| 156 | |
} |
| 157 | |
|
| 158 | |
public static Calendar convertTimestamp(Timestamp timestamp) { |
| 159 | 0 | if (timestamp == null) { |
| 160 | 0 | return null; |
| 161 | |
} |
| 162 | 0 | Calendar calendar = Calendar.getInstance(); |
| 163 | 0 | calendar.setTime(timestamp); |
| 164 | 0 | return calendar; |
| 165 | |
} |
| 166 | |
|
| 167 | |
public static Timestamp convertCalendar(Calendar calendar) { |
| 168 | 0 | if (calendar == null) { |
| 169 | 0 | return null; |
| 170 | |
} |
| 171 | 0 | return new Timestamp(calendar.getTime().getTime()); |
| 172 | |
} |
| 173 | |
|
| 174 | |
public static Set asSet(Object[] objects) { |
| 175 | 0 | Set set = new HashSet(); |
| 176 | 0 | if (objects != null) { |
| 177 | 0 | for (int index = 0; index < objects.length; index++) { |
| 178 | 0 | Object object = objects[index]; |
| 179 | 0 | set.add(object); |
| 180 | |
} |
| 181 | |
} |
| 182 | 0 | return set; |
| 183 | |
} |
| 184 | |
|
| 185 | |
public static Set asSet(Object object) { |
| 186 | 0 | Set set = new HashSet(); |
| 187 | 0 | if (object != null) { |
| 188 | 0 | set.add(object); |
| 189 | |
} |
| 190 | 0 | return set; |
| 191 | |
} |
| 192 | |
|
| 193 | |
public static List asList(Object object) { |
| 194 | 0 | List<Object> list = new ArrayList<Object>(1); |
| 195 | 0 | if (object != null) { |
| 196 | 0 | list.add(object); |
| 197 | |
} |
| 198 | 0 | return list; |
| 199 | |
} |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | 0 | public static class PrioritySorter implements Comparator<ActionRequestValue> { |
| 206 | |
public int compare(ActionRequestValue ar1, ActionRequestValue ar2) { |
| 207 | 0 | int value = ar1.getPriority().compareTo(ar2.getPriority()); |
| 208 | 0 | if (value == 0) { |
| 209 | 0 | value = ActionRequestValue.compareActionCode(ar1.getActionRequested(), ar2.getActionRequested(), true); |
| 210 | 0 | if (value == 0) { |
| 211 | 0 | if ( (ar1.getActionRequestId() != null) && (ar2.getActionRequestId() != null) ) { |
| 212 | 0 | value = ar1.getActionRequestId().compareTo(ar2.getActionRequestId()); |
| 213 | |
} else { |
| 214 | |
|
| 215 | 0 | value = 0; |
| 216 | |
} |
| 217 | |
} |
| 218 | |
} |
| 219 | 0 | return value; |
| 220 | |
} |
| 221 | |
} |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | 0 | public static class RouteLogActionRequestSorter extends PrioritySorter implements Comparator<ActionRequestValue> { |
| 228 | |
public int compare(ActionRequestValue ar1, ActionRequestValue ar2) { |
| 229 | 0 | if (! ar1.getChildrenRequests().isEmpty()) { |
| 230 | 0 | Collections.sort(ar1.getChildrenRequests(), this); |
| 231 | |
} |
| 232 | 0 | if (! ar2.getChildrenRequests().isEmpty()) { |
| 233 | 0 | Collections.sort(ar2.getChildrenRequests(), this); |
| 234 | |
} |
| 235 | |
|
| 236 | 0 | int routeLevelCompareVal = ar1.getRouteLevel().compareTo(ar2.getRouteLevel()); |
| 237 | 0 | if (routeLevelCompareVal != 0) { |
| 238 | 0 | return routeLevelCompareVal; |
| 239 | |
} |
| 240 | |
|
| 241 | 0 | if (ar1.isActive() && ar2.isPending()) { |
| 242 | 0 | return -1; |
| 243 | 0 | } else if (ar2.isActive() && ar1.isPending()) { |
| 244 | 0 | return 1; |
| 245 | |
} |
| 246 | |
|
| 247 | 0 | return super.compare(ar1, ar2); |
| 248 | |
} |
| 249 | |
} |
| 250 | |
|
| 251 | |
public static boolean validateDate(String date, boolean dateOptional) { |
| 252 | 0 | if ((date == null) || date.trim().equals("")) { |
| 253 | 0 | if (dateOptional) { |
| 254 | 0 | return true; |
| 255 | |
} else { |
| 256 | 0 | return false; |
| 257 | |
} |
| 258 | |
} |
| 259 | |
|
| 260 | |
try { |
| 261 | 0 | Date parsedDate = RiceConstants.getDefaultDateFormat().parse(date.trim()); |
| 262 | 0 | if (! RiceConstants.getDefaultDateFormat().format(parsedDate).equals(date)) return false; |
| 263 | 0 | Calendar calendar = Calendar.getInstance(); |
| 264 | 0 | calendar.setTime(parsedDate); |
| 265 | 0 | int yearInt = calendar.get(Calendar.YEAR); |
| 266 | 0 | if (yearInt <= 0 || yearInt > 2999) { |
| 267 | 0 | return false; |
| 268 | |
} |
| 269 | 0 | return true; |
| 270 | 0 | } catch (Exception ex) { |
| 271 | 0 | return false; |
| 272 | |
} |
| 273 | |
} |
| 274 | |
|
| 275 | |
public static boolean checkDateRanges(String fromDate, String toDate) { |
| 276 | |
try { |
| 277 | 0 | Date parsedDate = KNSServiceLocator.getDateTimeService().convertToDate(fromDate.trim()); |
| 278 | 0 | Calendar fromCalendar = Calendar.getInstance(); |
| 279 | 0 | fromCalendar.setLenient(false); |
| 280 | 0 | fromCalendar.setTime(parsedDate); |
| 281 | 0 | fromCalendar.set(Calendar.HOUR_OF_DAY, 0); |
| 282 | 0 | fromCalendar.set(Calendar.MINUTE, 0); |
| 283 | 0 | fromCalendar.set(Calendar.SECOND, 0); |
| 284 | 0 | fromCalendar.set(Calendar.MILLISECOND, 0); |
| 285 | 0 | parsedDate = KNSServiceLocator.getDateTimeService().convertToDate(toDate.trim()); |
| 286 | 0 | Calendar toCalendar = Calendar.getInstance(); |
| 287 | 0 | toCalendar.setLenient(false); |
| 288 | 0 | toCalendar.setTime(parsedDate); |
| 289 | 0 | toCalendar.set(Calendar.HOUR_OF_DAY, 0); |
| 290 | 0 | toCalendar.set(Calendar.MINUTE, 0); |
| 291 | 0 | toCalendar.set(Calendar.SECOND, 0); |
| 292 | 0 | toCalendar.set(Calendar.MILLISECOND, 0); |
| 293 | 0 | if (fromCalendar.after(toCalendar)) { |
| 294 | 0 | return false; |
| 295 | |
} |
| 296 | 0 | return true; |
| 297 | 0 | } catch (Exception ex) { |
| 298 | 0 | return false; |
| 299 | |
} |
| 300 | |
} |
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
public static boolean collectionsEquivalent(Collection a, Collection b) { |
| 309 | 0 | if (a == null && b == null) return true; |
| 310 | 0 | if (a == null ^ b == null) return false; |
| 311 | 0 | return a.containsAll(b) && b.containsAll(a); |
| 312 | |
} |
| 313 | |
|
| 314 | |
public static String getIpNumber() { |
| 315 | |
try { |
| 316 | 0 | return InetAddress.getLocalHost().getHostAddress(); |
| 317 | 0 | } catch (UnknownHostException e) { |
| 318 | 0 | throw new WorkflowRuntimeException("Error retrieving ip number.", e); |
| 319 | |
} |
| 320 | |
} |
| 321 | |
|
| 322 | |
public static String getHostName() { |
| 323 | |
try { |
| 324 | 0 | return InetAddress.getLocalHost().getHostName(); |
| 325 | 0 | } catch (UnknownHostException e) { |
| 326 | 0 | throw new WorkflowRuntimeException("Error retrieving host name.", e); |
| 327 | |
} |
| 328 | |
} |
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
public static <T,Z> Map<T, Z> getKeyValueCollectionAsMap(List<? extends KeyValue> collection) { |
| 338 | 0 | Map<T, Z> map = new HashMap<T, Z>(collection.size()); |
| 339 | 0 | for (KeyValue kv: collection) { |
| 340 | 0 | map.put((T) kv.getKey(), (Z) kv.getValue()); |
| 341 | |
} |
| 342 | 0 | return map; |
| 343 | |
} |
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
public static <T,Z extends KeyValue> Map<T, Z> getKeyValueCollectionAsLookupTable(List<Z> collection) { |
| 354 | 0 | Map<T, Z> map = new HashMap<T, Z>(collection.size()); |
| 355 | 0 | for (Z kv: collection) { |
| 356 | 0 | map.put((T) kv.getKey(), kv); |
| 357 | |
} |
| 358 | 0 | return map; |
| 359 | |
} |
| 360 | |
} |