View Javadoc

1   /**
2    * Copyright 2004-2012 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, Date asOfDate) {
42  		return tkRoleDao.findRoles(null, asOfDate, null, null, department, null);
43  	}
44  
45  	@Override
46  	public List<TkRole> getDepartmentRoles(String department, String roleName, Date asOfDate) {
47  		return tkRoleDao.findRoles(null, asOfDate, roleName, null, department, null);
48  	}
49  	
50  	@Override
51  	public List<TkRole> getDepartmentInactiveRoles(String department, String roleName, Date asOfDate) {
52  		return tkRoleDao.findInActiveRoles(null, asOfDate, roleName, null, department, null);
53  	}
54  
55  	@Override
56  	public List<TkRole> getWorkAreaRoles(Long workArea, Date asOfDate) {
57  		return tkRoleDao.findRoles(null, asOfDate, null, workArea, null, null);
58  	}
59  
60  	@Override
61  	public List<TkRole> getWorkAreaRoles(Long workArea, String roleName, Date asOfDate) {
62  		return tkRoleDao.findRoles(null, asOfDate, roleName, workArea, null, null);
63  	}
64  	
65  	@Override
66  	public List<TkRole> getInActiveWorkAreaRoles(Long workArea, Date asOfDate) {
67  		return tkRoleDao.findInActiveRoles(null, asOfDate, null, workArea, null, null);
68  	}
69  
70  	@Override
71  	public List<TkRole> getInActiveWorkAreaRoles(Long workArea, String roleName, Date asOfDate) {
72  		return tkRoleDao.findInActiveRoles(null, asOfDate, roleName, workArea, null, null);
73  	}
74  
75  	public void setTkRoleDao(TkRoleDao tkRoleDao) {
76  		this.tkRoleDao = tkRoleDao;
77  	}
78  
79  	@Override
80  	public void saveOrUpdate(List<TkRole> roles) {
81  		this.tkRoleDao.saveOrUpdateRoles(roles);
82  	}
83  
84  	@Override
85  	public void saveOrUpdate(TkRole role) {
86  		this.tkRoleDao.saveOrUpdateRole(role);
87  	}
88  
89  	/**
90  	 * Returns all active roles for the given principal as of the indi
91  	 */
92  	public List<TkRole> getRoles(String principalId, Date asOfDate) {
93  		return tkRoleDao.findRoles(principalId, asOfDate, null, null, null, null);
94  	}
95  
96  	/**
97  	 * Returns all active roles for the given principal as of the indi
98  	 */
99  	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 }