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.leave.payout.web;
17  
18  import org.joda.time.DateTime;
19  import org.joda.time.LocalDate;
20  import org.kuali.kpme.core.api.assignment.Assignment;
21  import org.kuali.kpme.core.api.department.Department;
22  import org.kuali.kpme.core.api.job.Job;
23  import org.kuali.kpme.core.api.namespace.KPMENamespace;
24  import org.kuali.kpme.core.lookup.KPMELookupableHelperServiceImpl;
25  import org.kuali.kpme.core.role.KPMERole;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.core.util.HrContext;
28  import org.kuali.kpme.core.util.TKUtils;
29  import org.kuali.kpme.tklm.leave.payout.LeavePayout;
30  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
31  import org.kuali.rice.kns.lookup.HtmlData;
32  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
33  import org.kuali.rice.krad.bo.BusinessObject;
34  import org.kuali.rice.krad.util.KRADConstants;
35  import org.kuali.rice.krad.util.UrlFactory;
36  
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Properties;
41  
42  @SuppressWarnings("deprecation")
43  public class LeavePayoutLookupableHelperServiceImpl extends KPMELookupableHelperServiceImpl {
44  
45  	private static final long serialVersionUID = -2654300078605742618L;
46  
47  	@Override
48  	@SuppressWarnings("rawtypes")
49      public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
50  		List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
51  
52  		LeavePayout lp = (LeavePayout) businessObject;
53  		String lmLeavePayoutId = lp.getLmLeavePayoutId();
54  		
55  		Properties params = new Properties();
56  		params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
57  		params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
58  		params.put("lmLeavePayoutId", lmLeavePayoutId);
59  		AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
60  		viewUrl.setDisplayText("view");
61  		viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
62  		customActionUrls.add(viewUrl);
63  		
64  		return customActionUrls;
65      }
66  
67      @Override
68      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
69          String principalId = fieldValues.get("principalId");
70          String fromAccrualCategory = fieldValues.get("fromAccrualCategory");
71          String payoutAmount = fieldValues.get("payoutAmount");
72          String earnCode = fieldValues.get("earnCode");
73          String forfeitedAmount = fieldValues.get("forfeitedAmount");
74          String fromEffdt = TKUtils.getFromDateString(fieldValues.get("effectiveDate"));
75          String toEffdt = TKUtils.getToDateString(fieldValues.get("effectiveDate"));
76  
77          List<LeavePayout> payouts =  LmServiceLocator.getLeavePayoutService().getLeavePayouts(principalId, fromAccrualCategory, payoutAmount, earnCode, forfeitedAmount, 
78          									TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt));
79          payouts = filterByPrincipalId(payouts);
80          
81          return payouts;
82      }
83      
84  	private List<LeavePayout> filterByPrincipalId(
85  			List<LeavePayout> payouts) {
86  		if(!payouts.isEmpty()) {
87  			Iterator<? extends BusinessObject> iter = payouts.iterator();
88  			while(iter.hasNext()) {
89  				LeavePayout payout = (LeavePayout) iter.next();
90  				LocalDate effectiveLocalDate = payout.getEffectiveLocalDate();
91  				DateTime effectiveDate = effectiveLocalDate.toDateTimeAtStartOfDay();
92  				String principalId = payout.getPrincipalId();
93  				List<Job> principalsJobs = HrServiceLocator.getJobService().getActiveLeaveJobs(principalId, effectiveLocalDate);
94  				String userPrincipalId = HrContext.getPrincipalId();
95  				boolean canView = false;
96  
97                  //TODO - performance, get a job/dept map in 1 query
98  				for(Job job : principalsJobs) {
99  					
100 					if(job.isEligibleForLeave()) {
101 						
102 						String department = job.getDept();
103 						String groupKeyCode = job.getGroupKeyCode();
104 						Department departmentObj = HrServiceLocator.getDepartmentService().getDepartment(department, groupKeyCode, effectiveLocalDate);
105 						String location = departmentObj != null ? departmentObj.getGroupKey().getLocationId() : null;
106 			        	if (LmServiceLocator.getLMPermissionService().isAuthorizedInDepartment(userPrincipalId, "View Leave Payout", department, groupKeyCode, effectiveDate)
107 							|| LmServiceLocator.getLMPermissionService().isAuthorizedInLocation(userPrincipalId, "View Leave Payout", location, effectiveDate)) {
108 								canView = true;
109 								break;
110 						}
111 			        	else {
112 			        		//do NOT block approvers, processors, delegates from approving the document.
113 							List<Assignment> assignments = HrServiceLocator.getAssignmentService().getActiveAssignmentsForJob(principalId, job.getJobNumber(), effectiveLocalDate);
114 							for(Assignment assignment : assignments) {
115 								if(HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(userPrincipalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), assignment.getWorkArea(), new DateTime(effectiveDate))
116 										|| HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(userPrincipalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), assignment.getWorkArea(), new DateTime(effectiveDate))
117 										|| HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(userPrincipalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), assignment.getDept(), assignment.getGroupKeyCode(), new DateTime(effectiveDate))
118 										|| HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(userPrincipalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), assignment.getDept(), assignment.getGroupKeyCode(), new DateTime(effectiveDate))) {
119 									canView = true;
120 									break;
121 								}
122 							}
123 			        	}
124 					}
125 				}
126 				if(!canView) {
127 					iter.remove();
128 				}
129 			}
130 			
131 
132 		}
133 
134 		return payouts;
135 	}
136 
137 }