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