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.timeoff.validation;
17  
18  import java.math.BigDecimal;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.joda.time.LocalDate;
22  import org.kuali.kpme.core.api.earncode.EarnCodeContract;
23  import org.kuali.kpme.core.service.HrServiceLocator;
24  import org.kuali.kpme.core.util.HrConstants;
25  import org.kuali.kpme.core.util.ValidationUtils;
26  import org.kuali.kpme.tklm.common.LMConstants;
27  import org.kuali.kpme.tklm.leave.timeoff.SystemScheduledTimeOff;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
30  import org.kuali.rice.krad.bo.PersistableBusinessObject;
31  
32  public class SystemScheduledTimeOffValidation extends MaintenanceDocumentRuleBase {	 
33  	boolean validateEffectiveDate(LocalDate effectiveDate) {
34  		boolean valid = true;
35  		if (effectiveDate != null && !ValidationUtils.validateOneYearFutureDate(effectiveDate)) {
36  			this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
37  			valid = false;
38  		}
39  		return valid;
40  	}
41  
42  	boolean validateAccruedDate(LocalDate accruedDate) {
43  		boolean valid = true;
44  		if (accruedDate != null && !ValidationUtils.validateFutureDate(accruedDate)) {
45  			this.putFieldError("accruedDate", "error.future.date", "Accrued Date");
46  			valid = false;
47  		}
48  		return valid;
49  	}
50  	
51  	boolean validateLocation(String location, LocalDate asOfDate) {
52  		boolean valid = true;
53  		if(!StringUtils.isEmpty(location) && location.equals(HrConstants.WILDCARD_CHARACTER)) {
54  			return true;
55  		}
56  		if (!ValidationUtils.validateLocation(location, asOfDate)) {
57  			this.putFieldError("location", "error.existence", "location '"
58  					+ location + "'");
59  			valid = false;
60  		}
61  		return valid;
62  	}
63  	
64  	boolean validateScheduledTimeOffDate(LocalDate scheduledTimeOffDate) {
65  		boolean valid = true;
66  		if (scheduledTimeOffDate!= null && !ValidationUtils.validateFutureDate(scheduledTimeOffDate)) {
67  			this.putFieldError("scheduledTimeOffDate", "error.future.date", "Scheduled Time Off Date");
68  			valid = false;
69  		}
70  		return valid;
71  	}
72  	
73  	boolean validateUnusedTimeForScheduledTimeOffDate(LocalDate scheduledTimeOffDate, String unusedTime) {
74  		boolean valid = true;
75  		if (scheduledTimeOffDate == null && (StringUtils.isEmpty(unusedTime) || StringUtils.equals("T", unusedTime) || StringUtils.equals("NUTA", unusedTime))) {
76  			this.putFieldError("scheduledTimeOffDate", "error.unusedtime.bank.required", LMConstants.UNUSED_TIME_MAP.get(unusedTime));
77  			valid = false;
78  		}		
79  		return valid;
80  	}
81  	
82  	//if no unsued time allowed is selected make sure Accrued date and Schedule Time off date are equal
83  	//if not throw an error
84  	boolean validateNoUnusedTimeAllowed(LocalDate scheduledTimeOffDate, LocalDate accruedDate, String unusedTime) {
85  		boolean valid = true;
86  		if(scheduledTimeOffDate != null) {
87  			if (scheduledTimeOffDate.equals(accruedDate) == false) {
88  				this.putFieldError("unusedTime", "error.nounusedtimeallowed.selected", "Unused Time");			
89  				valid = false;
90  			}
91  		}
92  		return valid;
93  		
94  	}
95  		
96  	boolean validateTransfertoEarnCode(String transfertoEarnCode, LocalDate localDate) {
97  		boolean valid = true;
98  		if (StringUtils.isEmpty(transfertoEarnCode)) {
99  			this.putFieldError("transfertoEarnCode", "error.required", "Transfer to Earn Code");
100 			valid = false;
101 		}
102 		else {
103 			valid &= this.validateEarnCode(transfertoEarnCode, localDate);
104 		}
105 		return valid;
106 	}
107 	
108 	boolean validatePremiumEarnCode(String premiumEarnCode, String premiumHoliday, LocalDate localDate) {
109 		boolean valid = true;
110 		if (!StringUtils.isEmpty(premiumEarnCode)){
111 			if(premiumHoliday.equalsIgnoreCase("N")) {
112 				this.putFieldError("premiumHoliday", "error.SSTO,premiumHoliday", "Premium Holiday");
113 				valid = false;
114 			} 
115 			if(valid) {
116 				if (!ValidationUtils.validateEarnCode(premiumEarnCode, localDate)) {
117 					this.putFieldError("premiumEarnCode", "error.existence", "earnCode '"
118 							+ premiumEarnCode + "'");
119 					valid = false;
120 				}
121 			}
122 			
123 		}
124 		return valid;
125 	}
126 	
127 	boolean validateTransferConversionFactor(BigDecimal transferConversionFactor) {
128 		boolean valid = true;
129 		if (transferConversionFactor == null) {
130 			this.putFieldError("transferConversionFactor", "error.required", "Transfer Conversion Factor");
131 			valid = false;
132 		}
133 		return valid;
134 	}
135 	
136 	boolean validateEarnCode(String earnCode, LocalDate asOfDate) {
137 		boolean valid = true;
138 		if (!ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
139 			this.putFieldError("earnCode", "error.existence", "earnCode '"
140 					+ earnCode + "'");
141 			valid = false;
142 		}
143 		return valid;
144 	}
145 
146     private boolean validateFraction(String earnCode, BigDecimal amount, LocalDate asOfDate, String fieldName) {
147         boolean valid = true;
148         if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
149             EarnCodeContract ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
150             if(ec != null && ec.getFractionalTimeAllowed() != null) {
151                 BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
152                 String[] parameters = new String[2];
153                 parameters[0] = earnCode;
154                 parameters[1] = Integer.toString(fracAllowed.scale());
155                 this.putFieldError(fieldName, "error.amount.fraction", parameters);
156                 valid = false;
157             }
158         }
159         return valid;
160     }
161     
162 	private boolean validateUnusedTime(SystemScheduledTimeOff sysSchTimeOff) {
163 		boolean valid = true;
164 		if(StringUtils.isNotBlank(sysSchTimeOff.getUnusedTime())) {
165 			valid &= this.validateUnusedTimeForScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffLocalDate(), sysSchTimeOff.getUnusedTime());
166 			if(StringUtils.equals(sysSchTimeOff.getUnusedTime(),LMConstants.UNUSED_TIME.TRANSFER)) {
167 				valid &= this.validateTransfertoEarnCode(sysSchTimeOff.getTransfertoEarnCode(), sysSchTimeOff.getEffectiveLocalDate());
168 				valid &= this.validateTransferConversionFactor(sysSchTimeOff.getTransferConversionFactor());
169 				if(valid) {
170 					// transfer to earn code is not blank, and exists.
171 					// TODO: xref earn code white[black] list? Should this earn code belong to the same leave plan?
172 				}
173 			}
174 			else if(StringUtils.equals(sysSchTimeOff.getUnusedTime(), LMConstants.UNUSED_TIME.BANK)) {
175 				//TODO: do "bank" validation
176 				// can there be a value specified for scheduled time off date, or must this field be left blank?
177 			}
178 			else if(StringUtils.equals(sysSchTimeOff.getUnusedTime(), LMConstants.UNUSED_TIME.NO_UNUSED)) {
179 				valid &= this.validateNoUnusedTimeAllowed(sysSchTimeOff.getScheduledTimeOffLocalDate(), sysSchTimeOff.getAccruedLocalDate(), sysSchTimeOff.getUnusedTime());
180 			}
181 		}
182 		else {
183 			this.putFieldError("unusedTime", "error.required", "Unused Time");
184 			valid = false;
185 		}
186 		return valid;
187 	}
188 
189 	@Override
190 	protected boolean processCustomRouteDocumentBusinessRules(
191 			MaintenanceDocument document) {
192 		boolean valid = false;
193 		LOG.debug("entering custom validation for SystemScheduledTimeOff");
194 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
195 		if (pbo instanceof SystemScheduledTimeOff) {
196 			SystemScheduledTimeOff sysSchTimeOff = (SystemScheduledTimeOff) pbo;
197 			if (sysSchTimeOff != null) {
198 				valid = true;
199 				valid &= this.validateEffectiveDate(sysSchTimeOff.getEffectiveLocalDate());
200 				valid &= this.validateAccruedDate(sysSchTimeOff.getAccruedLocalDate());
201 				valid &= this.validateScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffLocalDate());
202                 valid &= this.validateFraction(sysSchTimeOff.getEarnCode(),sysSchTimeOff.getAmountofTime(),sysSchTimeOff.getEffectiveLocalDate(),"amountofTime");
203 				//valid &= this.validateUnusedTimeForScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffLocalDate(), sysSchTimeOff.getUnusedTime());
204 				valid &= this.validateUnusedTime(sysSchTimeOff);
205 				
206 				//valid &= this.validateNoUnusedTimeAllowed(sysSchTimeOff.getScheduledTimeOffLocalDate(),sysSchTimeOff.getAccruedLocalDate(),sysSchTimeOff.getUnusedTime());
207 				valid &= this.validateLocation(sysSchTimeOff.getLocation(), sysSchTimeOff.getEffectiveLocalDate());
208 				valid &= this.validateEarnCode(sysSchTimeOff.getEarnCode(), sysSchTimeOff.getEffectiveLocalDate());
209 				valid &= this.validatePremiumEarnCode(sysSchTimeOff.getPremiumEarnCode(), sysSchTimeOff.getPremiumHoliday(), sysSchTimeOff.getEffectiveLocalDate());
210 			}
211 		}
212 		
213 		return valid;
214 	}
215 
216 }