protected List<Map<String, String>> buildAllSizesList(MasterImageMetaData mimd) {
    List<Map<String, String>> allSizes = new ArrayList<Map<String, String>>();
    Map<String, SizedImageMetaData> sizedImages = mimd.getSizedImages();

    for (ImageSizeDefinition size : imageSizeDefMgr.getAllImageSizes()) {
      Map<String, String> sizeData = new HashMap<String, String>();
      sizeData.put("code", size.getCode());
      sizeData.put("label", size.getLabel());
      if (sizedImages.containsKey(size.getCode())) {
        sizeData.put("checked", "checked");
      }
      allSizes.add(sizeData);
    }

    return allSizes;
  }
  @Override
  protected Map<String, Object> referenceData(
      HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
    UserSessionData usd = getUserSessionData(request);
    Map<String, Object> referenceData = new HashMap<String, Object>();
    int currentPage = getCurrentPage(request);

    log.debug("referenceData: Current page in refData " + currentPage);

    referenceData.put("dirtyFlag", usd.isDirty() ? "true" : "false");
    referenceData.put("ImageUrlBuilder", urlBuilder);
    referenceData.put("allSizes", imageSizeDefMgr.getAllImageSizes());
    referenceData.put("page", currentPage);

    MasterImageMetaData mimd = usd.getMimd();

    if (usd.getPages() == null) {
      log.debug("referenceData: no pages defined yet");
      setPagesDynamically(request, null);
    }
    int pageCount = usd.getPages().length;
    log.debug("Page count is " + pageCount);
    referenceData.put("pagecount", pageCount);

    if (currentPage == 0) {
      String sizedDivStyle = "none";
      if (StringUtils.isNotBlank(mimd.getImageKey())) {
        sizedDivStyle = "block";
      }
      referenceData.put("sizedDivStyle", sizedDivStyle);
      referenceData.put("allDisplaySizes", buildAllSizesList(mimd));
      referenceData.put("metadata", buildMasterImageDisplay(mimd));
    }

    if (currentPage > 0 && currentPage < getPageCount(request, command) - 1) {
      SizedImageMetaData simd = getCurrentSizedImage(mimd, currentPage - 1);
      referenceData.put("SizedImage", simd);
      Map<String, String> cropbox = buildCropBox(simd);
      referenceData.put("cropbox", cropbox);
      ImageSizeDefinition isd = simd.getSizeDefinition();
      referenceData.put("sizecode", isd.getCode());
      referenceData.put("sizelabel", isd.getLabel());
      referenceData.put("definedHeight", isd.getHeight());
      referenceData.put("definedWidth", isd.getWidth());

      Map<String, String> displayImage = buildDisplayImage(usd);
      referenceData.put("displayImage", displayImage);

    } else if (currentPage == getPageCount(request, command) - 1) { // this is the confirm page.

      Map<String, String> displayImage = buildDisplayImage(usd);
      referenceData.put("displayImage", displayImage);
      List<Map<String, String>> sizedImagesDisplay =
          buildAllSizedImagesDisplay(mimd.getSizedImages());
      referenceData.put("sizedImages", sizedImagesDisplay);
    }

    return referenceData;
  }
  @Override
  protected Object formBackingObject(HttpServletRequest request) throws Exception {
    Object cmd = "Exists";

    String openImed = request.getParameter("openImed");
    log.debug("formBackingObject: openImed is: " + openImed);

    if (openImed != null) {
      if (openImed.equals("true")) {
        cmd = null;
        setUserSessionData(request, null);
        log.debug("formBackingObject: set cmd to null and cleared USD");
      }
    }

    // log.debug("in the form backing object with page " + getCurrentPage(request));
    String contentid = request.getParameter(IPSHtmlParameters.SYS_CONTENTID);
    log.debug("formBackingObject: The contentid is: " + contentid);

    if (StringUtils.isBlank(contentid)) {

      log.debug("formBackingObject: cmd is: " + cmd);
      try {
        log.debug(
            "formBackingObject: attempting to get command, if it doesn't exist, catch error and return new");
        Object command = getCommand(request);

        log.debug("formBackingObject: command exists");
        if (cmd == null) {
          log.debug("formBackingObject: opening from Action Menu, returning new");
          return new ImageBean();
        }
        if (command != null) {
          log.debug("formBackingObject: navigating, so using same command");
          return command;
        }
        log.debug(
            "formBackingObject: not coming from Action Menu, but command null? returning new");
        return new ImageBean();
      } catch (HttpSessionRequiredException hsre) {
        log.debug(
            "formBackingObject: info - Command does not exist yet, need to create it, continuing");
      }

      return new ImageBean();
    }
    // Found a content id, so load the item.
    log.debug("formBackingObject: found contentid [id:" + contentid + "]");
    String user = RxRequestUtils.getUserName(request);
    log.debug("formBackingObject: user is " + user);
    String session = RxRequestUtils.getSessionId(request);
    log.debug("formBackingObject: session is " + session);

    if (StringUtils.isNotBlank(contentid) && contentid != null) {
      log.debug("formBackingObject: contentid is not blank, going to open the image...");
      OpenImageResult oir = imagePersistenceManager.OpenImage(contentid, session, user);
      log.debug("formBackingObject: ItemStatus is  " + oir.getItemStatus());

      MasterImageMetaData mimd = oir.getMasterImage();
      log.info("formBackingObject: masterimage " + mimd);

      UserSessionData usd = getUserSessionData(request);
      usd.setMimd(mimd);
      setupDisplayImage(mimd, usd);

      ImageBean ib = new ImageBean();
      ib.setSysTitle(mimd.getSysTitle());
      ib.setDescription(mimd.getDescription());
      ib.setAlt(mimd.getAlt());
      ib.setDisplayTitle(mimd.getDisplayTitle());

      String sizedImages = null;
      Iterator<SizedImageMetaData> simds = mimd.getSizedImages().values().iterator();
      while (simds.hasNext()) {
        SizedImageMetaData simd = simds.next();
        ImageSizeDefinition isd = simd.getSizeDefinition();
        String code = isd.getCode();
        sizedImages += code;
        if (simds.hasNext()) sizedImages += ",";

        // testing to ensure simds have x,y,w,h
        log.debug(
            "formBackingObject: simd ["
                + code
                + "]: x -> "
                + simd.getX()
                + " == y -> "
                + simd.getY());
      }

      ib.setSizedImages(sizedImages);
      setUserSessionData(request, usd);
      setPagesDynamically(request, sizedImages);

      return ib;
    }
    log.debug("formBackingObject: contentid is blank, so creating a new item / imagebean");
    return new ImageBean();
  }