View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class provides a set of utilities that can handle common tasks related to business objects.
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       * This method builds a map of business object with its property names and values
49       *
50       * @param businessObject the given business object
51       * @return the map of business object with its property names and values
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       * This method builds an OJB query criteria based on the input field map
75       *
76       * @param fieldValues the input field map
77       * @param businessObject the given business object
78       * @return an OJB query criteria
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          //FieldDefinition lookupField = entry.getLookupDefinition().getLookupField(attributeName);
85          //System.out.println(entry.getTitleAttribute());
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                      //propertyValue = (propertyValueObject != null) ? propertyValueObject.toString().trim() : "";
99                      propertyValue = (propertyValueObject != null) ? StringUtils.replace(propertyValueObject.toString().trim(), "*", "%") : "";
100                 }
101 
102                 // if searchValue is empty and the key is not a valid property ignore
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      * Limit the size of the result set from the given query operation
117      *
118      * @param query the given query operation
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      * This method calculates the actual size of given selection results
130      *
131      * @param result the given selection results
132      * @param recordCount the possible number of the given results
133      * @param fieldValues the input field map
134      * @param businessObject the given business object
135      * @return the actual size of given selection results
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      * This method gets the size of a result set from the given search criteria
151      *
152      * @param fieldValues the input field map
153      * @param businessObject the given business object
154      * @return the size of a result set from the given search criteria
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      * This method gets the limit of the selection results
163      *
164      * @return the limit of the selection results
165      */
166     public static Integer getResultLimit() {
167         // get the result limit number from configuration
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      * This method build OJB criteria from the given property value and name
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 }