001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.base.web;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    import org.apache.struts.action.ActionForm;
027    import org.apache.struts.action.ActionForward;
028    import org.apache.struts.action.ActionMapping;
029    import org.kuali.hr.job.Job;
030    import org.kuali.hr.time.assignment.Assignment;
031    import org.kuali.hr.time.principal.PrincipalHRAttributes;
032    import org.kuali.hr.time.roles.TkRole;
033    import org.kuali.hr.time.roles.UserRoles;
034    import org.kuali.hr.time.service.base.TkServiceLocator;
035    import org.kuali.hr.time.util.TKContext;
036    import org.kuali.hr.time.util.TKUtils;
037    import org.kuali.hr.time.util.TkConstants;
038    import org.kuali.rice.kim.api.identity.Person;
039    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
040    
041    public class PersonInfoAction extends TkAction {
042    
043    
044    
045        public ActionForward showInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
046            return mapping.findForward("basic");
047        }
048    
049            @Override
050            public ActionForward execute(ActionMapping mapping, ActionForm form,
051                            HttpServletRequest request, HttpServletResponse response)
052                            throws Exception {
053                    ActionForward actForw =  super.execute(mapping, form, request, response);
054                    PersonInfoActionForm personForm = (PersonInfoActionForm)form;
055                    
056                    personForm.setPrincipalId(TKContext.getTargetPrincipalId());
057                    Person person = KimApiServiceLocator.getPersonService().getPerson(personForm.getPrincipalId());
058                    personForm.setPrincipalName(person.getPrincipalName());
059                    // set name
060                    personForm.setName(person.getName());
061                    personForm.setJobs(TkServiceLocator.getJobService().getJobs(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate()));
062                    
063                    //KPME-1441
064                    PrincipalHRAttributes principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(personForm.getPrincipalId(), TKUtils.getCurrentDate());
065                    if ( principalHRAttributes != null && principalHRAttributes.getServiceDate() != null ){
066                            personForm.setServiceDate(principalHRAttributes.getServiceDate().toString());
067                    } else {
068                            personForm.setServiceDate("");
069                    }
070                    // KPME-1441
071                    
072                    setupRolesOnForm(personForm);
073    
074                    List<Assignment> lstAssign = TkServiceLocator.getAssignmentService().getAssignments(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate());
075                    Map<Long,List<Assignment>> jobNumberToListAssignments = new HashMap<Long,List<Assignment>>();
076                    
077                    Map<Long,List<TkRole>> workAreaToApprover = new HashMap<Long,List<TkRole>>();
078                    Map<Long,List<Person>> workAreaToApproverPerson = new HashMap<Long, List<Person>>();
079            Map<String,List<Person>> deptToDeptAdminPerson = new HashMap<String,List<Person>>();
080                    
081                    Map<String,Person> principalIdToPerson = new HashMap<String,Person>();
082                    for(Assignment assign : lstAssign){
083                            List<Assignment> lstCurrJobAssign = jobNumberToListAssignments.get(assign.getJobNumber());
084                            if(lstCurrJobAssign == null){
085                                    lstCurrJobAssign = new ArrayList<Assignment>();
086                            }
087                            lstCurrJobAssign.add(assign);
088                            jobNumberToListAssignments.put(assign.getJobNumber(), lstCurrJobAssign);
089    
090                            this.assignWorkAreaToApprover(assign.getWorkArea(), workAreaToApprover, workAreaToApproverPerson);
091                deptToDeptAdminPerson = assignDeptToDeptAdmin(assign.getDept());
092                    }
093                    
094                    personForm.setWorkAreaToApprover(workAreaToApprover);
095                    personForm.setWorkAreaToApproverPerson(workAreaToApproverPerson);
096            personForm.setDeptToDeptAdminPerson(deptToDeptAdminPerson);
097                    personForm.setJobNumberToListAssignments(jobNumberToListAssignments);
098                    personForm.setPrincipalIdToPerson(principalIdToPerson);
099                    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    }