1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.earngroup.validation;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.hr.time.earngroup.EarnGroup;
20 import org.kuali.hr.time.earngroup.EarnGroupDefinition;
21 import org.kuali.hr.time.service.base.TkServiceLocator;
22 import org.kuali.hr.time.util.ValidationUtils;
23 import org.kuali.rice.kns.document.MaintenanceDocument;
24 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
25 import org.kuali.rice.krad.service.BusinessObjectService;
26 import org.kuali.rice.krad.service.KRADServiceLocator;
27
28 import java.util.*;
29
30 public class EarnGroupValidation extends MaintenanceDocumentRuleBase{
31
32 @Override
33 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
34 EarnGroup earnGroup = (EarnGroup)this.getNewBo();
35 Set<String> earnCodes = new HashSet<String>();
36 int index = 0;
37 if(earnGroup.getEarnGroups().size() < 1){
38 this.putGlobalError("earncode.required");
39 return false;
40 }
41 for(EarnGroupDefinition earnGroupDef : earnGroup.getEarnGroups()){
42 if(earnCodes.contains(earnGroupDef.getEarnCode())){
43 this.putFieldError("earnGroups["+index+"].earnCode", "earngroup.duplicate.earncode",earnGroupDef.getEarnCode());
44
45 }
46 if(earnGroup.getShowSummary()) {
47 validateEarnCode(earnGroupDef.getEarnCode().toUpperCase(), index, earnGroup);
48 }
49 if (!ValidationUtils.validateEarnCode(earnGroupDef.getEarnCode(), earnGroup.getEffectiveDate())) {
50 this.putFieldError("earnGroups["+index+"].earnCode", "error.existence", "Earncode '" + earnGroupDef.getEarnCode()+ "'");
51 }
52 earnCodes.add(earnGroupDef.getEarnCode());
53 index++;
54 }
55 int count = TkServiceLocator.getEarnGroupService().getNewerEarnGroupCount(earnGroup.getEarnGroup(), earnGroup.getEffectiveDate());
56 if(count > 0) {
57 this.putFieldError("effectiveDate", "earngroup.effectiveDate.newr.exists");
58 return false;
59 }
60 return true;
61 }
62
63 protected void validateEarnCode(String earnCode, int index, EarnGroup editedEarnGroup) {
64 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
65 Map<String,Object> criteria = new HashMap<String,Object>();
66 criteria.put("showSummary", "Y");
67 criteria.put("active", "Y");
68 Collection aCol = businessObjectService.findMatching(EarnGroup.class, criteria);
69 Iterator<EarnGroup> itr = aCol.iterator();
70 while (itr.hasNext()) {
71 EarnGroup earnGroup = itr.next();
72 if(!earnGroup.getHrEarnGroupId().equals(editedEarnGroup.getHrEarnGroupId())) {
73 criteria = new HashMap<String,Object>();
74 criteria.put("hrEarnGroupId", earnGroup.getHrEarnGroupId());
75
76 Collection earnGroupDefs = businessObjectService.findMatching(EarnGroupDefinition.class, criteria);
77 Iterator<EarnGroupDefinition> iterator = earnGroupDefs.iterator();
78 while (iterator.hasNext()) {
79 EarnGroupDefinition def = iterator.next();
80 if(StringUtils.equals(earnCode, def.getEarnCode().toUpperCase())) {
81 String[] parameters = new String[2];
82 parameters[0] = earnCode;
83 parameters[1] = earnGroup.getDescr();
84 this.putFieldError("earnGroups["+index+"].earnCode", "earngroup.earncode.already.used", parameters);
85 }
86 }
87 }
88 }
89 }
90
91 }