View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.lm.leavecode.validation;
17  
18  import java.sql.Date;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.hr.lm.accrual.AccrualCategory;
22  import org.kuali.hr.lm.leavecode.LeaveCode;
23  import org.kuali.hr.time.service.base.TkServiceLocator;
24  import org.kuali.hr.time.util.ValidationUtils;
25  import org.kuali.rice.kns.document.MaintenanceDocument;
26  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
27  import org.kuali.rice.krad.bo.PersistableBusinessObject;
28  
29  public class LeaveCodeValidation extends MaintenanceDocumentRuleBase {	 
30  	boolean validateEffectiveDate(Date effectiveDate) {
31  		boolean valid = true;
32  		if (effectiveDate != null && !ValidationUtils.validateOneYearFutureDate(effectiveDate)) {
33  			this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
34  			valid = false;
35  		}
36  		return valid;
37  	}
38  /*	KPME-1477
39  	boolean validateLeaveCodeRole(LeaveCode leaveCode) {
40  		boolean valid = true;
41  		if (!leaveCode.getEmployee() && !leaveCode.getDepartmentAdmin() && !leaveCode.getApprover()) {
42  			this.putFieldError("employee", "error.leavecode.role.required");
43  			valid = false;
44  		}
45  		return valid;
46  	}
47  */	
48  	//KPME-1541 we need to set the leave plan but not silently override if the user uses the Leave Plan field
49  	boolean validateLeavePlan(LeaveCode leaveCode) {
50  		
51  		boolean valid = true;
52  		
53  		
54  		if (StringUtils.isNotBlank(leaveCode.getLeavePlan())) {
55  
56  			if (!ValidationUtils.validateLeavePlan(leaveCode.getLeavePlan(), leaveCode.getEffectiveDate())) {
57  				this.putFieldError("leavePlan", "error.existence", "leavePlan '"
58  						+ leaveCode.getLeavePlan() + "'");
59  				valid = false;
60  				return valid;
61  			}
62  			
63  			if (leaveCode.getEffectiveDate() != null && StringUtils.isNotBlank(leaveCode.getAccrualCategory())) {
64  				AccrualCategory myTestAccrualCategoryObj =  TkServiceLocator.getAccrualCategoryService().getAccrualCategory(leaveCode.getAccrualCategory(), leaveCode.getEffectiveDate());
65  				if (!myTestAccrualCategoryObj.getLeavePlan().equals(leaveCode.getLeavePlan())) {
66  					this.putFieldError("leavePlan", "error.leaveCode.leavePlanMismatch", myTestAccrualCategoryObj.getLeavePlan());
67  					valid = false;
68  					return valid;
69  				}
70  				leaveCode.setLeavePlan(myTestAccrualCategoryObj.getLeavePlan());
71  			}
72  		}
73  		return valid;
74  	}
75  	
76  	//KPME-1541 must have an accrual category for leave plan lookup
77  	boolean validateAccrualCategory(String accrualCategory, Date asOfDate) {
78  		boolean valid = true;
79  		if (!ValidationUtils.validateAccCategory(accrualCategory, asOfDate)) {
80  			this.putFieldError("accrualCategory", "error.existence", "accrualCategory '"
81  					+ accrualCategory + "'");
82  			valid = false;
83  		}
84  		return valid;
85  	}
86  	
87  	boolean validateEarnCode(String earnCode, Date asOfDate) {
88  		boolean valid = true;
89  		if (!StringUtils.isEmpty(earnCode) && !ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
90  			this.putFieldError("earnCode", "error.existence", "earnCode '"
91  					+ earnCode + "'");
92  			valid = false;
93  		}
94  		return valid;
95  	}
96  	
97  	boolean validateDefaultAmountOfTime(Long defaultAmountofTime) {
98  		boolean valid = true;
99  		if ( defaultAmountofTime != null ){
100 			if (defaultAmountofTime.compareTo(new Long(24)) > 0  || defaultAmountofTime.compareTo(new Long(0)) < 0) {
101 				this.putFieldError("defaultAmountofTime", "error.leaveCode.hours", "Default Amount of Time '"
102 						+ defaultAmountofTime + "'");
103 				valid = false;
104 			}
105 		}
106 		return valid;
107 	}
108 	
109 	@Override
110 	protected boolean processCustomRouteDocumentBusinessRules(
111 			MaintenanceDocument document) {
112 		boolean valid = false;
113 		LOG.debug("entering custom validation for Leave Code");
114 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
115 		if (pbo instanceof LeaveCode) {
116 			LeaveCode leaveCode = (LeaveCode) pbo;
117 			if (leaveCode != null) {
118 				valid = true;
119 				//valid &= this.validateEffectiveDate(leaveCode.getEffectiveDate());
120 				//valid &= this.validateLeaveCodeRole(leaveCode); // xichen. remove this validation, this field is not on the page. KPME1477
121 				valid &= this.validateAccrualCategory(leaveCode.getAccrualCategory(), leaveCode.getEffectiveDate());
122 				valid &= this.validateEarnCode(leaveCode.getEarnCode(), leaveCode.getEffectiveDate());
123 				valid &= this.validateDefaultAmountOfTime(leaveCode.getDefaultAmountofTime());
124 				valid &= this.validateLeavePlan(leaveCode); //validate accrual category and effdt before calling this
125 			}
126 		}
127 		return valid;
128 	}
129 }