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.lm.employeeoverride.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import com.google.common.collect.ImmutableList;
24  import edu.emory.mathcs.backport.java.util.Collections;
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.Query;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.apache.ojb.broker.query.ReportQueryByCriteria;
30  import org.kuali.hr.core.util.OjbSubQueryUtil;
31  import org.kuali.hr.lm.employeeoverride.EmployeeOverride;
32  import org.kuali.hr.time.util.TKUtils;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  
35  public class EmployeeOverrideDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EmployeeOverrideDao{
36      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
37              .add("principalId")
38              .add("leavePlan")
39              .add("accrualCategory")
40              .add("overrideType")
41              .build();
42  
43  	@SuppressWarnings("unchecked")
44  	@Override
45  	public List<EmployeeOverride> getEmployeeOverrides(String principalId, Date asOfDate) {
46          List<EmployeeOverride> employeeOverrides = new ArrayList<EmployeeOverride>();
47          Criteria root = new Criteria();
48  
49          root.addEqualTo("principalId", principalId);
50          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EmployeeOverride.class, asOfDate, EQUAL_TO_FIELDS, false));
51          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
52  
53          Criteria activeFilter = new Criteria(); // Inner Join For Activity
54          activeFilter.addEqualTo("active", true);
55          root.addAndCriteria(activeFilter);
56  
57          Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
58          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
59  
60          if (c != null) {
61          	employeeOverrides.addAll(c);
62          }
63          return employeeOverrides;
64      }
65  	
66  	@Override
67  	public EmployeeOverride getEmployeeOverride(String principalId, String leavePlan, String accrualCategory, String overrideType, Date asOfDate) {
68          Criteria root = new Criteria();
69  
70          root.addEqualTo("principalId", principalId);
71          root.addEqualTo("leavePlan", leavePlan);
72          root.addEqualTo("accrualCategory", accrualCategory);
73          root.addEqualTo("overrideType", overrideType);
74  
75          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EmployeeOverride.class, asOfDate, EQUAL_TO_FIELDS, false));
76          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
77  
78          Criteria activeFilter = new Criteria();
79          activeFilter.addEqualTo("active", true);
80          root.addAndCriteria(activeFilter);
81  
82          Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
83          return (EmployeeOverride) getPersistenceBrokerTemplate().getObjectByQuery(query);
84  	}
85  
86  	@Override
87  	public EmployeeOverride getEmployeeOverride(String lmEmployeeOverrideId) {
88  		Criteria crit = new Criteria();
89          crit.addEqualTo("lmEmployeeOverrideId", lmEmployeeOverrideId);
90          Query query = QueryFactory.newQuery(EmployeeOverride.class, crit);
91          return (EmployeeOverride) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
92  	}
93  
94      @Override
95      @SuppressWarnings("unchecked")
96      public List<EmployeeOverride> getEmployeeOverrides(String principalId, String leavePlan, String accrualCategory, String overrideType,
97                                                         Date fromEffdt, Date toEffdt, String active) {
98  
99          List<EmployeeOverride> results = new ArrayList<EmployeeOverride>();
100     	
101     	Criteria root = new Criteria();
102 
103         if (StringUtils.isNotBlank(principalId)) {
104         	root.addEqualTo("principalId",principalId);
105         }
106         
107         if (StringUtils.isNotBlank(leavePlan)) {
108         	root.addEqualTo("leavePlan",leavePlan);
109         }
110         
111         if (StringUtils.isNotBlank(accrualCategory)) {
112         	root.addEqualTo("accrualCategory",accrualCategory);
113         }
114         
115         if (StringUtils.isNotBlank(overrideType)) {
116         	root.addEqualTo("overrideType",overrideType);
117         }
118         
119         Criteria effectiveDateFilter = new Criteria();
120         if (fromEffdt != null) {
121             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
122         }
123         if (toEffdt != null) {
124             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
125         }
126         if (fromEffdt == null && toEffdt == null) {
127             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
128         }
129         root.addAndCriteria(effectiveDateFilter);
130 
131         if (StringUtils.isNotBlank(active)) {
132             Criteria activeFilter = new Criteria();
133             if (StringUtils.equals(active, "Y")) {
134                 activeFilter.addEqualTo("active", true);
135             } else if (StringUtils.equals(active, "N")) {
136                 activeFilter.addEqualTo("active", false);
137             }
138             root.addAndCriteria(activeFilter);
139         }
140 
141         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(EmployeeOverride.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
142         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
143 
144         Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
145         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
146 
147         return results;
148     }
149 
150 }