1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.timeoff.validation;
17
18 import java.math.BigDecimal;
19 import java.sql.Date;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.hr.lm.timeoff.SystemScheduledTimeOff;
23 import org.kuali.hr.time.earncode.EarnCode;
24 import org.kuali.hr.time.service.base.TkServiceLocator;
25 import org.kuali.hr.time.util.TkConstants;
26 import org.kuali.hr.time.util.ValidationUtils;
27 import org.kuali.rice.kns.document.MaintenanceDocument;
28 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
29 import org.kuali.rice.krad.bo.PersistableBusinessObject;
30
31 public class SystemScheduledTimeOffValidation extends MaintenanceDocumentRuleBase {
32 boolean validateEffectiveDate(Date effectiveDate) {
33 boolean valid = true;
34 if (effectiveDate != null && !ValidationUtils.validateOneYearFutureDate(effectiveDate)) {
35 this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
36 valid = false;
37 }
38 return valid;
39 }
40
41 boolean validateAccruedDate(Date accruedDate) {
42 boolean valid = true;
43 if (accruedDate != null && !ValidationUtils.validateFutureDate(accruedDate)) {
44 this.putFieldError("accruedDate", "error.future.date", "Accrued Date");
45 valid = false;
46 }
47 return valid;
48 }
49
50 boolean validateLocation(String location) {
51 boolean valid = true;
52 if(!StringUtils.isEmpty(location) && location.equals(TkConstants.WILDCARD_CHARACTER)) {
53 return true;
54 }
55 if (!ValidationUtils.validateLocation(location, null)) {
56 this.putFieldError("location", "error.existence", "location '"
57 + location + "'");
58 valid = false;
59 }
60 return valid;
61 }
62
63 boolean validateScheduledTimeOffDate(Date scheduledTimeOffDate) {
64 boolean valid = true;
65 if (scheduledTimeOffDate!= null && !ValidationUtils.validateFutureDate(scheduledTimeOffDate)) {
66 this.putFieldError("scheduledTimeOffDate", "error.future.date", "Scheduled Time Off Date");
67 valid = false;
68 }
69 return valid;
70 }
71
72 boolean validateUnusedTimeForScheduledTimeOffDate(Date scheduledTimeOffDate, String unusedTime) {
73 boolean valid = true;
74 if (scheduledTimeOffDate == null && (StringUtils.isEmpty(unusedTime) || StringUtils.equals("T", unusedTime) || StringUtils.equals("NUTA", unusedTime))) {
75 this.putFieldError("unusedTime", "error.unusedtime.bank.required", "Unused Time");
76 valid = false;
77 }
78 return valid;
79 }
80
81 boolean validateTransfertoEarnCode(String transfertoEarnCode) {
82 boolean valid = true;
83 if (StringUtils.isEmpty(transfertoEarnCode)) {
84 this.putFieldError("transfertoEarnCode", "error.required", "Transfer to Earn Code");
85 valid = false;
86 }
87 return valid;
88 }
89
90 boolean validateTransferConversionFactor(BigDecimal transferConversionFactor) {
91 boolean valid = true;
92 if (transferConversionFactor == null) {
93 this.putFieldError("transferConversionFactor", "error.required", "Transfer Conversion Factor");
94 valid = false;
95 }
96 return valid;
97 }
98
99 boolean validateEarnCode(String earnCode, Date asOfDate) {
100 boolean valid = true;
101 if (!ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
102 this.putFieldError("earnCode", "error.existence", "earnCode '"
103 + earnCode + "'");
104 valid = false;
105 }
106 return valid;
107 }
108
109 private boolean validateFraction(String earnCode, BigDecimal amount, Date asOfDate, String fieldName) {
110 boolean valid = true;
111 if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
112 EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
113 if(ec != null && ec.getFractionalTimeAllowed() != null) {
114 BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
115 String[] parameters = new String[2];
116 parameters[0] = earnCode;
117 parameters[1] = Integer.toString(fracAllowed.scale());
118 this.putFieldError(fieldName, "error.amount.fraction", parameters);
119 valid = false;
120 }
121 }
122 return valid;
123 }
124
125 @Override
126 protected boolean processCustomRouteDocumentBusinessRules(
127 MaintenanceDocument document) {
128 boolean valid = false;
129 LOG.debug("entering custom validation for SystemScheduledTimeOff");
130 PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
131 if (pbo instanceof SystemScheduledTimeOff) {
132 SystemScheduledTimeOff sysSchTimeOff = (SystemScheduledTimeOff) pbo;
133 if (sysSchTimeOff != null) {
134 valid = true;
135 valid &= this.validateEffectiveDate(sysSchTimeOff.getEffectiveDate());
136 valid &= this.validateAccruedDate(sysSchTimeOff.getAccruedDate());
137 valid &= this.validateScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffDate());
138 valid &= this.validateFraction(sysSchTimeOff.getEarnCode(),sysSchTimeOff.getAmountofTime(),sysSchTimeOff.getEffectiveDate(),"amountofTime");
139 valid &= this.validateUnusedTimeForScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffDate(), sysSchTimeOff.getUnusedTime());
140 valid &= this.validateLocation(sysSchTimeOff.getLocation());
141 valid &= this.validateEarnCode(sysSchTimeOff.getEarnCode(), sysSchTimeOff.getEffectiveDate());
142 }
143 }
144
145 return valid;
146 }
147 }