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.assignment.dao; 017 018 import java.sql.Date; 019 import java.util.ArrayList; 020 import java.util.Collection; 021 import java.util.HashSet; 022 import java.util.List; 023 import java.util.Set; 024 025 import com.google.common.collect.ImmutableList; 026 import org.apache.commons.collections.CollectionUtils; 027 import org.apache.commons.lang.StringUtils; 028 import org.apache.log4j.Logger; 029 import org.apache.ojb.broker.query.Criteria; 030 import org.apache.ojb.broker.query.Query; 031 import org.apache.ojb.broker.query.QueryFactory; 032 import org.apache.ojb.broker.query.ReportQueryByCriteria; 033 import org.kuali.hr.core.util.OjbSubQueryUtil; 034 import org.kuali.hr.time.assignment.Assignment; 035 import org.kuali.hr.time.service.base.TkServiceLocator; 036 import org.kuali.hr.time.util.TKContext; 037 import org.kuali.hr.time.util.TKUtils; 038 import org.kuali.hr.time.workarea.WorkArea; 039 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 040 041 public class AssignmentDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements AssignmentDao { 042 043 private static final Logger LOG = Logger.getLogger(AssignmentDaoSpringOjbImpl.class); 044 private static final ImmutableList<String> ASSIGNMENT_EQUAL_TO_FIELD = new ImmutableList.Builder<String>() 045 .add("jobNumber") 046 .add("workArea") 047 .add("task") 048 .add("principalId") 049 .build(); 050 051 @Override 052 public void saveOrUpdate(Assignment assignment) { 053 this.getPersistenceBrokerTemplate().store(assignment); 054 } 055 056 @Override 057 public void saveOrUpdate(List<Assignment> assignments) { 058 if (assignments != null) { 059 for (Assignment assign : assignments) { 060 this.getPersistenceBrokerTemplate().store(assign); 061 } 062 } 063 } 064 065 @Override 066 public void delete(Assignment assignment) { 067 if (assignment != null) { 068 LOG.debug("Deleting assignment:" + assignment.getTkAssignmentId()); 069 this.getPersistenceBrokerTemplate().delete(assignment); 070 } else { 071 LOG.warn("Attempt to delete null assignment."); 072 } 073 } 074 075 public Assignment getAssignment(String principalId, Long jobNumber, Long workArea, Long task, Date asOfDate) { 076 Criteria root = new Criteria(); 077 078 root.addEqualTo("principalId", principalId); 079 root.addEqualTo("jobNumber", jobNumber); 080 root.addEqualTo("workArea", workArea); 081 root.addEqualTo("task", task); 082 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false)); 083 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false)); 084 //root.addEqualTo("active", true); 085 086 Criteria activeFilter = new Criteria(); // Inner Join For Activity 087 activeFilter.addEqualTo("active", true); 088 root.addAndCriteria(activeFilter); 089 090 Query query = QueryFactory.newQuery(Assignment.class, root); 091 Object o = this.getPersistenceBrokerTemplate().getObjectByQuery(query); 092 093 return (Assignment) o; 094 } 095 096 097 @Override 098 public Assignment getAssignment(Long job, Long workArea, Long task, Date asOfDate) { 099 Criteria root = new Criteria(); 100 101 root.addEqualTo("jobNumber", job); 102 root.addEqualTo("workArea", workArea); 103 root.addEqualTo("task", task); 104 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false)); 105 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false)); 106 root.addEqualTo("principalId", TKContext.getTargetPrincipalId()); 107 //root.addEqualTo("active", true); 108 109 Criteria activeFilter = new Criteria(); // Inner Join For Activity 110 activeFilter.addEqualTo("active", true); 111 root.addAndCriteria(activeFilter); 112 113 Query query = QueryFactory.newQuery(Assignment.class, root); 114 Object o = this.getPersistenceBrokerTemplate().getObjectByQuery(query); 115 116 return (Assignment) o; 117 } 118 119 @SuppressWarnings({"unchecked", "rawtypes"}) 120 @Override 121 public List<Assignment> findAssignments(String principalId, Date asOfDate) { 122 List<Assignment> assignments = new ArrayList<Assignment>(); 123 Criteria root = new Criteria(); 124 125 root.addEqualTo("principalId", principalId); 126 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false)); 127 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false)); 128 //root.addEqualTo("active", true); 129 130 Criteria activeFilter = new Criteria(); // Inner Join For Activity 131 activeFilter.addEqualTo("active", true); 132 root.addAndCriteria(activeFilter); 133 134 Query query = QueryFactory.newQuery(Assignment.class, root); 135 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 136 137 if (c != null) { 138 assignments.addAll(c); 139 } 140 141 return assignments; 142 } 143 144 @SuppressWarnings({"unchecked", "rawtypes"}) 145 @Override 146 public List<Assignment> findAssignmentsWithinPeriod(String principalId, Date startDate, Date endDate) { 147 List<Assignment> assignments = new ArrayList<Assignment>(); 148 Criteria root = new Criteria(); 149 150 root.addGreaterOrEqualThan("effectiveDate", startDate); 151 root.addLessOrEqualThan("effectiveDate", endDate); 152 root.addEqualTo("principalId", principalId); 153 root.addEqualTo("active", true); 154 155 Query query = QueryFactory.newQuery(Assignment.class, root); 156 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 157 158 if (c != null) { 159 assignments.addAll(c); 160 } 161 162 return assignments; 163 } 164 165 @SuppressWarnings({"rawtypes", "unchecked"}) 166 public List<Assignment> getActiveAssignmentsInWorkArea(Long workArea, Date asOfDate) { 167 List<Assignment> assignments = new ArrayList<Assignment>(); 168 Criteria root = new Criteria(); 169 170 root.addEqualTo("workArea", workArea); 171 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, true)); 172 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, true)); 173 root.addEqualTo("active", true); 174 175 Criteria activeFilter = new Criteria(); // Inner Join For Activity 176 activeFilter.addEqualTo("active", true); 177 root.addAndCriteria(activeFilter); 178 179 Query query = QueryFactory.newQuery(Assignment.class, root); 180 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 181 182 if (c != null) { 183 assignments.addAll(c); 184 } 185 186 return assignments; 187 } 188 189 public List<Assignment> getActiveAssignments(Date asOfDate) { 190 List<Assignment> assignments = new ArrayList<Assignment>(); 191 192 Criteria root = new Criteria(); 193 root.addLessOrEqualThan("effectiveDate", asOfDate); 194 195 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, true)); 196 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, true)); 197 198 Criteria activeFilter = new Criteria(); 199 activeFilter.addEqualTo("active", true); 200 root.addAndCriteria(activeFilter); 201 202 Query query = QueryFactory.newQuery(Assignment.class, root); 203 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 204 205 if (c != null) { 206 assignments.addAll(c); 207 } 208 209 return assignments; 210 } 211 212 public Assignment getAssignment(String tkAssignmentId) { 213 Criteria crit = new Criteria(); 214 crit.addEqualTo("tkAssignmentId", tkAssignmentId); 215 Query query = QueryFactory.newQuery(Assignment.class, crit); 216 return (Assignment) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 217 } 218 219 // KPME-1129 Kagata 220 @SuppressWarnings({"rawtypes", "unchecked"}) 221 public List<Assignment> getActiveAssignmentsForJob(String principalId, Long jobNumber, Date asOfDate) { 222 List<Assignment> assignments = new ArrayList<Assignment>(); 223 Criteria root = new Criteria(); 224 225 root.addEqualTo("principalId", principalId); 226 root.addEqualTo("jobNumber", jobNumber); 227 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, asOfDate, ASSIGNMENT_EQUAL_TO_FIELD, false)); 228 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false)); 229 root.addEqualTo("active", true); 230 231 Criteria activeFilter = new Criteria(); // Inner Join For Activity 232 activeFilter.addEqualTo("active", true); 233 root.addAndCriteria(activeFilter); 234 235 Query query = QueryFactory.newQuery(Assignment.class, root); 236 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 237 238 if (c != null) { 239 assignments.addAll(c); 240 } 241 242 return assignments; 243 } 244 245 @Override 246 @SuppressWarnings("unchecked") 247 public List<Assignment> searchAssignments(Date fromEffdt, Date toEffdt, String principalId, String jobNumber, String dept, String workArea, 248 String active, String showHistory) { 249 250 List<Assignment> results = new ArrayList<Assignment>(); 251 252 Criteria root = new Criteria(); 253 254 Criteria effectiveDateFilter = new Criteria(); 255 if (fromEffdt != null) { 256 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt); 257 } 258 if (toEffdt != null) { 259 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt); 260 } 261 if (fromEffdt == null && toEffdt == null) { 262 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate()); 263 } 264 root.addAndCriteria(effectiveDateFilter); 265 266 if (StringUtils.isNotBlank(principalId)) { 267 root.addLike("principalId", principalId); 268 } 269 270 if (StringUtils.isNotBlank(jobNumber)) { 271 root.addLike("jobNumber", jobNumber); 272 } 273 274 if (StringUtils.isNotBlank(dept)) { 275 Criteria workAreaCriteria = new Criteria(); 276 Date asOfDate = toEffdt != null ? toEffdt : TKUtils.getCurrentDate(); 277 Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate); 278 if (CollectionUtils.isNotEmpty(workAreasForDept)) { 279 List<Long> longWorkAreas = new ArrayList<Long>(); 280 for(WorkArea cwa : workAreasForDept){ 281 longWorkAreas.add(cwa.getWorkArea()); 282 } 283 workAreaCriteria.addIn("workArea", longWorkAreas); 284 } 285 root.addAndCriteria(workAreaCriteria); 286 } 287 288 if (StringUtils.isNotBlank(workArea)) { 289 OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea); 290 } 291 292 if (StringUtils.isNotBlank(active)) { 293 Criteria activeFilter = new Criteria(); 294 if (StringUtils.equals(active, "Y")) { 295 activeFilter.addEqualTo("active", true); 296 } else if (StringUtils.equals(active, "N")) { 297 activeFilter.addEqualTo("active", false); 298 } 299 root.addAndCriteria(activeFilter); 300 } 301 302 if (StringUtils.equals(showHistory, "N")) { 303 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Assignment.class, effectiveDateFilter, ASSIGNMENT_EQUAL_TO_FIELD, false)); 304 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false)); 305 } 306 307 Query query = QueryFactory.newQuery(Assignment.class, root); 308 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query)); 309 310 return results; 311 } 312 313 @Override 314 public Assignment getMaxTimestampAssignment(String principalId) { 315 Criteria root = new Criteria(); 316 Criteria crit = new Criteria(); 317 318 crit.addEqualTo("principalId", principalId); 319 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Assignment.class, crit); 320 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 321 322 root.addEqualTo("principalId", principalId); 323 root.addEqualTo("timestamp", timestampSubQuery); 324 325 Query query = QueryFactory.newQuery(Assignment.class, root); 326 return (Assignment) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 327 } 328 329 public List<String> getPrincipalIds(List<String> workAreaList, Date effdt, Date startDate, Date endDate) { 330 List<Assignment> results = this.getAssignments(workAreaList, effdt, startDate, endDate); 331 Set<String> pids = new HashSet<String>(); 332 for(Assignment anAssignment : results) { 333 if(anAssignment != null) { 334 pids.add(anAssignment.getPrincipalId()); 335 } 336 } 337 List<String> ids = new ArrayList<String>(); 338 ids.addAll(pids); 339 return ids; 340 } 341 342 public List<Assignment> getAssignments(List<String> workAreaList, Date effdt, Date startDate, Date endDate) { 343 List<Assignment> results = new ArrayList<Assignment>(); 344 345 Criteria activeRoot = new Criteria(); 346 Criteria inactiveRoot = new Criteria(); 347 348 ReportQueryByCriteria effdtSubQuery = OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false); 349 ReportQueryByCriteria activeEffdtSubQuery = OjbSubQueryUtil.getEffectiveDateSubQuery(Assignment.class, effdt, ASSIGNMENT_EQUAL_TO_FIELD, false); 350 ReportQueryByCriteria timestampSubQuery = OjbSubQueryUtil.getTimestampSubQuery(Assignment.class, ASSIGNMENT_EQUAL_TO_FIELD, false); 351 352 inactiveRoot.addEqualTo("active", "N"); 353 inactiveRoot.addIn("workArea", workAreaList); 354 inactiveRoot.addGreaterOrEqualThan("effectiveDate", startDate); 355 inactiveRoot.addLessOrEqualThan("effectiveDate", endDate); 356 inactiveRoot.addEqualTo("effectiveDate", effdtSubQuery); 357 inactiveRoot.addEqualTo("timestamp", timestampSubQuery); 358 359 activeRoot.addIn("workArea", workAreaList); 360 activeRoot.addEqualTo("active", "Y"); 361 activeRoot.addEqualTo("effectiveDate", activeEffdtSubQuery); 362 activeRoot.addEqualTo("timestamp", timestampSubQuery); 363 activeRoot.addOrCriteria(inactiveRoot); 364 365 Query query = QueryFactory.newQuery(Assignment.class, activeRoot); 366 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 367 if (c != null) { 368 results.addAll(c); 369 } 370 371 return results; 372 } 373 374 }