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.earngroup.validation; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.hr.time.earngroup.EarnGroup; 020 import org.kuali.hr.time.earngroup.EarnGroupDefinition; 021 import org.kuali.hr.time.service.base.TkServiceLocator; 022 import org.kuali.hr.time.util.ValidationUtils; 023 import org.kuali.rice.kns.document.MaintenanceDocument; 024 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase; 025 import org.kuali.rice.krad.service.BusinessObjectService; 026 import org.kuali.rice.krad.service.KRADServiceLocator; 027 028 import java.util.*; 029 030 public class EarnGroupValidation extends MaintenanceDocumentRuleBase{ 031 032 @Override 033 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) { 034 EarnGroup earnGroup = (EarnGroup)this.getNewBo(); 035 Set<String> earnCodes = new HashSet<String>(); 036 int index = 0; 037 if(earnGroup.getEarnGroups().size() < 1){ 038 this.putGlobalError("earncode.required"); 039 return false; 040 } 041 for(EarnGroupDefinition earnGroupDef : earnGroup.getEarnGroups()){ 042 if(earnCodes.contains(earnGroupDef.getEarnCode())){ 043 this.putFieldError("earnGroups["+index+"].earnCode", "earngroup.duplicate.earncode",earnGroupDef.getEarnCode()); 044 045 } 046 if(earnGroup.getShowSummary()) { 047 validateEarnCode(earnGroupDef.getEarnCode().toUpperCase(), index, earnGroup); 048 } 049 if (!ValidationUtils.validateEarnCode(earnGroupDef.getEarnCode(), earnGroup.getEffectiveDate())) { 050 this.putFieldError("earnGroups["+index+"].earnCode", "error.existence", "Earncode '" + earnGroupDef.getEarnCode()+ "'"); 051 } 052 earnCodes.add(earnGroupDef.getEarnCode()); 053 index++; 054 } 055 int count = TkServiceLocator.getEarnGroupService().getNewerEarnGroupCount(earnGroup.getEarnGroup(), earnGroup.getEffectiveDate()); 056 if(count > 0) { 057 this.putFieldError("effectiveDate", "earngroup.effectiveDate.newr.exists"); 058 return false; 059 } 060 return true; 061 } 062 063 protected void validateEarnCode(String earnCode, int index, EarnGroup editedEarnGroup) { 064 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService(); 065 Map<String,Object> criteria = new HashMap<String,Object>(); 066 criteria.put("showSummary", "Y"); 067 criteria.put("active", "Y"); 068 Collection aCol = businessObjectService.findMatching(EarnGroup.class, criteria); 069 Iterator<EarnGroup> itr = aCol.iterator(); 070 while (itr.hasNext()) { 071 EarnGroup earnGroup = itr.next(); 072 if(!earnGroup.getHrEarnGroupId().equals(editedEarnGroup.getHrEarnGroupId())) { 073 criteria = new HashMap<String,Object>(); 074 criteria.put("hrEarnGroupId", earnGroup.getHrEarnGroupId()); 075 076 Collection earnGroupDefs = businessObjectService.findMatching(EarnGroupDefinition.class, criteria); 077 Iterator<EarnGroupDefinition> iterator = earnGroupDefs.iterator(); 078 while (iterator.hasNext()) { 079 EarnGroupDefinition def = iterator.next(); 080 if(StringUtils.equals(earnCode, def.getEarnCode().toUpperCase())) { 081 String[] parameters = new String[2]; 082 parameters[0] = earnCode; 083 parameters[1] = earnGroup.getDescr(); 084 this.putFieldError("earnGroups["+index+"].earnCode", "earngroup.earncode.already.used", parameters); 085 } 086 } 087 } 088 } 089 } 090 091 }