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.time.earncode.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.lm.accrual.AccrualCategory;
20  import org.kuali.hr.time.earncode.EarnCode;
21  import org.kuali.hr.time.service.base.TkServiceLocator;
22  import org.kuali.hr.time.timeblock.TimeBlock;
23  import org.kuali.hr.time.util.TkConstants;
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  
28  import java.math.BigDecimal;
29  import java.sql.Date;
30  import java.util.List;
31  
32  public class EarnCodeValidation extends MaintenanceDocumentRuleBase{
33  	
34  	boolean validateRollupToEarnCode(String earnCode, Date asOfDate) {
35  		boolean valid = true;
36  		if (!StringUtils.isEmpty(earnCode) && !ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
37  			this.putFieldError("rollupToEarnCode", "earncode.rollupToEarnCode.notfound", "Roll up to Earn code "
38  					+ earnCode + "'");
39  			valid = false;
40  		}
41  		return valid;
42  	}
43  	
44  	boolean validateDefaultAmountOfTime(Long defaultAmountofTime) {
45  		boolean valid = true;
46  		if ( defaultAmountofTime != null ){
47  			if (defaultAmountofTime.compareTo(24L) > 0  || defaultAmountofTime.compareTo(0L) < 0) {
48  				this.putFieldError("defaultAmountofTime", "error.leaveCode.hours", "Default Amount of Time '"
49  						+ defaultAmountofTime + "'");
50  				valid = false;
51  			}
52  		}
53  		return valid;
54  	}
55  	
56  	boolean validateRecordMethod(String recordMethod, String accrualCategory, Date asOfDate){
57  		boolean valid = true;
58  		if(recordMethod != null) {
59  			if(StringUtils.isNotEmpty(accrualCategory)) {
60  				valid = ValidationUtils.validateRecordMethod(recordMethod, accrualCategory, asOfDate);
61  				if(!valid) {
62  					this.putFieldError("recordMethod", "earncode.recordMethod.invalid", "Record Method");
63  				}
64  			}
65  		}
66  		return valid;
67  	}
68  	
69  	boolean validateLeavePlan(EarnCode earnCode) {
70  		
71  		boolean valid = true;
72  		
73  		if (StringUtils.isNotBlank(earnCode.getLeavePlan())) {
74  
75  			if (!ValidationUtils.validateLeavePlan(earnCode.getLeavePlan(), earnCode.getEffectiveDate())) {
76  				this.putFieldError("leavePlan", "error.existence", "leavePlan '"
77  						+ earnCode.getLeavePlan() + "'");
78  				valid = false;
79  				return valid;
80  			}
81  			
82  			if (earnCode.getEffectiveDate() != null && StringUtils.isNotBlank(earnCode.getAccrualCategory())) {
83  				AccrualCategory myTestAccrualCategoryObj =  TkServiceLocator.getAccrualCategoryService().getAccrualCategory(earnCode.getAccrualCategory(), earnCode.getEffectiveDate());
84  				if(myTestAccrualCategoryObj != null) {
85  					if (!myTestAccrualCategoryObj.getLeavePlan().equals(earnCode.getLeavePlan())) {
86  						this.putFieldError("leavePlan", "error.leaveCode.leavePlanMismatch", myTestAccrualCategoryObj.getLeavePlan());
87  						valid = false;
88  						return valid;
89  					}
90  					earnCode.setLeavePlan(myTestAccrualCategoryObj.getLeavePlan());
91  				} 
92  			}
93  		}
94  		return valid;
95  	}
96  	
97  	@Override
98  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
99  		EarnCode earnCode = (EarnCode)this.getNewBo();
100 		EarnCode oldEarnCode = (EarnCode)this.getOldBo();
101 		if ((StringUtils.equals(oldEarnCode.getEarnCode(), TkConstants.LUNCH_EARN_CODE) 
102 				|| StringUtils.equals(oldEarnCode.getEarnCode(), TkConstants.HOLIDAY_EARN_CODE))
103 					&& !earnCode.isActive()) {
104 			this.putFieldError("active", "earncode.inactivate.locked", earnCode
105 					.getEarnCode());
106 		}
107 		//if earn code is not designated how to record then throw error
108 		if (earnCode.getHrEarnCodeId() == null) {
109 			if (ValidationUtils.validateEarnCode(earnCode.getEarnCode(), null)) {
110 				// If there IS an earn code, ie, it is valid, we need to report
111 				// an error as earn codes must be unique.			
112 				this.putFieldError("earnCode", "earncode.earncode.unique");
113 				return false;
114 			}
115 		}
116 		
117 		 //check if the effective date of the leave plan is prior to effective date of the earn code 
118 		 //accrual category is an optional field
119 		if(StringUtils.isNotEmpty(earnCode.getLeavePlan())){
120 			boolean valid = validateLeavePlan(earnCode);
121 			if(!valid) {
122 				return false;
123 			}
124 		}
125 		//check if the effective date of the accrual category is prior to effective date of the earn code 
126 		//accrual category is an optional field
127 		if(StringUtils.isNotEmpty(earnCode.getAccrualCategory())){
128 			if (!ValidationUtils.validateAccrualCategory(earnCode.getAccrualCategory(), earnCode.getEffectiveDate())) {
129 				this.putFieldError("accrualCategory", "earncode.accrualCategory.invalid", new String[]{earnCode.getAccrualCategory(),earnCode.getLeavePlan()});
130 				return false;
131 			}
132 		}
133 		
134 		// check if there's a newer version of the Earn Code
135 		int count = TkServiceLocator.getEarnCodeService().getNewerEarnCodeCount(earnCode.getEarnCode(), earnCode.getEffectiveDate());
136 		if(count > 0) {
137 			this.putFieldError("effectiveDate", "earncode.effectiveDate.newer.exists");
138 			return false;
139 		}
140 		
141 		// kpme-937 can not deactivate an earn code if it used in active timeblocks
142 		List<TimeBlock> latestEndTimestampTimeBlocks =  TkServiceLocator.getTimeBlockService().getLatestEndTimestampForEarnCode(earnCode.getEarnCode());
143 		if ( !earnCode.isActive() && !latestEndTimestampTimeBlocks.isEmpty() && earnCode.getEffectiveDate().before(latestEndTimestampTimeBlocks.get(0).getEndDate()) ){
144 			this.putFieldError("earnCode", "earncode.earncode.inactivate", earnCode.getEarnCode());
145 			return false;
146 		}
147 		
148 		if(!(this.validateDefaultAmountOfTime(earnCode.getDefaultAmountofTime()))) {
149 			return false;
150 		}
151 		
152 		if(earnCode.getRollupToEarnCode() != null && !StringUtils.isEmpty(earnCode.getRollupToEarnCode())) {
153 			if(!(this.validateRollupToEarnCode(earnCode.getRollupToEarnCode(), earnCode.getEffectiveDate()))) {
154 				return false;
155 			}
156 		}
157 		
158 		if(!validateRecordMethod(earnCode.getRecordMethod(), earnCode.getAccrualCategory(), earnCode.getEffectiveDate())) {
159 			return false;
160 		}
161 		
162 		return true;
163 	}
164 
165 }