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.time.earncode.EarnCode;
20  import org.kuali.hr.time.service.base.TkServiceLocator;
21  import org.kuali.hr.time.timeblock.TimeBlock;
22  import org.kuali.hr.time.util.TkConstants;
23  import org.kuali.hr.time.util.ValidationUtils;
24  import org.kuali.rice.kns.document.MaintenanceDocument;
25  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
26  
27  import java.math.BigDecimal;
28  import java.sql.Date;
29  import java.util.List;
30  
31  public class EarnCodeValidation extends MaintenanceDocumentRuleBase{
32  	
33  	@Override
34  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
35  		EarnCode earnCode = (EarnCode)this.getNewBo();
36  		EarnCode oldEarnCode = (EarnCode)this.getOldBo();
37  		if ((StringUtils.equals(oldEarnCode.getEarnCode(), TkConstants.LUNCH_EARN_CODE) 
38  				|| StringUtils.equals(oldEarnCode.getEarnCode(), TkConstants.HOLIDAY_EARN_CODE))
39  					&& !earnCode.isActive()) {
40  			this.putFieldError("active", "earncode.inactivate.locked", earnCode
41  					.getEarnCode());
42  		}
43  		//if earn code is not designated how to record then throw error
44  		if (earnCode.getHrEarnCodeId() == null) {
45  			if (ValidationUtils.validateEarnCode(earnCode.getEarnCode(), null)) {
46  				// If there IS an earn code, ie, it is valid, we need to report
47  				// an error as earn codes must be unique.			
48  				this.putFieldError("earnCode", "earncode.earncode.unique");
49  				return false;
50  			}
51  		}
52  		
53  //		if(earnCode.getRecordAmount() == false && 
54  //			earnCode.getRecordHours() == false && 
55  //			earnCode.getRecordTime() == false){
56  //			this.putFieldError("recordTime", "earncode.record.not.specified");
57  //			return false;
58  //		}
59  //		//confirm that only one of the check boxes is checked
60  //		//if more than one are true then throw an error
61  //		if(earnCode.getRecordAmount() && earnCode.getRecordHours() && earnCode.getRecordTime()){
62  //			this.putFieldError("recordTime", "earncode.record.unique");
63  //			return false;
64  //		}
65  //		boolean result = earnCode.getRecordAmount() ^ earnCode.getRecordHours() ^ earnCode.getRecordTime();
66  //		if(!result){
67  //			this.putFieldError("recordTime", "earncode.record.unique");
68  //			return false;
69  //		}
70  
71  		//check if the effective date of the accrual category is prior to effective date of the earn code 
72  		//accrual category is an optional field
73  		if(StringUtils.isNotEmpty(earnCode.getAccrualCategory())){
74  			if (!ValidationUtils.validateAccrualCategory(earnCode.getAccrualCategory(), earnCode.getEffectiveDate())) {
75  				this.putFieldError("accrualCategory", "earncode.accrualCategory.invalid", earnCode.getAccrualCategory());
76  				return false;
77  			}
78  		}
79  		
80  		// check if there's a newer version of the Earn Code
81  		int count = TkServiceLocator.getEarnCodeService().getNewerEarnCodeCount(earnCode.getEarnCode(), earnCode.getEffectiveDate());
82  		if(count > 0) {
83  			this.putFieldError("effectiveDate", "earncode.effectiveDate.newer.exists");
84  			return false;
85  		}
86  		
87  //		//check if the ovtEarnCode and InflateMinHours is equal to 0
88  //		if((earnCode.getRecordAmount() || earnCode.getOvtEarnCode()) && earnCode.getInflateMinHours().compareTo(new BigDecimal(0))!=0){
89  //			this.putFieldError("inflateMinHours", "earncode.inflateminhours.should.be.zero");
90  //			return false;
91  //		}
92  //		
93  //		//check if the RecordAmount and AccrualCategory has no value
94  //		if(earnCode.getRecordAmount() && StringUtils.isNotBlank(earnCode.getAccrualCategory())){
95  //			this.putFieldError("accrualCategory", "earncode.accrualcategory.should.be.blank");
96  //			return false;
97  //		}
98  //		
99  //		//check if the RecordAmount and InflateFactor is equal to 1
100 //		if(earnCode.getRecordAmount() && (earnCode.getInflateFactor().compareTo(new BigDecimal(1))!=0)){
101 //			this.putFieldError("inflateFactor", "earncode.inflatefactor.should.be.one");
102 //			return false;
103 //		}
104 		
105 		// kpme-937 can not inactivation of a earn code if it used in active timeblocks
106 		List<TimeBlock> latestEndTimestampTimeBlocks =  TkServiceLocator.getTimeBlockService().getLatestEndTimestamp();
107 		
108 		if ( !earnCode.isActive() && earnCode.getEffectiveDate().before(latestEndTimestampTimeBlocks.get(0).getEndDate()) ){
109 			List<TimeBlock> activeTimeBlocks = TkServiceLocator.getTimeBlockService().getTimeBlocksWithEarnCode(earnCode.getEarnCode(), earnCode.getEffectiveDate());
110 			if(activeTimeBlocks != null && !activeTimeBlocks.isEmpty()) {
111 				this.putFieldError("earnCode", "earncode.earncode.inactivate", earnCode.getEarnCode());
112 				return false;
113 			}
114 		}
115 		
116 		return true;
117 	}
118 
119 }