View Javadoc
1   /**
2    * Copyright 2004-2015 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.adjustment.validation;
17  
18  import java.math.BigDecimal;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.joda.time.DateTime;
23  import org.joda.time.LocalDate;
24  import org.kuali.kpme.core.api.earncode.EarnCodeContract;
25  import org.kuali.kpme.core.api.job.Job;
26  import org.kuali.kpme.core.api.namespace.KPMENamespace;
27  import org.kuali.kpme.core.role.KPMERole;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.util.HrContext;
30  import org.kuali.kpme.core.util.ValidationUtils;
31  import org.kuali.kpme.tklm.leave.adjustment.LeaveAdjustment;
32  import org.kuali.rice.kns.document.MaintenanceDocument;
33  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
34  import org.kuali.rice.krad.bo.PersistableBusinessObject;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  public class LeaveAdjustmentValidation extends MaintenanceDocumentRuleBase{
38  	
39  	boolean validateLeavePlan(String leavePlan, LocalDate asOfDate) {
40  		boolean valid = true;
41  		if (!ValidationUtils.validateLeavePlan(leavePlan, asOfDate)) {
42  			this.putFieldError("leavePlan", "error.existence", "leavePlan '"
43  					+ leavePlan + "'");
44  			valid = false;
45  		}
46  		return valid;
47  	}
48  	
49  	boolean validateEarnCode(String earnCode, String accrualCategory, LocalDate asOfDate) {
50  		boolean valid = true;
51  		if (!ValidationUtils.validateEarnCodeOfAccrualCategory(earnCode, accrualCategory, asOfDate)) {
52  			this.putFieldError("earnCode", "error.earnCode.accrualCategory.mismatch", 
53  					earnCode);
54  			valid = false;
55  		}
56  		return valid;
57  	}
58  
59  	boolean validatePrincipal(String principalId) {
60  		boolean valid = true;
61  		if(principalId != null && StringUtils.isNotEmpty(principalId)) {
62  			if(StringUtils.equals(principalId, GlobalVariables.getUserSession().getPrincipalId())){
63  				this.putFieldError("principalId", "error.cannot.modify.own.balance",
64  						"principalId '" + principalId + "'");
65  				valid = false;
66  			}
67  			if(valid){
68  				if (!ValidationUtils.validatePrincipalId(principalId)) {
69  					this.putFieldError("principalId", "error.existence",
70  							"principalId '" + principalId + "'");
71  					valid = false;
72  				}
73  			}
74  		}
75  		return valid;
76  	}
77  
78  	boolean validateAccrualCategory(String accrualCategory, LocalDate asOfDate, String principalId) {
79  		boolean valid = true;
80  		if (!ValidationUtils.validateAccCategory(accrualCategory, principalId, asOfDate)) {
81  			this.putFieldError("accrualCategory", "error.existence", "accrualCategory '"
82  					+ accrualCategory + "'");
83  			valid = false;
84  		}
85  		return valid;
86  	}
87  
88      boolean validateDepartmentAdmin(String principalId) {
89  
90          boolean valid = false;
91          String LoggedInPrincipalID = GlobalVariables.getUserSession().getPrincipalId();
92          LocalDate loggedInDay =  LocalDate.now();
93          DateTime asOfDate =  LocalDate.now().toDateTimeAtStartOfDay();
94  
95          if(principalId != null && StringUtils.isNotEmpty(principalId)) {
96              List<Job> targetUserJob = HrServiceLocator.getJobService().getActiveLeaveJobs(principalId, loggedInDay);
97  
98              if(!targetUserJob.isEmpty()) {
99              //the target user should have at least one job and not have more than one leave eligible dept
100                 String targetUserDept = targetUserJob.get(0).getDept();
101                 String targetUserGroupKeyCode = targetUserJob.get(0).getGroupKeyCode();
102                   
103                   String targetUserLocation = "";
104                   if( targetUserJob.get(0).getGroupKey() != null ){
105                 	  targetUserLocation = targetUserJob.get(0).getGroupKey().getLocationId();
106                   }
107                   //check to see if the logged in user is the dept admin for the leave adjustment target user's dept
108                   if(HrContext.isSystemAdmin() 
109                 		|| HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(LoggedInPrincipalID, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName(), targetUserDept, targetUserGroupKeyCode, asOfDate)
110                         || HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(LoggedInPrincipalID, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName(), targetUserDept, targetUserGroupKeyCode, asOfDate)
111                         || HrServiceLocator.getKPMERoleService().principalHasRoleInLocation(LoggedInPrincipalID, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), targetUserLocation, asOfDate)
112                         || HrServiceLocator.getKPMERoleService().principalHasRoleInLocation(LoggedInPrincipalID, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), targetUserLocation, asOfDate))
113                   {
114                       valid = true;
115                   }
116             }//List
117         }
118         if(!valid) {
119         	GlobalVariables.getMessageMap().putError("document.newMaintainableObject.principalId", "principal.is.not.dept.admin");
120         }
121         return valid;
122     }
123 	
124 	private boolean validateFraction(String earnCode, BigDecimal amount, LocalDate asOfDate) {
125 		boolean valid = true;
126 		if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
127 			EarnCodeContract ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
128 			if(ec != null && ec.getFractionalTimeAllowed() != null) {
129 				BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
130 				String[] parameters = new String[2];
131 				parameters[0] = earnCode;
132 				parameters[1] = Integer.toString(fracAllowed.scale());
133 				this.putFieldError("adjustmentAmount", "error.amount.fraction", parameters);
134 				valid = false;
135 			 }
136 		}
137 		return valid;
138 	}
139 		
140 	@Override
141 	protected boolean processCustomRouteDocumentBusinessRules(
142 			MaintenanceDocument document) {
143 		boolean valid = false;
144 
145 		LOG.debug("entering custom validation for LeaveAdjustment");
146 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
147 		if (pbo instanceof LeaveAdjustment) {
148 			LeaveAdjustment leaveAdjustment = (LeaveAdjustment) pbo;
149 
150 			if (leaveAdjustment != null) {
151 				valid = true;
152 				if(leaveAdjustment.getPrincipalId() != null) {
153 					valid &= this.validatePrincipal(leaveAdjustment.getPrincipalId());
154                     valid &= this.validateDepartmentAdmin(leaveAdjustment.getPrincipalId());
155 				}
156 				if(leaveAdjustment.getAccrualCategory() != null) {
157 					valid &= this.validateAccrualCategory(leaveAdjustment.getAccrualCategory(),leaveAdjustment.getEffectiveLocalDate(), leaveAdjustment.getPrincipalId());
158 				}
159 				if(leaveAdjustment.getLeavePlan() != null) {
160 					valid &= this.validateLeavePlan(leaveAdjustment.getLeavePlan(), leaveAdjustment.getEffectiveLocalDate());
161 				}
162 				if(leaveAdjustment.getEarnCode() != null) {
163 					valid &= this.validateEarnCode(leaveAdjustment.getEarnCode(), leaveAdjustment.getAccrualCategory(), leaveAdjustment.getEffectiveLocalDate());
164 					if(leaveAdjustment.getAdjustmentAmount() != null) {
165 						valid &= this.validateFraction(leaveAdjustment.getEarnCode(), leaveAdjustment.getAdjustmentAmount(), leaveAdjustment.getEffectiveLocalDate());
166 					}
167 				}
168 			}
169 		}
170 
171 		return valid;
172 	}
173 
174 }