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     @Override
144     public List<String> getResponsibleParties(Assignment a, String roleName, Date asOfDate) {
145         List<String> users = new ArrayList<String>();
146 
147         List<TkRole> roles = this.getWorkAreaRoles(a.getWorkArea(), roleName, asOfDate);
148         for (TkRole role: roles) {
149         	if(StringUtils.isNotBlank(role.getPrincipalId())){
150         		users.add(role.getPrincipalId());
151         	} else if(StringUtils.isNotBlank(role.getPositionNumber())){
152         		List<Job> lstJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(role.getPositionNumber(), asOfDate);
153         		for(Job job : lstJobs){
154         			users.add(job.getPrincipalId());
155         		}
156         	
157         	}
158         }
159 
160         return users;
161     }
162 
163     @Override
164     public Set<Long> getWorkAreasForApprover(String principalId, Date asOfDate) {
165         Set<Long> workAreas = new HashSet<Long>();
166 
167         List<TkRole> roles = this.getRoles(principalId, TkConstants.ROLE_TK_APPROVER, asOfDate);
168         for (TkRole role : roles) {
169             Long wa = role.getWorkArea();
170             if (wa != null)
171                 workAreas.add(wa);
172             else
173                 LOG.warn(TkConstants.ROLE_TK_APPROVER + " found without WorkArea number, ignoring roleId: " + role.getHrRolesId());
174         }
175 
176         return workAreas;
177     }
178 
179 
180 
181     @Override
182     public Set<String> getActivePrincipalsForWorkAreas(Set<Long> workAreas, Date asOfDate) {
183         Set<String> principals = new HashSet<String>();
184 
185         for (Long workArea : workAreas) {
186             List<Assignment> assignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForWorkArea(workArea, asOfDate);
187             for (Assignment assignment : assignments) {
188                 principals.add(assignment.getPrincipalId());
189             }
190         }
191 
192         return principals;
193     }
194 
195 	@Override
196 	public TkRole getRole(String tkRoleId) {
197 		return tkRoleDao.getRole(tkRoleId);
198 	}
199 	
200 	@Override
201 	public TkRole getRolesByPosition(String positionNumber) {
202 		return tkRoleDao.getRolesByPosition(positionNumber);
203 	}
204 	
205 	@Override
206 	public TkRole getInactiveRolesByPosition(String positionNumber) {
207 		return tkRoleDao.getInactiveRolesByPosition(positionNumber);
208 	}
209 
210 	@Override
211 	public List<TkRole> getPositionRolesForWorkArea(Long workArea, Date asOfDate) {
212 		return tkRoleDao.getPositionRolesForWorkArea(workArea, asOfDate);
213 	}
214 }