View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.core.util;
17  
18  
19  import org.apache.commons.collections.CollectionUtils;
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.ojb.broker.query.Criteria;
22  import org.apache.ojb.broker.query.QueryFactory;
23  import org.apache.ojb.broker.query.ReportQueryByCriteria;
24  import org.kuali.hr.time.util.TKUtils;
25  import org.kuali.rice.core.api.search.SearchOperator;
26  
27  import java.sql.Date;
28  import java.util.List;
29  
30  public class OjbSubQueryUtil {
31  
32      public static ReportQueryByCriteria getEffectiveDateSubQuery(Class clazz, Date asOfDate, List<String> equalToField, boolean checkActive) {
33          //create base effective date filter
34          Criteria effDateFilter = new Criteria();
35          effDateFilter.addLessOrEqualThan("effectiveDate", asOfDate);
36          return OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(clazz, effDateFilter, equalToField, checkActive);
37      }
38  
39      public static ReportQueryByCriteria getEffectiveDateSubQueryWithoutFilter(Class clazz, List<String> equalToField, boolean checkActive) {
40          return OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(clazz, null, equalToField, checkActive);
41      }
42  
43      public static ReportQueryByCriteria getEffectiveDateSubQueryWithFilter(Class clazz, Criteria effectiveDateFilter, List<String> equalToField, boolean checkActive) {
44          Criteria effdt = new Criteria();
45          if (effectiveDateFilter != null) {
46              effdt.addAndCriteria(effectiveDateFilter);
47          }
48          if (CollectionUtils.isNotEmpty(equalToField)) {
49              for (String field : equalToField) {
50                  Criteria keyField = new Criteria();
51                  keyField.addEqualToField(field, Criteria.PARENT_QUERY_PREFIX+field);
52                  Criteria orNull = new Criteria();
53                  orNull.addIsNull(field);
54                  orNull.addIsNull(Criteria.PARENT_QUERY_PREFIX+field);
55                  keyField.addOrCriteria(orNull);
56                  effdt.addAndCriteria(keyField);
57              }
58          }
59          if (checkActive) {
60              effdt.addEqualTo("active", true);
61          }
62          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(clazz, effdt);
63          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
64  
65          return effdtSubQuery;
66      }
67  
68      public static ReportQueryByCriteria getTimestampSubQuery(Class clazz, List<String> equalToField, boolean checkActive) {
69          Criteria timestamp = new Criteria();
70          //need to check the effective date, always
71          timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
72  
73          if (CollectionUtils.isNotEmpty(equalToField)) {
74              for (String field : equalToField) {
75                  Criteria keyField = new Criteria();
76                  keyField.addEqualToField(field, Criteria.PARENT_QUERY_PREFIX+field);
77                  Criteria orNull = new Criteria();
78                  orNull.addIsNull(field);
79                  orNull.addIsNull(Criteria.PARENT_QUERY_PREFIX+field);
80                  keyField.addOrCriteria(orNull);
81                  timestamp.addAndCriteria(keyField);
82              }
83          }
84          if (checkActive) {
85              timestamp.addEqualTo("active", true);
86          }
87          ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(clazz, timestamp);
88          timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
89  
90          return timestampSubQuery;
91      }
92  
93      public static void addNumericCriteria(Criteria criteria, String propertyName, String propertyValue) {
94          if (StringUtils.contains(propertyValue, SearchOperator.BETWEEN.op())) {
95              String[] rangeValues = StringUtils.split(propertyValue, SearchOperator.BETWEEN.op());
96              criteria.addBetween(propertyName, TKUtils.cleanNumeric(rangeValues[0]), TKUtils.cleanNumeric( rangeValues[1] ));
97          } else if (propertyValue.startsWith(SearchOperator.GREATER_THAN_EQUAL.op())) {
98              criteria.addGreaterOrEqualThan(propertyName, TKUtils.cleanNumeric(propertyValue));
99          } else if (propertyValue.startsWith(SearchOperator.LESS_THAN_EQUAL.op())) {
100             criteria.addLessOrEqualThan(propertyName, TKUtils.cleanNumeric(propertyValue));
101         } else if (propertyValue.startsWith(SearchOperator.GREATER_THAN.op())) {
102             criteria.addGreaterThan(propertyName, TKUtils.cleanNumeric( propertyValue ) );
103         } else if (propertyValue.startsWith(SearchOperator.LESS_THAN.op())) {
104             criteria.addLessThan(propertyName, TKUtils.cleanNumeric(propertyValue));
105         } else {
106             criteria.addEqualTo(propertyName, TKUtils.cleanNumeric(propertyValue));
107         }
108     }
109 }