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.leaveplan.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.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.joda.time.DateTime;
31  import org.kuali.hr.core.util.OjbSubQueryUtil;
32  import org.kuali.hr.lm.leaveplan.LeavePlan;
33  import org.kuali.hr.time.calendar.CalendarEntries;
34  import org.kuali.hr.time.util.TKUtils;
35  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
36  
37  public class LeavePlanDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements LeavePlanDao {
38      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
39              .add("leavePlan")
40              .build();
41  
42  	@Override
43  	public LeavePlan getLeavePlan(String lmLeavePlanId) {
44  		Criteria crit = new Criteria();
45  		crit.addEqualTo("lmLeavePlanId", lmLeavePlanId);
46  		Query query = QueryFactory.newQuery(LeavePlan.class, crit);
47  		return (LeavePlan) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
48  	}
49  	
50  	@Override
51  	public LeavePlan getLeavePlan(String leavePlan, Date asOfDate) {
52  		LeavePlan lp = null;
53  
54  		Criteria root = new Criteria();
55  
56          java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
57  		root.addEqualTo("leavePlan", leavePlan);
58          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(LeavePlan.class, effDate, EQUAL_TO_FIELDS, false));
59          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(LeavePlan.class, EQUAL_TO_FIELDS, false));
60  		
61  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
62  		activeFilter.addEqualTo("active", true);
63  		root.addAndCriteria(activeFilter);
64  		
65  		
66  		Query query = QueryFactory.newQuery(LeavePlan.class, root);
67  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
68  
69  		if (obj != null) {
70  			lp = (LeavePlan) obj;
71  		}
72  
73  		return lp;
74  	}
75  
76  	@Override
77  	public int getNumberLeavePlan(String leavePlan) {
78  		Criteria crit = new Criteria();
79  		crit.addEqualTo("leavePlan", leavePlan);
80  		Query query = QueryFactory.newQuery(LeavePlan.class, crit);
81  		return this.getPersistenceBrokerTemplate().getCount(query);
82  	}
83  	
84  	@Override
85  	public List<LeavePlan> getAllActiveLeavePlan(String leavePlan, Date asOfDate) {
86  		Criteria root = new Criteria();
87          root.addEqualTo("leavePlan", leavePlan);
88          root.addEqualTo("active", true);
89          root.addLessOrEqualThan("effectiveDate", asOfDate);
90  
91          Query query = QueryFactory.newQuery(LeavePlan.class, root);
92          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
93          
94          List<LeavePlan> lps = new LinkedList<LeavePlan>();
95          if (c != null) {
96          	lps.addAll(c);
97          }
98          return lps;
99  		
100 	}
101 	
102 	@Override
103 	public List<LeavePlan> getAllInActiveLeavePlan(String leavePlan, Date asOfDate) {
104 		Criteria root = new Criteria();
105         root.addEqualTo("leavePlan", leavePlan);
106         root.addEqualTo("active", false);
107         root.addLessOrEqualThan("effectiveDate", asOfDate);
108         
109         Query query = QueryFactory.newQuery(LeavePlan.class, root);
110         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
111         
112         List<LeavePlan> lps = new LinkedList<LeavePlan>();
113         if (c != null) {
114         	lps.addAll(c);
115         }
116         return lps;
117 	}
118 
119     @Override
120     @SuppressWarnings("unchecked")
121     public List<LeavePlan> getLeavePlans(String leavePlan, String calendarYearStart, String descr, String planningMonths, Date fromEffdt, Date toEffdt, 
122     									 String active, String showHistory) {
123 
124         List<LeavePlan> results = new ArrayList<LeavePlan>();
125         
126     	Criteria root = new Criteria();
127 
128         if (StringUtils.isNotBlank(leavePlan)) {
129         	root.addLike("leavePlan", leavePlan);
130         }
131         
132         if (StringUtils.isNotBlank(calendarYearStart)) {
133         	root.addEqualTo("calendarYearStart", calendarYearStart);
134         }
135         
136         if (StringUtils.isNotBlank(descr)) {
137         	root.addLike("descr", descr);
138         }
139         
140         if (StringUtils.isNotBlank(planningMonths)) {
141         	root.addEqualTo("planningMonths", planningMonths);
142         }
143         
144         Criteria effectiveDateFilter = new Criteria();
145         if (fromEffdt != null) {
146             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
147         }
148         if (toEffdt != null) {
149             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
150         }
151         if (fromEffdt == null && toEffdt == null) {
152             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
153         }
154         root.addAndCriteria(effectiveDateFilter);
155 
156         if (StringUtils.isNotBlank(active)) {
157             Criteria activeFilter = new Criteria();
158             if (StringUtils.equals(active, "Y")) {
159                 activeFilter.addEqualTo("active", true);
160             } else if (StringUtils.equals(active, "N")) {
161                 activeFilter.addEqualTo("active", false);
162             }
163             root.addAndCriteria(activeFilter);
164         }
165 
166         if (StringUtils.equals(showHistory, "N")) {
167             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(LeavePlan.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
168             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(LeavePlan.class, EQUAL_TO_FIELDS, false));
169         }
170 
171         Query query = QueryFactory.newQuery(LeavePlan.class, root);
172         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
173 
174         return results;
175     }
176 
177 	@Override
178 	public List<LeavePlan> getLeavePlansNeedsScheduled(int thresholdDays,
179 			Date asOfDate) {
180 
181         Criteria root = new Criteria();
182 
183         root.addEqualTo("active", true);
184 
185         Query query = QueryFactory.newQuery(LeavePlan.class, root);
186         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
187 
188         List<LeavePlan> leavePlans = new ArrayList<LeavePlan>(c.size());
189         leavePlans.addAll(c);
190 
191         return leavePlans;
192 	}
193     
194 }