View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.roles.service;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.hr.job.Job;
21  import org.kuali.hr.time.assignment.Assignment;
22  import org.kuali.hr.time.roles.TkRole;
23  import org.kuali.hr.time.roles.dao.TkRoleDao;
24  import org.kuali.hr.time.service.base.TkServiceLocator;
25  import org.kuali.hr.time.util.TKUser;
26  import org.kuali.hr.time.util.TkConstants;
27  
28  import java.sql.Date;
29  import java.util.ArrayList;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Set;
33  
34  public class TkRoleServiceImpl implements TkRoleService {
35  
36      private static final Logger LOG = Logger.getLogger(TkRoleServiceImpl.class);
37  
38  	private TkRoleDao tkRoleDao;
39  
40  	@Override
41  	public List<TkRole> getDepartmentRoles(String department) {
42  		List<TkRole> departmentRoles = new ArrayList<TkRole>();
43  		departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_TK_DEPT_ADMIN, null, department, null));
44  		departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_TK_DEPT_VO, null, department, null));
45  		departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_LV_DEPT_ADMIN, null, department, null));
46  		departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_LV_DEPT_VO, null, department, null));
47  		return departmentRoles;
48  	}
49  
50  	@Override
51  	public List<TkRole> getDepartmentRoles(String department, String roleName, Date asOfDate) {
52  		return tkRoleDao.findRoles(null, asOfDate, roleName, null, department, null);
53  	}
54  	
55  	@Override
56  	public List<TkRole> getDepartmentInactiveRoles(String department) {
57  		List<TkRole> departmentRoles = new ArrayList<TkRole>();
58  		departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_TK_DEPT_ADMIN, null, department, null));
59  		departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_TK_DEPT_VO, null, department, null));
60  		departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_LV_DEPT_ADMIN, null, department, null));
61  		departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_LV_DEPT_VO, null, department, null));
62  		return departmentRoles;
63  	}
64  	
65  	@Override
66  	public List<TkRole> getDepartmentInactiveRoles(String department, String roleName, Date asOfDate) {
67  		return tkRoleDao.findInActiveRoles(null, asOfDate, roleName, null, department, null);
68  	}
69  
70  	@Override
71  	public List<TkRole> getWorkAreaRoles(Long workArea) {
72  		return tkRoleDao.findRoles(null, null, null, workArea, null, null);
73  	}
74  
75  	@Override
76  	public List<TkRole> getWorkAreaRoles(Long workArea, String roleName, Date asOfDate) {
77  		return tkRoleDao.findRoles(null, asOfDate, roleName, workArea, null, null);
78  	}
79  	
80  	@Override
81  	public List<TkRole> getInActiveWorkAreaRoles(Long workArea) {
82  		return tkRoleDao.findInActiveRoles(null, null, null, workArea, null, null);
83  	}
84  
85  	@Override
86  	public List<TkRole> getInActiveWorkAreaRoles(Long workArea, String roleName, Date asOfDate) {
87  		return tkRoleDao.findInActiveRoles(null, asOfDate, roleName, workArea, null, null);
88  	}
89  
90  	public void setTkRoleDao(TkRoleDao tkRoleDao) {
91  		this.tkRoleDao = tkRoleDao;
92  	}
93  
94  	@Override
95  	public void saveOrUpdate(List<TkRole> roles) {
96  		this.tkRoleDao.saveOrUpdateRoles(roles);
97  	}
98  
99  	@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 }