001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.lm.leavedonation.validation;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.hr.lm.leavedonation.LeaveDonation;
023    import org.kuali.hr.time.earncode.EarnCode;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.util.ValidationUtils;
026    import org.kuali.rice.kns.document.MaintenanceDocument;
027    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
028    import org.kuali.rice.krad.bo.PersistableBusinessObject;
029    
030    public class LeaveDonationValidation extends MaintenanceDocumentRuleBase {
031            public static final String DONOR = "donor";
032            public static final String RECEPIENT = "recepient";
033    
034            boolean validateEffectiveDate(Date effectiveDate) {
035                    boolean valid = true;
036                    if (effectiveDate != null
037                                    && !ValidationUtils.validateOneYearFutureDate(effectiveDate)) {
038                            this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
039                            valid = false;
040                    }
041                    return valid;
042            }
043    
044            boolean validatePrincipal(String principalId, String forPerson) {
045                    boolean valid = true;
046                    if (!ValidationUtils.validatePrincipalId(principalId)) {
047                            this.putFieldError(
048                                            forPerson.equals(LeaveDonationValidation.DONOR) ? "donorsPrincipalID"
049                                                            : "recipientsPrincipalID", "error.existence",
050                                            "principal Id '" + principalId + "'");
051                            valid = false;
052                    }
053                    return valid;
054            }
055    
056            boolean validateAccrualCategory(String accrualCategory, Date asOfDate,
057                            String forPerson) {
058                    boolean valid = true;
059                    if (!ValidationUtils.validateAccCategory(accrualCategory, asOfDate)) {
060                            this.putFieldError(
061                                            forPerson.equals(LeaveDonationValidation.DONOR) ? "donatedAccrualCategory"
062                                                            : "recipientsAccrualCategory", "error.leavePlan.mismatch",
063                                            "accrualCategory '" + accrualCategory + "'");
064                            valid = false;
065                    }
066                    return valid;
067            }
068            
069            boolean validateAccrualCategory(String accrualCategory, Date asOfDate,
070                            String forPerson, String principalId) {
071                    boolean valid = true;
072                    if (!ValidationUtils.validateAccCategory(accrualCategory, principalId, asOfDate)) {
073                            String[] myErrorsArgs={"accrualCategory '" + accrualCategory + "'", forPerson};
074                            this.putFieldError(
075                                            forPerson.equals(LeaveDonationValidation.DONOR) ? "donatedAccrualCategory"
076                                                            : "recipientsAccrualCategory", "error.leavePlan.mismatch",
077                                                            myErrorsArgs);
078                            valid = false;
079                            
080                    }
081                    return valid;
082            }
083    
084            boolean validateEarnCode(String principalAC, String formEarnCode, String forPerson, Date asOfDate) {
085                    boolean valid = true;
086    
087                    EarnCode testEarnCode = TkServiceLocator.getEarnCodeService().getEarnCode(formEarnCode, asOfDate);
088    //              LeaveCode testLeaveCode = TkServiceLocator.getLeaveCodeService().getLeaveCode(formEarnCode, asOfDate);
089                    String formEarnCodeAC = "NullAccrualCategoryPlaceholder";
090                    if (testEarnCode != null && testEarnCode.getAccrualCategory() != null) {
091                            formEarnCodeAC = testEarnCode.getAccrualCategory();
092                    }
093    
094                    if (!StringUtils.equalsIgnoreCase(principalAC, formEarnCodeAC)) {
095                            this.putFieldError(forPerson.equals(LeaveDonationValidation.DONOR) ? "donatedEarnCode"
096                                            : "recipientsEarnCode", "error.codeCategory.mismatch", forPerson);
097                            valid = false;
098                    }
099                    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    }