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.kpme.pm.positiondepartment.validation;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.kpme.core.department.Department;
020    import org.kuali.kpme.core.service.HrServiceLocator;
021    import org.kuali.kpme.core.util.ValidationUtils;
022    import org.kuali.kpme.pm.positiondepartment.PositionDepartment;
023    import org.kuali.kpme.pm.util.PmValidationUtils;
024    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
025    import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
026    
027    @SuppressWarnings("deprecation")
028    public class PositionDepartmentValidation extends MaintenanceDocumentRuleBase  {
029            @Override
030            protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
031                    boolean valid = false;
032                    LOG.debug("entering custom validation for Position Department");
033                    PositionDepartment positionDepartment = (PositionDepartment) this.getNewDataObject();
034                    
035                    if (positionDepartment != null) {
036                            valid = true;
037                            valid &= this.validateInstitution(positionDepartment);
038                            valid &= this.validateLocation(positionDepartment);
039                            valid &= this.validateDepartment(positionDepartment);
040                            valid &= this.validateAffiliation(positionDepartment);
041                    }
042                    return valid;
043            }
044            
045            private boolean validateInstitution(PositionDepartment positionDepartment) {
046                    if (StringUtils.isNotEmpty(positionDepartment.getInstitution())
047                                    && !ValidationUtils.validateInstitution(positionDepartment.getInstitution(), positionDepartment.getEffectiveLocalDate())) {
048                            this.putFieldError("institution", "error.existence", "Institution '"
049                                            + positionDepartment.getInstitution() + "'");
050                            return false;
051                    } else {
052                            return true;
053                    }
054            }
055            
056            private boolean validateLocation(PositionDepartment positionDepartment) {
057                    if (StringUtils.isNotEmpty(positionDepartment.getLocation())
058                                    && !ValidationUtils.validateLocation(positionDepartment.getLocation(), positionDepartment.getEffectiveLocalDate())) {
059                            this.putFieldError("location", "error.existence", "Location '"
060                                            + positionDepartment.getLocation() + "'");
061                            return false;
062                    } else {
063                            return true;
064                    }
065            }
066            
067            private boolean validateDepartment(PositionDepartment positionDepartment) {
068                    if (StringUtils.isNotEmpty(positionDepartment.getDepartment())
069                                    && !ValidationUtils.validateDepartment(positionDepartment.getDepartment(), positionDepartment.getEffectiveLocalDate())) {
070                            this.putFieldError("department", "error.existence", "Department '"
071                                            + positionDepartment.getDepartment() + "'");
072                            return false;
073                    }
074                    Department dep = HrServiceLocator.getDepartmentService().getDepartment(positionDepartment.getDepartment(), positionDepartment.getEffectiveLocalDate());
075                    if(dep == null ) {
076                            this.putFieldError("department", "error.existence", "Department '"
077                                            + positionDepartment.getDepartment() + "'");
078                            return false;
079                    } else {
080                            if(!ValidationUtils.wildCardMatch(dep.getLocation(), positionDepartment.getLocation())) {
081                                    String[] params = new String[3];
082                                    params[0] = positionDepartment.getLocation();
083                                    params[1] = dep.getLocation();
084                                    params[2] = "Department '" + positionDepartment.getDepartment() + "'";
085                                    this.putFieldError("department", "location.inconsistent", params);
086                                    return false;
087                            }
088                    }
089                    
090                    return true;
091            }
092            
093            private boolean validateAffiliation(PositionDepartment positionDepartment) {
094                    if (StringUtils.isNotEmpty(positionDepartment.getPositionDeptAffl())
095                                    && !PmValidationUtils.validateAffiliation(positionDepartment.getPositionDeptAffl(), positionDepartment.getEffectiveLocalDate())) {
096                            this.putFieldError("positionDeptAffl", "error.existence", "Affiliation '"
097                                            + positionDepartment.getPositionDeptAffl() + "'");
098                            return false;
099                    } else {
100                            return true;
101                    }
102            }
103    }
104