001 /** 002 * Copyright 2004-2012 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.earncodesec.validation; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.hr.earncodesec.EarnCodeSecurity; 020 import org.kuali.hr.time.roles.TkUserRoles; 021 import org.kuali.hr.time.service.base.TkServiceLocator; 022 import org.kuali.hr.time.timeblock.TimeBlock; 023 import org.kuali.hr.time.util.TkConstants; 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 import org.kuali.rice.krad.util.GlobalVariables; 029 030 import java.util.ArrayList; 031 import java.util.List; 032 033 034 public class EarnCodeSecurityRule extends MaintenanceDocumentRuleBase { 035 036 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 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 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 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 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 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 boolean isEarnCodeUsedByActiveTimeBlocks(EarnCodeSecurity departmentEarnCode){ 094 // KPME-1106 can not inactivation of a department earn code if it used in active time blocks 095 boolean valid = true; 096 List<TimeBlock> latestEndTimestampTimeBlocks = TkServiceLocator.getTimeBlockService().getLatestEndTimestamp(); 097 098 if ( !departmentEarnCode.isActive() && departmentEarnCode.getEffectiveDate().before(latestEndTimestampTimeBlocks.get(0).getEndDate()) ){ 099 List<TimeBlock> activeTimeBlocks = new ArrayList<TimeBlock>(); 100 activeTimeBlocks = TkServiceLocator.getTimeBlockService().getTimeBlocks(); 101 for(TimeBlock activeTimeBlock : activeTimeBlocks){ 102 if ( departmentEarnCode.getEarnCode().equals(activeTimeBlock.getEarnCode())){ 103 this.putFieldError("active", "deptEarncode.deptEarncode.inactivate", departmentEarnCode.getEarnCode()); 104 return false; 105 } 106 } 107 } 108 109 return valid; 110 111 } 112 113 /** 114 * It looks like the method that calls this class doesn't actually care 115 * about the return type. 116 */ 117 @Override 118 protected boolean processCustomRouteDocumentBusinessRules( 119 MaintenanceDocument document) { 120 boolean valid = false; 121 122 LOG.debug("entering custom validation for EarnCodeSecurity"); 123 PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo(); 124 if (pbo instanceof EarnCodeSecurity) { 125 EarnCodeSecurity departmentEarnCode = (EarnCodeSecurity) pbo; 126 127 if (departmentEarnCode != null) { 128 valid = true; 129 valid &= this.validateSalGroup(departmentEarnCode); 130 valid &= this.validateDept(departmentEarnCode); 131 valid &= this.validateEarnCode(departmentEarnCode); 132 valid &= this.validateDuplication(departmentEarnCode); 133 valid &= this.validateLocation(departmentEarnCode); 134 valid &= this.validateDepartmentCurrentUser(departmentEarnCode); 135 valid &= this.isEarnCodeUsedByActiveTimeBlocks(departmentEarnCode); 136 } 137 138 } 139 140 return valid; 141 } 142 143 }