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.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.earncode.EarnCode;
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 validateTransferConversionFactor(BigDecimal transferConversionFactor) {
109 		boolean valid = true;
110 		if (transferConversionFactor == null) {
111 			this.putFieldError("transferConversionFactor", "error.required", "Transfer Conversion Factor");
112 			valid = false;
113 		}
114 		return valid;
115 	}
116 	
117 	boolean validateEarnCode(String earnCode, LocalDate asOfDate) {
118 		boolean valid = true;
119 		if (!ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
120 			this.putFieldError("earnCode", "error.existence", "earnCode '"
121 					+ earnCode + "'");
122 			valid = false;
123 		}
124 		return valid;
125 	}
126 
127     private boolean validateFraction(String earnCode, BigDecimal amount, LocalDate asOfDate, String fieldName) {
128         boolean valid = true;
129         if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
130             EarnCode ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
131             if(ec != null && ec.getFractionalTimeAllowed() != null) {
132                 BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
133                 String[] parameters = new String[2];
134                 parameters[0] = earnCode;
135                 parameters[1] = Integer.toString(fracAllowed.scale());
136                 this.putFieldError(fieldName, "error.amount.fraction", parameters);
137                 valid = false;
138             }
139         }
140         return valid;
141     }
142     
143 	private boolean validateUnusedTime(SystemScheduledTimeOff sysSchTimeOff) {
144 		boolean valid = true;
145 		if(StringUtils.isNotBlank(sysSchTimeOff.getUnusedTime())) {
146 			valid &= this.validateUnusedTimeForScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffLocalDate(), sysSchTimeOff.getUnusedTime());
147 			if(StringUtils.equals(sysSchTimeOff.getUnusedTime(),LMConstants.UNUSED_TIME.TRANSFER)) {
148 				valid &= this.validateTransfertoEarnCode(sysSchTimeOff.getTransfertoEarnCode(), sysSchTimeOff.getEffectiveLocalDate());
149 				valid &= this.validateTransferConversionFactor(sysSchTimeOff.getTransferConversionFactor());
150 				if(valid) {
151 					// transfer to earn code is not blank, and exists.
152 					// TODO: xref earn code white[black] list? Should this earn code belong to the same leave plan?
153 				}
154 			}
155 			else if(StringUtils.equals(sysSchTimeOff.getUnusedTime(), LMConstants.UNUSED_TIME.BANK)) {
156 				//TODO: do "bank" validation
157 				// can there be a value specified for scheduled time off date, or must this field be left blank?
158 			}
159 			else if(StringUtils.equals(sysSchTimeOff.getUnusedTime(), LMConstants.UNUSED_TIME.NO_UNUSED)) {
160 				valid &= this.validateNoUnusedTimeAllowed(sysSchTimeOff.getScheduledTimeOffLocalDate(), sysSchTimeOff.getAccruedLocalDate(), sysSchTimeOff.getUnusedTime());
161 			}
162 		}
163 		else {
164 			this.putFieldError("unusedTime", "error.required", "Unused Time");
165 			valid = false;
166 		}
167 		return valid;
168 	}
169 
170 	@Override
171 	protected boolean processCustomRouteDocumentBusinessRules(
172 			MaintenanceDocument document) {
173 		boolean valid = false;
174 		LOG.debug("entering custom validation for SystemScheduledTimeOff");
175 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
176 		if (pbo instanceof SystemScheduledTimeOff) {
177 			SystemScheduledTimeOff sysSchTimeOff = (SystemScheduledTimeOff) pbo;
178 			if (sysSchTimeOff != null) {
179 				valid = true;
180 				valid &= this.validateEffectiveDate(sysSchTimeOff.getEffectiveLocalDate());
181 				valid &= this.validateAccruedDate(sysSchTimeOff.getAccruedLocalDate());
182 				valid &= this.validateScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffLocalDate());
183                 valid &= this.validateFraction(sysSchTimeOff.getEarnCode(),sysSchTimeOff.getAmountofTime(),sysSchTimeOff.getEffectiveLocalDate(),"amountofTime");
184 				//valid &= this.validateUnusedTimeForScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffLocalDate(), sysSchTimeOff.getUnusedTime());
185 				valid &= this.validateUnusedTime(sysSchTimeOff);
186 				//valid &= this.validateNoUnusedTimeAllowed(sysSchTimeOff.getScheduledTimeOffLocalDate(),sysSchTimeOff.getAccruedLocalDate(),sysSchTimeOff.getUnusedTime());
187 				valid &= this.validateLocation(sysSchTimeOff.getLocation(), sysSchTimeOff.getEffectiveLocalDate());
188 				valid &= this.validateEarnCode(sysSchTimeOff.getEarnCode(), sysSchTimeOff.getEffectiveLocalDate());
189 			}
190 		}
191 		
192 		return valid;
193 	}
194 
195 }