1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.leavecode.validation;
17
18 import java.sql.Date;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.hr.lm.accrual.AccrualCategory;
22 import org.kuali.hr.lm.leavecode.LeaveCode;
23 import org.kuali.hr.time.service.base.TkServiceLocator;
24 import org.kuali.hr.time.util.ValidationUtils;
25 import org.kuali.rice.kns.document.MaintenanceDocument;
26 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
27 import org.kuali.rice.krad.bo.PersistableBusinessObject;
28
29 public class LeaveCodeValidation extends MaintenanceDocumentRuleBase {
30 boolean validateEffectiveDate(Date effectiveDate) {
31 boolean valid = true;
32 if (effectiveDate != null && !ValidationUtils.validateOneYearFutureDate(effectiveDate)) {
33 this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
34 valid = false;
35 }
36 return valid;
37 }
38
39
40
41
42
43
44
45
46
47
48
49 boolean validateLeavePlan(LeaveCode leaveCode) {
50
51 boolean valid = true;
52
53
54 if (StringUtils.isNotBlank(leaveCode.getLeavePlan())) {
55
56 if (!ValidationUtils.validateLeavePlan(leaveCode.getLeavePlan(), leaveCode.getEffectiveDate())) {
57 this.putFieldError("leavePlan", "error.existence", "leavePlan '"
58 + leaveCode.getLeavePlan() + "'");
59 valid = false;
60 return valid;
61 }
62
63 if (leaveCode.getEffectiveDate() != null && StringUtils.isNotBlank(leaveCode.getAccrualCategory())) {
64 AccrualCategory myTestAccrualCategoryObj = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(leaveCode.getAccrualCategory(), leaveCode.getEffectiveDate());
65 if (!myTestAccrualCategoryObj.getLeavePlan().equals(leaveCode.getLeavePlan())) {
66 this.putFieldError("leavePlan", "error.leaveCode.leavePlanMismatch", myTestAccrualCategoryObj.getLeavePlan());
67 valid = false;
68 return valid;
69 }
70 leaveCode.setLeavePlan(myTestAccrualCategoryObj.getLeavePlan());
71 }
72 }
73 return valid;
74 }
75
76
77 boolean validateAccrualCategory(String accrualCategory, Date asOfDate) {
78 boolean valid = true;
79 if (!ValidationUtils.validateAccCategory(accrualCategory, asOfDate)) {
80 this.putFieldError("accrualCategory", "error.existence", "accrualCategory '"
81 + accrualCategory + "'");
82 valid = false;
83 }
84 return valid;
85 }
86
87 boolean validateEarnCode(String earnCode, Date asOfDate) {
88 boolean valid = true;
89 if (!StringUtils.isEmpty(earnCode) && !ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
90 this.putFieldError("earnCode", "error.existence", "earnCode '"
91 + earnCode + "'");
92 valid = false;
93 }
94 return valid;
95 }
96
97 boolean validateDefaultAmountOfTime(Long defaultAmountofTime) {
98 boolean valid = true;
99 if ( defaultAmountofTime != null ){
100 if (defaultAmountofTime.compareTo(new Long(24)) > 0 || defaultAmountofTime.compareTo(new Long(0)) < 0) {
101 this.putFieldError("defaultAmountofTime", "error.leaveCode.hours", "Default Amount of Time '"
102 + defaultAmountofTime + "'");
103 valid = false;
104 }
105 }
106 return valid;
107 }
108
109 @Override
110 protected boolean processCustomRouteDocumentBusinessRules(
111 MaintenanceDocument document) {
112 boolean valid = false;
113 LOG.debug("entering custom validation for Leave Code");
114 PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
115 if (pbo instanceof LeaveCode) {
116 LeaveCode leaveCode = (LeaveCode) pbo;
117 if (leaveCode != null) {
118 valid = true;
119
120
121 valid &= this.validateAccrualCategory(leaveCode.getAccrualCategory(), leaveCode.getEffectiveDate());
122 valid &= this.validateEarnCode(leaveCode.getEarnCode(), leaveCode.getEffectiveDate());
123 valid &= this.validateDefaultAmountOfTime(leaveCode.getDefaultAmountofTime());
124 valid &= this.validateLeavePlan(leaveCode);
125 }
126 }
127 return valid;
128 }
129 }