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