View Javadoc

1   /**
2    * Copyright 2004-2012 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.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  import org.kuali.hr.job.Job;
30  import org.kuali.hr.time.assignment.Assignment;
31  import org.kuali.hr.time.principal.PrincipalHRAttributes;
32  import org.kuali.hr.time.roles.TkRole;
33  import org.kuali.hr.time.roles.UserRoles;
34  import org.kuali.hr.time.service.base.TkServiceLocator;
35  import org.kuali.hr.time.util.TKContext;
36  import org.kuali.hr.time.util.TKUtils;
37  import org.kuali.hr.time.util.TkConstants;
38  import org.kuali.rice.kim.api.identity.Person;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  
41  public class PersonInfoAction extends TkAction {
42  
43  
44  
45      public ActionForward showInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
46      	return mapping.findForward("basic");
47      }
48  
49  	@Override
50  	public ActionForward execute(ActionMapping mapping, ActionForm form,
51  			HttpServletRequest request, HttpServletResponse response)
52  			throws Exception {
53  		ActionForward actForw =  super.execute(mapping, form, request, response);
54  		PersonInfoActionForm personForm = (PersonInfoActionForm)form;
55  		
56  		personForm.setPrincipalId(TKContext.getTargetPrincipalId());
57  		Person person = KimApiServiceLocator.getPersonService().getPerson(personForm.getPrincipalId());
58  		personForm.setPrincipalName(person.getPrincipalName());
59  		// set name
60  		personForm.setName(person.getName());
61  		personForm.setJobs(TkServiceLocator.getJobService().getJobs(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate()));
62  		
63  		//KPME-1441
64  		PrincipalHRAttributes principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(personForm.getPrincipalId(), TKUtils.getCurrentDate());
65  		if ( principalHRAttributes != null && principalHRAttributes.getServiceDate() != null ){
66  			personForm.setServiceDate(principalHRAttributes.getServiceDate().toString());
67  		} else {
68  			personForm.setServiceDate("");
69  		}
70  		// KPME-1441
71  		
72  		setupRolesOnForm(personForm);
73  
74  		List<Assignment> lstAssign = TkServiceLocator.getAssignmentService().getAssignments(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate());
75  		Map<Long,List<Assignment>> jobNumberToListAssignments = new HashMap<Long,List<Assignment>>();
76  		
77  		Map<Long,List<TkRole>> workAreaToApprover = new HashMap<Long,List<TkRole>>();
78  		Map<Long,List<Person>> workAreaToApproverPerson = new HashMap<Long, List<Person>>();
79          Map<String,List<Person>> deptToDeptAdminPerson = new HashMap<String,List<Person>>();
80  		
81  		Map<String,Person> principalIdToPerson = new HashMap<String,Person>();
82  		for(Assignment assign : lstAssign){
83  			List<Assignment> lstCurrJobAssign = jobNumberToListAssignments.get(assign.getJobNumber());
84  			if(lstCurrJobAssign == null){
85  				lstCurrJobAssign = new ArrayList<Assignment>();
86  			}
87  			lstCurrJobAssign.add(assign);
88  			jobNumberToListAssignments.put(assign.getJobNumber(), lstCurrJobAssign);
89  
90  			this.assignWorkAreaToApprover(assign.getWorkArea(), workAreaToApprover, workAreaToApproverPerson);
91              deptToDeptAdminPerson = assignDeptToDeptAdmin(assign.getDept());
92  		}
93  		
94  		personForm.setWorkAreaToApprover(workAreaToApprover);
95  		personForm.setWorkAreaToApproverPerson(workAreaToApproverPerson);
96          personForm.setDeptToDeptAdminPerson(deptToDeptAdminPerson);
97  		personForm.setJobNumberToListAssignments(jobNumberToListAssignments);
98  		personForm.setPrincipalIdToPerson(principalIdToPerson);
99  		return actForw;
100 	}
101 	
102 	private void setupRolesOnForm(PersonInfoActionForm paForm){
103 		UserRoles roles = TKContext.getUser().getCurrentTargetRoles();
104 		for(Long waApprover : roles.getApproverWorkAreas()){
105 			paForm.getApproverWorkAreas().add(waApprover);
106 		}
107 		
108 		for(Long waReviewer : roles.getReviewerWorkAreas()){
109 			paForm.getReviewerWorkAreas().add(waReviewer);
110 		}
111 		
112 		for(String deptAdmin : roles.getOrgAdminDepartments()){
113 			paForm.getDeptAdminDepts().add(deptAdmin);
114 		}
115 		
116 		for(String deptViewOnly : roles.getDepartmentViewOnlyDepartments()){
117 			paForm.getDeptViewOnlyDepts().add(deptViewOnly);
118 		}
119 		
120 		for(String location : roles.getOrgAdminCharts()){
121 			paForm.getLocationAdminDepts().add(location);
122 		}
123 		
124 		paForm.setGlobalViewOnlyRoles(roles.isGlobalViewOnly());
125 		paForm.setSystemAdmin(roles.isSystemAdmin());
126 	}
127 
128     private Map<String,List<Person>> assignDeptToDeptAdmin(String dept) {
129         List<TkRole> deptAdminRoles = TkServiceLocator.getTkRoleService().getDepartmentRoles(dept, TkConstants.ROLE_TK_DEPT_ADMIN, TKUtils.getCurrentDate());
130         List<Person> deptAdminPeople = new ArrayList<Person>();
131         Map<String,List<Person>> deptAdmins = new HashMap<String, List<Person>>();
132 
133         for(TkRole role : deptAdminRoles) {
134         	if(role.getPositionNumber() != null){
135 				List<Job> lstJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(role.getPositionNumber(), TKUtils.getCurrentDate());
136 				for(Job j : lstJobs){
137 					Person person = KimApiServiceLocator.getPersonService().getPerson(j.getPrincipalId());
138 					if(person !=null){
139 						deptAdminPeople.add(person);
140 					}
141 				}
142 			} else{
143 				Person person = KimApiServiceLocator.getPersonService().getPerson(role.getPrincipalId());
144 				if(person!=null){
145 					deptAdminPeople.add(person);
146 				}
147 			}
148         }
149         deptAdmins.put(dept, deptAdminPeople);
150 
151         return deptAdmins;
152     }
153 	
154 	private void assignWorkAreaToApprover(Long workArea, Map<Long,List<TkRole>> workAreaToApprover, Map<Long,List<Person>> workAreaToApproverPerson ){
155 		List<TkRole> lstApproverRoles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(workArea, TkConstants.ROLE_TK_APPROVER,
156 				TKUtils.getCurrentDate());
157 		workAreaToApprover.put(workArea, lstApproverRoles);
158 		for(TkRole role : lstApproverRoles){
159 			if(role.getPositionNumber() != null){
160 				List<Job> lstJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(role.getPositionNumber(), TKUtils.getCurrentDate());
161 				for(Job j : lstJobs){
162 					Person approver = KimApiServiceLocator.getPersonService().getPerson(j.getPrincipalId());
163 					if(approver!=null){
164 					addApproverPersonForWorkArea(workArea, approver, workAreaToApproverPerson);
165 					}
166 				}
167 			} else{
168 				Person approver = KimApiServiceLocator.getPersonService().getPerson(role.getPrincipalId());
169 				if(approver!=null){
170 					addApproverPersonForWorkArea(workArea, approver, workAreaToApproverPerson);
171 				}
172 			}
173 		}
174 	}
175 	
176 	private void addApproverPersonForWorkArea(Long workArea, Person person, Map<Long,List<Person>> workAreaToApproverPerson){
177 		List<Person> approvers = workAreaToApproverPerson.get(workArea);
178 		if(approvers == null){
179 			approvers = new ArrayList<Person>();
180 		}
181 		approvers.add(person);
182 		workAreaToApproverPerson.put(workArea, approvers);
183 	}
184 }