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.time.overtime.weekly.rule;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.hr.time.util.ValidationUtils;
020    import org.kuali.rice.kns.document.MaintenanceDocument;
021    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
022    import org.kuali.rice.krad.bo.PersistableBusinessObject;
023    
024    public class WeeklyOvertimeRuleValidation extends MaintenanceDocumentRuleBase {
025    
026        private static final String ADD_LINE_LOCATION = "add.lstWeeklyOvertimeRules.";
027    
028        @Override
029        public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject line) {
030            boolean valid = true;
031    
032            if (line instanceof WeeklyOvertimeRule) {
033                WeeklyOvertimeRule rule = (WeeklyOvertimeRule)line;
034                valid = validateWeeklyOvertimeRule(rule, ADD_LINE_LOCATION);
035            }
036    
037            return valid;
038        }
039    
040            @Override
041            protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
042            boolean valid = true;
043                    WeeklyOvertimeRuleGroup ruleGroup = (WeeklyOvertimeRuleGroup)this.getNewBo();
044    
045                    int index = 0;
046                    for(WeeklyOvertimeRule rule : ruleGroup.getLstWeeklyOvertimeRules()) {
047                valid &= validateWeeklyOvertimeRule(rule, "lstWeeklyOvertimeRules["+index+"].");
048                            index++;
049                    }
050    
051                    return valid;
052            }
053    
054        boolean validateWeeklyOvertimeRule(WeeklyOvertimeRule rule, String errorFieldPrefix) {
055            boolean valid = true;
056    
057            if (errorFieldPrefix == null)
058                errorFieldPrefix = "";
059    
060            if(!StringUtils.isEmpty(rule.getMaxHoursEarnGroup())) {
061                if(!ValidationUtils.validateEarnGroup(rule.getMaxHoursEarnGroup(), rule.getEffectiveDate())) {
062                    this.putFieldError(errorFieldPrefix + "maxHoursEarnGroup", "error.existence", "maxHoursEarnGroup '" + rule.getMaxHoursEarnGroup() + "'");
063                    valid = false;
064                }
065            }
066            if(!StringUtils.isEmpty(rule.getConvertFromEarnGroup())) {
067                if(!ValidationUtils.validateEarnGroup(rule.getConvertFromEarnGroup(), rule.getEffectiveDate())) {
068                    this.putFieldError(errorFieldPrefix + "convertFromEarnGroup", "error.existence", "convertFromEarnGroup '" + rule.getConvertFromEarnGroup() + "'");
069                    valid = false;
070                }
071            }
072            
073                    if (!StringUtils.isEmpty(rule.getConvertFromEarnGroup())
074                                    && ValidationUtils.earnGroupHasOvertimeEarnCodes(rule.getConvertFromEarnGroup(), rule.getEffectiveDate())){
075                            this.putFieldError(errorFieldPrefix + "convertFromEarnGroup", "earngroup.earncode.overtime",  rule.getConvertFromEarnGroup());
076                            valid = false;;
077                    }
078    
079            if(!StringUtils.isEmpty(rule.getConvertToEarnCode())) {
080                if(!ValidationUtils.validateEarnCode(rule.getConvertToEarnCode(), rule.getEffectiveDate())) {
081                    this.putFieldError(errorFieldPrefix + "convertToEarnCode", "error.existence", "convertToEarnCode '" + rule.getConvertToEarnCode() + "'");
082                    valid = false;
083                } else if (!ValidationUtils.validateEarnCode(rule.getConvertToEarnCode(), true, rule.getEffectiveDate())) {
084                    this.putFieldError(errorFieldPrefix + "convertToEarnCode", "earncode.ovt.required", rule.getConvertToEarnCode());
085                    valid = false;
086                }
087            }
088    
089            return valid;
090        }
091    
092    }