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.leaveadjustment.validation;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    
021    import org.kuali.hr.lm.leaveadjustment.LeaveAdjustment;
022    import org.kuali.hr.time.earncode.EarnCode;
023    import org.kuali.hr.time.service.base.TkServiceLocator;
024    import org.kuali.hr.time.util.ValidationUtils;
025    import org.kuali.rice.kns.document.MaintenanceDocument;
026    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
027    import org.kuali.rice.krad.bo.PersistableBusinessObject;
028    
029    public class LeaveAdjustmentValidation extends MaintenanceDocumentRuleBase{
030            
031            boolean validateLeavePlan(String leavePlan, Date asOfDate) {
032                    boolean valid = true;
033                    if (!ValidationUtils.validateLeavePlan(leavePlan, asOfDate)) {
034                            this.putFieldError("leavePlan", "error.existence", "leavePlan '"
035                                            + leavePlan + "'");
036                            valid = false;
037                    }
038                    return valid;
039            }
040            
041            boolean validateEarnCode(String earnCode, String accrualCategory, Date asOfDate) {
042                    boolean valid = true;
043                    if (!ValidationUtils.validateEarnCodeOfAccrualCategory(earnCode, accrualCategory, asOfDate)) {
044                            this.putFieldError("earnCode", "error.earnCode.accrualCategory.mismatch", 
045                                            earnCode);
046                            valid = false;
047                    }
048                    return valid;
049            }
050    
051            boolean validatePrincipal(String principalId) {
052                    boolean valid = true;
053                    if (!ValidationUtils.validatePrincipalId(principalId)) {
054                            this.putFieldError("principalId", "error.existence",
055                                            "principalId '" + principalId + "'");
056                            valid = false;
057                    }
058                    return valid;
059            }
060    
061            boolean validateAccrualCategory(String accrualCategory, Date asOfDate, String principalId) {
062                    boolean valid = true;
063                    if (!ValidationUtils.validateAccCategory(accrualCategory, principalId, asOfDate)) {
064                            this.putFieldError("accrualCategory", "error.existence", "accrualCategory '"
065                                            + accrualCategory + "'");
066                            valid = false;
067                    }
068                    return valid;
069            }
070            
071            private boolean validateFraction(String earnCode, BigDecimal amount, Date asOfDate) {
072                    boolean valid = true;
073                    if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
074                            EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
075                            if(ec != null && ec.getFractionalTimeAllowed() != null) {
076                                    BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
077                                    String[] parameters = new String[2];
078                                    parameters[0] = earnCode;
079                                    parameters[1] = Integer.toString(fracAllowed.scale());
080                                    this.putFieldError("adjustmentAmount", "error.amount.fraction", parameters);
081                                    valid = false;
082                             }
083                    }
084                    return valid;
085            }
086                    
087            @Override
088            protected boolean processCustomRouteDocumentBusinessRules(
089                            MaintenanceDocument document) {
090                    boolean valid = false;
091    
092                    LOG.debug("entering custom validation for LeaveAdjustment");
093                    PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
094                    if (pbo instanceof LeaveAdjustment) {
095                            LeaveAdjustment leaveAdjustment = (LeaveAdjustment) pbo;
096    
097                            if (leaveAdjustment != null) {
098                                    valid = true;
099                                    if(leaveAdjustment.getPrincipalId() != null) {
100                                            valid &= this.validatePrincipal(leaveAdjustment.getPrincipalId());
101                                    }
102                                    if(leaveAdjustment.getAccrualCategory() != null) {
103                                            valid &= this.validateAccrualCategory(leaveAdjustment.getAccrualCategory(),leaveAdjustment.getEffectiveDate(), leaveAdjustment.getPrincipalId());
104                                    }
105                                    if(leaveAdjustment.getLeavePlan() != null) {
106                                            valid &= this.validateLeavePlan(leaveAdjustment.getLeavePlan(), leaveAdjustment.getEffectiveDate());
107                                    }
108                                    if(leaveAdjustment.getEarnCode() != null) {
109                                            valid &= this.validateEarnCode(leaveAdjustment.getEarnCode(), leaveAdjustment.getAccrualCategory(), leaveAdjustment.getEffectiveDate());
110                                            if(leaveAdjustment.getAdjustmentAmount() != null) {
111                                                    valid &= this.validateFraction(leaveAdjustment.getEarnCode(), leaveAdjustment.getAdjustmentAmount(), leaveAdjustment.getEffectiveDate());
112                                            }
113                                    }
114                            }
115                    }
116    
117                    return valid;
118            }
119    
120    }