View Javadoc

1   /**
2    * Copyright 2004-2014 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.kpme.core.department.service;
17  
18  import java.util.*;
19  
20  import org.apache.commons.collections.CollectionUtils;
21  import org.joda.time.LocalDate;
22  import org.kuali.kpme.core.KPMENamespace;
23  import org.kuali.kpme.core.department.Department;
24  import org.kuali.kpme.core.department.dao.DepartmentDao;
25  import org.kuali.kpme.core.permission.KPMEPermissionTemplate;
26  import org.kuali.kpme.core.role.KPMERole;
27  import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
28  import org.kuali.kpme.core.role.department.DepartmentPrincipalRoleMemberBo;
29  import org.kuali.kpme.core.service.HrServiceLocator;
30  import org.kuali.rice.kim.api.KimConstants;
31  import org.kuali.rice.kim.api.role.RoleMember;
32  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33  import org.kuali.rice.kim.impl.role.RoleMemberBo;
34  
35  public class DepartmentServiceImpl implements DepartmentService {
36  
37  	private DepartmentDao departmentDao;
38  	
39  	public DepartmentDao getDepartmentDao() {
40  		return departmentDao;
41  	}
42  	
43  	public void setDepartmentDao(DepartmentDao departmentDao) {
44  		this.departmentDao = departmentDao;
45  	}
46  	
47  	@Override
48  	public Department getDepartment(String hrDeptId) {
49  		Department departmentObj = departmentDao.getDepartment(hrDeptId);
50  		
51  		if (departmentObj != null) {
52  			populateDepartmentRoleMembers(departmentObj, departmentObj.getEffectiveLocalDate());
53  		}
54  		
55  		return departmentObj;
56  	}
57  	
58      @Override
59      public List<Department> getDepartments(String userPrincipalId, String department, String location, String descr, String active, String showHistory, String payrollApproval) {
60      	List<Department> results = new ArrayList<Department>();
61      	
62      	List<Department> departmentObjs = departmentDao.getDepartments(department, location, descr, active, showHistory, payrollApproval);
63          
64      	for (Department departmentObj : departmentObjs) {
65          	Map<String, String> roleQualification = new HashMap<String, String>();
66          	roleQualification.put(KimConstants.AttributeConstants.PRINCIPAL_ID, userPrincipalId);
67          	roleQualification.put(KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(), departmentObj.getDept());
68          	roleQualification.put(KPMERoleMemberAttribute.LOCATION.getRoleMemberAttributeName(), departmentObj.getLocation());
69          	
70          	if (!KimApiServiceLocator.getPermissionService().isPermissionDefinedByTemplate(KPMENamespace.KPME_WKFLW.getNamespaceCode(),
71      				KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>())
72      		  || KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(userPrincipalId, KPMENamespace.KPME_WKFLW.getNamespaceCode(),
73      				  KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>(), roleQualification)) {
74          		results.add(departmentObj);
75          	}
76      	}
77  
78          //this shouldn't be needed here
79          //for (Department result : results) {
80          //	populateDepartmentRoleMembers(result, result.getEffectiveLocalDate());
81          //}
82          
83          return results;
84      }
85      
86  	@Override
87  	public int getDepartmentCount(String department) {
88  		return departmentDao.getDepartmentCount(department);
89  	}
90  	
91      @Override
92  	public Department getDepartment(String department, LocalDate asOfDate) {
93          Department departmentObj = departmentDao.getDepartment(department, asOfDate);
94          
95          if (departmentObj != null) {
96          	populateDepartmentRoleMembers(departmentObj, asOfDate);
97          }
98  
99  		return departmentObj;
100 	}
101 
102     @Override
103     public Department getDepartmentWithoutRoles(String department, LocalDate asOfDate) {
104         return departmentDao.getDepartment(department, asOfDate);
105     }
106 
107 
108     @Override
109     public List<String> getDepartmentsForLocation(String location, LocalDate asOfDate) {
110         List<Department> departmentObjs = departmentDao.getDepartments(location, asOfDate);
111         List<String> depts = new ArrayList<String>();
112         for (Department departmentObj : departmentObjs) {
113             depts.add(departmentObj.getDept());
114         }
115 
116         return depts;
117     }
118 
119     @Override
120     public List<String> getDepartmentsForLocations(List<String> locations, LocalDate asOfDate) {
121         if (CollectionUtils.isEmpty(locations)) {
122             return Collections.emptyList();
123         }
124         List<Department> departmentObjs = departmentDao.getDepartmentsForLocations(locations, asOfDate);
125         List<String> depts = new ArrayList<String>();
126         for (Department departmentObj : departmentObjs) {
127             depts.add(departmentObj.getDept());
128         }
129 
130         return depts;
131     }
132 
133     @Override
134     public List<Department> getDepartments(String location, LocalDate asOfDate) {
135         List<Department> departmentObjs = departmentDao.getDepartments(location, asOfDate);
136 
137         for (Department departmentObj : departmentObjs) {
138         	populateDepartmentRoleMembers(departmentObj, departmentObj.getEffectiveLocalDate());
139         }
140 
141         return departmentObjs;
142     }
143     
144     @Override
145 	public List<Department> getDepartments(String department) {
146 		return departmentDao.getDepartments(department);
147 	}
148 
149     private void populateDepartmentRoleMembers(Department department, LocalDate asOfDate) {
150     	if (department != null && asOfDate != null 
151     			&& CollectionUtils.isEmpty(department.getRoleMembers()) && CollectionUtils.isEmpty(department.getInactiveRoleMembers())) {
152     		Set<RoleMember> roleMembers = new HashSet<RoleMember>();
153 
154 	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_VIEW_ONLY.getRoleName(), department.getDept(), asOfDate.toDateTimeAtStartOfDay(), false));
155 	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName(), department.getDept(), asOfDate.toDateTimeAtStartOfDay(), false));
156 	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_VIEW_ONLY.getRoleName(), department.getDept(), asOfDate.toDateTimeAtStartOfDay(), false));
157 	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName(), department.getDept(), asOfDate.toDateTimeAtStartOfDay(), false));
158 	
159 	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), department.getDept(), asOfDate.toDateTimeAtStartOfDay(), false));
160 	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), department.getDept(), asOfDate.toDateTimeAtStartOfDay(), false));
161 	
162 	    	for (RoleMember roleMember : roleMembers) {
163 	    		RoleMemberBo roleMemberBo = RoleMemberBo.from(roleMember);
164 	    		
165 	    		if (roleMemberBo.isActive()) {
166 	    			department.addRoleMember(DepartmentPrincipalRoleMemberBo.from(roleMemberBo, roleMember.getAttributes()));
167 	    		} else {
168 	    			department.addInactiveRoleMember(DepartmentPrincipalRoleMemberBo.from(roleMemberBo, roleMember.getAttributes()));
169 	    		}
170 	    	}
171     	}
172     }
173 
174 }