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