protected SimpleImageMetaData createScaledImage(MasterImageMetaData mimd, double scaleFactor)
     throws Exception {
   ImageMetaData imageData = mimd.getMetaData();
   SimpleImageMetaData output = new SimpleImageMetaData();
   long height = Math.round(imageData.getHeight() / scaleFactor);
   long width = Math.round(imageData.getWidth() / scaleFactor);
   Dimension dim = new Dimension(new Long(width).intValue(), new Long(height).intValue());
   resizeSimpleImage(output, mimd.getImageKey(), null, dim);
   return output;
 }
 protected void setupDisplayImage(MasterImageMetaData mimd, UserSessionData usd) throws Exception {
   log.debug(
       "height is "
           + mimd.getMetaData().getHeight()
           + " width is "
           + mimd.getMetaData().getWidth());
   double scalefactor =
       computeScaleFactor(mimd.getMetaData().getHeight(), mimd.getMetaData().getWidth());
   log.debug("Scale Factor is  " + scalefactor);
   usd.setScaleFactor(scalefactor);
   SimpleImageMetaData displayImage;
   if (scalefactor > 1.0) { // the image must be resized.
     displayImage = createScaledImage(mimd, scalefactor);
     log.debug("Display image is " + displayImage);
   } else {
     displayImage = new SimpleImageMetaData(mimd);
   }
   usd.setDisplayImage(displayImage);
 }
  protected Map<String, String> buildDisplayImage(UserSessionData usd) {
    MasterImageMetaData mimd = usd.getMimd();
    Map<String, String> displayImage = new HashMap<String, String>();
    displayImage.put("title", mimd.getDisplayTitle());
    displayImage.put("alt", mimd.getAlt());
    String url;
    if (usd.getDisplayImage() != null) {
      displayImage.put("height", String.valueOf(usd.getDisplayImage().getHeight()));
      displayImage.put("width", String.valueOf(usd.getDisplayImage().getWidth()));
      url = this.urlBuilder.buildUrl(usd.getDisplayImage().getImageKey());
      displayImage.put("url", url);
    } else {
      displayImage.put("height", String.valueOf(mimd.getMetaData().getHeight()));
      displayImage.put("width", String.valueOf(mimd.getMetaData().getWidth()));
      url = this.urlBuilder.buildUrl(mimd.getImageKey());
      displayImage.put("url", url);
    }

    return displayImage;
  }
  protected Map<String, String> buildMasterImageDisplay(MasterImageMetaData mimd) {
    ImageMetaData md = mimd.getMetaData();
    Map<String, String> masterData = new HashMap<String, String>();
    masterData.put("filename", md.getFilename());
    masterData.put("ext", md.getExt());
    masterData.put("mimetype", md.getMimeType());
    masterData.put("size", String.valueOf(md.getSize()));
    masterData.put("height", String.valueOf(md.getHeight()));
    masterData.put("width", String.valueOf(md.getWidth()));
    String url = this.urlBuilder.buildUrl(mimd.getImageKey());
    masterData.put("url", url);

    return masterData;
  }
  @Override
  protected void postProcessPage(
      HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
    UserSessionData usd = getUserSessionData(request);
    MasterImageMetaData mimd = usd.getMimd();

    if (page == 0) {
      log.debug("postProcessPage: processing main page");
      if (mimd.getMetaData().getSize() == 0L) {
        log.info("no image uploaded");
        errors.reject("master.image.missing");
      }
      if (mimd.getSizedImages().isEmpty()) {
        log.info("no image sizes selected");
        errors.reject("no.image.sizes.selected");
      }
    }
  }
  @Override
  protected void onBind(HttpServletRequest request, Object command, BindException errors)
      throws Exception {
    log.debug("onBind: doing onBind...");
    UserSessionData usd = getUserSessionData(request);
    MasterImageMetaData mimd = usd.getMimd();

    ImageBean ib = (ImageBean) command;

    if (mimd == null) {
      log.error("onBind: mimd attribute is not set in session.");
    } else {
      int currentPage = getCurrentPage(request);

      if (request instanceof MultipartHttpServletRequest) {
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
        MultipartFile mpFile = mRequest.getFile(FILE_UPLOAD_FIELD);

        String action = request.getParameter("action");
        log.debug("onBind: the action is: " + action);

        if (mpFile.isEmpty() && StringUtils.isBlank(action)) {
          log.debug("onBind: nothing has been uploaded - not storing image...");
        } else if (mpFile.isEmpty() && action.equals("cleared")) {
          log.debug(
              "onBind: nothing has been uploaded and something has been cleared - clearing stored image if any...");
          mimd.setImageKey(null);
          mimd.setMetaData(null);
          mimd.clearSizedImages();
          usd.setDirty(true); // clearing the file always sets dirty.
        } else {
          log.debug(
              "onBind: the original name of the file being uploaded is: "
                  + mpFile.getOriginalFilename());
          storeImage(mimd, mpFile);
          setupDisplayImage(mimd, usd);
          usd.setDirty(true); // uploaded files are always dirty.
        }

        mimd.setAlt(ib.getAlt());
        mimd.setDescription(ib.getDescription());
        mimd.setDisplayTitle(ib.getDisplayTitle());
        mimd.setSysTitle(ib.getSysTitle());
      }
      // set the dirty flag if the form is dirty or the session is already dirty.
      usd.setDirty(usd.isDirty() || ib.isDirty());
      log.debug("onBind: The dirty flag is " + usd.isDirty());

      log.debug(
          "onBind: There are "
              + getPageCount(request, command)
              + " pages. -- currently on page "
              + getCurrentPage(request));

      log.debug("onBind: current Page is " + currentPage);
      // Figure out how many pages you will need to go through
      if (currentPage == 0) {
        String contentId = request.getParameter("sys_contentid");
        String folderId = request.getParameter("sys_folderid");
        String sessionId = RxRequestUtils.getSessionId(request);
        String username = request.getRemoteUser();
        if (StringUtils.isBlank(contentId)
            && StringUtils.isNotBlank(folderId)
            && StringUtils.isNotBlank(ib.getSysTitle())) {
          boolean bval =
              imagePersistenceManager.validateSystemTitleUnique(
                  ib.getSysTitle(), folderId, sessionId, username);
          if (!bval) {
            log.info("system title is not unique in folder " + ib.getSysTitle());
            errors.rejectValue(
                "sysTitle",
                "title.not.unique",
                new String[] {ib.getSysTitle()},
                "System Title is not unique in the specified folder.");
          }
        }

        log.debug("onBind: ib.getSizedImages(): " + ib.getSizedImages());
        setPagesDynamically(request, ib.getSizedImages());
        setupSizedImages(request, ib.getSizedImages(), mimd);
      }

      if (currentPage > 0 && currentPage < getPageCount(request, command) - 1) {
        log.debug("onBind: dealing with binding data for page " + currentPage);

        SizedImageMetaData simd = getCurrentSizedImage(mimd, currentPage - 1);
        ImageSizeDefinition isd = simd.getSizeDefinition();
        if (ib.getX() == 0
            && ib.getY() == 0
            && ib.getWidth() == 0
            && ib.getHeight() == 0) { // ignore this request, it was likely caused by a refresh...
          log.debug("No crop box data present, skipping this request");
          return;
        }
        // can't do this on the client side because MSIE cannot detect change events.
        if (!usd.isDirty()) { // not already dirty, so we must check to see if the box has changed
          if (simd.getX() != ib.getX()
              || simd.getY() != ib.getY()
              || simd.getHeight() != ib.getHeight()
              || simd.getWidth() != ib.getWidth()) {
            usd.setDirty(true);
          }
        }

        Rectangle cropBox = new Rectangle(ib.getX(), ib.getY(), ib.getWidth(), ib.getHeight());
        double scaleFactor = usd.getScaleFactor();
        if (scaleFactor > 1.0) {
          log.debug("scaling crop box " + scaleFactor);
          ImageMetaData imd = mimd.getMetaData();
          cropBox =
              scaledRectangle(cropBox, scaleFactor, new Dimension(imd.getWidth(), imd.getHeight()));
          log.debug("new crop box is " + cropBox);
        }
        Dimension size = new Dimension(isd.getWidth(), isd.getHeight());

        resizeSimpleImage(simd, mimd.getImageKey(), cropBox, size);
        simd.setX(ib.getX());
        simd.setY(ib.getY());
        simd.setHeight(ib.getHeight());
        simd.setWidth(ib.getWidth());

        Boolean constraint = ib.isConstraint();
        log.debug("constraint from ib: " + constraint);
        log.debug("request constraint: " + request.getParameter("constraint"));
        if (constraint == null) simd.setConstraint(false);
        else if (constraint.booleanValue()) simd.setConstraint(constraint);

      } else log.debug("onBind: page is not > 0 or < " + (getPageCount(request, command) - 1));
    }

    log.debug("onBind: END");
  }