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.lang.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.TKUser;
042    import org.kuali.hr.time.util.TKUtils;
043    import org.kuali.hr.time.util.TkConstants;
044    import org.kuali.rice.kim.api.identity.Person;
045    import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
046    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
047    
048    public class PersonInfoAction extends TkAction {
049    
050    
051    
052        public ActionForward showInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
053            return mapping.findForward("basic");
054        }
055    
056            @Override
057            public ActionForward execute(ActionMapping mapping, ActionForm form,
058                            HttpServletRequest request, HttpServletResponse response)
059                            throws Exception {
060                    ActionForward actForw =  super.execute(mapping, form, request, response);
061                    PersonInfoActionForm personForm = (PersonInfoActionForm)form;
062                    
063                    personForm.setPrincipalId(TKContext.getTargetPrincipalId());
064            EntityNamePrincipalName name = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(personForm.getPrincipalId());
065                    //Person person = KimApiServiceLocator.getPersonService().getPerson(personForm.getPrincipalId());
066                    if (name != null) {
067                personForm.setPrincipalName(name.getPrincipalName());
068                // set name
069                personForm.setName(name.getDefaultName() != null ? name.getDefaultName().getCompositeName() : StringUtils.EMPTY);
070            }
071                    personForm.setJobs(TkServiceLocator.getJobService().getJobs(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate()));
072                    
073                    //KPME-1441
074                    PrincipalHRAttributes principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(personForm.getPrincipalId(), TKUtils.getCurrentDate());
075                    if ( principalHRAttributes != null && principalHRAttributes.getServiceDate() != null ){
076                            personForm.setServiceDate(principalHRAttributes.getServiceDate().toString());
077                    } else {
078                            personForm.setServiceDate("");
079                    }
080                    // KPME-1441
081                    
082                    if (principalHRAttributes != null && principalHRAttributes.getLeavePlan() != null) {
083                            List<AccrualCategory> accrualCategories = new ArrayList<AccrualCategory>();
084                            Map<String, BigDecimal> accrualCategoryRates = new HashMap<String, BigDecimal>();
085                        Map<String, String> accrualEarnIntervals = new HashMap<String, String>();
086                        Map<String, String> unitOfTime = new HashMap<String, String>();
087                            
088                            List<AccrualCategory> allAccrualCategories = TkServiceLocator.getAccrualCategoryService().getActiveLeaveAccrualCategoriesForLeavePlan(principalHRAttributes.getLeavePlan(), TKUtils.getCurrentDate());
089                        for (AccrualCategory accrualCategory : allAccrualCategories) {
090                                    if (StringUtils.equalsIgnoreCase(accrualCategory.getHasRules(), "Y")) {
091                                            AccrualCategoryRule accrualCategoryRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(accrualCategory, TKUtils.getCurrentDate(), principalHRAttributes.getServiceDate());
092                                            if (accrualCategoryRule != null) {
093                                                    accrualCategories.add(accrualCategory);
094                                                    
095                                                    accrualCategoryRates.put(accrualCategory.getAccrualCategory(), accrualCategoryRule.getAccrualRate());
096    
097                                                    for (Map.Entry<String, String> entry : LMConstants.ACCRUAL_EARN_INTERVAL_MAP.entrySet()) {                                                    
098                                                if (accrualCategory.getAccrualEarnInterval().equals(entry.getKey())) {
099                                                    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    }