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