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.override.validation;
17  
18  import org.apache.cxf.common.util.StringUtils;
19  import org.kuali.kpme.core.accrualcategory.AccrualCategory;
20  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
21  import org.kuali.kpme.core.service.HrServiceLocator;
22  import org.kuali.kpme.tklm.leave.override.EmployeeOverride;
23  import org.kuali.rice.kns.document.MaintenanceDocument;
24  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
25  import org.kuali.rice.krad.bo.PersistableBusinessObject;
26  
27  public class EmployeeOverrideRule extends MaintenanceDocumentRuleBase{
28  	@Override
29  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
30  		boolean valid = true;
31  		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
32  		if (pbo instanceof EmployeeOverride) {
33  			EmployeeOverride eo = (EmployeeOverride) pbo;
34  			valid &= this.validatePrincipalHRAttributes(eo);
35  			valid &= this.validateAccrualCategory(eo);
36  			valid &= this.validateLeavePlan(eo);
37  		}
38  		return valid;
39  	}
40  	
41  	boolean validateLeavePlan(EmployeeOverride eo) {
42  		if(StringUtils.isEmpty(eo.getLeavePlan())) {
43  			this.putFieldError("leavePlan", "error.employeeOverride.leavePlan.notfound");
44  			return false;
45  		} else if(eo.getEffectiveDate() != null) {
46  			AccrualCategory ac = HrServiceLocator.getAccrualCategoryService().
47  				getAccrualCategory(eo.getAccrualCategory(), eo.getEffectiveLocalDate());
48  			PrincipalHRAttributes pha = HrServiceLocator.getPrincipalHRAttributeService().
49  				getPrincipalCalendar(eo.getPrincipalId(), eo.getEffectiveLocalDate());
50  			if(ac != null && pha != null && !ac.getLeavePlan().equals(pha.getLeavePlan())) {
51  				this.putFieldError("leavePlan", "error.employeeOverride.leavePlan.inconsistent", eo.getAccrualCategory() );
52  				return false;
53  			}
54  		}
55  		return true;
56  	}
57  	
58  	boolean validateAccrualCategory(EmployeeOverride eo) {
59  		if(eo.getAccrualCategory() != null && eo.getEffectiveDate() != null) {
60  			AccrualCategory ac = HrServiceLocator.getAccrualCategoryService().
61  				getAccrualCategory(eo.getAccrualCategory(), eo.getEffectiveLocalDate());
62  			if(ac == null) {
63  				this.putFieldError("accrualCategory", "error.employeeOverride.accrualCategory.notfound", eo.getAccrualCategory() );
64  				return false;
65  			}
66  		}
67  		return true;
68  	}
69  	
70  	boolean validatePrincipalHRAttributes(EmployeeOverride eo) {
71  		if(eo.getPrincipalId() != null && eo.getEffectiveDate() != null) {
72  			PrincipalHRAttributes pha = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(eo.getPrincipalId(), eo.getEffectiveLocalDate());
73  			if(pha == null) {
74  				this.putFieldError("principalId", "error.employeeOverride.principalHRAttributes.notfound", eo.getPrincipalId() );
75  				return false;
76  			}
77  		}
78  		return true;
79  	}
80  
81  }