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.leavecode.validation;
017    
018    import java.sql.Date;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.hr.lm.accrual.AccrualCategory;
022    import org.kuali.hr.lm.leavecode.LeaveCode;
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 LeaveCodeValidation extends MaintenanceDocumentRuleBase {   
030            boolean validateEffectiveDate(Date effectiveDate) {
031                    boolean valid = true;
032                    if (effectiveDate != null && !ValidationUtils.validateOneYearFutureDate(effectiveDate)) {
033                            this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
034                            valid = false;
035                    }
036                    return valid;
037            }
038    /*      KPME-1477
039            boolean validateLeaveCodeRole(LeaveCode leaveCode) {
040                    boolean valid = true;
041                    if (!leaveCode.getEmployee() && !leaveCode.getDepartmentAdmin() && !leaveCode.getApprover()) {
042                            this.putFieldError("employee", "error.leavecode.role.required");
043                            valid = false;
044                    }
045                    return valid;
046            }
047    */      
048            //KPME-1541 we need to set the leave plan but not silently override if the user uses the Leave Plan field
049            boolean validateLeavePlan(LeaveCode leaveCode) {
050                    
051                    boolean valid = true;
052                    
053                    
054                    if (StringUtils.isNotBlank(leaveCode.getLeavePlan())) {
055    
056                            if (!ValidationUtils.validateLeavePlan(leaveCode.getLeavePlan(), leaveCode.getEffectiveDate())) {
057                                    this.putFieldError("leavePlan", "error.existence", "leavePlan '"
058                                                    + leaveCode.getLeavePlan() + "'");
059                                    valid = false;
060                                    return valid;
061                            }
062                            
063                            if (leaveCode.getEffectiveDate() != null && StringUtils.isNotBlank(leaveCode.getAccrualCategory())) {
064                                    AccrualCategory myTestAccrualCategoryObj =  TkServiceLocator.getAccrualCategoryService().getAccrualCategory(leaveCode.getAccrualCategory(), leaveCode.getEffectiveDate());
065                                    if (!myTestAccrualCategoryObj.getLeavePlan().equals(leaveCode.getLeavePlan())) {
066                                            this.putFieldError("leavePlan", "error.leaveCode.leavePlanMismatch", myTestAccrualCategoryObj.getLeavePlan());
067                                            valid = false;
068                                            return valid;
069                                    }
070                                    leaveCode.setLeavePlan(myTestAccrualCategoryObj.getLeavePlan());
071                            }
072                    }
073                    return valid;
074            }
075            
076            //KPME-1541 must have an accrual category for leave plan lookup
077            boolean validateAccrualCategory(String accrualCategory, Date asOfDate) {
078                    boolean valid = true;
079                    if (!ValidationUtils.validateAccCategory(accrualCategory, asOfDate)) {
080                            this.putFieldError("accrualCategory", "error.existence", "accrualCategory '"
081                                            + accrualCategory + "'");
082                            valid = false;
083                    }
084                    return valid;
085            }
086            
087            boolean validateEarnCode(String earnCode, Date asOfDate) {
088                    boolean valid = true;
089                    if (!StringUtils.isEmpty(earnCode) && !ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
090                            this.putFieldError("earnCode", "error.existence", "earnCode '"
091                                            + earnCode + "'");
092                            valid = false;
093                    }
094                    return valid;
095            }
096            
097            boolean validateDefaultAmountOfTime(Long defaultAmountofTime) {
098                    boolean valid = true;
099                    if ( defaultAmountofTime != null ){
100                            if (defaultAmountofTime.compareTo(new Long(24)) > 0  || defaultAmountofTime.compareTo(new Long(0)) < 0) {
101                                    this.putFieldError("defaultAmountofTime", "error.leaveCode.hours", "Default Amount of Time '"
102                                                    + defaultAmountofTime + "'");
103                                    valid = false;
104                            }
105                    }
106                    return valid;
107            }
108            
109            @Override
110            protected boolean processCustomRouteDocumentBusinessRules(
111                            MaintenanceDocument document) {
112                    boolean valid = false;
113                    LOG.debug("entering custom validation for Leave Code");
114                    PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
115                    if (pbo instanceof LeaveCode) {
116                            LeaveCode leaveCode = (LeaveCode) pbo;
117                            if (leaveCode != null) {
118                                    valid = true;
119                                    //valid &= this.validateEffectiveDate(leaveCode.getEffectiveDate());
120                                    //valid &= this.validateLeaveCodeRole(leaveCode); // xichen. remove this validation, this field is not on the page. KPME1477
121                                    valid &= this.validateAccrualCategory(leaveCode.getAccrualCategory(), leaveCode.getEffectiveDate());
122                                    valid &= this.validateEarnCode(leaveCode.getEarnCode(), leaveCode.getEffectiveDate());
123                                    valid &= this.validateDefaultAmountOfTime(leaveCode.getDefaultAmountofTime());
124                                    valid &= this.validateLeavePlan(leaveCode); //validate accrual category and effdt before calling this
125                            }
126                    }
127                    return valid;
128            }
129    }