1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl;
17
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
22
23 import org.apache.commons.beanutils.DynaClass;
24 import org.apache.commons.beanutils.DynaProperty;
25 import org.apache.commons.beanutils.PropertyUtils;
26 import org.apache.commons.beanutils.WrapDynaClass;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.ojb.broker.query.Criteria;
29 import org.apache.ojb.broker.query.Query;
30 import org.kuali.ole.sys.OLEConstants;
31 import org.kuali.ole.sys.context.SpringContext;
32 import org.kuali.ole.sys.service.impl.OleParameterConstants;
33 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
34 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
35 import org.kuali.rice.kns.datadictionary.FieldDefinition;
36 import org.kuali.rice.krad.dao.LookupDao;
37 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
38
39
40
41
42 public class OJBUtility {
43 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OJBUtility.class);
44
45 public static final String LOOKUP_DAO = "lookupDao";
46
47
48
49
50
51
52
53 public static LinkedHashMap buildPropertyMap(Object businessObject) {
54 DynaClass dynaClass = WrapDynaClass.createDynaClass(businessObject.getClass());
55 DynaProperty[] properties = dynaClass.getDynaProperties();
56 LinkedHashMap propertyMap = new LinkedHashMap();
57
58 try {
59 for (int numOfProperty = 0; numOfProperty < properties.length; numOfProperty++) {
60 String propertyName = properties[numOfProperty].getName();
61 if (PropertyUtils.isWriteable(businessObject, propertyName)) {
62 Object propertyValue = PropertyUtils.getProperty(businessObject, propertyName);
63 propertyMap.put(propertyName, propertyValue);
64 }
65 }
66 }
67 catch (Exception e) {
68 LOG.error("OJBUtility.buildPropertyMap()" + e);
69 }
70 return propertyMap;
71 }
72
73
74
75
76
77
78
79
80 public static Criteria buildCriteriaFromMap(Map fieldValues, Object businessObject) {
81
82 Criteria criteria = new Criteria();
83 BusinessObjectEntry entry = (BusinessObjectEntry) KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(businessObject.getClass().getName());
84
85
86 try {
87 Iterator propsIter = fieldValues.keySet().iterator();
88 while (propsIter.hasNext()) {
89 String propertyName = (String) propsIter.next();
90 Object propertyValueObject = fieldValues.get(propertyName);
91 String propertyValue = "";
92
93
94 FieldDefinition lookupField = (entry != null) ? entry.getLookupDefinition().getLookupField(propertyName) : null;
95 if (lookupField != null && lookupField.isTreatWildcardsAndOperatorsAsLiteral()) {
96 propertyValue = (propertyValueObject != null) ? StringUtils.replace(propertyValueObject.toString().trim(), "*", "\\*") : "";
97 } else {
98
99 propertyValue = (propertyValueObject != null) ? StringUtils.replace(propertyValueObject.toString().trim(), "*", "%") : "";
100 }
101
102
103 boolean isCreated = createCriteria(businessObject, propertyValue, propertyName, criteria);
104 if (!isCreated) {
105 continue;
106 }
107 }
108 }
109 catch (Exception e) {
110 LOG.error("OJBUtility.buildCriteriaFromMap()" + e);
111 }
112 return criteria;
113 }
114
115
116
117
118
119
120 public static void limitResultSize(Query query) {
121 int startingIndex = 1;
122 int endingIndex = getResultLimit().intValue();
123
124 query.setStartAtIndex(startingIndex);
125 query.setEndAtIndex(endingIndex);
126 }
127
128
129
130
131
132
133
134
135
136
137 public static Long getResultActualSize(Collection result, Integer recordCount, Map fieldValues, Object businessObject) {
138 int resultSize = result.size();
139 Integer limit = getResultLimit();
140 Long resultActualSize = new Long(resultSize);
141
142 if (recordCount > limit) {
143 long actualCount = recordCount.longValue() + resultSize - limit.longValue();
144 resultActualSize = new Long(actualCount);
145 }
146 return resultActualSize;
147 }
148
149
150
151
152
153
154
155
156 public static Long getResultSizeFromMap(Map fieldValues, Object businessObject) {
157 LookupDao lookupDao = SpringContext.getBean(LookupDao.class);
158 return lookupDao.findCountByMap(businessObject, fieldValues);
159 }
160
161
162
163
164
165
166 public static Integer getResultLimit() {
167
168 String limitConfig = SpringContext.getBean(ParameterService.class).getParameterValueAsString(OleParameterConstants.NERVOUS_SYSTEM_LOOKUP.class, OLEConstants.LOOKUP_RESULTS_LIMIT_URL_KEY);
169
170 Integer limit = Integer.MAX_VALUE;
171 if (limitConfig != null) {
172 limit = Integer.valueOf(limitConfig);
173 }
174 return limit;
175 }
176
177
178
179
180 public static boolean createCriteria(Object businessObject, String propertyValue, String propertyName, Criteria criteria) {
181 LookupDao lookupDao = SpringContext.getBean(LookupDao.class);
182 return lookupDao.createCriteria(businessObject, propertyValue, propertyName, criteria);
183 }
184 }