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.base.web;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.kuali.hr.job.Job;
32  import org.kuali.hr.lm.LMConstants;
33  import org.kuali.hr.lm.accrual.AccrualCategory;
34  import org.kuali.hr.lm.accrual.AccrualCategoryRule;
35  import org.kuali.hr.time.assignment.Assignment;
36  import org.kuali.hr.time.principal.PrincipalHRAttributes;
37  import org.kuali.hr.time.roles.TkRole;
38  import org.kuali.hr.time.roles.UserRoles;
39  import org.kuali.hr.time.service.base.TkServiceLocator;
40  import org.kuali.hr.time.util.TKContext;
41  import org.kuali.hr.time.util.TKUtils;
42  import org.kuali.hr.time.util.TkConstants;
43  import org.kuali.rice.kim.api.identity.Person;
44  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
45  
46  public class PersonInfoAction extends TkAction {
47  
48  
49  
50      public ActionForward showInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
51      	return mapping.findForward("basic");
52      }
53  
54  	@Override
55  	public ActionForward execute(ActionMapping mapping, ActionForm form,
56  			HttpServletRequest request, HttpServletResponse response)
57  			throws Exception {
58  		ActionForward actForw =  super.execute(mapping, form, request, response);
59  		PersonInfoActionForm personForm = (PersonInfoActionForm)form;
60  		
61  		personForm.setPrincipalId(TKContext.getTargetPrincipalId());
62  		Person person = KimApiServiceLocator.getPersonService().getPerson(personForm.getPrincipalId());
63  		personForm.setPrincipalName(person.getPrincipalName());
64  		// set name
65  		personForm.setName(person.getName());
66  		personForm.setJobs(TkServiceLocator.getJobService().getJobs(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate()));
67  		
68  		//KPME-1441
69  		PrincipalHRAttributes principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(personForm.getPrincipalId(), TKUtils.getCurrentDate());
70  		if ( principalHRAttributes != null && principalHRAttributes.getServiceDate() != null ){
71  			personForm.setServiceDate(principalHRAttributes.getServiceDate().toString());
72  		} else {
73  			personForm.setServiceDate("");
74  		}
75  		// KPME-1441
76  		
77  		if (principalHRAttributes != null && principalHRAttributes.getLeavePlan() != null) {
78  			List<AccrualCategory> accrualCategories = new ArrayList<AccrualCategory>();
79  			Map<String, BigDecimal> accrualCategoryRates = new HashMap<String, BigDecimal>();
80  		    Map<String, String> accrualEarnIntervals = new HashMap<String, String>();
81  		    Map<String, String> unitOfTime = new HashMap<String, String>();
82  			
83  			List<AccrualCategory> allAccrualCategories = TkServiceLocator.getAccrualCategoryService().getActiveLeaveAccrualCategoriesForLeavePlan(principalHRAttributes.getLeavePlan(), TKUtils.getCurrentDate());
84  		    for (AccrualCategory accrualCategory : allAccrualCategories) {
85  				if (StringUtils.equalsIgnoreCase(accrualCategory.getHasRules(), "Y")) {
86  					AccrualCategoryRule accrualCategoryRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(accrualCategory, TKUtils.getCurrentDate(), principalHRAttributes.getServiceDate());
87  					if (accrualCategoryRule != null) {
88  						accrualCategories.add(accrualCategory);
89  						
90  						accrualCategoryRates.put(accrualCategory.getAccrualCategory(), accrualCategoryRule.getAccrualRate());
91  
92  						for (Map.Entry<String, String> entry : LMConstants.ACCRUAL_EARN_INTERVAL_MAP.entrySet()) {					            
93  				            if (accrualCategory.getAccrualEarnInterval().equals(entry.getKey())) {
94  				            	accrualEarnIntervals.put(accrualCategory.getAccrualCategory(), entry.getValue());
95  				            	break;
96  				            }
97  				        } 
98  						
99  						for (Map.Entry<String, String> entry : TkConstants.UNIT_OF_TIME.entrySet()) {					            
100 				            if (accrualCategory.getUnitOfTime().equals(entry.getKey()) ){
101 				            	unitOfTime.put(accrualCategory.getAccrualCategory(), entry.getValue());
102 				            	break;
103 				            }
104 				        } 
105 					}
106 				}
107 			}
108 			personForm.setAccrualCategories(accrualCategories);
109 			personForm.setAccrualCategoryRates(accrualCategoryRates);
110 			personForm.setAccrualEarnIntervals(accrualEarnIntervals);
111 			personForm.setUnitOfTime(unitOfTime);
112 		}
113 		
114 		setupRolesOnForm(personForm);
115 
116 		List<Assignment> lstAssign = TkServiceLocator.getAssignmentService().getAssignments(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate());
117 		Map<Long,List<Assignment>> jobNumberToListAssignments = new HashMap<Long,List<Assignment>>();
118 		
119 		Map<Long,List<TkRole>> workAreaToApprover = new HashMap<Long,List<TkRole>>();
120 		Map<Long,List<Person>> workAreaToApproverPerson = new HashMap<Long, List<Person>>();
121         Map<String,List<Person>> deptToDeptAdminPerson = new HashMap<String,List<Person>>();
122 		
123 		Map<String,Person> principalIdToPerson = new HashMap<String,Person>();
124 		for(Assignment assign : lstAssign){
125 			List<Assignment> lstCurrJobAssign = jobNumberToListAssignments.get(assign.getJobNumber());
126 			if(lstCurrJobAssign == null){
127 				lstCurrJobAssign = new ArrayList<Assignment>();
128 			}
129 			lstCurrJobAssign.add(assign);
130 			jobNumberToListAssignments.put(assign.getJobNumber(), lstCurrJobAssign);
131 
132 			this.assignWorkAreaToApprover(assign.getWorkArea(), workAreaToApprover, workAreaToApproverPerson);
133             deptToDeptAdminPerson = assignDeptToDeptAdmin(assign.getDept());
134 		}
135 		
136 		personForm.setWorkAreaToApprover(workAreaToApprover);
137 		personForm.setWorkAreaToApproverPerson(workAreaToApproverPerson);
138         personForm.setDeptToDeptAdminPerson(deptToDeptAdminPerson);
139 		personForm.setJobNumberToListAssignments(jobNumberToListAssignments);
140 		personForm.setPrincipalIdToPerson(principalIdToPerson);
141 		return actForw;
142 	}
143 	
144 	private void setupRolesOnForm(PersonInfoActionForm paForm){
145 		UserRoles roles = TKContext.getUser().getCurrentTargetRoles();
146 		for(Long waApprover : roles.getApproverWorkAreas()){
147 			paForm.getApproverWorkAreas().add(waApprover);
148 		}
149 		
150 		for(Long waReviewer : roles.getReviewerWorkAreas()){
151 			paForm.getReviewerWorkAreas().add(waReviewer);
152 		}
153 		
154 		for(String deptAdmin : roles.getOrgAdminDepartments()){
155 			paForm.getDeptAdminDepts().add(deptAdmin);
156 		}
157 		
158 		for(String deptViewOnly : roles.getDepartmentViewOnlyDepartments()){
159 			paForm.getDeptViewOnlyDepts().add(deptViewOnly);
160 		}
161 		
162 		for(String location : roles.getOrgAdminCharts()){
163 			paForm.getLocationAdminDepts().add(location);
164 		}
165 		
166 		paForm.setGlobalViewOnlyRoles(roles.isGlobalViewOnly());
167 		paForm.setSystemAdmin(roles.isSystemAdmin());
168 	}
169 
170     private Map<String,List<Person>> assignDeptToDeptAdmin(String dept) {
171         List<TkRole> deptAdminRoles = TkServiceLocator.getTkRoleService().getDepartmentRoles(dept, TkConstants.ROLE_TK_DEPT_ADMIN, TKUtils.getCurrentDate());
172         List<Person> deptAdminPeople = new ArrayList<Person>();
173         Map<String,List<Person>> deptAdmins = new HashMap<String, List<Person>>();
174 
175         for(TkRole role : deptAdminRoles) {
176         	if(role.getPositionNumber() != null){
177 				List<Job> lstJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(role.getPositionNumber(), TKUtils.getCurrentDate());
178 				for(Job j : lstJobs){
179 					Person person = KimApiServiceLocator.getPersonService().getPerson(j.getPrincipalId());
180 					if(person !=null){
181 						deptAdminPeople.add(person);
182 					}
183 				}
184 			} else{
185 				Person person = KimApiServiceLocator.getPersonService().getPerson(role.getPrincipalId());
186 				if(person!=null){
187 					deptAdminPeople.add(person);
188 				}
189 			}
190         }
191         deptAdmins.put(dept, deptAdminPeople);
192 
193         return deptAdmins;
194     }
195 	
196 	private void assignWorkAreaToApprover(Long workArea, Map<Long,List<TkRole>> workAreaToApprover, Map<Long,List<Person>> workAreaToApproverPerson ){
197 		List<TkRole> lstApproverRoles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(workArea, TkConstants.ROLE_TK_APPROVER,
198 				TKUtils.getCurrentDate());
199 		workAreaToApprover.put(workArea, lstApproverRoles);
200 		for(TkRole role : lstApproverRoles){
201 			if(role.getPositionNumber() != null){
202 				List<Job> lstJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(role.getPositionNumber(), TKUtils.getCurrentDate());
203 				for(Job j : lstJobs){
204 					Person approver = KimApiServiceLocator.getPersonService().getPerson(j.getPrincipalId());
205 					if(approver!=null){
206 					addApproverPersonForWorkArea(workArea, approver, workAreaToApproverPerson);
207 					}
208 				}
209 			} else{
210 				Person approver = KimApiServiceLocator.getPersonService().getPerson(role.getPrincipalId());
211 				if(approver!=null){
212 					addApproverPersonForWorkArea(workArea, approver, workAreaToApproverPerson);
213 				}
214 			}
215 		}
216 	}
217 	
218 	private void addApproverPersonForWorkArea(Long workArea, Person person, Map<Long,List<Person>> workAreaToApproverPerson){
219 		List<Person> approvers = workAreaToApproverPerson.get(workArea);
220 		if(approvers == null){
221 			approvers = new ArrayList<Person>();
222 		}
223 		approvers.add(person);
224 		workAreaToApproverPerson.put(workArea, approvers);
225 	}
226 }