1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.lookup;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.ojb.broker.query.Criteria;
20 import org.kuali.rice.core.api.CoreApiServiceLocator;
21 import org.kuali.rice.core.api.encryption.EncryptionService;
22 import org.kuali.rice.core.api.search.SearchOperator;
23 import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
24 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
25 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
26 import org.kuali.rice.krad.datadictionary.exception.UnknownBusinessClassAttributeException;
27 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
29 import org.kuali.rice.krad.util.ExternalizableBusinessObjectUtils;
30 import org.kuali.rice.krad.util.KRADConstants;
31 import org.kuali.rice.krad.util.KRADPropertyConstants;
32 import org.kuali.rice.krad.util.ObjectUtils;
33
34 import java.sql.Date;
35 import java.sql.Timestamp;
36 import java.text.ParseException;
37 import java.util.ArrayList;
38 import java.util.Calendar;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Set;
44 import java.util.StringTokenizer;
45
46
47
48
49
50
51 public class LookupUtils {
52
53
54
55
56
57
58
59
60
61
62
63 public static String forceUppercase(Class<?> dataObjectClass, String fieldName, String fieldValue) {
64
65 if (StringUtils.isBlank(fieldValue)) {
66 return fieldValue;
67 }
68
69
70 if (dataObjectClass == null) {
71 throw new IllegalArgumentException("Parameter dataObjectClass passed in with null value.");
72 }
73
74 if (StringUtils.isBlank(fieldName)) {
75 throw new IllegalArgumentException("Parameter fieldName passed in with empty value.");
76 }
77
78 if (!KRADServiceLocatorWeb.getDataDictionaryService().isAttributeDefined(dataObjectClass, fieldName)
79 .booleanValue()) {
80 return fieldValue;
81 }
82
83 boolean forceUpperCase = false;
84 try {
85 forceUpperCase = KRADServiceLocatorWeb.getDataDictionaryService()
86 .getAttributeForceUppercase(dataObjectClass, fieldName).booleanValue();
87 } catch (UnknownBusinessClassAttributeException ubae) {
88
89 }
90 if (forceUpperCase && !fieldValue.endsWith(EncryptionService.ENCRYPTION_POST_PREFIX)) {
91 return fieldValue.toUpperCase();
92 }
93
94 return fieldValue;
95 }
96
97
98
99
100
101
102
103
104
105 public static Map<String, String> forceUppercase(Class<?> dataObjectClass, Map<String, String> fieldValues) {
106 if (dataObjectClass == null) {
107 throw new IllegalArgumentException("Parameter boClass passed in with null value.");
108 }
109
110 if (fieldValues == null) {
111 throw new IllegalArgumentException("Parameter fieldValues passed in with null value.");
112 }
113
114 for (String fieldName : fieldValues.keySet()) {
115 fieldValues.put(fieldName, forceUppercase(dataObjectClass, fieldName, (String) fieldValues.get(fieldName)));
116 }
117
118 return fieldValues;
119 }
120
121
122
123
124
125
126
127
128 public static Integer getSearchResultsLimit(Class dataObjectClass) {
129 Integer limit = KRADServiceLocatorWeb.getViewDictionaryService().getResultSetLimitForLookup(dataObjectClass);
130 if (limit == null) {
131 limit = getApplicationSearchResultsLimit();
132 }
133
134 return limit;
135 }
136
137
138
139
140
141 public static Integer getApplicationSearchResultsLimit() {
142 String limitString = CoreFrameworkServiceLocator.getParameterService()
143 .getParameterValueAsString(KRADConstants.KRAD_NAMESPACE,
144 KRADConstants.DetailTypes.LOOKUP_PARM_DETAIL_TYPE,
145 KRADConstants.SystemGroupParameterNames.LOOKUP_RESULTS_LIMIT);
146 if (limitString != null) {
147 return Integer.valueOf(limitString);
148 }
149
150 return null;
151 }
152
153
154
155
156
157
158
159
160 public static void applySearchResultsLimit(Class businessObjectClass, Criteria criteria,
161 DatabasePlatform platform) {
162 Integer limit = getSearchResultsLimit(businessObjectClass);
163 if (limit != null) {
164 platform.applyLimit(limit, criteria);
165 }
166 }
167
168
169
170
171
172
173
174 public static void applySearchResultsLimit(Class businessObjectClass,
175 org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria criteria) {
176 Integer limit = getSearchResultsLimit(businessObjectClass);
177 if (limit != null) {
178 criteria.setSearchLimit(limit);
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public static Timestamp getActiveDateTimestampForCriteria(Map searchValues) {
197 Date activeDate = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDate();
198 Timestamp activeTimestamp = null;
199 if (searchValues.containsKey(KRADPropertyConstants.ACTIVE_AS_OF_DATE)) {
200 String activeAsOfDate = (String) searchValues.get(KRADPropertyConstants.ACTIVE_AS_OF_DATE);
201 if (StringUtils.isNotBlank(activeAsOfDate)) {
202 try {
203 activeDate = CoreApiServiceLocator.getDateTimeService()
204 .convertToSqlDate(ObjectUtils.clean(activeAsOfDate));
205 } catch (ParseException e) {
206
207 try {
208 activeTimestamp = CoreApiServiceLocator.getDateTimeService()
209 .convertToSqlTimestamp(ObjectUtils.clean(activeAsOfDate));
210 } catch (ParseException e1) {
211 throw new RuntimeException("Unable to convert date: " + ObjectUtils.clean(activeAsOfDate));
212 }
213 }
214 }
215 }
216
217
218 if (activeTimestamp == null) {
219 Calendar cal = Calendar.getInstance();
220 cal.setTime(activeDate);
221 cal.set(Calendar.HOUR, cal.getMaximum(Calendar.HOUR));
222 cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
223 cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
224
225 activeTimestamp = new Timestamp(cal.getTime().getTime());
226 }
227
228 return activeTimestamp;
229 }
230
231
232
233
234
235
236
237
238 public static Map<String, String> preprocessDateFields(Map<String, String> searchCriteria) {
239 Map<String, String> fieldsToUpdate = new HashMap<String, String>();
240 Set<String> fieldsForLookup = searchCriteria.keySet();
241 for (String propName : fieldsForLookup) {
242 if (propName.startsWith(KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX)) {
243 String from_DateValue = searchCriteria.get(propName);
244 String dateFieldName =
245 StringUtils.remove(propName, KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX);
246 String to_DateValue = searchCriteria.get(dateFieldName);
247 String newPropValue = to_DateValue;
248
249 if (StringUtils.isNotEmpty(from_DateValue) && StringUtils.isNotEmpty(to_DateValue)) {
250 newPropValue = from_DateValue + SearchOperator.BETWEEN + to_DateValue;
251 } else if (StringUtils.isNotEmpty(from_DateValue) && StringUtils.isEmpty(to_DateValue)) {
252 newPropValue = SearchOperator.GREATER_THAN_EQUAL.op() + from_DateValue;
253 } else if (StringUtils.isNotEmpty(to_DateValue) && StringUtils.isEmpty(from_DateValue)) {
254 newPropValue = SearchOperator.LESS_THAN_EQUAL.op() + to_DateValue;
255 }
256
257 fieldsToUpdate.put(dateFieldName, newPropValue);
258 }
259 }
260
261
262 Set<String> keysToUpdate = fieldsToUpdate.keySet();
263 for (String updateKey : keysToUpdate) {
264 searchCriteria.put(updateKey, fieldsToUpdate.get(updateKey));
265 }
266
267 return fieldsToUpdate;
268 }
269
270
271
272
273 public static boolean hasExternalBusinessObjectProperty(Class<?> boClass,
274 Map<String, String> fieldValues) throws IllegalAccessException, InstantiationException {
275 Object sampleBo = boClass.newInstance();
276 for (String key : fieldValues.keySet()) {
277 if (isExternalBusinessObjectProperty(sampleBo, key)) {
278 return true;
279 }
280 }
281
282 return false;
283 }
284
285
286
287
288
289
290 public static boolean isExternalBusinessObjectProperty(Object sampleBo, String propertyName) {
291 if (propertyName.indexOf(".") > 0 && !StringUtils.contains(propertyName, "add.")) {
292 Class<?> propertyClass =
293 ObjectPropertyUtils.getPropertyType(sampleBo, StringUtils.substringBeforeLast(propertyName, "."));
294 if (propertyClass != null) {
295 return ExternalizableBusinessObjectUtils.isExternalizableBusinessObjectInterface(propertyClass);
296 }
297 }
298
299 return false;
300 }
301
302
303
304
305
306
307 public static Map<String, String> removeExternalizableBusinessObjectFieldValues(Class<?> boClass,
308 Map<String, String> fieldValues) throws IllegalAccessException, InstantiationException {
309 Map<String, String> eboFieldValues = new HashMap<String, String>();
310 Object sampleBo = boClass.newInstance();
311 for (String key : fieldValues.keySet()) {
312 if (!isExternalBusinessObjectProperty(sampleBo, key)) {
313 eboFieldValues.put(key, fieldValues.get(key));
314 }
315 }
316
317 return eboFieldValues;
318 }
319
320
321
322
323
324 public static Map<String, String> getExternalizableBusinessObjectFieldValues(String eboPropertyName,
325 Map<String, String> fieldValues) {
326 Map<String, String> eboFieldValues = new HashMap<String, String>();
327 for (String key : fieldValues.keySet()) {
328 if (key.startsWith(eboPropertyName + ".")) {
329 eboFieldValues.put(StringUtils.substringAfterLast(key, "."), fieldValues.get(key));
330 }
331 }
332
333 return eboFieldValues;
334 }
335
336
337
338
339
340
341 public static List<String> getExternalizableBusinessObjectProperties(Class<?> boClass,
342 Map<String, String> fieldValues) throws IllegalAccessException, InstantiationException {
343 Set<String> eboPropertyNames = new HashSet<String>();
344
345 Object sampleBo = boClass.newInstance();
346 for (String key : fieldValues.keySet()) {
347 if (isExternalBusinessObjectProperty(sampleBo, key)) {
348 eboPropertyNames.add(StringUtils.substringBeforeLast(key, "."));
349 }
350 }
351
352 return new ArrayList<String>(eboPropertyNames);
353 }
354
355
356
357
358
359
360
361
362
363 public static Class<? extends ExternalizableBusinessObject> getExternalizableBusinessObjectClass(Class<?> boClass,
364 String propertyName) throws IllegalAccessException, InstantiationException {
365 return (Class<? extends ExternalizableBusinessObject>) ObjectPropertyUtils
366 .getPropertyType(boClass.newInstance(), StringUtils.substringBeforeLast(propertyName, "."));
367 }
368
369 }