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.kpme.tklm.leave.adjustment.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.ojb.broker.query.Criteria;
24  import org.apache.ojb.broker.query.Query;
25  import org.apache.ojb.broker.query.QueryFactory;
26  import org.joda.time.LocalDate;
27  import org.kuali.kpme.core.util.OjbSubQueryUtil;
28  import org.kuali.kpme.tklm.leave.adjustment.LeaveAdjustment;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class LeaveAdjustmentDaoOjbImpl extends PlatformAwareDaoBaseOjb implements LeaveAdjustmentDao{
32  
33  	@SuppressWarnings("unchecked")
34  	@Override
35  	public List<LeaveAdjustment> getLeaveAdjustments(String principalId, LocalDate asOfDate) {
36          List<LeaveAdjustment> leaveAdjustments = new ArrayList<LeaveAdjustment>();
37          Criteria root = new Criteria();
38          root.addEqualTo("principalId", principalId);
39          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(LeaveAdjustment.class, asOfDate, LeaveAdjustment.EQUAL_TO_FIELDS, false));
40          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(LeaveAdjustment.class, LeaveAdjustment.EQUAL_TO_FIELDS, false));
41  
42          Criteria activeFilter = new Criteria(); // Inner Join For Activity
43          activeFilter.addEqualTo("active", true);
44          root.addAndCriteria(activeFilter);
45  
46          Query query = QueryFactory.newQuery(LeaveAdjustment.class, root);
47          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
48  
49          if (c != null) {
50          	leaveAdjustments.addAll(c);
51          }
52          return leaveAdjustments;
53      }
54  
55  	@Override
56  	public LeaveAdjustment getLeaveAdjustment(String lmLeaveAdjustmentId) {
57  		Criteria crit = new Criteria();
58          crit.addEqualTo("lmLeaveAdjustmentId", lmLeaveAdjustmentId);
59          Query query = QueryFactory.newQuery(LeaveAdjustment.class, crit);
60          return (LeaveAdjustment) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
61  	}
62  
63  	@Override
64  	public List<LeaveAdjustment> getLeaveAdjustments(LocalDate fromEffdt, LocalDate toEffdt, String principalId, String accrualCategory, String earnCode) {
65          List<LeaveAdjustment> results = new ArrayList<LeaveAdjustment>();
66      	
67      	Criteria root = new Criteria();
68      	
69          Criteria effectiveDateFilter = new Criteria();
70          if (fromEffdt != null) {
71              effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
72          }
73          if (toEffdt != null) {
74              effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
75          }
76          if (fromEffdt == null && toEffdt == null) {
77              effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
78          }
79          root.addAndCriteria(effectiveDateFilter);
80          
81          if (StringUtils.isNotBlank(principalId)) {
82              root.addLike("UPPER(`principal_id`)", principalId.toUpperCase()); // KPME-2695 in case principal id is not a number
83          }
84  
85          if (StringUtils.isNotBlank(accrualCategory)) {
86              root.addLike("UPPER(`accrual_cat`)", accrualCategory.toUpperCase()); // KPME-2695
87          }
88  
89          if (StringUtils.isNotBlank(earnCode)) {
90          	root.addLike("UPPER(`earn_code`)", earnCode.toUpperCase()); // KPME-2695
91          }
92  
93          Query query = QueryFactory.newQuery(LeaveAdjustment.class, root);
94          results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
95  
96          return results;
97  	}
98  
99  }