001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.gl.util;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.LinkedHashMap;
021: import java.util.Map;
022:
023: import org.apache.commons.beanutils.DynaClass;
024: import org.apache.commons.beanutils.DynaProperty;
025: import org.apache.commons.beanutils.PropertyUtils;
026: import org.apache.commons.beanutils.WrapDynaClass;
027: import org.apache.ojb.broker.query.Criteria;
028: import org.apache.ojb.broker.query.Query;
029: import org.kuali.core.dao.LookupDao;
030: import org.kuali.kfs.KFSConstants;
031: import org.kuali.kfs.context.SpringContext;
032: import org.kuali.kfs.service.ParameterService;
033: import org.kuali.kfs.service.impl.ParameterConstants;
034:
035: /**
036: * This class provides a set of utilities that can handle common tasks related to business objects.
037: */
038: public class OJBUtility {
039: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
040: .getLogger(OJBUtility.class);
041:
042: public static final String LOOKUP_DAO = "lookupDao";
043:
044: /**
045: * This method builds a map of business object with its property names and values
046: *
047: * @param businessObject the given business object
048: * @return the map of business object with its property names and values
049: */
050: public static LinkedHashMap buildPropertyMap(Object businessObject) {
051: DynaClass dynaClass = WrapDynaClass
052: .createDynaClass(businessObject.getClass());
053: DynaProperty[] properties = dynaClass.getDynaProperties();
054: LinkedHashMap propertyMap = new LinkedHashMap();
055:
056: try {
057: for (int numOfProperty = 0; numOfProperty < properties.length; numOfProperty++) {
058: String propertyName = properties[numOfProperty]
059: .getName();
060: if (PropertyUtils.isWriteable(businessObject,
061: propertyName)) {
062: Object propertyValue = PropertyUtils.getProperty(
063: businessObject, propertyName);
064: propertyMap.put(propertyName, propertyValue);
065: }
066: }
067: } catch (Exception e) {
068: LOG.error("OJBUtility.buildPropertyMap()" + e);
069: }
070: return propertyMap;
071: }
072:
073: /**
074: * This method builds an OJB query criteria based on the input field map
075: *
076: * @param fieldValues the input field map
077: * @param businessObject the given business object
078: * @return an OJB query criteria
079: */
080: public static Criteria buildCriteriaFromMap(Map fieldValues,
081: Object businessObject) {
082: Criteria criteria = new Criteria();
083:
084: try {
085: Iterator propsIter = fieldValues.keySet().iterator();
086: while (propsIter.hasNext()) {
087: String propertyName = (String) propsIter.next();
088: Object propertyValueObject = fieldValues
089: .get(propertyName);
090: String propertyValue = (propertyValueObject != null) ? propertyValueObject
091: .toString().trim()
092: : "";
093:
094: // if searchValue is empty and the key is not a valid property ignore
095: boolean isCreated = createCriteria(businessObject,
096: propertyValue, propertyName, criteria);
097: if (!isCreated) {
098: continue;
099: }
100: }
101: } catch (Exception e) {
102: LOG.error("OJBUtility.buildCriteriaFromMap()" + e);
103: }
104: return criteria;
105: }
106:
107: /**
108: * Limit the size of the result set from the given query operation
109: *
110: * @param query the given query operation
111: */
112: public static void limitResultSize(Query query) {
113: int startingIndex = 1;
114: int endingIndex = getResultLimit().intValue();
115:
116: query.setStartAtIndex(startingIndex);
117: query.setEndAtIndex(endingIndex);
118: }
119:
120: /**
121: * This method calculates the actual size of given selection results
122: *
123: * @param result the given selection results
124: * @param recordCount the possible number of the given results
125: * @param fieldValues the input field map
126: * @param businessObject the given business object
127: * @return the actual size of given selection results
128: */
129: public static Long getResultActualSize(Collection result,
130: Integer recordCount, Map fieldValues, Object businessObject) {
131: int resultSize = result.size();
132: Integer limit = getResultLimit();
133: Long resultActualSize = new Long(resultSize);
134:
135: if (recordCount > limit) {
136: long actualCount = recordCount.longValue() + resultSize
137: - limit.longValue();
138: resultActualSize = new Long(actualCount);
139: }
140: return resultActualSize;
141: }
142:
143: /**
144: * This method gets the size of a result set from the given search criteria
145: *
146: * @param fieldValues the input field map
147: * @param businessObject the given business object
148: * @return the size of a result set from the given search criteria
149: */
150: public static Long getResultSizeFromMap(Map fieldValues,
151: Object businessObject) {
152: LookupDao lookupDao = SpringContext.getBean(LookupDao.class);
153: return lookupDao.findCountByMap(businessObject, fieldValues);
154: }
155:
156: /**
157: * This method gets the limit of the selection results
158: *
159: * @return the limit of the selection results
160: */
161: public static Integer getResultLimit() {
162: // get the result limit number from configuration
163: String limitConfig = SpringContext.getBean(
164: ParameterService.class).getParameterValue(
165: ParameterConstants.NERVOUS_SYSTEM_LOOKUP.class,
166: KFSConstants.LOOKUP_RESULTS_LIMIT_URL_KEY);
167:
168: Integer limit = Integer.MAX_VALUE;
169: if (limitConfig != null) {
170: limit = Integer.valueOf(limitConfig);
171: }
172: return limit;
173: }
174:
175: /**
176: * This method build OJB criteria from the given property value and name
177: */
178: public static boolean createCriteria(Object businessObject,
179: String propertyValue, String propertyName, Criteria criteria) {
180: LookupDao lookupDao = SpringContext.getBean(LookupDao.class);
181: return lookupDao.createCriteria(businessObject, propertyValue,
182: propertyName, criteria);
183: }
184: }
|