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 beginDate, Date endDate) {
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(beginDate != null) {
201 root.addGreaterOrEqualThan("leaveDate", beginDate);
202 }
203 if (endDate != null){
204 root.addLessOrEqualThan("leaveDate", endDate);
205 }
206 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
207 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
208 if(c!= null) {
209 leaveBlocks.addAll(c);
210 }
211 return leaveBlocks;
212 }
213
214 @Override
215 public List<LeaveBlock> getLeaveBlocks(String principalId, String leaveBlockType, String requestStatus, Date currentDate) {
216 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
217 Criteria root = new Criteria();
218 root.addEqualTo("principalId", principalId);
219 root.addEqualTo("leaveBlockType", leaveBlockType);
220 root.addEqualTo("requestStatus", requestStatus);
221 if(currentDate != null) {
222 root.addGreaterThan("leaveDate", currentDate);
223 }
224 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
225 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
226 if(c!= null) {
227 leaveBlocks.addAll(c);
228 }
229 return leaveBlocks;
230 }
231
232 @Override
233 public List<LeaveBlock> getLeaveBlocksForDate(String principalId, Date leaveDate) {
234 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
235 Criteria root = new Criteria();
236 root.addEqualTo("principalId", principalId);
237 root.addEqualTo("leaveDate", leaveDate);
238 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
239 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
240 if(c!= null) {
241 leaveBlocks.addAll(c);
242 }
243 return leaveBlocks;
244 }
245
246 @Override
247 public List<LeaveBlock> getLeaveBlocks(Date leaveDate, String accrualCategory, String principalId) {
248 Criteria root = new Criteria();
249 root.addLessOrEqualThan("timestamp", leaveDate);
250 root.addEqualTo("accrualCategory", accrualCategory);
251 root.addEqualTo("principalId", principalId);
252 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
253 List<LeaveBlock> leaveBlocks = (List<LeaveBlock>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
254 return leaveBlocks;
255 }
256
257 @Override
258 public List<LeaveBlock> getLeaveBlocks(String principalId, String accrualCategory, Date beginDate, Date endDate) {
259 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
260 Criteria root = new Criteria();
261 root.addEqualTo("principalId", principalId);
262 root.addGreaterOrEqualThan("leaveDate", beginDate);
263 root.addLessOrEqualThan("leaveDate", endDate);
264 root.addEqualTo("accrualCategory", accrualCategory);
265 // root.addEqualTo("active", true);
266
267 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
268 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
269
270 if (c != null) {
271 leaveBlocks.addAll(c);
272 }
273
274 return leaveBlocks;
275 }
276
277 @Override
278 public List<LeaveBlock> getFMLALeaveBlocks(String principalId, String accrualCategory, Date beginDate, Date endDate) {
279 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
280 Criteria root = new Criteria();
281 root.addEqualTo("principalId", principalId);
282 root.addGreaterOrEqualThan("leaveDate", beginDate);
283 root.addLessOrEqualThan("leaveDate", endDate);
284 root.addEqualTo("accrualCategory", accrualCategory);
285
286 Criteria earnCode = new Criteria();
287 earnCode.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
288 earnCode.addEqualTo("fmla", "Y");
289 ReportQueryByCriteria earnCodeSubQuery = QueryFactory.newReportQuery(EarnCode.class, earnCode);
290 root.addEqualTo("earnCode", earnCodeSubQuery);
291
292 //root.add
293 // root.addEqualTo("active", true);
294
295 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
296 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
297
298 if (c != null) {
299 leaveBlocks.addAll(c);
300 }
301
302 return leaveBlocks;
303 }
304
305 @Override
306 public List<LeaveBlock> getNotAccrualGeneratedLeaveBlocksForDate(String principalId, Date leaveDate) {
307 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
308 Criteria root = new Criteria();
309 root.addEqualTo("principalId", principalId);
310 root.addEqualTo("leaveDate", leaveDate);
311 root.addEqualTo("accrualGenerated", "N");
312 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
313 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
314 if(c!= null) {
315 leaveBlocks.addAll(c);
316 }
317 return leaveBlocks;
318 }
319
320 public List<LeaveBlock> getCalendarLeaveBlocks(String principalId, Date beginDate, Date endDate) {
321 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
322
323 Criteria root = new Criteria();
324 root.addEqualTo("principalId", principalId);
325 root.addGreaterOrEqualThan("leaveDate", beginDate);
326 root.addLessOrEqualThan("leaveDate", endDate);
327 List<String> typeValues = new ArrayList<String>();
328 typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR);
329 typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.TIME_CALENDAR);
330 typeValues.add(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE);
331 root.addIn("leaveBlockType", typeValues);
332
333 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
334 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
335
336 if (c != null) {
337 leaveBlocks.addAll(c);
338 }
339 return leaveBlocks;
340
341 }
342
343 public void deleteLeaveBlock(String leaveBlockId) {
344 Criteria crit = new Criteria();
345 crit.addEqualTo("lmLeaveBlockId",leaveBlockId);
346 this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveBlock.class, crit));
347 }
348
349 public void deleteLeaveBlocksForDocumentId(String documentId){
350 Criteria crit = new Criteria();
351 crit.addEqualTo("documentId",documentId);
352 this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(LeaveBlock.class, crit));
353
354 }
355
356
357 @Override
358 public List<LeaveBlock> getAccrualGeneratedLeaveBlocks(String principalId, Date beginDate, Date endDate) {
359 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
360 Criteria root = new Criteria();
361
362 root.addEqualTo("principalId", principalId);
363 root.addGreaterOrEqualThan("leaveDate", beginDate);
364 root.addLessOrEqualThan("leaveDate", endDate);
365 root.addEqualTo("accrualGenerated", "Y");
366
367 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
368 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
369 if (c != null) {
370 leaveBlocks.addAll(c);
371 }
372 return leaveBlocks;
373 }
374
375 @Override
376 public List<LeaveBlock> getSSTOLeaveBlocks(String principalId, String sstoId, Date accruledDate) {
377 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
378 Criteria root = new Criteria();
379
380 root.addEqualTo("principalId", principalId);
381 root.addEqualTo("leaveDate", accruledDate);
382 root.addEqualTo("scheduleTimeOffId", sstoId);
383
384 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
385 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
386 if (c != null) {
387 leaveBlocks.addAll(c);
388 }
389 return leaveBlocks;
390 }
391
392 @Override
393 public List<LeaveBlock> getABELeaveBlocksSinceTime(String principalId, Timestamp lastRanTime) {
394 List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
395 Criteria root = new Criteria();
396
397 root.addEqualTo("principalId", principalId);
398 root.addGreaterThan("timestamp", lastRanTime);
399 Query query = QueryFactory.newQuery(LeaveBlock.class, root);
400 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
401 List<LeaveBlock> tempList = new ArrayList<LeaveBlock>();
402 if (c != null) {
403 tempList.addAll(c);
404 }
405 for(LeaveBlock lb : tempList) {
406 if(lb != null && StringUtils.isNotEmpty(lb.getEarnCode())) {
407 EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(lb.getEarnCode(), lb.getLeaveDate());
408 if(ec != null && ec.getEligibleForAccrual().equals("N")) {
409 leaveBlocks.add(lb);
410 }
411 }
412 }
413 return leaveBlocks;
414 }
415
416 }