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