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.kpme.tklm.common;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Comparator;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.commons.lang.ObjectUtils;
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  import org.joda.time.DateTime;
37  import org.joda.time.LocalDate;
38  import org.kuali.kpme.core.KPMENamespace;
39  import org.kuali.kpme.core.accrualcategory.AccrualCategory;
40  import org.kuali.kpme.core.accrualcategory.rule.AccrualCategoryRule;
41  import org.kuali.kpme.core.api.accrualcategory.AccrualEarnInterval;
42  import org.kuali.kpme.core.assignment.Assignment;
43  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
44  import org.kuali.kpme.core.role.KPMERole;
45  import org.kuali.kpme.core.service.HrServiceLocator;
46  import org.kuali.kpme.core.util.HrConstants;
47  import org.kuali.kpme.core.util.HrContext;
48  import org.kuali.kpme.core.web.KPMEAction;
49  import org.kuali.rice.kim.api.identity.IdentityService;
50  import org.kuali.rice.kim.api.identity.Person;
51  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
52  import org.kuali.rice.kim.api.role.RoleMember;
53  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
54  
55  public class PersonInfoAction extends KPMEAction {
56      private IdentityService identityService;
57  
58      public ActionForward showInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
59      	return mapping.findForward("basic");
60      }
61  
62  	@Override
63  	public ActionForward execute(ActionMapping mapping, ActionForm form,
64  			HttpServletRequest request, HttpServletResponse response)
65  			throws Exception {
66  		ActionForward actForw =  super.execute(mapping, form, request, response);
67  		PersonInfoActionForm personForm = (PersonInfoActionForm)form;
68  		
69  		personForm.setPrincipalId(HrContext.getTargetPrincipalId());
70          EntityNamePrincipalName name = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(personForm.getPrincipalId());
71  		//Person person = KimApiServiceLocator.getPersonService().getPerson(personForm.getPrincipalId());
72  		if (name != null) {
73              personForm.setPrincipalName(name.getPrincipalName());
74              // set name
75              personForm.setName(name.getDefaultName() != null ? name.getDefaultName().getCompositeName() : StringUtils.EMPTY);
76          }
77  		personForm.setJobs(HrServiceLocator.getJobService().getJobs(HrContext.getTargetPrincipalId(), LocalDate.now()));
78  		
79  		//KPME-1441
80  		PrincipalHRAttributes principalHRAttributes = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(personForm.getPrincipalId(), LocalDate.now());
81  		if ( principalHRAttributes != null && principalHRAttributes.getServiceDate() != null ){
82  			personForm.setServiceDate(principalHRAttributes.getServiceDate().toString());
83  		} else {
84  			personForm.setServiceDate("");
85  		}
86  		// KPME-1441
87  		
88  		if (principalHRAttributes != null && principalHRAttributes.getLeavePlan() != null) {
89  			List<AccrualCategory> accrualCategories = new ArrayList<AccrualCategory>();
90  			Map<String, BigDecimal> accrualCategoryRates = new HashMap<String, BigDecimal>();
91  		    Map<String, String> accrualEarnIntervals = new HashMap<String, String>();
92  		    Map<String, String> unitOfTime = new HashMap<String, String>();
93  			
94  			List<AccrualCategory> allAccrualCategories = HrServiceLocator.getAccrualCategoryService().getActiveLeaveAccrualCategoriesForLeavePlan(principalHRAttributes.getLeavePlan(), LocalDate.now());
95  		    for (AccrualCategory accrualCategory : allAccrualCategories) {
96  				if (StringUtils.equalsIgnoreCase(accrualCategory.getHasRules(), "Y")) {
97  					AccrualCategoryRule accrualCategoryRule = HrServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(accrualCategory, LocalDate.now(), principalHRAttributes.getServiceLocalDate());
98  					if (accrualCategoryRule != null) {
99  						accrualCategories.add(accrualCategory);
100 						
101 						accrualCategoryRates.put(accrualCategory.getAccrualCategory(), accrualCategoryRule.getAccrualRate());
102 
103 						for (AccrualEarnInterval entry : AccrualEarnInterval.values()) {
104 				            if (accrualCategory.getAccrualEarnInterval().equals(entry.getCode())) {
105 				            	accrualEarnIntervals.put(accrualCategory.getAccrualCategory(), entry.getDescription());
106 				            	break;
107 				            }
108 				        } 
109 						
110 						for (Map.Entry<String, String> entry : HrConstants.UNIT_OF_TIME.entrySet()) {					            
111 				            if (accrualCategory.getUnitOfTime().equals(entry.getKey()) ){
112 				            	unitOfTime.put(accrualCategory.getAccrualCategory(), entry.getValue());
113 				            	break;
114 				            }
115 				        } 
116 					}
117 				}
118 			}
119 			personForm.setAccrualCategories(accrualCategories);
120 			personForm.setAccrualCategoryRates(accrualCategoryRates);
121 			personForm.setAccrualEarnIntervals(accrualEarnIntervals);
122 			personForm.setUnitOfTime(unitOfTime);
123 		}
124 		
125 		setupRolesOnForm(personForm);
126 
127 		List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(HrContext.getTargetPrincipalId(), LocalDate.now());
128 		
129 		Map<Long, Set<Assignment>> jobNumberToListAssignments = new HashMap<Long,Set<Assignment>>();
130 		Map<Long, Set<Person>> workAreaToApproverPerson = new HashMap<Long, Set<Person>>();
131         Map<String, Set<Person>> deptToDeptAdminPerson = new HashMap<String, Set<Person>>();
132         Map<String, Set<Person>> deptToPayrollPerson = new HashMap<String, Set<Person>>();
133 		
134 		for (Assignment assignment : assignments) {
135 			Set<Assignment> jobAssignments = jobNumberToListAssignments.get(assignment.getJobNumber());
136 			if (jobAssignments == null) {
137 				jobAssignments = new HashSet<Assignment>();
138 			}
139 			jobAssignments.add(assignment);
140 			jobNumberToListAssignments.put(assignment.getJobNumber(), jobAssignments);
141 			
142 			Set<Person> approvers = workAreaToApproverPerson.get(assignment.getWorkArea());
143 			if (approvers == null) {
144 				approvers = new TreeSet<Person>(new Comparator<Person>() {
145 					@Override
146 					public int compare(Person person1, Person person2) {
147 						return ObjectUtils.compare(person1.getPrincipalId(), person2.getPrincipalId());
148 					}
149 				});
150 			}
151 			approvers.addAll(getApprovers(assignment.getWorkArea()));
152 			workAreaToApproverPerson.put(assignment.getWorkArea(), approvers);
153 			
154 			Set<Person> departmentAdmins = deptToDeptAdminPerson.get(assignment.getDept());
155 			if (departmentAdmins == null) {
156 				departmentAdmins = new TreeSet<Person>(new Comparator<Person>() {
157 					@Override
158 					public int compare(Person person1, Person person2) {
159 						return ObjectUtils.compare(person1.getPrincipalId(), person2.getPrincipalId());
160 					}
161 				});
162 			}
163 			departmentAdmins.addAll(getDepartmentAdmins(assignment.getDept()));
164             deptToDeptAdminPerson.put(assignment.getDept(), departmentAdmins);
165 
166             Set<Person> payrollProcessors = deptToPayrollPerson.get(assignment.getDept());
167             if (payrollProcessors == null) {
168                 payrollProcessors = new TreeSet<Person>(new Comparator<Person>() {
169                     @Override
170                     public int compare(Person person1, Person person2) {
171                         return ObjectUtils.compare(person1.getPrincipalId(), person2.getPrincipalId());
172                     }
173                 });
174             }
175             payrollProcessors.addAll(getPayrollProcessors(assignment.getDept()));
176             deptToPayrollPerson.put(assignment.getDept(), payrollProcessors);
177 		}
178 		
179 		personForm.setJobNumberToListAssignments(jobNumberToListAssignments);
180 		personForm.setWorkAreaToApproverPerson(workAreaToApproverPerson);
181         personForm.setDeptToDeptAdminPerson(deptToDeptAdminPerson);
182         personForm.setDeptToPayrollPerson(deptToPayrollPerson);
183 		
184 		return actForw;
185 	}
186 	
187 	private void setupRolesOnForm(PersonInfoActionForm personInfoActionForm) {
188 		String principalId = HrContext.getTargetPrincipalId();
189 		
190 		Set<Long> allApproverWorkAreas = new HashSet<Long>();
191 		allApproverWorkAreas.addAll(HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), DateTime.now(), true));
192 		allApproverWorkAreas.addAll(HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), DateTime.now(), true));
193 		personInfoActionForm.setApproverWorkAreas(new ArrayList<Long>(allApproverWorkAreas));
194 		
195 		List<Long> reviewerWorkAreas = HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), DateTime.now(), true);
196 		personInfoActionForm.setReviewerWorkAreas(reviewerWorkAreas);
197 		
198 		Set<String> allViewOnlyDepartments = new HashSet<String>();
199 		allViewOnlyDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_VIEW_ONLY.getRoleName(), DateTime.now(), true));
200 		allViewOnlyDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_VIEW_ONLY.getRoleName(), DateTime.now(), true));
201 		allViewOnlyDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_VIEW_ONLY.getRoleName(), DateTime.now(), true));
202 		allViewOnlyDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_VIEW_ONLY.getRoleName(), DateTime.now(), true));
203 		personInfoActionForm.setDeptViewOnlyDepts(new ArrayList<String>(allViewOnlyDepartments));
204 		
205 		Set<String> allAdministratorDepartments = new HashSet<String>();
206 		allAdministratorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName(), DateTime.now(), true));
207 		allAdministratorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName(), DateTime.now(), true));
208 		allAdministratorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), DateTime.now(), true));
209 		allAdministratorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), DateTime.now(), true));
210 		personInfoActionForm.setDeptAdminDepts(new ArrayList<String>(allAdministratorDepartments));
211 
212         Set<String> allPayrollProcessorDepartments = new HashSet<String>();
213         allPayrollProcessorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), DateTime.now(), true));
214         allPayrollProcessorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), DateTime.now(), true));
215         personInfoActionForm.setPayrollProcessorDepts(new ArrayList<String>(allPayrollProcessorDepartments));
216 
217 		Set<String> allAdministratorLocations = new HashSet<String>();
218 		allAdministratorLocations.addAll(HrServiceLocator.getKPMERoleService().getLocationsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), DateTime.now(), true));
219 		allAdministratorLocations.addAll(HrServiceLocator.getKPMERoleService().getLocationsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), DateTime.now(), true));
220 		personInfoActionForm.setLocationAdminDepts(new ArrayList<String>(allAdministratorLocations));
221 		
222 		personInfoActionForm.setGlobalViewOnlyRoles(HrServiceLocator.getKPMEGroupService().isMemberOfSystemViewOnlyGroup(principalId, DateTime.now()));
223 		personInfoActionForm.setSystemAdmin(HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(principalId, DateTime.now()));
224 	}
225 
226     private Set<Person> getDepartmentAdmins(String dept) {
227     	Set<Person> departmentAdmins = new HashSet<Person>();
228     	
229     	List<RoleMember> roleMembers = new ArrayList<RoleMember>();
230     	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName(), dept, DateTime.now(), true));
231     	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName(), dept, DateTime.now(), true));
232 	        
233     	for (RoleMember roleMember : roleMembers) {
234     		Person person = KimApiServiceLocator.getPersonService().getPerson(roleMember.getMemberId());
235 			if (person != null) {
236 				departmentAdmins.add(person);
237 			}
238         }
239     	
240         return departmentAdmins;
241     }
242 
243     private Set<Person> getPayrollProcessors(String dept) {
244         Set<Person> payrollProcs = new HashSet<Person>();
245 
246         List<RoleMember> roleMembers = new ArrayList<RoleMember>();
247         roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), dept, DateTime.now(), true));
248 
249         for (RoleMember roleMember : roleMembers) {
250             Person person = KimApiServiceLocator.getPersonService().getPerson(roleMember.getMemberId());
251             if (person != null) {
252                 payrollProcs.add(person);
253             }
254         }
255 
256         return payrollProcs;
257     }
258 	
259 	private Set<Person> getApprovers(Long workArea) {
260 		Set<Person> approvers = new HashSet<Person>();
261 		
262 		List<RoleMember> roleMembers = HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workArea, DateTime.now(), true);
263 		
264 		for (RoleMember roleMember : roleMembers) {
265 			Person person = KimApiServiceLocator.getPersonService().getPerson(roleMember.getMemberId());
266 			if (person != null) {
267 				approvers.add(person);
268 			}
269         }
270 		
271 		return approvers;
272 	}
273 
274 
275 }