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 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       * Validate the earn code exists on every day within the date rage. This method is moving to CalendarValidationUtil
48       * @param earnCode
49       * @param startDateString
50       * @param endDateString
51       *
52       * @return A list of error strings.
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  		// tempDate and localEnd could be the same day
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           * LeaveCalendarValidationUtil uses DateTimeZone... TimeDetailValidation does not...
77           * Does one require a non converted DateTime and the other not?
78           */
79          //DateTimeZone utz = HrServiceLocator.getTimezoneService().getUserTimezoneWithFallback();
80          //DateTime p_cal_b_dt = pcb_ldt.toDateTime(utz);
81          //DateTime p_cal_e_dt = pce_ldt.toDateTime(utz);
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 	 * Validates if the state/end dates is within the range of the calendar entry
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         // use beginning hour of the start date and ending hour of the end date to fake the time to validate intervals 
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");            //errorMessages
149 	    }
150 	    
151 	    return errors;
152 	}
153 
154 }