/**
  * return size of capacity units at its maximum use in given bookings. does NOT check the {@link
  * BookingStatus} of given bookings. <br>
  * do not use it with many bookings, because:<br>
  * <code>complexity = bookings.size()^2 + m</code>
  *
  * @param bookings to check
  * @return size of capacity units used maximum at once in given bookings.
  */
 public static int getSumOfMaxCapacityUsedAtOneTime(List<Booking> bookings) {
   int max = 0;
   for (Booking booking1 : bookings) {
     int tmpmax = 0;
     for (Booking booking2 : bookings) {
       if (booking1.overlaps(booking2)) { // booking 1 and 2 are identical or overlapping
         tmpmax += booking2.getCapacityUnits();
       }
     }
     if (tmpmax > max) {
       max = tmpmax;
     }
   }
   return max;
 }
  @Test
  public void getJobWithId() {
    this.clearDatabase();

    // ↘ this is the job
    JSONObject jobSurvey = new JSONObject();
    try {
      jobSurvey.put("foo", "bar");
      List<String> animals = new ArrayList<String>();
      animals.add("dog");
      animals.add("cat");
      animals.add("mouse");
      jobSurvey.put("zoo", animals);
    } catch (JSONException e) {
      fail("should not throw " + e);
    }

    // ↘ get job of this booking as json
    Booking booking = TeztBeanSimpleFactory.getNewValidBooking();
    booking.setBooked();
    booking.insert();
    Job document = new Job();
    document.setJobId(booking.getId());
    document.setUsername(booking.getUsername());
    document.setStep(0);
    document.setIdJobDataProcessing("foo");
    document.setJobSurvey(jobSurvey);
    assertTrue(document.insertOrUpdate());

    // ↘ get json stuff being tested
    GetJobController gjc = new GetJobController(booking.getUser());
    MockHttpServletRequest rq = new MockHttpServletRequest();
    rq.setParameter("id", booking.getId() + "");
    JSONObject got = gjc.getJSONObject(rq, null);

    // ↘ test results
    try {
      assertEquals("bar", got.getString("foo"));
      @SuppressWarnings("rawtypes")
      Map zoo = (Map) got.get("zoo");
      assertEquals("dog", zoo.get("0"));
      assertEquals("cat", zoo.get("1"));
      assertEquals("mouse", zoo.get("2"));
    } catch (JSONException e) {
      fail("should not throw " + e);
    }
  }
  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;
  }