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