View Javadoc

1   /**
2    * Copyright 2004-2014 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.transfer.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.log4j.Logger;
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.joda.time.LocalDate;
28  import org.kuali.kpme.tklm.leave.transfer.BalanceTransfer;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class BalanceTransferDaoOjbImpl extends PlatformAwareDaoBaseOjb implements
32  		BalanceTransferDao {
33  	
34  	private static final Logger LOG = Logger.getLogger(BalanceTransfer.class);
35  
36  	@Override
37  	public List<BalanceTransfer> getAllBalanceTransfersForPrincipalId(
38  			String principalId) {
39  		Criteria crit = new Criteria();
40  		List<BalanceTransfer> balanceTransfers = new ArrayList<BalanceTransfer>();
41  		crit.addEqualTo("principalId",principalId);
42  		Query query = QueryFactory.newQuery(BalanceTransfer.class,crit);
43  		
44  		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
45  		
46  		if(c != null)
47  			balanceTransfers.addAll(c);
48  		
49  		return balanceTransfers;
50  	}
51  
52  	@Override
53  	public List<BalanceTransfer> getAllBalanceTransferForPrincipalIdAsOfDate(
54  			String principalId, LocalDate effectiveDate) {
55  		List<BalanceTransfer> balanceTransfers = new ArrayList<BalanceTransfer>();
56  		Criteria crit = new Criteria();
57  		crit.addEqualTo("principalId",principalId);
58  		Criteria effDate = new Criteria();
59  		effDate.addGreaterOrEqualThan("effectiveDate", effectiveDate.toDate());
60  		crit.addAndCriteria(effDate);
61  		Query query = QueryFactory.newQuery(BalanceTransfer.class,crit);
62  		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
63  		
64  		if(c != null)
65  			balanceTransfers.addAll(c);
66  		
67  		return balanceTransfers;
68  	}
69  
70  	@Override
71  	public List<BalanceTransfer> getAllBalanceTransferByEffectiveDate(
72  			LocalDate effectiveDate) {
73  		List<BalanceTransfer> balanceTransfers = new ArrayList<BalanceTransfer>();
74  		Criteria effDate = new Criteria();
75  		effDate.addGreaterOrEqualThan("effectiveDate", effectiveDate.toDate());
76  		Query query = QueryFactory.newQuery(BalanceTransfer.class,effDate);
77  		
78  		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
79  		
80  		if(c != null)
81  			balanceTransfers.addAll(c);
82  		
83  		return balanceTransfers;
84  	}
85  
86  	@Override
87  	public BalanceTransfer getBalanceTransferById(String balanceTransferId) {
88  		Criteria crit = new Criteria();
89  		crit.addEqualTo("balanceTransferId",balanceTransferId);
90  		Query query = QueryFactory.newQuery(BalanceTransfer.class,crit);
91  		return (BalanceTransfer) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
92  	}
93  
94  	@Override
95  	public List<BalanceTransfer> getAllBalanceTransfersMarkedPayoutForPrincipalId(
96  			String principalId) {
97  		Criteria crit = new Criteria();
98  		List<BalanceTransfer> balanceTransfers = new ArrayList<BalanceTransfer>();
99  		crit.addEqualTo("principalId",principalId);
100 		Criteria payoutCrit = new Criteria();
101 		payoutCrit.addNotNull("earnCode");
102 		crit.addAndCriteria(payoutCrit);
103 		Query query = QueryFactory.newQuery(BalanceTransfer.class,crit);
104 		
105 		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
106 		
107 		if(c != null)
108 			balanceTransfers.addAll(c);
109 		
110 		return balanceTransfers;
111 	}
112 
113 	@Override
114 	public List<BalanceTransfer> getAllBalanceTransfersForAccrualCategoryRuleByDate(
115 			String accrualRuleId, LocalDate asOfDate) {
116 		Criteria crit = new Criteria();
117 		List<BalanceTransfer> balanceTransfers = new ArrayList<BalanceTransfer>();
118 		crit.addGreaterOrEqualThan("effectiveDate",asOfDate.toDate());
119 		Criteria accrualCategory = new Criteria();
120 		accrualCategory.addEqualTo("accrualCategoryRule", accrualRuleId);
121 		crit.addAndCriteria(accrualCategory);
122 		Query query = QueryFactory.newQuery(BalanceTransfer.class,crit);
123 		
124 		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
125 		
126 		if(c != null)
127 			balanceTransfers.addAll(c);
128 		
129 		return balanceTransfers;
130 	}
131 
132 	@Override
133 	public List<BalanceTransfer> getBalanceTransfers(String viewPrincipal, LocalDate beginPeriodDate, LocalDate endPeriodDate) {
134 		// TODO Auto-generated method stub
135 		List<BalanceTransfer> balanceTransfers = new ArrayList<BalanceTransfer>();
136 		Criteria crit = new Criteria();
137 		crit.addEqualTo("principalId",viewPrincipal);
138 		
139 		Criteria effDate = new Criteria();
140 		effDate.addGreaterOrEqualThan("effectiveDate", beginPeriodDate.toDate());
141 		effDate.addLessOrEqualThan("effectiveDate", endPeriodDate.toDate());
142 		
143 		crit.addAndCriteria(effDate);
144 		
145 		Query query = QueryFactory.newQuery(BalanceTransfer.class,crit);
146 		
147 		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
148 		
149 		if(c != null)
150 			balanceTransfers.addAll(c);
151 		
152 		return balanceTransfers;
153 	}
154 
155     @Override
156     public void saveOrUpdate(BalanceTransfer balanceTransfer) {
157         this.getPersistenceBrokerTemplate().store(balanceTransfer);
158     }
159     
160     @Override
161     public List<BalanceTransfer> getBalanceTransfers(String principalId, String fromAccrualCategory, String transferAmount, String toAccrualCategory, String amountTransferred, String forfeitedAmount, LocalDate fromEffdt, LocalDate toEffdt) {
162         List<BalanceTransfer> results = new ArrayList<BalanceTransfer>();
163     	
164     	Criteria root = new Criteria();
165 
166         if (StringUtils.isNotBlank(principalId)) {
167             root.addLike("UPPER(principalId)", principalId.toUpperCase()); // KPME-2695 in case principal id is not a number
168         }
169         
170         if (StringUtils.isNotBlank(fromAccrualCategory)) {
171             root.addLike("UPPER(fromAccrualCategory)", fromAccrualCategory.toUpperCase()); // KPME-2695
172         }
173         
174         if (StringUtils.isNotBlank(transferAmount)) {
175         	root.addLike("transferAmount", transferAmount);
176         }
177         
178         if (StringUtils.isNotBlank(toAccrualCategory)) {
179         	root.addLike("UPPER(toAccrualCategory)", toAccrualCategory.toUpperCase()); // KPME-2695
180         }
181         
182         if (StringUtils.isNotBlank(amountTransferred)) {
183         	root.addLike("amountTransferred", amountTransferred);
184         }
185         
186         if (StringUtils.isNotBlank(forfeitedAmount)) {
187         	root.addLike("forfeitedAmount", forfeitedAmount);
188         }
189         
190         Criteria effectiveDateFilter = new Criteria();
191         if (fromEffdt != null) {
192             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
193         }
194         if (toEffdt != null) {
195             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
196         }
197         if (fromEffdt == null && toEffdt == null) {
198             effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
199         }
200         root.addAndCriteria(effectiveDateFilter);
201 
202         Query query = QueryFactory.newQuery(BalanceTransfer.class, root);
203         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
204 
205         return results;
206     }
207 
208 }