001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.lm.leaveblock.dao;
017
018 import java.sql.Timestamp;
019 import java.util.*;
020
021 import org.apache.commons.collections.CollectionUtils;
022 import org.apache.commons.lang3.StringUtils;
023 import org.apache.log4j.Logger;
024 import org.apache.ojb.broker.query.Criteria;
025 import org.apache.ojb.broker.query.Query;
026 import org.apache.ojb.broker.query.QueryFactory;
027 import org.apache.ojb.broker.query.ReportQueryByCriteria;
028 import org.joda.time.DateTime;
029 import org.kuali.hr.lm.LMConstants;
030 import org.kuali.hr.lm.leaveblock.LeaveBlock;
031 import org.kuali.hr.time.earncode.EarnCode;
032 import org.kuali.hr.time.service.base.TkServiceLocator;
033 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
034
035 public class LeaveBlockDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements LeaveBlockDao {
036
037 private static final Logger LOG = Logger.getLogger(LeaveBlockDaoSpringOjbImpl.class);
038
039 @Override
040 public LeaveBlock getLeaveBlock(String leaveBlockId) {
041 Criteria root = new Criteria();
042 root.addEqualTo("lmLeaveBlockId", leaveBlockId);
043 // root.addEqualTo("active", true);
044
045 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
046
047 return (LeaveBlock) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
048 }
049
050 @Override
051 public List<LeaveBlock> getLeaveBlocksForDocumentId(String documentId) {
052 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
053 Criteria root = new Criteria();
054 root.addEqualTo("documentId", documentId);
055 // root.addEqualTo("active", true);
056
057 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
058 @SuppressWarnings("rawtypes")
059 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
060
061 if (c != null) {
062 leaveBlocks.addAll(c);
063 }
064 return leaveBlocks;
065 }
066
067 @SuppressWarnings("rawtypes")
068 @Override
069 public List<LeaveBlock> getLeaveBlocks(String principalId, Date beginDate, Date endDate) {
070 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
071 Criteria root = new Criteria();
072 root.addEqualTo("principalId", principalId);
073 root.addGreaterOrEqualThan("leaveDate", beginDate);
074 root.addLessOrEqualThan("leaveDate", endDate);
075 // root.addEqualTo("active", true);
076
077 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
078 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
079
080 if (c != null) {
081 leaveBlocks.addAll(c);
082 }
083
084 return leaveBlocks;
085 }
086
087 @SuppressWarnings("rawtypes")
088 @Override
089 public List<LeaveBlock> getLeaveBlocksWithType(String principalId, Date beginDate, Date endDate, String leaveBlockType) {
090 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
091 Criteria root = new Criteria();
092 root.addEqualTo("principalId", principalId);
093 root.addGreaterOrEqualThan("leaveDate", beginDate);
094 root.addLessOrEqualThan("leaveDate", endDate);
095 root.addEqualTo("leaveBlockType", leaveBlockType);
096 // root.addEqualTo("active", true);
097
098 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
099 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
100
101 if (c != null) {
102 leaveBlocks.addAll(c);
103 }
104
105 return leaveBlocks;
106 }
107
108 @Override
109 public List<LeaveBlock> getLeaveBlocksWithAccrualCategory(String principalId, Date beginDate, Date endDate, String accrualCategory) {
110 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
111 Criteria root = new Criteria();
112 root.addEqualTo("principalId", principalId);
113 root.addGreaterOrEqualThan("leaveDate", beginDate);
114 root.addLessOrEqualThan("leaveDate", endDate);
115 root.addEqualTo("accrualCategory", accrualCategory);
116 // root.addEqualTo("active", true);
117
118 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
119 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
120
121 if (c != null) {
122 leaveBlocks.addAll(c);
123 }
124
125 return leaveBlocks;
126 }
127
128 public List<LeaveBlock> getLeaveBlocksSinceCarryOver(String principalId, Map<String, LeaveBlock> carryOverDates, DateTime endDate, boolean includeAllAccrualCategories) {
129 Criteria root = new Criteria();
130 root.addEqualTo("principalId", principalId);
131 if (endDate != null) {
132 root.addLessOrEqualThan("leaveDate", endDate.toDate());
133 }
134
135 Criteria orCriteria = new Criteria();
136 for (Map.Entry<String, LeaveBlock> entry : carryOverDates.entrySet()) {
137 Criteria crit = new Criteria();
138 crit.addEqualTo("accrualCategory", entry.getKey());
139 crit.addGreaterThan("leaveDate", entry.getValue().getLeaveDate());
140 orCriteria.addOrCriteria(crit);
141 }
142 if (!orCriteria.isEmpty()) {
143 if (CollectionUtils.isNotEmpty(carryOverDates.keySet()) && includeAllAccrualCategories) {
144 Criteria crit = new Criteria();
145 crit.addNotIn("accrualCategory", carryOverDates.keySet());
146 orCriteria.addOrCriteria(crit);
147 }
148 root.addAndCriteria(orCriteria);
149 }
150
151
152 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
153 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
154 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
155 if (c != null) {
156 leaveBlocks.addAll(c);
157 }
158
159 return leaveBlocks;
160 }
161
162 public Map<String, LeaveBlock> getLastCarryOverBlocks(String principalId, String leaveBlockType, Date asOfDate) {
163 Map<String, LeaveBlock> carryOver = new HashMap<String, LeaveBlock>();
164 Criteria root = new Criteria();
165 root.addEqualTo("principalId", principalId);
166 root.addEqualTo("leaveBlockType", leaveBlockType);
167
168 Criteria dateSubquery = new Criteria();
169 dateSubquery.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
170 dateSubquery.addEqualToField("leaveBlockType", Criteria.PARENT_QUERY_PREFIX + "leaveBlockType");
171 dateSubquery.addEqualToField("accrualCategory", Criteria.PARENT_QUERY_PREFIX + "accrualCategory");
172 if (asOfDate != null) {
173 dateSubquery.addLessThan("leaveDate", asOfDate);
174 }
175
176 ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(LeaveBlock.class, dateSubquery);
177 String[] attributes = new String[] { "max(leaveDate)" };
178 subQuery.setAttributes(attributes);
179
180 root.addEqualTo("leaveDate", subQuery);
181
182 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
183 Collection<LeaveBlock> c = (Collection<LeaveBlock>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
184 //Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
185 for (LeaveBlock lb : c) {
186 carryOver.put(lb.getAccrualCategory(), lb);
187 }
188
189 return carryOver;
190
191 }
192
193 @Override
194 public List<LeaveBlock> getLeaveBlocks(String principalId, String leaveBlockType, String requestStatus, Date currentDate) {
195 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
196 Criteria root = new Criteria();
197 root.addEqualTo("principalId", principalId);
198 root.addEqualTo("leaveBlockType", leaveBlockType);
199 root.addEqualTo("requestStatus", requestStatus);
200 if(currentDate != null) {
201 root.addGreaterThan("leaveDate", currentDate);
202 }
203 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
204 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
205 if(c!= null) {
206 leaveBlocks.addAll(c);
207 }
208 return leaveBlocks;
209 }
210
211 @Override
212 public List<LeaveBlock> getLeaveBlocksForDate(String principalId, Date leaveDate) {
213 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
214 Criteria root = new Criteria();
215 root.addEqualTo("principalId", principalId);
216 root.addEqualTo("leaveDate", leaveDate);
217 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
218 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
219 if(c!= null) {
220 leaveBlocks.addAll(c);
221 }
222 return leaveBlocks;
223 }
224
225 @Override
226 public List<LeaveBlock> getLeaveBlocks(Date leaveDate, String accrualCategory, String principalId) {
227 Criteria root = new Criteria();
228 root.addLessOrEqualThan("timestamp", leaveDate);
229 root.addEqualTo("accrualCategory", accrualCategory);
230 root.addEqualTo("principalId", principalId);
231 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
232 List<LeaveBlock> leaveBlocks = (List<LeaveBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
233 return leaveBlocks;
234 }
235
236 @Override
237 public List<LeaveBlock> getLeaveBlocks(String principalId, String accrualCategory, Date beginDate, Date endDate) {
238 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
239 Criteria root = new Criteria();
240 root.addEqualTo("principalId", principalId);
241 root.addGreaterOrEqualThan("leaveDate", beginDate);
242 root.addLessOrEqualThan("leaveDate", endDate);
243 root.addEqualTo("accrualCategory", accrualCategory);
244 // root.addEqualTo("active", true);
245
246 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
247 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
248
249 if (c != null) {
250 leaveBlocks.addAll(c);
251 }
252
253 return leaveBlocks;
254 }
255
256 @Override
257 public List<LeaveBlock> getFMLALeaveBlocks(String principalId, String accrualCategory, Date beginDate, Date endDate) {
258 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
259 Criteria root = new Criteria();
260 root.addEqualTo("principalId", principalId);
261 root.addGreaterOrEqualThan("leaveDate", beginDate);
262 root.addLessOrEqualThan("leaveDate", endDate);
263 root.addEqualTo("accrualCategory", accrualCategory);
264
265 Criteria earnCode = new Criteria();
266 earnCode.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
267 earnCode.addEqualTo("fmla", "Y");
268 ReportQueryByCriteria earnCodeSubQuery = QueryFactory.newReportQuery(EarnCode.class, earnCode);
269 root.addEqualTo("earnCode", earnCodeSubQuery);
270
271 //root.add
272 // root.addEqualTo("active", true);
273
274 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
275 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
276
277 if (c != null) {
278 leaveBlocks.addAll(c);
279 }
280
281 return leaveBlocks;
282 }
283
284 @Override
285 public List<LeaveBlock> getNotAccrualGeneratedLeaveBlocksForDate(String principalId, Date leaveDate) {
286 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
287 Criteria root = new Criteria();
288 root.addEqualTo("principalId", principalId);
289 root.addEqualTo("leaveDate", leaveDate);
290 root.addEqualTo("accrualGenerated", "N");
291 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
292 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
293 if(c!= null) {
294 leaveBlocks.addAll(c);
295 }
296 return leaveBlocks;
297 }
298
299 public List<LeaveBlock> getCalendarLeaveBlocks(String principalId, Date beginDate, Date endDate) {
300 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
301
302 Criteria root = new Criteria();
303 root.addEqualTo("principalId", principalId);
304 root.addGreaterOrEqualThan("leaveDate", beginDate);
305 root.addLessOrEqualThan("leaveDate", endDate);
306 List<String> typeValues = new ArrayList<String>();
307 typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR);
308 typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.TIME_CALENDAR);
309 typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE);
310 root.addIn("leaveBlockType", typeValues);
311
312 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
313 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
314
315 if (c != null) {
316 leaveBlocks.addAll(c);
317 }
318 return leaveBlocks;
319
320 }
321
322 public void deleteLeaveBlock(String leaveBlockId) {
323 Criteria crit = new Criteria();
324 crit.addEqualTo("lmLeaveBlockId",leaveBlockId);
325 this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveBlock.class, crit));
326 }
327
328 public void deleteLeaveBlocksForDocumentId(String documentId){
329 Criteria crit = new Criteria();
330 crit.addEqualTo("documentId",documentId);
331 this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveBlock.class, crit));
332
333 }
334
335
336 @Override
337 public List<LeaveBlock> getAccrualGeneratedLeaveBlocks(String principalId, Date beginDate, Date endDate) {
338 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
339 Criteria root = new Criteria();
340
341 root.addEqualTo("principalId", principalId);
342 root.addGreaterOrEqualThan("leaveDate", beginDate);
343 root.addLessOrEqualThan("leaveDate", endDate);
344 root.addEqualTo("accrualGenerated", "Y");
345
346 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
347 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
348 if (c != null) {
349 leaveBlocks.addAll(c);
350 }
351 return leaveBlocks;
352 }
353
354 @Override
355 public List<LeaveBlock> getSSTOLeaveBlocks(String principalId, String sstoId, Date accruledDate) {
356 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
357 Criteria root = new Criteria();
358
359 root.addEqualTo("principalId", principalId);
360 root.addEqualTo("leaveDate", accruledDate);
361 root.addEqualTo("scheduleTimeOffId", sstoId);
362
363 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
364 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
365 if (c != null) {
366 leaveBlocks.addAll(c);
367 }
368 return leaveBlocks;
369 }
370
371 @Override
372 public List<LeaveBlock> getABELeaveBlocksSinceTime(String principalId, Timestamp lastRanTime) {
373 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
374 Criteria root = new Criteria();
375
376 root.addEqualTo("principalId", principalId);
377 root.addGreaterThan("timestamp", lastRanTime);
378 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
379 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
380 List<LeaveBlock> tempList = new ArrayList<LeaveBlock>();
381 if (c != null) {
382 tempList.addAll(c);
383 }
384 for(LeaveBlock lb : tempList) {
385 if(lb != null && StringUtils.isNotEmpty(lb.getEarnCode())) {
386 EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(lb.getEarnCode(), lb.getLeaveDate());
387 if(ec != null && ec.getEligibleForAccrual().equals("N")) {
388 leaveBlocks.add(lb);
389 }
390 }
391 }
392 return leaveBlocks;
393 }
394
395 }