001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.kpme.tklm.common;
017
018import java.math.BigDecimal;
019import java.util.ArrayList;
020import java.util.Comparator;
021import java.util.HashMap;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026import java.util.TreeSet;
027
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030
031import org.apache.commons.lang.StringUtils;
032import org.apache.commons.lang.ObjectUtils;
033import org.apache.struts.action.ActionForm;
034import org.apache.struts.action.ActionForward;
035import org.apache.struts.action.ActionMapping;
036import org.joda.time.DateTime;
037import org.joda.time.LocalDate;
038import org.kuali.kpme.core.KPMENamespace;
039import org.kuali.kpme.core.accrualcategory.AccrualCategory;
040import org.kuali.kpme.core.accrualcategory.rule.AccrualCategoryRule;
041import org.kuali.kpme.core.api.accrualcategory.AccrualEarnInterval;
042import org.kuali.kpme.core.assignment.Assignment;
043import org.kuali.kpme.core.principal.PrincipalHRAttributes;
044import org.kuali.kpme.core.role.KPMERole;
045import org.kuali.kpme.core.service.HrServiceLocator;
046import org.kuali.kpme.core.util.HrConstants;
047import org.kuali.kpme.core.util.HrContext;
048import org.kuali.kpme.core.web.KPMEAction;
049import org.kuali.rice.kim.api.identity.IdentityService;
050import org.kuali.rice.kim.api.identity.Person;
051import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
052import org.kuali.rice.kim.api.role.RoleMember;
053import org.kuali.rice.kim.api.services.KimApiServiceLocator;
054
055public class PersonInfoAction extends KPMEAction {
056    private IdentityService identityService;
057
058    public ActionForward showInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
059        return mapping.findForward("basic");
060    }
061
062        @Override
063        public ActionForward execute(ActionMapping mapping, ActionForm form,
064                        HttpServletRequest request, HttpServletResponse response)
065                        throws Exception {
066                ActionForward actForw =  super.execute(mapping, form, request, response);
067                PersonInfoActionForm personForm = (PersonInfoActionForm)form;
068                
069                personForm.setPrincipalId(HrContext.getTargetPrincipalId());
070        EntityNamePrincipalName name = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(personForm.getPrincipalId());
071                //Person person = KimApiServiceLocator.getPersonService().getPerson(personForm.getPrincipalId());
072                if (name != null) {
073            personForm.setPrincipalName(name.getPrincipalName());
074            // set name
075            personForm.setName(name.getDefaultName() != null ? name.getDefaultName().getCompositeName() : StringUtils.EMPTY);
076        }
077                personForm.setJobs(HrServiceLocator.getJobService().getJobs(HrContext.getTargetPrincipalId(), LocalDate.now()));
078                
079                //KPME-1441
080                PrincipalHRAttributes principalHRAttributes = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(personForm.getPrincipalId(), LocalDate.now());
081                if ( principalHRAttributes != null && principalHRAttributes.getServiceDate() != null ){
082                        personForm.setServiceDate(principalHRAttributes.getServiceDate().toString());
083                } else {
084                        personForm.setServiceDate("");
085                }
086                // KPME-1441
087                
088                if (principalHRAttributes != null && principalHRAttributes.getLeavePlan() != null) {
089                        List<AccrualCategory> accrualCategories = new ArrayList<AccrualCategory>();
090                        Map<String, BigDecimal> accrualCategoryRates = new HashMap<String, BigDecimal>();
091                    Map<String, String> accrualEarnIntervals = new HashMap<String, String>();
092                    Map<String, String> unitOfTime = new HashMap<String, String>();
093                        
094                        List<AccrualCategory> allAccrualCategories = HrServiceLocator.getAccrualCategoryService().getActiveLeaveAccrualCategoriesForLeavePlan(principalHRAttributes.getLeavePlan(), LocalDate.now());
095                    for (AccrualCategory accrualCategory : allAccrualCategories) {
096                                if (StringUtils.equalsIgnoreCase(accrualCategory.getHasRules(), "Y")) {
097                                        AccrualCategoryRule accrualCategoryRule = HrServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(accrualCategory, LocalDate.now(), principalHRAttributes.getServiceLocalDate());
098                                        if (accrualCategoryRule != null) {
099                                                accrualCategories.add(accrualCategory);
100                                                
101                                                accrualCategoryRates.put(accrualCategory.getAccrualCategory(), accrualCategoryRule.getAccrualRate());
102
103                                                for (AccrualEarnInterval entry : AccrualEarnInterval.values()) {
104                                            if (accrualCategory.getAccrualEarnInterval().equals(entry.getCode())) {
105                                                accrualEarnIntervals.put(accrualCategory.getAccrualCategory(), entry.getDescription());
106                                                break;
107                                            }
108                                        } 
109                                                
110                                                for (Map.Entry<String, String> entry : HrConstants.UNIT_OF_TIME.entrySet()) {                                                 
111                                            if (accrualCategory.getUnitOfTime().equals(entry.getKey()) ){
112                                                unitOfTime.put(accrualCategory.getAccrualCategory(), entry.getValue());
113                                                break;
114                                            }
115                                        } 
116                                        }
117                                }
118                        }
119                        personForm.setAccrualCategories(accrualCategories);
120                        personForm.setAccrualCategoryRates(accrualCategoryRates);
121                        personForm.setAccrualEarnIntervals(accrualEarnIntervals);
122                        personForm.setUnitOfTime(unitOfTime);
123                }
124                
125                setupRolesOnForm(personForm);
126
127                List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(HrContext.getTargetPrincipalId(), LocalDate.now());
128                
129                Map<Long, Set<Assignment>> jobNumberToListAssignments = new HashMap<Long,Set<Assignment>>();
130                Map<Long, Set<Person>> workAreaToApproverPerson = new HashMap<Long, Set<Person>>();
131        Map<String, Set<Person>> deptToDeptAdminPerson = new HashMap<String, Set<Person>>();
132        Map<String, Set<Person>> deptToPayrollPerson = new HashMap<String, Set<Person>>();
133                
134                for (Assignment assignment : assignments) {
135                        Set<Assignment> jobAssignments = jobNumberToListAssignments.get(assignment.getJobNumber());
136                        if (jobAssignments == null) {
137                                jobAssignments = new HashSet<Assignment>();
138                        }
139                        jobAssignments.add(assignment);
140                        jobNumberToListAssignments.put(assignment.getJobNumber(), jobAssignments);
141                        
142                        Set<Person> approvers = workAreaToApproverPerson.get(assignment.getWorkArea());
143                        if (approvers == null) {
144                                approvers = new TreeSet<Person>(new Comparator<Person>() {
145                                        @Override
146                                        public int compare(Person person1, Person person2) {
147                                                return ObjectUtils.compare(person1.getPrincipalId(), person2.getPrincipalId());
148                                        }
149                                });
150                        }
151                        approvers.addAll(getApprovers(assignment.getWorkArea()));
152                        workAreaToApproverPerson.put(assignment.getWorkArea(), approvers);
153                        
154                        Set<Person> departmentAdmins = deptToDeptAdminPerson.get(assignment.getDept());
155                        if (departmentAdmins == null) {
156                                departmentAdmins = new TreeSet<Person>(new Comparator<Person>() {
157                                        @Override
158                                        public int compare(Person person1, Person person2) {
159                                                return ObjectUtils.compare(person1.getPrincipalId(), person2.getPrincipalId());
160                                        }
161                                });
162                        }
163                        departmentAdmins.addAll(getDepartmentAdmins(assignment.getDept()));
164            deptToDeptAdminPerson.put(assignment.getDept(), departmentAdmins);
165
166            Set<Person> payrollProcessors = deptToPayrollPerson.get(assignment.getDept());
167            if (payrollProcessors == null) {
168                payrollProcessors = new TreeSet<Person>(new Comparator<Person>() {
169                    @Override
170                    public int compare(Person person1, Person person2) {
171                        return ObjectUtils.compare(person1.getPrincipalId(), person2.getPrincipalId());
172                    }
173                });
174            }
175            payrollProcessors.addAll(getPayrollProcessors(assignment.getDept()));
176            deptToPayrollPerson.put(assignment.getDept(), payrollProcessors);
177                }
178                
179                personForm.setJobNumberToListAssignments(jobNumberToListAssignments);
180                personForm.setWorkAreaToApproverPerson(workAreaToApproverPerson);
181        personForm.setDeptToDeptAdminPerson(deptToDeptAdminPerson);
182        personForm.setDeptToPayrollPerson(deptToPayrollPerson);
183                
184                return actForw;
185        }
186        
187        private void setupRolesOnForm(PersonInfoActionForm personInfoActionForm) {
188                String principalId = HrContext.getTargetPrincipalId();
189                DateTime date = LocalDate.now().toDateTimeAtStartOfDay();
190                Set<Long> allApproverWorkAreas = new HashSet<Long>();
191                allApproverWorkAreas.addAll(HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), date, true));
192                allApproverWorkAreas.addAll(HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), date, true));
193                personInfoActionForm.setApproverWorkAreas(new ArrayList<Long>(allApproverWorkAreas));
194                
195                List<Long> reviewerWorkAreas = HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), date, true);
196                personInfoActionForm.setReviewerWorkAreas(reviewerWorkAreas);
197                
198                Set<String> allViewOnlyDepartments = new HashSet<String>();
199                allViewOnlyDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_VIEW_ONLY.getRoleName(), date, true));
200                allViewOnlyDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_VIEW_ONLY.getRoleName(), date, true));
201                allViewOnlyDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_VIEW_ONLY.getRoleName(), date, true));
202                allViewOnlyDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_VIEW_ONLY.getRoleName(), date, true));
203                personInfoActionForm.setDeptViewOnlyDepts(new ArrayList<String>(allViewOnlyDepartments));
204                
205                Set<String> allAdministratorDepartments = new HashSet<String>();
206                allAdministratorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName(), date, true));
207                allAdministratorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName(), date, true));
208                allAdministratorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), date, true));
209                allAdministratorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), date, true));
210                personInfoActionForm.setDeptAdminDepts(new ArrayList<String>(allAdministratorDepartments));
211
212        Set<String> allPayrollProcessorDepartments = new HashSet<String>();
213        allPayrollProcessorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), date, true));
214        allPayrollProcessorDepartments.addAll(HrServiceLocator.getKPMERoleService().getDepartmentsForPrincipalInRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), date, true));
215        personInfoActionForm.setPayrollProcessorDepts(new ArrayList<String>(allPayrollProcessorDepartments));
216
217                Set<String> allAdministratorLocations = new HashSet<String>();
218                allAdministratorLocations.addAll(HrServiceLocator.getKPMERoleService().getLocationsForPrincipalInRole(principalId, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), date, true));
219                allAdministratorLocations.addAll(HrServiceLocator.getKPMERoleService().getLocationsForPrincipalInRole(principalId, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), date, true));
220                personInfoActionForm.setLocationAdminDepts(new ArrayList<String>(allAdministratorLocations));
221                
222                personInfoActionForm.setGlobalViewOnlyRoles(HrServiceLocator.getKPMEGroupService().isMemberOfSystemViewOnlyGroup(principalId, date));
223                personInfoActionForm.setSystemAdmin(HrServiceLocator.getKPMEGroupService().isMemberOfSystemAdministratorGroup(principalId, date));
224        }
225
226    private Set<Person> getDepartmentAdmins(String dept) {
227        Set<Person> departmentAdmins = new HashSet<Person>();
228        DateTime date = LocalDate.now().toDateTimeAtStartOfDay();
229        List<RoleMember> roleMembers = new ArrayList<RoleMember>();
230        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName(), dept, date, true));
231        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName(), dept, date, true));
232                
233        for (RoleMember roleMember : roleMembers) {
234                Person person = KimApiServiceLocator.getPersonService().getPerson(roleMember.getMemberId());
235                        if (person != null) {
236                                departmentAdmins.add(person);
237                        }
238        }
239        
240        return departmentAdmins;
241    }
242
243    private Set<Person> getPayrollProcessors(String dept) {
244        Set<Person> payrollProcs = new HashSet<Person>();
245        DateTime date = LocalDate.now().toDateTimeAtStartOfDay();
246        List<RoleMember> roleMembers = new ArrayList<RoleMember>();
247        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInDepartment(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), dept, date, true));
248
249        for (RoleMember roleMember : roleMembers) {
250            Person person = KimApiServiceLocator.getPersonService().getPerson(roleMember.getMemberId());
251            if (person != null) {
252                payrollProcs.add(person);
253            }
254        }
255
256        return payrollProcs;
257    }
258        
259        private Set<Person> getApprovers(Long workArea) {
260                Set<Person> approvers = new HashSet<Person>();
261        DateTime date = LocalDate.now().toDateTimeAtStartOfDay();
262                List<RoleMember> roleMembers = HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workArea, date, true);
263                
264                for (RoleMember roleMember : roleMembers) {
265                        Person person = KimApiServiceLocator.getPersonService().getPerson(roleMember.getMemberId());
266                        if (person != null) {
267                                approvers.add(person);
268                        }
269        }
270                
271                return approvers;
272        }
273
274
275}