@Test
  public void getActualMonth() throws Exception {
    Calendar give = Calendar.getInstance();
    TimeFrame got = new TimeFrameFactory(give).getMonth();
    Calendar start = got.getCalendarStart();
    assertEquals(start.get(Calendar.MILLISECOND), 0);
    assertEquals(start.get(Calendar.SECOND), 0);
    assertEquals(start.get(Calendar.MINUTE), 0);
    assertEquals(start.get(Calendar.HOUR_OF_DAY), 0);
    assertEquals(start.get(Calendar.DAY_OF_MONTH), 1);
    assertEquals(start.get(Calendar.MONTH), give.get(Calendar.MONTH));
    assertEquals(start.get(Calendar.YEAR), give.get(Calendar.YEAR));

    start.add(Calendar.MONTH, 1);

    Calendar end = got.getCalendarEnd();

    assertEquals(end.get(Calendar.MILLISECOND), 0);
    assertEquals(end.get(Calendar.SECOND), 0);
    assertEquals(end.get(Calendar.MINUTE), 0);
    assertEquals(end.get(Calendar.HOUR_OF_DAY), 0);
    assertEquals(end.get(Calendar.DAY_OF_MONTH), 1);
    assertEquals(end.get(Calendar.MONTH), start.get(Calendar.MONTH));
    assertEquals(end.get(Calendar.YEAR), start.get(Calendar.YEAR));
  }
 private void createIntervalTimeFrameNotSet() {
   if (this.getIntervalTimeFrame() == null) {
     this.intervalTimeFrame = new SimpleIntervalTimeFrame();
     TimeFrame tf = this.intervalTimeFrame.getBasePeriodOfTime();
     tf.add(Calendar.YEAR, -1000);
     this.intervalTimeFrame.setBasePeriodOfTime(tf);
   }
 }
 @Test
 public void getDuration() throws Exception {
   Calendar give = Calendar.getInstance();
   int minutes = 5;
   TimeFrame got = new TimeFrameFactory(give).getDuration(Calendar.MINUTE, minutes);
   Calendar end = (Calendar) give.clone();
   end.add(Calendar.MINUTE, minutes);
   assertEquals(new SimpleTimeFrame(give, end).toString(), got.toString());
 }
 @Test
 public void getMonthWithFullWeeks() throws Exception {
   Calendar give = Calendar.getInstance();
   give.add(Calendar.MONTH, 3);
   TimeFrameFactory tff = new TimeFrameFactory(give);
   TimeFrame got = tff.getMonthWithFullWeeks();
   // assert "first day of week" and more or equal units as the pure month
   assertEquals(got.getCalendarStart().get(Calendar.DAY_OF_WEEK), give.getFirstDayOfWeek());
   assertEquals(got.getCalendarEnd().get(Calendar.DAY_OF_WEEK), give.getFirstDayOfWeek());
   assertTrue(got.getDuration() >= tff.getMonth().getDuration());
 }
 /**
  * return true, if this facility availability is applicable to the given time frame. at most, this
  * is, if the given time frame overlaps with this facility availability. if availability is {@link
  * #BOOKING_MUST_NOT_START_HERE}, return true, if given time frame starts on interval time frame.
  * Otherwise return true,
  *
  * @param timeframe to check
  * @return true, if this facility availability is applicable to the given time frame.
  */
 public boolean applicableTo(TimeFrame timeframe) {
   if (this.mustNotStartHere()) {
     boolean result = false;
     for (TimeFrame overlapping : this.getSingleSimpleTimeFrames(timeframe)) {
       if (overlapping.contains(timeframe.getCalendarStart().getTime())) {
         result = true;
         break;
       }
     }
     return result;
   } else {
     return this.overlaps(timeframe);
   }
 }
  @Test
  public void getMonthWithFullWeeks_recognizeFirstDayOfWeek() throws Exception {
    Calendar give1 = Calendar.getInstance();
    Calendar give2 = Calendar.getInstance();
    give2.setFirstDayOfWeek(give1.getFirstDayOfWeek() + 3);

    TimeFrame got1 = new TimeFrameFactory(give1).getMonthWithFullWeeks();
    TimeFrame got2 = new TimeFrameFactory(give2).getMonthWithFullWeeks();

    assertTrue(
        got1.getCalendarStart().get(Calendar.DAY_OF_YEAR) + 3
                == got2.getCalendarStart().get(Calendar.DAY_OF_YEAR)
            || got1.getCalendarStart().get(Calendar.DAY_OF_YEAR) - 4
                == got2.getCalendarStart().get(Calendar.DAY_OF_YEAR));
    assertTrue(
        got1.getCalendarEnd().get(Calendar.DAY_OF_YEAR) + 3
                == got2.getCalendarEnd().get(Calendar.DAY_OF_YEAR)
            || got1.getCalendarEnd().get(Calendar.DAY_OF_YEAR) - 4
                == got2.getCalendarEnd().get(Calendar.DAY_OF_YEAR));
  }
 /**
  * construct availability for given facility and time frame. after constructing the availability
  * is set to <code>null</code>.
  *
  * @param facilityKey representing the facility the availability is set for.
  * @param timeFrame base period of time the availability is set for.
  */
 public FacilityAvailability(String facilityKey, TimeFrame timeFrame) {
   this(facilityKey, timeFrame.getCalendarStart(), timeFrame.getCalendarEnd());
 }
  private HtmlElement getSummary(HttpServletRequest request) {
    HtmlElement result = HtmlFactory.get("ul").addClassName("asList");
    HtmlElement warningSpan = HtmlFactory.get("span").addClassName("warning");

    // summary availability
    Integer availability = RequestInterpreter.getAvailability(request);
    // is valid availability
    if (availability != null
        && (availability == COMPLETE_AVAILABLE
            || availability == GENERAL_NOT_AVAILABLE
            || availability == BOOKED_NOT_AVAILABLE
            || availability == MAINTENANCE_NOT_AVAILABLE
            || availability == BOOKING_MUST_NOT_START_HERE)) {
      result.add(
          HtmlFactory.get("li")
              .add(HtmlFactory.get("strong").add("Availability"))
              .add(": ")
              .add(FamText.facilityAvailability(availability))); // INTLANG;
    }

    // summary interval
    Integer interval = RequestInterpreter.getInterval(request);
    if (interval != null) {
      result.add(
          HtmlFactory.get("li")
              .add(HtmlFactory.get("strong").add("Interval"))
              .add(": ")
              .add(FamText.message("calendar.iteration." + interval))); // INTLANG;
    }

    // summary notice
    String notice = RequestInterpreter.getNotice(request);
    if (notice != null) {
      result.add(
          HtmlFactory.get("li")
              .add(HtmlFactory.get("strong").add("Notice"))
              .add(": ")
              .add(notice)); // INTLANG;
    }

    if (availability != null
        && availability.intValue() != FacilityAvailability.COMPLETE_AVAILABLE) {
      FacilityAvailability da =
          RequestInterpreter.getCompleteFacilityAvailabilityForInsertion(
              request, SessionAuth.user(request));
      if (da != null) {

        TimeFrame baseTimeFrame = da.getBasePeriodOfTime();
        if (baseTimeFrame != null) {
          String timeText =
              interval == FacilityAvailability.ONE_TIME
                  ? "Coming into effect"
                  : "First time coming into effect"; // INTLANG
          result.add(
              HtmlFactory.get("li")
                  .add(HtmlFactory.get("strong").add(timeText))
                  .add(": ")
                  .add(baseTimeFrame));

          // summarize bookings being canceled
          List<Facility> facilities = new ArrayList<Facility>();
          facilities.add(da.getFacility());
          List<Booking> bookings = FamDaoProxy.bookingDao().getAll(facilities);
          int negativeAnswers = 0;
          for (Booking booking : bookings) {
            if (!booking.isCanceled()
                && !booking.sessionAlreadyBegun()
                && booking.getIdBookedInBookingStrategy() != BookingStrategy.QUEUE_BASED
                && da.applicableTo(booking.getSessionTimeFrame())) {
              negativeAnswers++;
            }
          }

          result.add(
              HtmlFactory.get("li")
                  .add(
                      HtmlFactory.get("strong")
                          .add("Number of letters of refusal sent with this input"))
                  .add(": ")
                  .add(
                      negativeAnswers == 0
                          ? "no letter"
                          : warningSpan
                              .setContent(negativeAnswers + " letter(s)")
                              .toString())); // INTLANG;

          // warning on nothing set or left
          long durationOfBaseTime = baseTimeFrame.getDuration();
          long durationOfAnHour = 1000l * 60 * 60;
          boolean willBlockResource = false;
          boolean nothingSet = durationOfBaseTime <= 0;
          if (!nothingSet
              && interval != FacilityAvailability.ONE_TIME
              && availability != FacilityAvailability.COMPLETE_AVAILABLE) {
            if ((interval == FacilityAvailability.EACH_YEAR
                    && durationOfBaseTime >= durationOfAnHour * 24 * 365)
                || (interval == FacilityAvailability.EACH_MONTH
                    && durationOfBaseTime >= durationOfAnHour * 24 * 365 / 12)
                || (interval == FacilityAvailability.EACH_WEEK
                    && durationOfBaseTime >= durationOfAnHour * 24 * 7)
                || (interval == FacilityAvailability.EACH_DAY
                    && durationOfBaseTime >= durationOfAnHour * 24)
                || (interval == FacilityAvailability.EACH_HOUR
                    && durationOfBaseTime >= durationOfAnHour)) {
              willBlockResource = true;
            }
          }
          if (willBlockResource) {
            result.add(
                HtmlFactory.get("li")
                    .add(
                        warningSpan.setContent(
                            "If setting this, the facility will never ever be available!"))); // INTLANG;
          }
          if (nothingSet) {
            result.add(
                HtmlFactory.get("li")
                    .add(warningSpan.setContent("The duration of your setting is 0!"))); // INTLANG;
          }
        }
      }
    }
    return result;
  }