1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.leaveplan.validation;
17
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.joda.time.LocalDate;
24 import org.kuali.kpme.core.leaveplan.LeavePlan;
25 import org.kuali.kpme.core.principal.PrincipalHRAttributes;
26 import org.kuali.kpme.core.service.HrServiceLocator;
27 import org.kuali.kpme.core.util.ValidationUtils;
28 import org.kuali.rice.kns.document.MaintenanceDocument;
29 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
30 import org.kuali.rice.krad.bo.PersistableBusinessObject;
31
32 public class LeavePlanValidation extends MaintenanceDocumentRuleBase {
33
34
35
36 boolean validateInactivation(LeavePlan leavePlan) {
37 boolean valid = true;
38
39
40
41
42 if (!leavePlan.isActive()) {
43
44 List<PrincipalHRAttributes> pList = HrServiceLocator
45 .getPrincipalHRAttributeService()
46 .getActiveEmployeesForLeavePlan(leavePlan.getLeavePlan(),
47 leavePlan.getEffectiveLocalDate());
48
49 if (pList != null && pList.size() > 0) {
50
51
52 this.putFieldError("active", "error.leaveplan.inactivate",
53 leavePlan.getLeavePlan());
54 valid = false;
55 }
56 }
57
58 return valid;
59 }
60
61
62 boolean validatePlanningMonths(String planningMonths) {
63 boolean valid = true;
64 if (planningMonths != null) {
65 int iPlanningMonths = Integer.parseInt(planningMonths);
66
67 if (iPlanningMonths > 24 || iPlanningMonths <= 0) {
68 this.putFieldError("planningMonths",
69 "error.leaveplan.planningMonths", "Planning Months");
70 valid = false;
71 }
72 }
73 return valid;
74 }
75
76 boolean validateEffectiveDate(LocalDate effectiveDate) {
77 boolean valid = true;
78 valid = ValidationUtils.validateOneYearFutureEffectiveDate(effectiveDate);
79 if(!valid) {
80 this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
81 }
82 return valid;
83 }
84
85 boolean validateCalendarYearStart(String dateString) {
86 if (StringUtils.isBlank(dateString)) {
87 return false;
88 }
89 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
90 sdf.setLenient(false);
91 try {
92 sdf.parse(dateString);
93 } catch (ParseException e) {
94 this.putFieldError("calendarYearStart",
95 "error.calendar.year.start", "Calendar Year Start");
96 return false;
97 }
98 return true;
99 }
100
101 @Override
102 protected boolean processCustomRouteDocumentBusinessRules(
103 MaintenanceDocument document) {
104 boolean valid = true;
105 LOG.debug("entering custom validation for Leave Plan");
106 PersistableBusinessObject pbo = (PersistableBusinessObject) this
107 .getNewBo();
108 if (pbo instanceof LeavePlan) {
109 LeavePlan leavePlan = (LeavePlan) pbo;
110 if (leavePlan != null) {
111 valid = true;
112 valid &= this.validateInactivation(leavePlan);
113 if (StringUtils.isNotEmpty(leavePlan.getPlanningMonths())) {
114 valid &= this.validatePlanningMonths(leavePlan
115 .getPlanningMonths());
116 }
117 if (leavePlan.getEffectiveDate() != null) {
118 valid &= this.validateEffectiveDate(
119 leavePlan.getEffectiveLocalDate());
120 }
121 valid &= this.validateCalendarYearStart(leavePlan
122 .getCalendarYearStart());
123 }
124 }
125 return valid;
126 }
127 }