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