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.lm.leavedonation.validation;
17  
18  import java.math.BigDecimal;
19  import java.sql.Date;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.hr.lm.leavedonation.LeaveDonation;
23  import org.kuali.hr.time.earncode.EarnCode;
24  import org.kuali.hr.time.service.base.TkServiceLocator;
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  import org.kuali.rice.krad.bo.PersistableBusinessObject;
29  
30  public class LeaveDonationValidation extends MaintenanceDocumentRuleBase {
31  	public static final String DONOR = "donor";
32  	public static final String RECEPIENT = "recepient";
33  
34  	boolean validateEffectiveDate(Date effectiveDate) {
35  		boolean valid = true;
36  		if (effectiveDate != null
37  				&& !ValidationUtils.validateOneYearFutureDate(effectiveDate)) {
38  			this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
39  			valid = false;
40  		}
41  		return valid;
42  	}
43  
44  	boolean validatePrincipal(String principalId, String forPerson) {
45  		boolean valid = true;
46  		if (!ValidationUtils.validatePrincipalId(principalId)) {
47  			this.putFieldError(
48  					forPerson.equals(LeaveDonationValidation.DONOR) ? "donorsPrincipalID"
49  							: "recipientsPrincipalID", "error.existence",
50  					"principal Id '" + principalId + "'");
51  			valid = false;
52  		}
53  		return valid;
54  	}
55  
56  	boolean validateAccrualCategory(String accrualCategory, Date asOfDate,
57  			String forPerson) {
58  		boolean valid = true;
59  		if (!ValidationUtils.validateAccCategory(accrualCategory, asOfDate)) {
60  			this.putFieldError(
61  					forPerson.equals(LeaveDonationValidation.DONOR) ? "donatedAccrualCategory"
62  							: "recipientsAccrualCategory", "error.leavePlan.mismatch",
63  					"accrualCategory '" + accrualCategory + "'");
64  			valid = false;
65  		}
66  		return valid;
67  	}
68  	
69  	boolean validateAccrualCategory(String accrualCategory, Date asOfDate,
70  			String forPerson, String principalId) {
71  		boolean valid = true;
72  		if (!ValidationUtils.validateAccCategory(accrualCategory, principalId, asOfDate)) {
73  			String[] myErrorsArgs={"accrualCategory '" + accrualCategory + "'", forPerson};
74  			this.putFieldError(
75  					forPerson.equals(LeaveDonationValidation.DONOR) ? "donatedAccrualCategory"
76  							: "recipientsAccrualCategory", "error.leavePlan.mismatch",
77  							myErrorsArgs);
78  			valid = false;
79  			
80  		}
81  		return valid;
82  	}
83  
84  	boolean validateEarnCode(String principalAC, String formEarnCode, String forPerson, Date asOfDate) {
85  		boolean valid = true;
86  
87  		EarnCode testEarnCode = TkServiceLocator.getEarnCodeService().getEarnCode(formEarnCode, asOfDate);
88  //		LeaveCode testLeaveCode = TkServiceLocator.getLeaveCodeService().getLeaveCode(formEarnCode, asOfDate);
89  		String formEarnCodeAC = "NullAccrualCategoryPlaceholder";
90  		if (testEarnCode != null && testEarnCode.getAccrualCategory() != null) {
91  			formEarnCodeAC = testEarnCode.getAccrualCategory();
92  		}
93  
94  		if (!StringUtils.equalsIgnoreCase(principalAC, formEarnCodeAC)) {
95  			this.putFieldError(forPerson.equals(LeaveDonationValidation.DONOR) ? "donatedEarnCode"
96  					: "recipientsEarnCode", "error.codeCategory.mismatch", forPerson);
97  			valid = false;
98  		}
99  		return valid;
100 	}
101 	
102 	private boolean validateFraction(String earnCode, BigDecimal amount, Date asOfDate, String fieldName) {
103 		boolean valid = true;
104 		if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
105 			EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
106 			if(ec != null && ec.getFractionalTimeAllowed() != null) {
107 				BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
108 				String[] parameters = new String[2];
109 				parameters[0] = earnCode;
110 				parameters[1] = Integer.toString(fracAllowed.scale());
111 				this.putFieldError(fieldName, "error.amount.fraction", parameters);
112 				valid = false;
113 			 }
114 		}
115 		return valid;
116 	}
117 
118 	@Override
119 	protected boolean processCustomRouteDocumentBusinessRules(
120 			MaintenanceDocument document) {
121 		boolean valid = false;
122 		LOG.debug("entering custom validation for Leave Donation");
123 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
124 		if (pbo instanceof LeaveDonation) {
125 			LeaveDonation leaveDonation = (LeaveDonation) pbo;
126 			if (leaveDonation != null) {
127 				valid = true;
128 				//valid &= this.validateEffectiveDate(leaveDonation.getEffectiveDate()); // KPME-1207, effectiveDate can be past, current or future
129 				if(leaveDonation.getDonatedAccrualCategory() != null) {
130 						valid &= this.validateAccrualCategory(
131 						leaveDonation.getDonatedAccrualCategory(),
132 						leaveDonation.getEffectiveDate(),
133 						LeaveDonationValidation.DONOR, leaveDonation.getDonorsPrincipalID());
134 				}
135 				if(leaveDonation.getRecipientsAccrualCategory() != null) {
136 						valid &= this.validateAccrualCategory(
137 						leaveDonation.getRecipientsAccrualCategory(),
138 						leaveDonation.getEffectiveDate(),
139 						LeaveDonationValidation.RECEPIENT, leaveDonation.getRecipientsPrincipalID());
140 				}
141 				if(leaveDonation.getDonorsPrincipalID() != null) {
142 						valid &= this.validatePrincipal(
143 						leaveDonation.getDonorsPrincipalID(),
144 						LeaveDonationValidation.DONOR);
145 				}
146 				if(leaveDonation.getRecipientsPrincipalID() != null){
147 						valid &= this.validatePrincipal(
148 						leaveDonation.getRecipientsPrincipalID(),
149 						LeaveDonationValidation.RECEPIENT);
150 				}
151 				if(leaveDonation.getDonatedAccrualCategory() != null) {
152 						valid &= this.validateEarnCode(
153 						leaveDonation.getDonatedAccrualCategory(),
154 						leaveDonation.getDonatedEarnCode(),
155 						LeaveDonationValidation.DONOR, leaveDonation.getEffectiveDate());
156 				}
157 				if(leaveDonation.getRecipientsAccrualCategory() != null) {
158 						valid &= this.validateEarnCode(
159 						leaveDonation.getRecipientsAccrualCategory(),
160 						leaveDonation.getRecipientsEarnCode(),
161 						LeaveDonationValidation.RECEPIENT, leaveDonation.getEffectiveDate());
162 				}
163 				if(leaveDonation.getAmountDonated() != null && leaveDonation.getDonatedEarnCode() != null) {
164 					valid &= this.validateFraction(
165 					leaveDonation.getDonatedEarnCode(), 
166 					leaveDonation.getAmountDonated(), 
167 					leaveDonation.getEffectiveDate(),
168 					"amountDonated");
169 				}
170 				if(leaveDonation.getAmountReceived() != null && leaveDonation.getRecipientsEarnCode() != null) {
171 					valid &= this.validateFraction(
172 					leaveDonation.getRecipientsEarnCode(), 
173 					leaveDonation.getAmountReceived(), 
174 					leaveDonation.getEffectiveDate(),
175 					"amountReceived");
176 				}
177 			}
178 		}
179 		return valid;
180 	}
181 }