1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.util; |
17 | |
|
18 | |
import org.apache.commons.lang.text.StrSubstitutor; |
19 | |
import org.kuali.rice.core.api.services.CoreApiServiceLocator; |
20 | |
import org.kuali.rice.core.util.KeyValue; |
21 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
22 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
23 | |
import org.kuali.rice.kim.util.KimConstants; |
24 | |
|
25 | |
import java.net.InetAddress; |
26 | |
import java.net.UnknownHostException; |
27 | |
import java.util.*; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public final class Utilities { |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | private static final StrSubstitutor SUBSTITUTOR = new StrSubstitutor(new ConfigStringLookup()); |
41 | |
|
42 | 0 | private Utilities() { |
43 | 0 | throw new UnsupportedOperationException("do not call"); |
44 | |
} |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static String substituteConfigParameters(String applicationId, String string) { |
55 | 0 | StrSubstitutor sub = new StrSubstitutor(new ConfigStringLookup(applicationId)); |
56 | 0 | return sub.replace(string); |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public static String substituteConfigParameters(String string) { |
66 | 0 | return SUBSTITUTOR.replace(string); |
67 | |
} |
68 | |
|
69 | |
public static String parseGroupNamespaceCode(String namespaceAndNameCombo) { |
70 | 0 | if (namespaceAndNameCombo == null) { |
71 | 0 | return null; |
72 | |
} |
73 | 0 | String[] groupData = namespaceAndNameCombo.split(KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER); |
74 | 0 | if (groupData.length == 1) { |
75 | 0 | return KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE; |
76 | 0 | } else if (groupData.length == 2) { |
77 | 0 | return groupData[0].trim(); |
78 | |
} else { |
79 | 0 | return null; |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
public static String parseGroupName(String namespaceAndNameCombo) { |
84 | 0 | if (namespaceAndNameCombo == null) { |
85 | 0 | return null; |
86 | |
} |
87 | 0 | String[] groupData = namespaceAndNameCombo.split(KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER); |
88 | 0 | if (groupData.length == 1) { |
89 | 0 | return groupData[0].trim(); |
90 | 0 | } else if (groupData.length == 2) { |
91 | 0 | return groupData[1].trim(); |
92 | |
} else { |
93 | 0 | return null; |
94 | |
} |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | 0 | public static class PrioritySorter implements Comparator<ActionRequestValue> { |
102 | |
@Override |
103 | |
public int compare(ActionRequestValue ar1, ActionRequestValue ar2) { |
104 | 0 | int value = ar1.getPriority().compareTo(ar2.getPriority()); |
105 | 0 | if (value == 0) { |
106 | 0 | value = ActionRequestValue.compareActionCode(ar1.getActionRequested(), ar2.getActionRequested(), true); |
107 | 0 | if (value == 0) { |
108 | 0 | if ( (ar1.getActionRequestId() != null) && (ar2.getActionRequestId() != null) ) { |
109 | 0 | value = ar1.getActionRequestId().compareTo(ar2.getActionRequestId()); |
110 | |
} else { |
111 | |
|
112 | 0 | value = 0; |
113 | |
} |
114 | |
} |
115 | |
} |
116 | 0 | return value; |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | 0 | public static class RouteLogActionRequestSorter extends PrioritySorter implements Comparator<ActionRequestValue> { |
125 | |
@Override |
126 | |
public int compare(ActionRequestValue ar1, ActionRequestValue ar2) { |
127 | 0 | if (! ar1.getChildrenRequests().isEmpty()) { |
128 | 0 | Collections.sort(ar1.getChildrenRequests(), this); |
129 | |
} |
130 | 0 | if (! ar2.getChildrenRequests().isEmpty()) { |
131 | 0 | Collections.sort(ar2.getChildrenRequests(), this); |
132 | |
} |
133 | |
|
134 | 0 | int routeLevelCompareVal = ar1.getRouteLevel().compareTo(ar2.getRouteLevel()); |
135 | 0 | if (routeLevelCompareVal != 0) { |
136 | 0 | return routeLevelCompareVal; |
137 | |
} |
138 | |
|
139 | 0 | if (ar1.isActive() && ar2.isPending()) { |
140 | 0 | return -1; |
141 | 0 | } else if (ar2.isActive() && ar1.isPending()) { |
142 | 0 | return 1; |
143 | |
} |
144 | |
|
145 | 0 | return super.compare(ar1, ar2); |
146 | |
} |
147 | |
} |
148 | |
|
149 | |
public static boolean checkDateRanges(String fromDate, String toDate) { |
150 | |
try { |
151 | 0 | Date parsedDate = CoreApiServiceLocator.getDateTimeService().convertToDate(fromDate.trim()); |
152 | 0 | Calendar fromCalendar = Calendar.getInstance(); |
153 | 0 | fromCalendar.setLenient(false); |
154 | 0 | fromCalendar.setTime(parsedDate); |
155 | 0 | fromCalendar.set(Calendar.HOUR_OF_DAY, 0); |
156 | 0 | fromCalendar.set(Calendar.MINUTE, 0); |
157 | 0 | fromCalendar.set(Calendar.SECOND, 0); |
158 | 0 | fromCalendar.set(Calendar.MILLISECOND, 0); |
159 | 0 | parsedDate = CoreApiServiceLocator.getDateTimeService().convertToDate(toDate.trim()); |
160 | 0 | Calendar toCalendar = Calendar.getInstance(); |
161 | 0 | toCalendar.setLenient(false); |
162 | 0 | toCalendar.setTime(parsedDate); |
163 | 0 | toCalendar.set(Calendar.HOUR_OF_DAY, 0); |
164 | 0 | toCalendar.set(Calendar.MINUTE, 0); |
165 | 0 | toCalendar.set(Calendar.SECOND, 0); |
166 | 0 | toCalendar.set(Calendar.MILLISECOND, 0); |
167 | 0 | if (fromCalendar.after(toCalendar)) { |
168 | 0 | return false; |
169 | |
} |
170 | 0 | return true; |
171 | 0 | } catch (Exception ex) { |
172 | 0 | return false; |
173 | |
} |
174 | |
} |
175 | |
|
176 | |
public static String getIpNumber() { |
177 | |
try { |
178 | 0 | return InetAddress.getLocalHost().getHostAddress(); |
179 | 0 | } catch (UnknownHostException e) { |
180 | 0 | throw new WorkflowRuntimeException("Error retrieving ip number.", e); |
181 | |
} |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public static <T extends KeyValue> Map<String, String> getKeyValueCollectionAsMap(List<T> collection) { |
190 | 0 | Map<String, String> map = new HashMap<String, String>(collection.size()); |
191 | 0 | for (KeyValue kv: collection) { |
192 | 0 | map.put(kv.getKey(), kv.getValue()); |
193 | |
} |
194 | 0 | return map; |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public static <T extends KeyValue> Map<String, T> getKeyValueCollectionAsLookupTable(List<T> collection) { |
205 | 0 | Map<String, T> map = new HashMap<String, T>(collection.size()); |
206 | 0 | for (T kv: collection) { |
207 | 0 | map.put(kv.getKey(), kv); |
208 | |
} |
209 | 0 | return map; |
210 | |
} |
211 | |
} |