001    /**
002     * Copyright 2004-2013 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.math.BigDecimal;
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    import org.apache.commons.lang3.StringUtils;
028    import org.apache.struts.action.ActionForm;
029    import org.apache.struts.action.ActionForward;
030    import org.apache.struts.action.ActionMapping;
031    import org.kuali.hr.job.Job;
032    import org.kuali.hr.lm.LMConstants;
033    import org.kuali.hr.lm.accrual.AccrualCategory;
034    import org.kuali.hr.lm.accrual.AccrualCategoryRule;
035    import org.kuali.hr.time.assignment.Assignment;
036    import org.kuali.hr.time.principal.PrincipalHRAttributes;
037    import org.kuali.hr.time.roles.TkRole;
038    import org.kuali.hr.time.roles.UserRoles;
039    import org.kuali.hr.time.service.base.TkServiceLocator;
040    import org.kuali.hr.time.util.TKContext;
041    import org.kuali.hr.time.util.TKUtils;
042    import org.kuali.hr.time.util.TkConstants;
043    import org.kuali.rice.kim.api.identity.Person;
044    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
045    
046    public class PersonInfoAction extends TkAction {
047    
048    
049    
050        public ActionForward showInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
051            return mapping.findForward("basic");
052        }
053    
054            @Override
055            public ActionForward execute(ActionMapping mapping, ActionForm form,
056                            HttpServletRequest request, HttpServletResponse response)
057                            throws Exception {
058                    ActionForward actForw =  super.execute(mapping, form, request, response);
059                    PersonInfoActionForm personForm = (PersonInfoActionForm)form;
060                    
061                    personForm.setPrincipalId(TKContext.getTargetPrincipalId());
062                    Person person = KimApiServiceLocator.getPersonService().getPerson(personForm.getPrincipalId());
063                    personForm.setPrincipalName(person.getPrincipalName());
064                    // set name
065                    personForm.setName(person.getName());
066                    personForm.setJobs(TkServiceLocator.getJobService().getJobs(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate()));
067                    
068                    //KPME-1441
069                    PrincipalHRAttributes principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(personForm.getPrincipalId(), TKUtils.getCurrentDate());
070                    if ( principalHRAttributes != null && principalHRAttributes.getServiceDate() != null ){
071                            personForm.setServiceDate(principalHRAttributes.getServiceDate().toString());
072                    } else {
073                            personForm.setServiceDate("");
074                    }
075                    // KPME-1441
076                    
077                    if (principalHRAttributes != null && principalHRAttributes.getLeavePlan() != null) {
078                            List<AccrualCategory> accrualCategories = new ArrayList<AccrualCategory>();
079                            Map<String, BigDecimal> accrualCategoryRates = new HashMap<String, BigDecimal>();
080                        Map<String, String> accrualEarnIntervals = new HashMap<String, String>();
081                        Map<String, String> unitOfTime = new HashMap<String, String>();
082                            
083                            List<AccrualCategory> allAccrualCategories = TkServiceLocator.getAccrualCategoryService().getActiveLeaveAccrualCategoriesForLeavePlan(principalHRAttributes.getLeavePlan(), TKUtils.getCurrentDate());
084                        for (AccrualCategory accrualCategory : allAccrualCategories) {
085                                    if (StringUtils.equalsIgnoreCase(accrualCategory.getHasRules(), "Y")) {
086                                            AccrualCategoryRule accrualCategoryRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(accrualCategory, TKUtils.getCurrentDate(), principalHRAttributes.getServiceDate());
087                                            if (accrualCategoryRule != null) {
088                                                    accrualCategories.add(accrualCategory);
089                                                    
090                                                    accrualCategoryRates.put(accrualCategory.getAccrualCategory(), accrualCategoryRule.getAccrualRate());
091    
092                                                    for (Map.Entry<String, String> entry : LMConstants.ACCRUAL_EARN_INTERVAL_MAP.entrySet()) {                                                    
093                                                if (accrualCategory.getAccrualEarnInterval().equals(entry.getKey())) {
094                                                    accrualEarnIntervals.put(accrualCategory.getAccrualCategory(), entry.getValue());
095                                                    break;
096                                                }
097                                            } 
098                                                    
099                                                    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    }