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.roles.dao; 017 018 import java.sql.Date; 019 import java.util.ArrayList; 020 import java.util.Collection; 021 import java.util.List; 022 023 import com.google.common.collect.ImmutableList; 024 import org.apache.commons.collections.CollectionUtils; 025 import org.apache.commons.lang.StringUtils; 026 import org.apache.ojb.broker.query.Criteria; 027 import org.apache.ojb.broker.query.Query; 028 import org.apache.ojb.broker.query.QueryFactory; 029 import org.apache.ojb.broker.query.ReportQueryByCriteria; 030 import org.kuali.hr.core.util.OjbSubQueryUtil; 031 import org.kuali.hr.job.Job; 032 import org.kuali.hr.time.roles.TkRole; 033 import org.kuali.hr.time.service.base.TkServiceLocator; 034 import org.kuali.hr.time.util.TKUtils; 035 import org.kuali.hr.time.util.TkConstants; 036 import org.kuali.hr.time.workarea.WorkArea; 037 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 038 import org.kuali.rice.krad.service.KRADServiceLocator; 039 040 public class TkRoleDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TkRoleDao { 041 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>() 042 .add("roleName") 043 .add("positionNumber") 044 .add("principalId") 045 .add("workArea") 046 .add("department") 047 .add("chart") 048 .build(); 049 050 051 public List<TkRole> findAllRoles(String principalId, Date asOfDate) { 052 return findRoles(principalId, asOfDate, null, null, null, null); 053 } 054 055 @SuppressWarnings("unchecked") 056 @Override 057 public List<TkRole> findPositionRoles(String positionNumber, Date asOfDate, String roleName, Long workArea, String department, String chart) { 058 List<TkRole> roles = new ArrayList<TkRole>(); 059 060 Criteria root = new Criteria(); 061 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false)); 062 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false)); 063 064 // Optional ROOT criteria added : 065 if (workArea != null) 066 root.addEqualTo("workArea", workArea); 067 if (StringUtils.isNotEmpty(department)) 068 root.addEqualTo("department", department); 069 if (chart != null) 070 root.addEqualTo("chart", chart); 071 if (roleName != null) 072 root.addEqualTo("roleName", roleName); 073 if (positionNumber != null) 074 root.addEqualTo("positionNumber", positionNumber); 075 076 // Filter for ACTIVE = 'Y' 077 Criteria activeFilter = new Criteria(); 078 activeFilter.addEqualTo("active", true); 079 root.addAndCriteria(activeFilter); 080 081 Query query = QueryFactory.newQuery(TkRole.class, root); 082 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 083 084 if (c != null) { 085 roles.addAll(c); 086 } 087 088 return roles; 089 } 090 091 @SuppressWarnings("unchecked") 092 @Override 093 public List<TkRole> findRoles(String principalId, Date asOfDate, String roleName, Long workArea, String department, String chart) { 094 List<TkRole> roles = new ArrayList<TkRole>(); 095 096 Criteria root = new Criteria(); 097 098 if (StringUtils.isNotEmpty(principalId)) { 099 root.addEqualTo("principalId", principalId); 100 } 101 102 if (asOfDate != null) { 103 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false)); 104 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false)); 105 } 106 107 if (StringUtils.isNotEmpty(roleName)) { 108 root.addEqualTo("roleName", roleName); 109 } 110 111 if (workArea != null) { 112 root.addEqualTo("workArea", workArea); 113 } 114 if (StringUtils.isNotEmpty(department)) { 115 Criteria departmentCriteria = new Criteria(); 116 departmentCriteria.addEqualTo("department", department); 117 Collection<WorkArea> collectionWorkAreas = TkServiceLocator.getWorkAreaService().getWorkAreas(department, asOfDate); 118 if (CollectionUtils.isNotEmpty(collectionWorkAreas)) { 119 List<Long> longWorkAreas = new ArrayList<Long>(); 120 for(WorkArea cwa : collectionWorkAreas){ 121 longWorkAreas.add(cwa.getWorkArea()); 122 } 123 Criteria workAreaCriteria = new Criteria(); 124 workAreaCriteria.addIn("workArea", longWorkAreas); 125 departmentCriteria.addOrCriteria(workAreaCriteria); 126 } 127 root.addAndCriteria(departmentCriteria); 128 } 129 130 if (StringUtils.isNotEmpty(chart)) { 131 root.addEqualTo("chart", chart); 132 } 133 134 root.addEqualTo("active", true); 135 136 Query query = QueryFactory.newQuery(TkRole.class, root); 137 // limit the number of the resultset 138 // TODO: hard coding the limits? probably not the most user friendly of ways to do this 139 query.setStartAtIndex(0); 140 query.setEndAtIndex(299); 141 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 142 143 if (c != null) { 144 roles.addAll(c); 145 } 146 147 if (StringUtils.isNotBlank(principalId)) { 148 //Fetch all the jobs and grab any position roles for this persons jobs 149 List<Job> lstActiveJobs = TkServiceLocator.getJobService().getJobs(principalId, asOfDate); 150 for (Job job : lstActiveJobs) { 151 if (job.getPositionNumber() != null) { 152 List<TkRole> lstRoles = findPositionRoles(job.getPositionNumber(), 153 asOfDate, roleName, workArea, department, chart); 154 roles.addAll(lstRoles); 155 } 156 } 157 } else if (workArea != null) { 158 List<TkRole> lstPosRoles = getPositionRolesForWorkArea(workArea, asOfDate); 159 for (TkRole tkRole : lstPosRoles) { 160 if (!roles.contains(tkRole)) { 161 roles.add(tkRole); 162 } 163 } 164 } 165 return roles; 166 } 167 168 @SuppressWarnings("unchecked") 169 @Override 170 public List<TkRole> findInActiveRoles(String principalId, Date asOfDate, String roleName, Long workArea, String department, String chart) { 171 List<TkRole> roles = new ArrayList<TkRole>(); 172 173 Criteria root = new Criteria(); 174 175 if (StringUtils.isNotEmpty(principalId)) { 176 root.addEqualTo("principalId", principalId); 177 } 178 179 if (asOfDate != null) { 180 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false)); 181 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false)); 182 } 183 184 185 if (StringUtils.isNotEmpty(roleName)) { 186 root.addEqualTo("roleName", roleName); 187 } 188 189 if (workArea != null) { 190 root.addEqualTo("workArea", workArea); 191 } 192 193 if (StringUtils.isNotEmpty(department)) { 194 root.addEqualTo("department", department); 195 } 196 197 if (StringUtils.isNotEmpty(chart)) { 198 root.addEqualTo("chart", chart); 199 } 200 201 root.addEqualTo("active", false); 202 203 Query query = QueryFactory.newQuery(TkRole.class, root); 204 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 205 206 if (c != null) { 207 roles.addAll(c); 208 } 209 210 return roles; 211 } 212 213 @Override 214 public void saveOrUpdateRole(TkRole role) { 215 KRADServiceLocator.getBusinessObjectService().save(role); 216 } 217 218 @Override 219 public void saveOrUpdateRoles(List<TkRole> roles) { 220 if (roles != null) { 221 for (TkRole role : roles) { 222 saveOrUpdateRole(role); 223 } 224 } 225 } 226 227 @Override 228 public TkRole getRole(String tkRoleId) { 229 Criteria currentRecordCriteria = new Criteria(); 230 currentRecordCriteria.addEqualTo("hrRolesId", tkRoleId); 231 232 return (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria)); 233 } 234 235 @Override 236 public TkRole getRolesByPosition(String positionNumber) { 237 Criteria currentRecordCriteria = new Criteria(); 238 currentRecordCriteria.addEqualTo("positionNumber", positionNumber); 239 240 ImmutableList<String> fields = new ImmutableList.Builder<String>() 241 .add("positionNumber") 242 .build(); 243 currentRecordCriteria.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, TKUtils.getCurrentDate(), EQUAL_TO_FIELDS, false)); 244 245 246 // Filter for ACTIVE = 'Y' 247 Criteria activeFilter = new Criteria(); 248 activeFilter.addEqualTo("active", true); 249 currentRecordCriteria.addAndCriteria(activeFilter); 250 251 252 TkRole tkRole = (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria)); 253 return tkRole; 254 } 255 256 @Override 257 public TkRole getInactiveRolesByPosition(String positionNumber) { 258 Criteria currentRecordCriteria = new Criteria(); 259 currentRecordCriteria.addEqualTo("positionNumber", positionNumber); 260 261 ImmutableList<String> fields = new ImmutableList.Builder<String>() 262 .add("positionNumber") 263 .build(); 264 currentRecordCriteria.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, TKUtils.getCurrentDate(), EQUAL_TO_FIELDS, false)); 265 266 // Filter for ACTIVE = 'N' 267 Criteria activeFilter = new Criteria(); 268 activeFilter.addEqualTo("active", false); 269 currentRecordCriteria.addAndCriteria(activeFilter); 270 271 272 TkRole tkRole = (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria)); 273 return tkRole; 274 } 275 276 277 @Override 278 public List<TkRole> getPositionRolesForWorkArea(Long workArea, Date asOfDate) { 279 List<TkRole> roles = new ArrayList<TkRole>(); 280 281 Criteria root = new Criteria(); 282 283 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false)); 284 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false)); 285 286 // Optional ROOT criteria added : 287 if (workArea != null) 288 root.addEqualTo("workArea", workArea); 289 root.addEqualTo("roleName", TkConstants.ROLE_TK_APPROVER); 290 291 // Filter for ACTIVE = 'Y' 292 Criteria activeFilter = new Criteria(); 293 activeFilter.addEqualTo("active", true); 294 root.addAndCriteria(activeFilter); 295 296 Query query = QueryFactory.newQuery(TkRole.class, root); 297 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 298 299 if (c != null) { 300 roles.addAll(c); 301 } 302 303 return roles; 304 } 305 }