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          List<String> fields = Collections.singletonList("principalId");
50          root.addEqualTo("principalId", principalId);
51          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EmployeeOverride.class, asOfDate, fields, false));
52          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, fields, false));
53  
54          Criteria activeFilter = new Criteria(); // Inner Join For Activity
55          activeFilter.addEqualTo("active", true);
56          root.addAndCriteria(activeFilter);
57  
58          Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
59          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
60  
61          if (c != null) {
62          	employeeOverrides.addAll(c);
63          }
64          return employeeOverrides;
65      }
66  	
67  	@Override
68  	public EmployeeOverride getEmployeeOverride(String principalId, String leavePlan, String accrualCategory, String overrideType, Date asOfDate) {
69          Criteria root = new Criteria();
70  
71          root.addEqualTo("principalId", principalId);
72          root.addEqualTo("leavePlan", leavePlan);
73          root.addEqualTo("accrualCategory", accrualCategory);
74          root.addEqualTo("overrideType", overrideType);
75  
76          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EmployeeOverride.class, asOfDate, EQUAL_TO_FIELDS, false));
77          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
78  
79          Criteria activeFilter = new Criteria();
80          activeFilter.addEqualTo("active", true);
81          root.addAndCriteria(activeFilter);
82  
83          Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
84          return (EmployeeOverride) getPersistenceBrokerTemplate().getObjectByQuery(query);
85  	}
86  
87  	@Override
88  	public EmployeeOverride getEmployeeOverride(String lmEmployeeOverrideId) {
89  		Criteria crit = new Criteria();
90          crit.addEqualTo("lmEmployeeOverrideId", lmEmployeeOverrideId);
91          Query query = QueryFactory.newQuery(EmployeeOverride.class, crit);
92          return (EmployeeOverride) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
93  	}
94  
95      @Override
96      @SuppressWarnings("unchecked")
97      public List<EmployeeOverride> getEmployeeOverrides(String principalId, String leavePlan, String accrualCategory, String overrideType,
98                                                         Date fromEffdt, Date toEffdt, String active) {
99  
100         List<EmployeeOverride> results = new ArrayList<EmployeeOverride>();
101     	
102     	Criteria root = new Criteria();
103 
104         if (StringUtils.isNotBlank(principalId)) {
105         	root.addEqualTo("principalId",principalId);
106         }
107         
108         if (StringUtils.isNotBlank(leavePlan)) {
109         	root.addEqualTo("leavePlan",leavePlan);
110         }
111         
112         if (StringUtils.isNotBlank(accrualCategory)) {
113         	root.addEqualTo("accrualCategory",accrualCategory);
114         }
115         
116         if (StringUtils.isNotBlank(overrideType)) {
117         	root.addEqualTo("overrideType",overrideType);
118         }
119         
120         Criteria effectiveDateFilter = new Criteria();
121         if (fromEffdt != null) {
122             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
123         }
124         if (toEffdt != null) {
125             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
126         }
127         if (fromEffdt == null && toEffdt == null) {
128             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
129         }
130         root.addAndCriteria(effectiveDateFilter);
131 
132         if (StringUtils.isNotBlank(active)) {
133             Criteria activeFilter = new Criteria();
134             if (StringUtils.equals(active, "Y")) {
135                 activeFilter.addEqualTo("active", true);
136             } else if (StringUtils.equals(active, "N")) {
137                 activeFilter.addEqualTo("active", false);
138             }
139             root.addAndCriteria(activeFilter);
140         }
141 
142         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(EmployeeOverride.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
143         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EmployeeOverride.class, EQUAL_TO_FIELDS, false));
144 
145         Query query = QueryFactory.newQuery(EmployeeOverride.class, root);
146         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
147 
148         return results;
149     }
150 
151 }