1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.common;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.joda.time.*;
20 import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
21 import org.kuali.kpme.core.api.earncode.EarnCode;
22 import org.kuali.kpme.core.util.HrConstants;
23 import org.kuali.kpme.core.util.TKUtils;
24 import org.kuali.kpme.core.util.ValidationUtils;
25
26 import java.math.BigDecimal;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 public class CalendarValidationUtil {
31
32 public static List<String> validateDates(String startDateS, String endDateS) {
33 List<String> errors = new ArrayList<String>();
34 if (errors.size() == 0 && StringUtils.isEmpty(startDateS)) errors.add("The start date is blank.");
35 if (errors.size() == 0 && StringUtils.isEmpty(endDateS)) errors.add("The end date is blank.");
36 return errors;
37 }
38
39 public static List<String> validateTimes(String startTimeS, String endTimeS) {
40 List<String> errors = new ArrayList<String>();
41 if (errors.size() == 0 && startTimeS == null) errors.add("The start time is blank.");
42 if (errors.size() == 0 && endTimeS == null) errors.add("The end time is blank.");
43 return errors;
44 }
45
46
47
48
49
50
51
52
53
54 public static List<String> validateEarnCode(String earnCode, String startDateString, String endDateString) {
55 List<String> errors = new ArrayList<String>();
56
57 LocalDate tempDate = TKUtils.formatDateTimeStringNoTimezone(startDateString).toLocalDate();
58 LocalDate localEnd = TKUtils.formatDateTimeStringNoTimezone(endDateString).toLocalDate();
59
60 while(!localEnd.isBefore(tempDate)) {
61 if(!ValidationUtils.validateEarnCode(earnCode, tempDate)) {
62 errors.add("Earn Code " + earnCode + " is not available for " + tempDate);
63 break;
64 }
65 tempDate = tempDate.plusDays(1);
66 }
67
68 return errors;
69 }
70
71 public static List<String> validateInterval(CalendarEntry payCalEntry, Long startTime, Long endTime) {
72 List<String> errors = new ArrayList<String>();
73 LocalDateTime pcb_ldt = payCalEntry.getBeginPeriodLocalDateTime();
74 LocalDateTime pce_ldt = payCalEntry.getEndPeriodLocalDateTime();
75
76
77
78
79
80
81
82 DateTime p_cal_b_dt = pcb_ldt.toDateTime();
83 DateTime p_cal_e_dt = pce_ldt.toDateTime();
84
85 Interval payInterval = new Interval(p_cal_b_dt, p_cal_e_dt);
86 if (errors.size() == 0 && !payInterval.contains(startTime)) {
87 errors.add("The start date/time is outside the calendar period");
88 }
89 if (errors.size() == 0 && !payInterval.contains(endTime) && p_cal_e_dt.getMillis() != endTime) {
90 errors.add("The end date/time is outside the calendar period");
91 }
92 return errors;
93 }
94
95 protected static List<String> validateDayParametersForLeaveEntry(EarnCode earnCode,
96 CalendarEntry calendarEntry, String startDate, String endDate, BigDecimal leaveAmount) {
97 List<String> errors = new ArrayList<String>();
98 if(leaveAmount == null) {
99 errors.add("The Day field should not be empty.");
100 return errors;
101 }
102 errors.addAll(validateDateTimeParametersForCalendarEntry(earnCode, calendarEntry, startDate, endDate));
103 return errors;
104 }
105
106 public static List<String> validateHourParametersForLeaveEntry(EarnCode earnCode,
107 CalendarEntry calendarEntry, String startDate, String endDate, BigDecimal leaveAmount) {
108 List<String> errors = new ArrayList<String>();
109 if(leaveAmount == null) {
110 errors.add("The Hour field should not be empty.");
111 return errors;
112 }
113 errors.addAll(validateDateTimeParametersForCalendarEntry(earnCode, calendarEntry, startDate, endDate));
114 return errors;
115 }
116
117
118
119
120 public static List<String> validateDateTimeParametersForCalendarEntry(EarnCode earnCode,
121 CalendarEntry calendarEntry, String startDate, String endDate) {
122 if(!(earnCode.getRecordMethod().equalsIgnoreCase(HrConstants.EARN_CODE_HOUR)
123 || earnCode.getRecordMethod().equalsIgnoreCase(HrConstants.EARN_CODE_AMOUNT) ))
124 return new ArrayList<String>();
125
126 List<String> errors = new ArrayList<String>();
127 errors.addAll(CalendarValidationUtil.validateDates(startDate, endDate));
128 if (errors.size() > 0)
129 return errors;
130
131 Long startTime= TKUtils.convertDateStringToDateTimeWithoutZone(startDate, "00:00:00").getMillis();
132 Long endTime= TKUtils.convertDateStringToDateTimeWithoutZone(endDate, "11:59:59").getMillis();
133 errors.addAll(CalendarValidationUtil.validateInterval(calendarEntry, startTime, endTime));
134 return errors;
135 }
136
137 public static List<String> validateSpanningWeeks(DateTime startDate, DateTime endDate) {
138 List<String> errors = new ArrayList<String>();
139
140 boolean isOnlyWeekendSpan = true;
141 while ((startDate.isBefore(endDate) || startDate.isEqual(endDate)) && isOnlyWeekendSpan) {
142 if (startDate.getDayOfWeek() != DateTimeConstants.SATURDAY && startDate.getDayOfWeek() != DateTimeConstants.SUNDAY) {
143 isOnlyWeekendSpan = false;
144 }
145 startDate = startDate.plusDays(1);
146 }
147 if (isOnlyWeekendSpan) {
148 errors.add("Weekend day is selected, but include weekends checkbox is not checked");
149 }
150
151 return errors;
152 }
153
154 }