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.donation.dao;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
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.donation.LeaveDonation;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class LeaveDonationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements LeaveDonationDao {
32      
33  	private static final Logger LOG = Logger.getLogger(LeaveDonationDaoOjbImpl.class);
34  
35  	@Override
36  	public LeaveDonation getLeaveDonation(String lmLeaveDonationId) {
37  		Criteria crit = new Criteria();
38  		crit.addEqualTo("lmLeaveDonationId", lmLeaveDonationId);
39  		Query query = QueryFactory.newQuery(LeaveDonation.class, crit);
40  		return (LeaveDonation) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
41  	}
42  
43  	@Override
44  	public List<LeaveDonation> getLeaveDonations(LocalDate fromEffdt, LocalDate toEffdt, String donorsPrincipalId, String donatedAccrualCategory, String amountDonated, String recipientsPrincipalId, String recipientsAccrualCategory, String amountReceived, String active, String showHist) {
45          List<LeaveDonation> results = new ArrayList<LeaveDonation>();
46      	
47      	Criteria root = new Criteria();
48      	
49          Criteria effectiveDateFilter = new Criteria();
50          if (fromEffdt != null) {
51              effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
52          }
53          if (toEffdt != null) {
54              effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
55          }
56          if (fromEffdt == null && toEffdt == null) {
57              effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
58          }
59          root.addAndCriteria(effectiveDateFilter);
60  
61          if (StringUtils.isNotBlank(donorsPrincipalId)) {
62              root.addLike("UPPER(donor)", donorsPrincipalId.toUpperCase()); // KPME-2695 in case principal id is not a number
63          }
64          
65          if (StringUtils.isNotBlank(donatedAccrualCategory)) {
66              root.addLike("UPPER(donatedAccrualCategory)", donatedAccrualCategory.toUpperCase()); // KPME-2695
67          }
68          
69          if (StringUtils.isNotBlank(amountDonated)) {
70          	root.addLike("amountDonated", amountDonated);
71          }
72          
73          if (StringUtils.isNotBlank(recipientsPrincipalId)) {
74          	root.addLike("UPPER(recepient)", recipientsPrincipalId.toUpperCase()); // KPME-2695 in case principal id is not a number
75          }
76          
77          if (StringUtils.isNotBlank(recipientsAccrualCategory)) {
78          	root.addLike("UPPER(recipientsAccrualCategory)", recipientsAccrualCategory.toUpperCase()); // KPME-2695
79          }
80          
81          if (StringUtils.isNotBlank(amountReceived)) {
82          	root.addLike("amountReceived", amountReceived);
83          }
84          
85          if (StringUtils.isNotBlank(active)) {
86          	Criteria activeFilter = new Criteria();
87              if (StringUtils.equals(active, "Y")) {
88                  activeFilter.addEqualTo("active", true);
89              } else if (StringUtils.equals(active, "N")) {
90                  activeFilter.addEqualTo("active", false);
91              }
92              root.addAndCriteria(activeFilter);
93          }
94  
95          if (StringUtils.equals(showHist, "N")) {
96              root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(LeaveDonation.class, effectiveDateFilter, LeaveDonation.BUSINESS_KEYS, false));
97              root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(LeaveDonation.class, LeaveDonation.BUSINESS_KEYS, false));
98          }
99  
100         Query query = QueryFactory.newQuery(LeaveDonation.class, root);
101         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
102 
103         return results;
104 	}
105 	
106 }