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.time.principal.dao; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 import java.util.Collections; 021 import java.util.Date; 022 import java.util.HashSet; 023 import java.util.Iterator; 024 import java.util.List; 025 import java.util.Set; 026 027 import com.google.common.collect.ImmutableList; 028 import org.apache.commons.collections.CollectionUtils; 029 import org.apache.commons.lang.StringUtils; 030 import org.apache.ojb.broker.query.Criteria; 031 import org.apache.ojb.broker.query.Query; 032 import org.apache.ojb.broker.query.QueryFactory; 033 import org.apache.ojb.broker.query.ReportQueryByCriteria; 034 import org.kuali.hr.core.util.OjbSubQueryUtil; 035 import org.kuali.hr.time.principal.PrincipalHRAttributes; 036 import org.kuali.hr.time.util.TKUtils; 037 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 038 039 public class PrincipalHRAttributesDaoImpl extends PlatformAwareDaoBaseOjb implements PrincipalHRAttributesDao { 040 041 @Override 042 public PrincipalHRAttributes getPrincipalCalendar(String principalId, 043 java.util.Date asOfDate) { 044 PrincipalHRAttributes pc = null; 045 046 Criteria root = new Criteria(); 047 048 ImmutableList<String> fields = new ImmutableList.Builder<String>() 049 .add("principalId") 050 .build(); 051 052 root.addEqualTo("principalId", principalId); 053 java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime()); 054 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false)); 055 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false)); 056 057 Criteria activeFilter = new Criteria(); // Inner Join For Activity 058 activeFilter.addEqualTo("active", true); 059 root.addAndCriteria(activeFilter); 060 061 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 062 Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query); 063 064 if (obj != null) { 065 pc = (PrincipalHRAttributes) obj; 066 } 067 068 return pc; 069 } 070 071 @Override 072 public void saveOrUpdate(PrincipalHRAttributes principalCalendar) { 073 this.getPersistenceBrokerTemplate().store(principalCalendar); 074 075 } 076 077 @Override 078 public void saveOrUpdate(List<PrincipalHRAttributes> lstPrincipalCalendar) { 079 if(lstPrincipalCalendar != null){ 080 for(PrincipalHRAttributes principalCal : lstPrincipalCalendar){ 081 this.getPersistenceBrokerTemplate().store(principalCal); 082 } 083 } 084 085 } 086 087 @SuppressWarnings({"rawtypes", "unchecked"}) 088 public List<PrincipalHRAttributes> getActiveEmployeesForPayCalendar(String payCalendarName, java.util.Date asOfDate) { 089 List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>(); 090 Criteria root = new Criteria(); 091 092 root.addEqualTo("payCalendar", payCalendarName); 093 ImmutableList<String> fields = new ImmutableList.Builder<String>() 094 .add("payCalendar") 095 .add("principalId") 096 .build(); 097 098 Criteria activeFilter = new Criteria(); 099 activeFilter.addEqualTo("active", true); 100 root.addAndCriteria(activeFilter); 101 java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime()); 102 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false)); 103 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false)); 104 105 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 106 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 107 108 if (c != null) { 109 principalHRAttributes.addAll(c); 110 } 111 112 return principalHRAttributes; 113 } 114 115 @SuppressWarnings({"rawtypes", "unchecked"}) 116 public List<PrincipalHRAttributes> getActiveEmployeesForLeaveCalendar(String leaveCalendarName, Date asOfDate) { 117 List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>(); 118 Criteria root = new Criteria(); 119 120 121 root.addEqualTo("leaveCalendar", leaveCalendarName); 122 123 ImmutableList<String> fields = new ImmutableList.Builder<String>() 124 .add("leaveCalendar") 125 .add("principalId") 126 .build(); 127 java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime()); 128 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false)); 129 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false)); 130 131 Criteria activeFilter = new Criteria(); 132 activeFilter.addEqualTo("active", true); 133 root.addAndCriteria(activeFilter); 134 135 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 136 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 137 138 if (c != null) { 139 principalHRAttributes.addAll(c); 140 } 141 142 return principalHRAttributes; 143 } 144 145 public List<String> getActiveEmployeesIdForLeaveCalendarAndIdList(String leaveCalendarName, List<String> pidList, Date asOfDate) { 146 List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>(); 147 Criteria root = new Criteria(); 148 149 root.addEqualTo("leaveCalendar", leaveCalendarName); 150 root.addIn("principalId", pidList); 151 152 ImmutableList<String> fields = new ImmutableList.Builder<String>() 153 .add("leaveCalendar") 154 .add("principalId") 155 .build(); 156 java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime()); 157 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class,effDate, fields, false)); 158 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false)); 159 160 Criteria activeFilter = new Criteria(); 161 activeFilter.addEqualTo("active", true); 162 root.addAndCriteria(activeFilter); 163 164 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 165 Collection c = getPersistenceBrokerTemplate().getCollectionByQuery(query); 166 if (c != null) { 167 principalHRAttributes.addAll(c); 168 } 169 Set<String> pids = new HashSet<String>(); 170 for(PrincipalHRAttributes phra : principalHRAttributes) { 171 if(phra != null) { 172 pids.add(phra.getPrincipalId()); 173 } 174 } 175 List<String> ids = new ArrayList<String>(); 176 ids.addAll(pids); 177 178 return ids; 179 } 180 181 public List<String> getActiveEmployeesIdForTimeCalendarAndIdList(String timeCalendarName, List<String> pidList, Date asOfDate) { 182 List<PrincipalHRAttributes> principalHRAttributes = new ArrayList<PrincipalHRAttributes>(); 183 Criteria root = new Criteria(); 184 185 root.addEqualTo("payCalendar", timeCalendarName); 186 root.addIn("principalId", pidList); 187 188 ImmutableList<String> fields = new ImmutableList.Builder<String>() 189 .add("payCalendar") 190 .add("principalId") 191 .build(); 192 java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime()); 193 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false)); 194 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false)); 195 196 Criteria activeFilter = new Criteria(); 197 activeFilter.addEqualTo("active", true); 198 root.addAndCriteria(activeFilter); 199 200 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 201 Collection c = getPersistenceBrokerTemplate().getCollectionByQuery(query); 202 if (c != null) { 203 principalHRAttributes.addAll(c); 204 } 205 Set<String> pids = new HashSet<String>(); 206 for(PrincipalHRAttributes phra : principalHRAttributes) { 207 if(phra != null) { 208 pids.add(phra.getPrincipalId()); 209 } 210 } 211 List<String> ids = new ArrayList<String>(); 212 ids.addAll(pids); 213 214 return ids; 215 } 216 217 // KPME-1250 Kagata 218 @SuppressWarnings({"rawtypes", "unchecked"}) 219 public List<PrincipalHRAttributes> getActiveEmployeesForLeavePlan(String leavePlan, java.util.Date asOfDate) { 220 221 List<PrincipalHRAttributes> principals = new ArrayList<PrincipalHRAttributes>(); 222 Criteria root = new Criteria(); 223 Criteria effdt = new Criteria(); 224 Criteria timestamp = new Criteria(); 225 226 ImmutableList<String> fields = new ImmutableList.Builder<String>() 227 .add("leavePlan") 228 .add("principalId") 229 .build(); 230 java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime()); 231 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PrincipalHRAttributes.class, effDate, fields, false)); 232 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false)); 233 234 root.addEqualTo("leavePlan", leavePlan); 235 root.addEqualTo("active", true); 236 237 Criteria activeFilter = new Criteria(); // Inner Join For Activity 238 activeFilter.addEqualTo("active", true); 239 root.addAndCriteria(activeFilter); 240 241 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 242 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 243 244 if (c != null) { 245 principals.addAll(c); 246 } 247 248 return principals; 249 } 250 251 @Override 252 public List<String> getUniqueLeavePayGroupsForPrincipalIds(List<String> principalIds) { 253 if (CollectionUtils.isEmpty(principalIds)) { 254 return Collections.emptyList(); 255 } 256 List<String> leaveCalendars = new ArrayList<String>(); 257 Criteria crit = new Criteria(); 258 crit.addEqualTo("active", true); 259 crit.addIn("principalId", principalIds); 260 ReportQueryByCriteria q = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit, true); 261 q.setDistinct(true); 262 q.setAttributes(new String[] {"leaveCalendar"}); 263 Iterator iter = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q); 264 while (iter.hasNext()) { 265 Object[] values = (Object[]) iter.next(); 266 String leaveCalendar = (String)values[0]; 267 if (StringUtils.isNotBlank(leaveCalendar)) { 268 leaveCalendars.add(leaveCalendar); 269 } 270 } 271 return leaveCalendars; 272 } 273 274 @SuppressWarnings("rawtypes") 275 @Override 276 public List<String> getUniqueTimePayGroups() { 277 List<String> payCalendars = new ArrayList<String>(); 278 Criteria crit = new Criteria(); 279 crit.addEqualTo("active", true); 280 ReportQueryByCriteria q = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit, true); 281 q.setDistinct(true); 282 q.setAttributes(new String[] {"pay_calendar"}); 283 Iterator iter = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q); 284 while (iter.hasNext()) { 285 Object[] values = (Object[]) iter.next(); 286 String leaveCalendar = (String)values[0]; 287 if (StringUtils.isNotBlank(leaveCalendar)) { 288 payCalendars.add(leaveCalendar); 289 } 290 } 291 return payCalendars; 292 } 293 294 // @Override 295 // public PrincipalHRAttributes getPrincipalHRAttributes(String principalId) { 296 // Criteria crit = new Criteria(); 297 // crit.addEqualTo("principalId", principalId); 298 // Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit); 299 // return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 300 // } 301 302 @Override 303 public PrincipalHRAttributes getInactivePrincipalHRAttributes(String principalId, java.util.Date asOfDate) { 304 PrincipalHRAttributes pc = null; 305 306 Criteria root = new Criteria(); 307 Criteria effdt = new Criteria(); 308 Criteria timestamp = new Criteria(); 309 310 effdt.addGreaterOrEqualThan("effectiveDate", asOfDate); 311 ImmutableList<String> fields = new ImmutableList.Builder<String>() 312 .add("leavePlan") 313 .add("principalId") 314 .build(); 315 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(PrincipalHRAttributes.class, effdt, fields, false)); 316 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false)); 317 318 root.addEqualTo("principalId", principalId); 319 320 Criteria activeFilter = new Criteria(); // Inner Join For Activity 321 activeFilter.addEqualTo("active", false); 322 root.addAndCriteria(activeFilter); 323 324 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 325 Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query); 326 327 if (obj != null) { 328 pc = (PrincipalHRAttributes) obj; 329 } 330 331 return pc; 332 } 333 334 @Override 335 public PrincipalHRAttributes getPrincipalHRAttributes(String hrPrincipalAttributeId) { 336 Criteria crit = new Criteria(); 337 crit.addEqualTo("hrPrincipalAttributeId", hrPrincipalAttributeId); 338 339 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit); 340 return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 341 } 342 343 @Override 344 public List<PrincipalHRAttributes> getAllActivePrincipalHrAttributesForPrincipalId(String principalId, java.util.Date asOfDate) { 345 346 List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>(); 347 Criteria root = new Criteria(); 348 root.addEqualTo("principalId", principalId); 349 root.addLessOrEqualThan("effectiveDate", asOfDate); 350 root.addEqualTo("active", true); 351 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 352 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 353 if (c != null) { 354 phaList.addAll(c); 355 } 356 return phaList; 357 } 358 359 @Override 360 public List<PrincipalHRAttributes> getAllInActivePrincipalHrAttributesForPrincipalId(String principalId, java.util.Date asOfDate) { 361 List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>(); 362 Criteria root = new Criteria(); 363 root.addEqualTo("principalId", principalId); 364 root.addLessOrEqualThan("effectiveDate", asOfDate); 365 root.addEqualTo("active", false); 366 367 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 368 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 369 if (c != null) { 370 phaList.addAll(c); 371 } 372 return phaList; 373 } 374 @Override 375 public PrincipalHRAttributes getMaxTimeStampPrincipalHRAttributes(String principalId) { 376 Criteria root = new Criteria(); 377 Criteria crit = new Criteria(); 378 379 crit.addEqualTo("principalId", principalId); 380 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit); 381 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 382 383 root.addEqualTo("principalId", principalId); 384 root.addEqualTo("timestamp", timestampSubQuery); 385 386 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 387 return (PrincipalHRAttributes) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 388 } 389 390 @Override 391 public List<PrincipalHRAttributes> getActivePrincipalHrAttributesForRange(String principalId, java.util.Date startDate, java.util.Date endDate) { 392 List<PrincipalHRAttributes> activeList = new ArrayList<PrincipalHRAttributes>(); 393 Criteria root = new Criteria(); 394 root.addEqualTo("principalId", principalId); 395 root.addGreaterOrEqualThan("effectiveDate", startDate); 396 root.addLessOrEqualThan("effectiveDate", endDate); 397 root.addEqualTo("active", true); 398 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 399 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 400 if (c != null) { 401 activeList.addAll(c); 402 } 403 List<PrincipalHRAttributes> aList = new ArrayList<PrincipalHRAttributes>(); 404 aList.addAll(activeList); 405 for(PrincipalHRAttributes aPha : aList) { 406 List<PrincipalHRAttributes> inactivePhas = this.getInactivePrincipalHRAttributesForRange(principalId, aPha.getEffectiveDate(), endDate); 407 if(CollectionUtils.isNotEmpty(inactivePhas)) { 408 for(PrincipalHRAttributes inactive : inactivePhas) { 409 if(inactive.getTimestamp().after(aPha.getTimestamp())) { 410 activeList.remove(aPha); 411 } 412 } 413 } 414 } 415 416 return activeList; 417 } 418 419 @Override 420 public List<PrincipalHRAttributes> getInactivePrincipalHRAttributesForRange(String principalId, java.util.Date startDate, java.util.Date endDate) { 421 List<PrincipalHRAttributes> inactiveList = new ArrayList<PrincipalHRAttributes>(); 422 Criteria root = new Criteria(); 423 root.addEqualTo("principalId", principalId); 424 root.addGreaterOrEqualThan("effectiveDate", startDate); 425 root.addLessOrEqualThan("effectiveDate", endDate); 426 root.addEqualTo("active", false); 427 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 428 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 429 if (c != null) { 430 inactiveList.addAll(c); 431 } 432 return inactiveList; 433 } 434 435 @Override 436 @SuppressWarnings("unchecked") 437 public List<PrincipalHRAttributes> getPrincipalHrAtributes(String principalId, String leavePlan, java.sql.Date fromEffdt, java.sql.Date toEffdt, String active, String showHistory) { 438 List<PrincipalHRAttributes> results = new ArrayList<PrincipalHRAttributes>(); 439 440 Criteria root = new Criteria(); 441 442 if (StringUtils.isNotBlank(principalId)) { 443 root.addLike("principalId", principalId); 444 } 445 446 if (StringUtils.isNotBlank(leavePlan)) { 447 root.addLike("leavePlan", leavePlan); 448 } 449 450 Criteria effectiveDateFilter = new Criteria(); 451 if (fromEffdt != null) { 452 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt); 453 } 454 if (toEffdt != null) { 455 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt); 456 } 457 if (fromEffdt == null && toEffdt == null) { 458 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate()); 459 } 460 root.addAndCriteria(effectiveDateFilter); 461 462 if (StringUtils.isNotBlank(active)) { 463 Criteria activeFilter = new Criteria(); 464 if (StringUtils.equals(active, "Y")) { 465 activeFilter.addEqualTo("active", true); 466 } else if (StringUtils.equals(active, "N")) { 467 activeFilter.addEqualTo("active", false); 468 } 469 root.addAndCriteria(activeFilter); 470 } 471 472 if (StringUtils.equals(showHistory, "N")) { 473 ImmutableList<String> fields = new ImmutableList.Builder<String>() 474 .add("principalId") 475 .add("leavePlan") 476 .build(); 477 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(PrincipalHRAttributes.class, effectiveDateFilter, fields, false)); 478 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PrincipalHRAttributes.class, fields, false)); 479 } 480 481 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 482 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query)); 483 484 return results; 485 } 486 487 }