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.earncodesec.validation;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.hr.lm.earncodesec.EarnCodeSecurity;
023    import org.kuali.hr.time.roles.TkUserRoles;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.timeblock.TimeBlock;
026    import org.kuali.hr.time.util.TkConstants;
027    import org.kuali.hr.time.util.ValidationUtils;
028    import org.kuali.rice.kns.document.MaintenanceDocument;
029    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
030    import org.kuali.rice.krad.bo.PersistableBusinessObject;
031    import org.kuali.rice.krad.util.GlobalVariables;
032    
033    
034    public class EarnCodeSecurityRule extends MaintenanceDocumentRuleBase {
035    
036            private boolean validateSalGroup(EarnCodeSecurity departmentEarnCode ) {
037                    if (!ValidationUtils.validateSalGroup(departmentEarnCode.getHrSalGroup(), departmentEarnCode.getEffectiveDate())) {
038                            this.putFieldError("hrSalGroup", "error.existence", "Salgroup '" + departmentEarnCode.getHrSalGroup()+ "'");
039                            return false;
040                    } else {
041                            return true;
042                    }
043            }
044    
045            private boolean validateDept(EarnCodeSecurity clr) {
046                    if (!ValidationUtils.validateDepartment(clr.getDept(), clr.getEffectiveDate()) && !StringUtils.equals(clr.getDept(), TkConstants.WILDCARD_CHARACTER)) {
047                            this.putFieldError("dept", "error.existence", "department '" + clr.getDept() + "'");
048                            return false;
049                    } else {
050                            return true;
051                    }
052            }
053    
054            private boolean validateEarnCode(EarnCodeSecurity departmentEarnCode ) {
055                    if (!ValidationUtils.validateEarnCode(departmentEarnCode.getEarnCode(), departmentEarnCode.getEffectiveDate())) {
056                            this.putFieldError("earnCode", "error.existence", "Earncode '" + departmentEarnCode.getEarnCode()+ "'");
057                            return false;
058                    } else {
059                            return true;
060                    }
061            }
062    
063            private boolean validateDuplication(EarnCodeSecurity departmentEarnCode) {
064                    if(ValidationUtils.duplicateDeptEarnCodeExists(departmentEarnCode)) {
065                            this.putFieldError("effectiveDate", "deptEarncode.duplicate.exists");
066                            return false;
067                    } else {
068                            return true;
069                    }
070            }
071    
072            private boolean validateLocation(EarnCodeSecurity departmentEarnCode) {
073                    if (departmentEarnCode.getLocation() != null
074                                    && !ValidationUtils.validateLocation(departmentEarnCode.getLocation(), null) && 
075                                    !StringUtils.equals(departmentEarnCode.getLocation(), TkConstants.WILDCARD_CHARACTER)) {
076                            this.putFieldError("location", "error.existence", "location '"
077                                            + departmentEarnCode.getLocation() + "'");
078                            return false;
079                    } else {
080                            return true;
081                    }
082            }
083            
084            private boolean validateDepartmentCurrentUser(EarnCodeSecurity departmentEarnCode) {
085                    if (!TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isSystemAdmin() && !TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getOrgAdminDepartments().contains(departmentEarnCode.getDept())) {
086                            this.putFieldError("dept", "error.department.permissions", departmentEarnCode.getDept());
087                            return false;
088                    } else {
089                            return true;
090                    }
091            }
092            
093            private boolean isEarnCodeUsedByActiveTimeBlocks(EarnCodeSecurity departmentEarnCode){
094                    // KPME-1106 can not deactivate a department earn code if it used in active time blocks
095                    boolean valid = true;
096                    List<TimeBlock> latestEndTimestampTimeBlocks =  TkServiceLocator.getTimeBlockService().getLatestEndTimestampForEarnCode(departmentEarnCode.getEarnCode());
097                    
098                    if ( !departmentEarnCode.isActive() && !latestEndTimestampTimeBlocks.isEmpty() && departmentEarnCode.getEffectiveDate().before(latestEndTimestampTimeBlocks.get(0).getEndDate()) ){
099                            this.putFieldError("active", "deptEarncode.deptEarncode.inactivate", departmentEarnCode.getEarnCode());
100                            return false;
101                    } 
102                    
103                    return valid;
104                    
105            }
106    
107            /**
108             * It looks like the method that calls this class doesn't actually care
109             * about the return type.
110             */
111            @Override
112            protected boolean processCustomRouteDocumentBusinessRules(
113                            MaintenanceDocument document) {
114                    boolean valid = false;
115    
116                    LOG.debug("entering custom validation for EarnCodeSecurity");
117                    PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
118                    if (pbo instanceof EarnCodeSecurity) {
119                            EarnCodeSecurity departmentEarnCode = (EarnCodeSecurity) pbo;
120    
121                            if (departmentEarnCode != null) {
122                                    valid = true;
123                                    valid &= this.validateSalGroup(departmentEarnCode);
124                                    valid &= this.validateDept(departmentEarnCode);
125                                    valid &= this.validateEarnCode(departmentEarnCode);
126                                    valid &= this.validateDuplication(departmentEarnCode);
127                                    valid &= this.validateLocation(departmentEarnCode);
128                                    valid &= this.validateDepartmentCurrentUser(departmentEarnCode);
129                                    valid &= this.isEarnCodeUsedByActiveTimeBlocks(departmentEarnCode);
130                            }
131    
132                    }
133    
134                    return valid;
135            }
136    
137    }