Esempio n. 1
0
  /**
   * Grab random holiday from the equivalence class that falls between the two dates
   *
   * @param earliest the earliest date parameter as defined in the model
   * @param latest the latest date parameter as defined in the model
   * @return a holiday that falls between the dates
   */
  public String getRandomHoliday(String earliest, String latest) {
    String dateString = "";
    DateTimeFormatter parser = ISODateTimeFormat.date();
    DateTime earlyDate = parser.parseDateTime(earliest);
    DateTime lateDate = parser.parseDateTime(latest);
    List<Holiday> holidays = new LinkedList<>();

    int min = Integer.parseInt(earlyDate.toString().substring(0, 4));
    int max = Integer.parseInt(lateDate.toString().substring(0, 4));
    int range = max - min + 1;
    int randomYear = (int) (Math.random() * range) + min;

    for (Holiday s : EquivalenceClassTransformer.HOLIDAYS) {
      holidays.add(s);
    }
    Collections.shuffle(holidays);

    for (Holiday holiday : holidays) {
      dateString = convertToReadableDate(holiday.forYear(randomYear));
      if (toDate(dateString).after(toDate(earliest)) && toDate(dateString).before(toDate(latest))) {
        break;
      }
    }
    return dateString;
  }
Esempio n. 2
0
 private JsonArrayNodeBuilder getHolidayInRegion(Date begin, Date end, Region region) {
   JsonArrayNodeBuilder builder = anArrayBuilder();
   List<Holiday> holidays = calendarService.getHolidaysOnlyForRegion(begin, end, region);
   for (Holiday holiday : holidays) {
     builder.withElement(
         aStringBuilder(dateToString(holiday.getCalDate().getCalDate(), VIEW_DATE_PATTERN)));
   }
   return builder;
 }
Esempio n. 3
0
 /**
  * Checks if the date is a holiday
  *
  * @param dateString the date
  * @return true if it is a holiday, false otherwise
  */
 public boolean isHoliday(String dateString) {
   boolean isHoliday = false;
   for (Holiday date : EquivalenceClassTransformer.HOLIDAYS) {
     if (convertToReadableDate(date.forYear(Integer.parseInt(dateString.substring(0, 4))))
         .equals(dateString)) {
       isHoliday = true;
     }
   }
   return isHoliday;
 }
Esempio n. 4
0
 public boolean isHoliday(Date date) {
   if (holidays != null) {
     for (Holiday holiday : holidays) {
       if (holiday.includes(date)) {
         return true;
       }
     }
   }
   return false;
 }
Esempio n. 5
0
 public void hello() {
   StringBuilder buffy = new StringBuilder("\nHello, ");
   buffy.append(message).append("\n");
   for (Holiday holiday : holidays) {
     buffy.append("Today is ");
     buffy.append(holiday.getDay());
     buffy.append("/").append(holiday.getMonth()).append(".  ");
     buffy.append(holiday.getGreeting()).append(".\n");
   }
   logger.info(buffy);
   buffy.delete(0, buffy.length());
 }
Esempio n. 6
0
 public String getHolidayListJSON(Date beginDate, Date endDate) {
   final JsonArrayNodeBuilder result = anArrayBuilder();
   // т.к. отпуска могут начинаться ранее или позднее заданных дат, то на всякий случай прибавим к
   // диапазону
   // по месяцу с обоих концов
   List<Holiday> holidays =
       calendarService.getHolidaysForRegion(
           DateUtils.addDays(beginDate, -30), DateUtils.addDays(endDate, 30), null);
   for (Holiday holiday : holidays) {
     result.withElement(
         aStringBuilder(dateToString(holiday.getCalDate().getCalDate(), VIEW_DATE_PATTERN)));
   }
   return JsonUtil.format(result);
 }
Esempio n. 7
0
  /**
   * Convert the holiday format from EquivalenceClassTransformer into a date format
   *
   * @param holiday the date
   * @return a date String in the format yyyy-MM-dd
   */
  public String convertToReadableDate(Holiday holiday) {
    DateTimeFormatter parser = ISODateTimeFormat.date();

    if (holiday.isInDateForm()) {
      String month =
          Integer.toString(holiday.getMonth()).length() < 2
              ? "0" + holiday.getMonth()
              : Integer.toString(holiday.getMonth());
      String day =
          Integer.toString(holiday.getDayOfMonth()).length() < 2
              ? "0" + holiday.getDayOfMonth()
              : Integer.toString(holiday.getDayOfMonth());
      return holiday.getYear() + "-" + month + "-" + day;
    } else {
      /*
       * 5 denotes the final occurrence of the day in the month. Need to find actual
       * number of occurrences
       */
      if (holiday.getOccurrence() == 5) {
        holiday.setOccurrence(
            numOccurrences(holiday.getYear(), holiday.getMonth(), holiday.getDayOfWeek()));
      }

      DateTime date =
          parser.parseDateTime(holiday.getYear() + "-" + holiday.getMonth() + "-" + "01");
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date.toDate());
      int count = 0;

      while (count < holiday.getOccurrence()) {
        if (calendar.get(Calendar.DAY_OF_WEEK) == holiday.getDayOfWeek()) {
          count++;
          if (count == holiday.getOccurrence()) {
            break;
          }
        }
        date = date.plusDays(1);
        calendar.setTime(date.toDate());
      }
      return date.toString().substring(0, 10);
    }
  }
Esempio n. 8
0
 /**
  * Determines whether a holiday can be taken by this driver.
  *
  * @param start_date the start date of the requested holiday
  * @param end_date the end date of the requested holiday
  * @return int[] array of ints. The first element is 0 if request denied, otherwise 1. The
  *     remaining elements correspond to each day in the requested range, where 0 = no
  *     availability, 1 = already booked by this driver, 2 = available.
  */
 public int[] request_holiday(GregorianCalendar start_date, GregorianCalendar end_date) {
   return Holiday.process_request(start_date, end_date, this);
 }