View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.earncodegroup.validation;
17  
18  import java.util.Collection;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.hr.time.earncodegroup.EarnCodeGroup;
27  import org.kuali.hr.time.earncodegroup.EarnCodeGroupDefinition;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.util.ValidationUtils;
30  import org.kuali.rice.kns.document.MaintenanceDocument;
31  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
32  import org.kuali.rice.krad.service.BusinessObjectService;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  
35  public class EarnCodeGroupValidation  extends MaintenanceDocumentRuleBase{
36  
37  	@Override
38  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
39  		EarnCodeGroup earnGroup = (EarnCodeGroup)this.getNewBo();
40  		Set<String> earnCodes = new HashSet<String>();
41  		int index = 0;
42  		if(earnGroup.getEarnCodeGroups().size() < 1){
43  			this.putGlobalError("earncode.required");
44  			return false;
45  		}
46  		for(EarnCodeGroupDefinition earnGroupDef : earnGroup.getEarnCodeGroups()){
47  			if(earnCodes.contains(earnGroupDef.getEarnCode())){
48  				this.putFieldError("earnCodeGroups["+index+"].earnCode", "earngroup.duplicate.earncode",earnGroupDef.getEarnCode());
49  
50  			}
51  			if(earnGroup.getShowSummary()) {
52  				validateEarnCode(earnGroupDef.getEarnCode().toUpperCase(), index, earnGroup);
53  			}
54  			if (!ValidationUtils.validateEarnCode(earnGroupDef.getEarnCode(), earnGroup.getEffectiveDate())) {
55  				this.putFieldError("earnCodeGroups["+index+"].earnCode", "error.existence", "Earncode '" + earnGroupDef.getEarnCode()+ "'");
56  			}
57  			earnCodes.add(earnGroupDef.getEarnCode());
58  			index++;
59  		}
60  		int count = TkServiceLocator.getEarnCodeGroupService().getNewerEarnCodeGroupCount(earnGroup.getEarnCodeGroup(), earnGroup.getEffectiveDate());
61  		if(count > 0) {
62  			this.putFieldError("effectiveDate", "earngroup.effectiveDate.newr.exists");
63  			return false;
64  		}
65  		return true;
66  	}
67  
68      protected void validateEarnCode(String earnCode, int index, EarnCodeGroup editedEarnGroup) {
69      	BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
70      	Map<String,Object> criteria = new HashMap<String,Object>();
71  		criteria.put("showSummary", "Y");
72  		criteria.put("active", "Y");
73      	Collection aCol = businessObjectService.findMatching(EarnCodeGroup.class, criteria);
74  		Iterator<EarnCodeGroup> itr = aCol.iterator();
75  		while (itr.hasNext()) {
76  			EarnCodeGroup earnGroup = itr.next();
77  			if(!earnGroup.getHrEarnCodeGroupId().equals(editedEarnGroup.getHrEarnCodeGroupId())) {
78  				criteria = new HashMap<String,Object>();
79  				criteria.put("hrEarnCodeGroupId", earnGroup.getHrEarnCodeGroupId());
80  
81  				Collection earnGroupDefs = businessObjectService.findMatching(EarnCodeGroupDefinition.class, criteria);
82  				Iterator<EarnCodeGroupDefinition> iterator = earnGroupDefs.iterator();
83  				while (iterator.hasNext()) {
84  					EarnCodeGroupDefinition def = iterator.next();
85  					if(StringUtils.equals(earnCode, def.getEarnCode().toUpperCase())) {
86  						String[] parameters = new String[2];
87  						parameters[0] = earnCode;
88  						parameters[1] = earnGroup.getDescr();
89  						this.putFieldError("earnCodeGroups["+index+"].earnCode", "earngroup.earncode.already.used", parameters);
90  					}
91  				}
92  			}
93  		}
94      }
95  
96   }