View Javadoc

1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * Validate the earn code exists on every day within the date rage. This method is moving to CalendarValidationUtil
52       * @param earnCode
53       * @param startDateString
54       * @param endDateString
55       *
56       * @return A list of error strings.
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  		// tempDate and localEnd could be the same day
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           * LeaveCalendarValidationUtil uses DateTimeZone... TimeDetailValidation does not...
81           * Does one require a non converted DateTime and the other not?
82           */
83          //DateTimeZone utz = HrServiceLocator.getTimezoneService().getUserTimezoneWithFallback();
84          //DateTime p_cal_b_dt = pcb_ldt.toDateTime(utz);
85          //DateTime p_cal_e_dt = pce_ldt.toDateTime(utz);
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 	 * Validates if the state/end dates is within the range of the calendar entry
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         // use beginning hour of the start date and ending hour of the end date to fake the time to validate intervals 
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");            //errorMessages
153 	    }
154 	    
155 	    return errors;
156 	}
157 
158 }