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