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.collections.MapUtils;
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  
25  import java.sql.Date;
26  import java.util.List;
27  import java.util.Map;
28  
29  public class OjbSubQueryUtil {
30  
31      public static ReportQueryByCriteria getEffectiveDateSubQuery(Class clazz, Date asOfDate, List<String> equalToField, boolean checkActive) {
32          //create base effective date filter
33          Criteria effDateFilter = new Criteria();
34          effDateFilter.addLessOrEqualThan("effectiveDate", asOfDate);
35          return OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(clazz, effDateFilter, equalToField, checkActive);
36      }
37  
38      public static ReportQueryByCriteria getEffectiveDateSubQueryWithoutFilter(Class clazz, List<String> equalToField, boolean checkActive) {
39          return OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(clazz, null, equalToField, checkActive);
40      }
41  
42      public static ReportQueryByCriteria getEffectiveDateSubQueryWithFilter(Class clazz, Criteria effectiveDateFilter, List<String> equalToField, boolean checkActive) {
43          Criteria effdt = new Criteria();
44          if (effectiveDateFilter != null) {
45              effdt.addAndCriteria(effectiveDateFilter);
46          }
47          if (CollectionUtils.isNotEmpty(equalToField)) {
48              for (String field : equalToField) {
49                  Criteria keyField = new Criteria();
50                  keyField.addEqualToField(field, Criteria.PARENT_QUERY_PREFIX+field);
51                  Criteria orNull = new Criteria();
52                  orNull.addIsNull(field);
53                  orNull.addIsNull(Criteria.PARENT_QUERY_PREFIX+field);
54                  keyField.addOrCriteria(orNull);
55                  effdt.addAndCriteria(keyField);
56              }
57          }
58          if (checkActive) {
59              effdt.addEqualTo("active", true);
60          }
61          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(clazz, effdt);
62          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
63  
64          return effdtSubQuery;
65      }
66  
67      public static ReportQueryByCriteria getTimestampSubQuery(Class clazz, List<String> equalToField, boolean checkActive) {
68          Criteria timestamp = new Criteria();
69          //need to check the effective date, always
70          timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
71  
72          if (CollectionUtils.isNotEmpty(equalToField)) {
73              for (String field : equalToField) {
74                  Criteria keyField = new Criteria();
75                  keyField.addEqualToField(field, Criteria.PARENT_QUERY_PREFIX+field);
76                  Criteria orNull = new Criteria();
77                  orNull.addIsNull(field);
78                  orNull.addIsNull(Criteria.PARENT_QUERY_PREFIX+field);
79                  keyField.addOrCriteria(orNull);
80                  timestamp.addAndCriteria(keyField);
81              }
82          }
83          if (checkActive) {
84              timestamp.addEqualTo("active", true);
85          }
86          ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(clazz, timestamp);
87          timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
88  
89          return timestampSubQuery;
90      }
91  }