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.coreservice.framework.CoreFrameworkServiceLocator;
21 import org.kuali.rice.core.api.CoreApiServiceLocator;
22 import org.kuali.rice.core.api.encryption.EncryptionService;
23 import org.kuali.rice.core.api.search.SearchOperator;
24 import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
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 import org.kuali.rice.krad.web.form.LookupForm;
34
35 import java.sql.Date;
36 import java.sql.Timestamp;
37 import java.text.ParseException;
38 import java.util.ArrayList;
39 import java.util.Calendar;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
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
129
130
131 public static Integer getSearchResultsLimit(Class dataObjectClass, LookupForm lookupForm) {
132 Integer limit = KRADServiceLocatorWeb.getViewDictionaryService().getResultSetLimitForLookup(dataObjectClass,
133 lookupForm);
134 if (limit == null) {
135 limit = getApplicationSearchResultsLimit();
136 }
137
138 return limit;
139 }
140
141
142
143
144
145
146
147 public static Integer getApplicationSearchResultsLimit() {
148 String limitString = CoreFrameworkServiceLocator.getParameterService()
149 .getParameterValueAsString(KRADConstants.KRAD_NAMESPACE,
150 KRADConstants.DetailTypes.LOOKUP_PARM_DETAIL_TYPE,
151 KRADConstants.SystemGroupParameterNames.LOOKUP_RESULTS_LIMIT);
152 if (limitString != null) {
153 return Integer.valueOf(limitString);
154 }
155
156 return null;
157 }
158
159
160
161
162
163
164
165
166
167
168 public static void applySearchResultsLimit(Class businessObjectClass, Criteria criteria, DatabasePlatform platform,
169 Integer limit) {
170 if (limit != null) {
171 platform.applyLimit(limit, criteria);
172 } else {
173 limit = getSearchResultsLimit(businessObjectClass, null);
174 if (limit != null) {
175 platform.applyLimit(limit, criteria);
176 }
177 }
178 }
179
180
181
182
183
184
185
186 public static void applySearchResultsLimit(Class businessObjectClass,
187 org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria criteria) {
188 Integer limit = getSearchResultsLimit(businessObjectClass, null);
189 if (limit != null) {
190 criteria.setSearchLimit(limit);
191 }
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 public static Timestamp getActiveDateTimestampForCriteria(Map searchValues) {
209 Date activeDate = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDate();
210 Timestamp activeTimestamp = null;
211 if (searchValues.containsKey(KRADPropertyConstants.ACTIVE_AS_OF_DATE)) {
212 String activeAsOfDate = (String) searchValues.get(KRADPropertyConstants.ACTIVE_AS_OF_DATE);
213 if (StringUtils.isNotBlank(activeAsOfDate)) {
214 try {
215 activeDate = CoreApiServiceLocator.getDateTimeService()
216 .convertToSqlDate(ObjectUtils.clean(activeAsOfDate));
217 } catch (ParseException e) {
218
219 try {
220 activeTimestamp = CoreApiServiceLocator.getDateTimeService()
221 .convertToSqlTimestamp(ObjectUtils.clean(activeAsOfDate));
222 } catch (ParseException e1) {
223 throw new RuntimeException("Unable to convert date: " + ObjectUtils.clean(activeAsOfDate));
224 }
225 }
226 }
227 }
228
229
230 if (activeTimestamp == null) {
231 Calendar cal = Calendar.getInstance();
232 cal.setTime(activeDate);
233 cal.set(Calendar.HOUR, cal.getMaximum(Calendar.HOUR));
234 cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
235 cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
236
237 activeTimestamp = new Timestamp(cal.getTime().getTime());
238 }
239
240 return activeTimestamp;
241 }
242
243
244
245
246
247
248
249
250 public static Map<String, String> preprocessDateFields(Map<String, String> searchCriteria) {
251 Map<String, String> fieldsToUpdate = new HashMap<String, String>();
252 Map<String, String> searchCriteriaUpdated = new HashMap<String, String>(searchCriteria);
253 Set<String> fieldsForLookup = searchCriteria.keySet();
254 for (String propName : fieldsForLookup) {
255 if (propName.startsWith(KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX)) {
256 String from_DateValue = searchCriteria.get(propName);
257 String dateFieldName =
258 StringUtils.remove(propName, KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX);
259 String to_DateValue = searchCriteria.get(dateFieldName);
260 String newPropValue = to_DateValue;
261
262 if (StringUtils.isNotEmpty(from_DateValue) && StringUtils.isNotEmpty(to_DateValue)) {
263 newPropValue = from_DateValue + SearchOperator.BETWEEN + to_DateValue;
264 } else if (StringUtils.isNotEmpty(from_DateValue) && StringUtils.isEmpty(to_DateValue)) {
265 newPropValue = SearchOperator.GREATER_THAN_EQUAL.op() + from_DateValue;
266 } else if (StringUtils.isNotEmpty(to_DateValue) && StringUtils.isEmpty(from_DateValue)) {
267 newPropValue = SearchOperator.LESS_THAN_EQUAL.op() + to_DateValue;
268 }
269
270 fieldsToUpdate.put(dateFieldName, newPropValue);
271 }
272 }
273
274
275 Set<String> keysToUpdate = fieldsToUpdate.keySet();
276 for (String updateKey : keysToUpdate) {
277 searchCriteriaUpdated.put(updateKey, fieldsToUpdate.get(updateKey));
278 }
279
280 return searchCriteriaUpdated;
281 }
282
283
284
285
286
287
288
289
290
291
292 public static boolean hasExternalBusinessObjectProperty(Class<?> boClass,
293 Map<String, String> fieldValues) throws IllegalAccessException, InstantiationException {
294 Object sampleBo = boClass.newInstance();
295 for (String key : fieldValues.keySet()) {
296 if (isExternalBusinessObjectProperty(sampleBo, key)) {
297 return true;
298 }
299 }
300
301 return false;
302 }
303
304
305
306
307
308
309
310
311
312
313 public static boolean isExternalBusinessObjectProperty(Object sampleBo, String propertyName) {
314 if (propertyName.indexOf(".") > 0 && !StringUtils.contains(propertyName, "add.")) {
315 Class<?> propertyClass =
316 ObjectPropertyUtils.getPropertyType(sampleBo, StringUtils.substringBeforeLast(propertyName, "."));
317 if (propertyClass != null) {
318 return ExternalizableBusinessObjectUtils.isExternalizableBusinessObjectInterface(propertyClass);
319 }
320 }
321
322 return false;
323 }
324
325
326
327
328
329
330
331
332
333
334 public static Map<String, String> removeExternalizableBusinessObjectFieldValues(Class<?> boClass,
335 Map<String, String> fieldValues) throws IllegalAccessException, InstantiationException {
336 Map<String, String> eboFieldValues = new HashMap<String, String>();
337 Object sampleBo = boClass.newInstance();
338 for (String key : fieldValues.keySet()) {
339 if (!isExternalBusinessObjectProperty(sampleBo, key)) {
340 eboFieldValues.put(key, fieldValues.get(key));
341 }
342 }
343
344 return eboFieldValues;
345 }
346
347
348
349
350
351
352
353
354
355 public static Map<String, String> getExternalizableBusinessObjectFieldValues(String eboPropertyName,
356 Map<String, String> fieldValues) {
357 Map<String, String> eboFieldValues = new HashMap<String, String>();
358 for (String key : fieldValues.keySet()) {
359 if (key.startsWith(eboPropertyName + ".")) {
360 eboFieldValues.put(StringUtils.substringAfterLast(key, "."), fieldValues.get(key));
361 }
362 }
363
364 return eboFieldValues;
365 }
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380 public static List<String> getExternalizableBusinessObjectProperties(Class<?> boClass,
381 Map<String, String> fieldValues) throws IllegalAccessException, InstantiationException {
382 Set<String> eboPropertyNames = new HashSet<String>();
383
384 Object sampleBo = boClass.newInstance();
385 for (String key : fieldValues.keySet()) {
386 if (isExternalBusinessObjectProperty(sampleBo, key)) {
387 eboPropertyNames.add(StringUtils.substringBeforeLast(key, "."));
388 }
389 }
390
391 return new ArrayList<String>(eboPropertyNames);
392 }
393
394
395
396
397
398
399
400
401
402
403
404 public static Class<? extends ExternalizableBusinessObject> getExternalizableBusinessObjectClass(Class<?> boClass,
405 String propertyName) throws IllegalAccessException, InstantiationException {
406 return (Class<? extends ExternalizableBusinessObject>) ObjectPropertyUtils
407 .getPropertyType(boClass.newInstance(), StringUtils.substringBeforeLast(propertyName, "."));
408 }
409
410 }