View Javadoc

1   /**
2    * Copyright 2004-2014 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.kpme.tklm.common;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Set;
21  import java.util.TreeSet;
22  
23  import org.apache.commons.collections.CollectionUtils;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.struts.action.ActionForm;
26  import org.hsqldb.lib.StringUtil;
27  import org.joda.time.DateTime;
28  import org.joda.time.LocalDate;
29  import org.kuali.kpme.core.KPMENamespace;
30  import org.kuali.kpme.core.role.KPMERole;
31  import org.kuali.kpme.core.service.HrServiceLocator;
32  import org.kuali.kpme.core.util.HrContext;
33  import org.kuali.kpme.core.web.KPMEAction;
34  import org.kuali.kpme.core.workarea.WorkArea;
35  import org.kuali.rice.kim.api.role.RoleService;
36  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
37  
38  public abstract class ApprovalFormAction extends KPMEAction {
39  	
40  	protected void setSearchFields(ApprovalForm approvalForm) {
41  		String principalId = HrContext.getTargetPrincipalId();
42  //        String principalId = GlobalVariables.getUserSession().getPrincipalId();
43  		LocalDate currentDate = LocalDate.now();
44          DateTime currentDateTime = currentDate.toDateTimeAtStartOfDay();
45  
46          List<String> roleIds = new ArrayList<String>();
47          RoleService roleService = KimApiServiceLocator.getRoleService();
48          roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName()));
49          roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName()));
50          roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName()));
51          roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName()));
52          roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName()));
53  
54          Set<Long> workAreas = new TreeSet<Long>();
55          //TODO: create permission for approval, and get all roles with that permission
56          workAreas.addAll(HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRoles(principalId, roleIds, currentDateTime, true));
57          if (CollectionUtils.isEmpty(approvalForm.getPayCalendarGroups())) {
58              List<String> principalIds = HrServiceLocator.getAssignmentService().getPrincipalIdsInActiveAssignmentsForWorkAreas(new ArrayList<Long>(workAreas), currentDate);
59  	
60  	        List<String> calendarGroups = new ArrayList<String>();
61  	        if (CollectionUtils.isNotEmpty(principalIds)) {
62  	            calendarGroups = getCalendars(principalIds);
63  	        }
64  	        if (StringUtils.isEmpty(approvalForm.getSelectedPayCalendarGroup())) {
65  	        	approvalForm.setSelectedPayCalendarGroup(CollectionUtils.isNotEmpty(calendarGroups) ? calendarGroups.get(0) : null);
66  	        }
67  	        approvalForm.setPayCalendarGroups(calendarGroups);
68  		}
69  
70          List<WorkArea> workAreasWithoutRoles = HrServiceLocator.getWorkAreaService().getWorkAreasWithoutRoles(new ArrayList<Long>(workAreas), currentDate);
71  		if (CollectionUtils.isEmpty(approvalForm.getDepartments())) {
72  			Set<String> departments = new TreeSet<String>();
73  			
74  
75              for (WorkArea workArea : workAreasWithoutRoles) {
76  			    departments.add(workArea.getDept());
77  			}
78  			approvalForm.setDepartments(new ArrayList<String>(departments));
79              if (StringUtils.isEmpty(approvalForm.getSelectedDept())) {
80  			    approvalForm.setSelectedDept(CollectionUtils.isNotEmpty(approvalForm.getDepartments()) ? approvalForm.getDepartments().get(0) : null);
81              }
82  		}
83  
84  
85          //have depts in approvalForm.getDepartments
86          //have workareas too...
87  
88  		approvalForm.getWorkAreaDescr().clear();
89      	//List<Long> workAreaIds = HrServiceLocator.getWorkAreaService().getWorkAreasForDepartment(approvalForm.getSelectedDept(), currentDate);
90          //List<WorkArea> workAreaObjs = HrServiceLocator.getWorkAreaService().getWorkAreasWithoutRoles(workAreaIds, currentDate);
91          for (WorkArea workAreaObj : workAreasWithoutRoles) {
92              //only want workareas in selected department
93              if (StringUtils.equals(workAreaObj.getDept(), approvalForm.getSelectedDept())) {
94                  Long workArea = workAreaObj.getWorkArea();
95                  //String department = workAreaObj.getDept();
96                  String description = workAreaObj.getDescription();
97  
98                  //TODO :  this if statement, in this loop, is not a good thing... lots of stuff happening here.
99                  //if (HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), workArea, currentDateTime)
100                 //        || HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workArea, currentDateTime)
101                 //        || HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workArea, currentDateTime)
102                 //        || HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), department, currentDateTime)
103                 //        || HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), department, currentDateTime)) {
104                     approvalForm.getWorkAreaDescr().put(workArea, description + "(" + workArea + ")");
105                 //}
106             }
107         }
108 	}
109 	
110 	protected abstract List<String> getCalendars(List<String> principalIds);
111 	
112 	protected List<String> getWorkAreas(ActionForm form) {
113 		ApprovalForm approvalForm = (ApprovalForm) form;
114 		
115 		List<String> workAreas = new ArrayList<String>();
116 		
117 	    if (StringUtil.isEmpty(approvalForm.getSelectedWorkArea())) {
118 	    	for (Long workAreaKey : approvalForm.getWorkAreaDescr().keySet()) {
119 	    		workAreas.add(workAreaKey.toString());
120 	    	}
121 	    } else {
122 	    	workAreas.add(approvalForm.getSelectedWorkArea());
123 	    }
124 	    
125 	    return workAreas;
126 	}
127 
128 }