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.time.accrual.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import com.google.common.collect.ImmutableList;
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.log4j.Logger;
27  import org.apache.ojb.broker.query.Criteria;
28  import org.apache.ojb.broker.query.Query;
29  import org.apache.ojb.broker.query.QueryFactory;
30  import org.apache.ojb.broker.query.ReportQueryByCriteria;
31  import org.kuali.hr.core.util.OjbSubQueryUtil;
32  import org.kuali.hr.time.accrual.TimeOffAccrual;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  
35  public class TimeOffAccrualDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TimeOffAccrualDao {
36      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
37              .add("principalId")
38              .add("accrualCategory")
39              .build();
40  
41  	@SuppressWarnings("unused")
42  	private static final Logger LOG = Logger.getLogger(TimeOffAccrualDaoSpringOjbImpl.class);
43  
44  	public void saveOrUpdate(TimeOffAccrual timeOffAccrual) {
45  		this.getPersistenceBrokerTemplate().store(timeOffAccrual);
46  	}
47  
48  	public void saveOrUpdate(List<TimeOffAccrual> timeOffAccrualList) {
49  		if (timeOffAccrualList != null) {
50  			for (TimeOffAccrual timeOffAccrual : timeOffAccrualList) {
51  				this.getPersistenceBrokerTemplate().store(timeOffAccrual);
52  			}
53  		}
54  	}
55  	
56  	public List<TimeOffAccrual> getTimeOffAccruals (String principalId) {
57  		List<TimeOffAccrual> timeOffAccruals = new LinkedList<TimeOffAccrual>();
58  		
59  		Criteria root = new Criteria();
60  		root.addEqualTo("principalId", principalId);
61  
62          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(TimeOffAccrual.class, EQUAL_TO_FIELDS, false));
63  
64  		Query query = QueryFactory.newQuery(TimeOffAccrual.class, root);
65  		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
66  		
67  		if (c != null) {
68  			timeOffAccruals.addAll(c);
69  		}
70  
71  		return timeOffAccruals;
72  	}
73  
74  	@Override
75  	public TimeOffAccrual getTimeOffAccrual(Long laTimeOffAccrualId) {
76  		Criteria crit = new Criteria();
77  		crit.addEqualTo("lmAccrualId", laTimeOffAccrualId);
78  		
79  		Query query = QueryFactory.newQuery(TimeOffAccrual.class, crit);
80  		return (TimeOffAccrual)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
81  
82  	}
83  	
84  	// KPME-1011
85  	public List<TimeOffAccrual> getActiveTimeOffAccruals (String principalId, Date asOfDate) {
86  		List<TimeOffAccrual> timeOffAccruals = new LinkedList<TimeOffAccrual>();
87  			
88  	    Criteria root = new Criteria();
89          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TimeOffAccrual.class, asOfDate, EQUAL_TO_FIELDS, false));
90  
91          root.addEqualTo("principalId", principalId);
92  
93          Query query = QueryFactory.newQuery(TimeOffAccrual.class, root);
94          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
95  
96          if (c != null) {
97              timeOffAccruals.addAll(c);
98          }
99  
100         return timeOffAccruals;
101     }
102 	
103 	@Override
104 	public int getTimeOffAccrualCount(String accrualCategory, Date effectiveDate, String principalId, String lmAccrualId) {
105 		Criteria crit = new Criteria();
106 		crit.addEqualTo("accrualCategory", accrualCategory);
107 		crit.addEqualTo("effectiveDate", effectiveDate);
108 		crit.addEqualTo("principalId", principalId);
109 		if(lmAccrualId != null) {
110 			crit.addEqualTo("lmAccrualId", lmAccrualId);
111 		}
112 		Query query = QueryFactory.newQuery(TimeOffAccrual.class, crit);
113 		return this.getPersistenceBrokerTemplate().getCount(query);
114 	}
115 
116     @Override
117     public List<TimeOffAccrual> getTimeOffAccruals(String principalId, String accrualCategory) {
118         Criteria crit = new Criteria();
119 
120         List<TimeOffAccrual> results = new ArrayList<TimeOffAccrual>();
121 
122         if(StringUtils.isNotBlank(principalId) && StringUtils.isNotEmpty(principalId)){
123             crit.addLike("principalId", principalId);
124         }
125         if(StringUtils.isNotBlank(accrualCategory)){
126             crit.addLike("accrualCategory", accrualCategory);
127         }
128 
129         Query query = QueryFactory.newQuery(TimeOffAccrual.class, crit);
130         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
131         results.addAll(c);
132 
133         return results;
134     }
135 }