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.employeeoverride.validation;
017    
018    import org.apache.cxf.common.util.StringUtils;
019    import org.kuali.hr.lm.accrual.AccrualCategory;
020    import org.kuali.hr.lm.employeeoverride.EmployeeOverride;
021    import org.kuali.hr.time.principal.PrincipalHRAttributes;
022    import org.kuali.hr.time.service.base.TkServiceLocator;
023    import org.kuali.rice.kns.document.MaintenanceDocument;
024    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
025    import org.kuali.rice.krad.bo.PersistableBusinessObject;
026    
027    public class EmployeeOverrideRule extends MaintenanceDocumentRuleBase{
028            @Override
029            protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
030                    boolean valid = true;
031                    PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
032                    if (pbo instanceof EmployeeOverride) {
033                            EmployeeOverride eo = (EmployeeOverride) pbo;
034                            valid &= this.validatePrincipalHRAttributes(eo);
035                            valid &= this.validateAccrualCategory(eo);
036                            valid &= this.validateLeavePlan(eo);
037                    }
038                    return valid;
039            }
040            
041            boolean validateLeavePlan(EmployeeOverride eo) {
042                    if(StringUtils.isEmpty(eo.getLeavePlan())) {
043                            this.putFieldError("leavePlan", "error.employeeOverride.leavePlan.notfound");
044                            return false;
045                    } else if(eo.getEffectiveDate() != null) {
046                            AccrualCategory ac = TkServiceLocator.getAccrualCategoryService().
047                                    getAccrualCategory(eo.getAccrualCategory(), eo.getEffectiveDate());
048                            PrincipalHRAttributes pha = TkServiceLocator.getPrincipalHRAttributeService().
049                                    getPrincipalCalendar(eo.getPrincipalId(), eo.getEffectiveDate());
050                            if(ac != null && pha != null && !ac.getLeavePlan().equals(pha.getLeavePlan())) {
051                                    this.putFieldError("leavePlan", "error.employeeOverride.leavePlan.inconsistent", eo.getAccrualCategory() );
052                                    return false;
053                            }
054                    }
055                    return true;
056            }
057            
058            boolean validateAccrualCategory(EmployeeOverride eo) {
059                    if(eo.getAccrualCategory() != null && eo.getEffectiveDate() != null) {
060                            AccrualCategory ac = TkServiceLocator.getAccrualCategoryService().
061                                    getAccrualCategory(eo.getAccrualCategory(), eo.getEffectiveDate());
062                            if(ac == null) {
063                                    this.putFieldError("accrualCategory", "error.employeeOverride.accrualCategory.notfound", eo.getAccrualCategory() );
064                                    return false;
065                            }
066                    }
067                    return true;
068            }
069            
070            boolean validatePrincipalHRAttributes(EmployeeOverride eo) {
071                    if(eo.getPrincipalId() != null && eo.getEffectiveDate() != null) {
072                            PrincipalHRAttributes pha = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(eo.getPrincipalId(), eo.getEffectiveDate());
073                            if(pha == null) {
074                                    this.putFieldError("principalId", "error.employeeOverride.principalHRAttributes.notfound", eo.getPrincipalId() );
075                                    return false;
076                            }
077                    }
078                    return true;
079            }
080    
081    }