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