@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;
  }
  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;
  }
  public void setupSizedImages(
      HttpServletRequest request, String sizedImages, MasterImageMetaData mimd) {
    Collection<SizedImageMetaData> simds = mimd.getSizedImages().values();

    UserSessionData usd = getUserSessionData(request);
    if (!StringUtils.isBlank(sizedImages)) {
      log.debug("setupSizedImages: Handling the sized images...");
      // Images have been selected and we didn't have any images stored previously
      String[] sizedImagesArray = sizedImages.split(",");
      log.debug(
          "setupSizedImages: the bean has " + sizedImagesArray.length + " sized image(s) selected");

      if (simds.size() == 0) {
        log.debug("setupSizedImages: no simds were previously defined");
        for (int x = 1; x <= sizedImagesArray.length; x++) {
          SizedImageMetaData simd = new SizedImageMetaData();
          ImageSizeDefinition isd = imageSizeDefMgr.getImageSize(sizedImagesArray[x - 1]);
          simd.setSizeDefinition(isd);
          mimd.addSizedImage(simd);
          log.debug("setupSizedImages: adding new image size " + isd.getLabel());
          usd.setDirty(true);
        }

      } else
      // Images have been selected and there were others stored previously
      {
        log.debug("setupSizedImages: simds have been previously defined");

        Map<String, SizedImageMetaData> previousSimds = mimd.getSizedImages();
        Map<String, SizedImageMetaData> newSimds = new HashMap<String, SizedImageMetaData>();

        for (int x = 0; x < sizedImagesArray.length; x++) {
          SizedImageMetaData simd = null;

          String key = sizedImagesArray[x];
          if (previousSimds.containsKey(key)) {
            simd = previousSimds.get(key);
            log.debug(
                "setupSizedImages: Image of type ["
                    + key
                    + "] previously chosen, re-using existing one");
            previousSimds.remove(key);
          } else {
            simd = new SizedImageMetaData();
            ImageSizeDefinition isd = imageSizeDefMgr.getImageSize(key);
            simd.setSizeDefinition(isd);
            log.debug(
                "setupSizedImages: Image of type ["
                    + key
                    + "] not previously chosen, created a new one");
            usd.setDirty(true);
          }
          newSimds.put(simd.getSizeDefinition().getCode(), simd);
        }
        mimd.setSizedImages(newSimds);
        if (previousSimds.size() > 0) { // some SIMDs were "left over", something has changed.
          usd.setDirty(true);
        }
      }

      log.debug("setupSizedImages: Sized Images in MIMD: " + mimd.getSizedImages().size());
    } else {
      if (simds.size() > 0) {
        log.debug(
            "setupSizedImages: There were sized images selected, but none have been selected now, so clearing out sized images");
        mimd.setSizedImages(new LinkedHashMap<String, SizedImageMetaData>());
        usd.setDirty(true);
      }
    }
  }