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.service; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.apache.log4j.Logger; 020 import org.kuali.hr.job.Job; 021 import org.kuali.hr.time.assignment.Assignment; 022 import org.kuali.hr.time.roles.TkRole; 023 import org.kuali.hr.time.roles.dao.TkRoleDao; 024 import org.kuali.hr.time.service.base.TkServiceLocator; 025 import org.kuali.hr.time.util.TKUser; 026 import org.kuali.hr.time.util.TkConstants; 027 028 import java.sql.Date; 029 import java.util.ArrayList; 030 import java.util.HashSet; 031 import java.util.List; 032 import java.util.Set; 033 034 public class TkRoleServiceImpl implements TkRoleService { 035 036 private static final Logger LOG = Logger.getLogger(TkRoleServiceImpl.class); 037 038 private TkRoleDao tkRoleDao; 039 040 @Override 041 public List<TkRole> getDepartmentRoles(String department) { 042 List<TkRole> departmentRoles = new ArrayList<TkRole>(); 043 departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_TK_DEPT_ADMIN, null, department, null)); 044 departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_TK_DEPT_VO, null, department, null)); 045 departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_LV_DEPT_ADMIN, null, department, null)); 046 departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_LV_DEPT_VO, null, department, null)); 047 return departmentRoles; 048 } 049 050 @Override 051 public List<TkRole> getDepartmentRoles(String department, String roleName, Date asOfDate) { 052 return tkRoleDao.findRoles(null, asOfDate, roleName, null, department, null); 053 } 054 055 @Override 056 public List<TkRole> getDepartmentInactiveRoles(String department) { 057 List<TkRole> departmentRoles = new ArrayList<TkRole>(); 058 departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_TK_DEPT_ADMIN, null, department, null)); 059 departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_TK_DEPT_VO, null, department, null)); 060 departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_LV_DEPT_ADMIN, null, department, null)); 061 departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_LV_DEPT_VO, null, department, null)); 062 return departmentRoles; 063 } 064 065 @Override 066 public List<TkRole> getDepartmentInactiveRoles(String department, String roleName, Date asOfDate) { 067 return tkRoleDao.findInActiveRoles(null, asOfDate, roleName, null, department, null); 068 } 069 070 @Override 071 public List<TkRole> getWorkAreaRoles(Long workArea) { 072 return tkRoleDao.findRoles(null, null, null, workArea, null, null); 073 } 074 075 @Override 076 public List<TkRole> getWorkAreaRoles(Long workArea, String roleName, Date asOfDate) { 077 return tkRoleDao.findRoles(null, asOfDate, roleName, workArea, null, null); 078 } 079 080 @Override 081 public List<TkRole> getInActiveWorkAreaRoles(Long workArea) { 082 return tkRoleDao.findInActiveRoles(null, null, null, workArea, null, null); 083 } 084 085 @Override 086 public List<TkRole> getInActiveWorkAreaRoles(Long workArea, String roleName, Date asOfDate) { 087 return tkRoleDao.findInActiveRoles(null, asOfDate, roleName, workArea, null, null); 088 } 089 090 public void setTkRoleDao(TkRoleDao tkRoleDao) { 091 this.tkRoleDao = tkRoleDao; 092 } 093 094 @Override 095 public void saveOrUpdate(List<TkRole> roles) { 096 this.tkRoleDao.saveOrUpdateRoles(roles); 097 } 098 099 @Override 100 public void saveOrUpdate(TkRole role) { 101 this.tkRoleDao.saveOrUpdateRole(role); 102 } 103 104 /** 105 * Returns all active roles for the given principal as of the indi 106 */ 107 public List<TkRole> getRoles(String principalId, Date asOfDate) { 108 return tkRoleDao.findRoles(principalId, asOfDate, null, null, null, null); 109 } 110 111 /** 112 * Returns all active roles for the given principal as of the indi 113 */ 114 public List<TkRole> getInactiveRoles(String principalId, Date asOfDate) { 115 return tkRoleDao.findInActiveRoles(principalId, asOfDate, null, null, null, null); 116 } 117 118 /** 119 * Return a List of TkRoles that match the principal ID and roleName. 120 * 121 * ex: 122 * 123 * admin,TK_APPROVER will return all TK_APPROVER roles for the user admin. 124 */ 125 public List<TkRole> getRoles(String principalId, String roleName, Date asOfDate) { 126 return this.tkRoleDao.findRoles(principalId, asOfDate, roleName, null, null, null); 127 } 128 129 /** 130 * Return a List of TkRoles that matches criteria. 131 * @param principalId 132 * @param asOfDate 133 * @param roleName 134 * @param workArea 135 * @param department 136 * @return 137 */ 138 @Override 139 public List<TkRole> getRoles(String principalId, Date asOfDate, String roleName, Long workArea, String department) { 140 return this.tkRoleDao.findRoles(principalId, asOfDate, roleName, workArea, department, null); 141 } 142 143 //TODO: this won't work at all. We can't use TKUser here, as that just grabs stuff from the session 144 // we need a wrapper class for TKUser, though I'm not sure why we can't just return Person, or Entity... 145 public List<TKUser> getEmployeesForWorkArea(Long workArea, Date asOfDate){ 146 List<TKUser> lstEmployees = new ArrayList<TKUser>(); 147 List<Assignment> lstActiveAssignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForWorkArea(workArea, asOfDate); 148 149 for(Assignment assign: lstActiveAssignments){ 150 TKUser tkUser = TKUser.getUser(assign.getPrincipal(), assign.getEffectiveDate()); 151 lstEmployees.add(tkUser); 152 } 153 return lstEmployees; 154 } 155 156 @Override 157 public List<String> getResponsibleParties(Assignment a, String roleName, Date asOfDate) { 158 List<String> users = new ArrayList<String>(); 159 160 List<TkRole> roles = this.getWorkAreaRoles(a.getWorkArea(), roleName, asOfDate); 161 for (TkRole role: roles) { 162 if(StringUtils.isNotBlank(role.getPrincipalId())){ 163 users.add(role.getPrincipalId()); 164 } else if(StringUtils.isNotBlank(role.getPositionNumber())){ 165 List<Job> lstJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(role.getPositionNumber(), asOfDate); 166 for(Job job : lstJobs){ 167 users.add(job.getPrincipalId()); 168 } 169 170 } 171 } 172 173 return users; 174 } 175 176 @Override 177 public Set<Long> getWorkAreasForApprover(String principalId, Date asOfDate) { 178 Set<Long> workAreas = new HashSet<Long>(); 179 180 List<TkRole> roles = this.getRoles(principalId, TkConstants.ROLE_TK_APPROVER, asOfDate); 181 for (TkRole role : roles) { 182 Long wa = role.getWorkArea(); 183 if (wa != null) 184 workAreas.add(wa); 185 else 186 LOG.warn(TkConstants.ROLE_TK_APPROVER + " found without WorkArea number, ignoring roleId: " + role.getHrRolesId()); 187 } 188 189 return workAreas; 190 } 191 192 193 194 @Override 195 public Set<String> getActivePrincipalsForWorkAreas(Set<Long> workAreas, Date asOfDate) { 196 Set<String> principals = new HashSet<String>(); 197 198 for (Long workArea : workAreas) { 199 List<Assignment> assignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForWorkArea(workArea, asOfDate); 200 for (Assignment assignment : assignments) { 201 principals.add(assignment.getPrincipalId()); 202 } 203 } 204 205 return principals; 206 } 207 208 @Override 209 public TkRole getRole(String tkRoleId) { 210 return tkRoleDao.getRole(tkRoleId); 211 } 212 213 @Override 214 public TkRole getRolesByPosition(String positionNumber) { 215 return tkRoleDao.getRolesByPosition(positionNumber); 216 } 217 218 @Override 219 public TkRole getInactiveRolesByPosition(String positionNumber) { 220 return tkRoleDao.getInactiveRolesByPosition(positionNumber); 221 } 222 223 @Override 224 public List<TkRole> getPositionRolesForWorkArea(Long workArea, Date asOfDate) { 225 return tkRoleDao.getPositionRolesForWorkArea(workArea, asOfDate); 226 } 227 }