public Image updateImage(long imageId, byte[] bytes, String type, int height, int width, int size)
      throws PortalException, SystemException {

    Image image = imagePersistence.fetchByPrimaryKey(imageId);

    if (image == null) {
      image = imagePersistence.create(imageId);
    }

    image.setModifiedDate(new Date());
    image.setType(type);
    image.setHeight(height);
    image.setWidth(width);
    image.setSize(size);

    Hook hook = HookFactory.getInstance();

    hook.updateImage(image, type, bytes);

    imagePersistence.update(image, false);

    WebServerServletTokenUtil.resetToken(imageId);

    return image;
  }
  protected Image convertFileEntry(boolean smallImage, FileEntry fileEntry)
      throws PortalException, SystemException {

    try {
      Image image = new ImageImpl();

      image.setModifiedDate(fileEntry.getModifiedDate());

      InputStream is = null;

      if (smallImage) {
        is = ImageProcessorUtil.getThumbnailAsStream(fileEntry.getFileVersion(), 0);
      } else {
        is = fileEntry.getContentStream();
      }

      byte[] bytes = FileUtil.getBytes(is);

      image.setTextObj(bytes);

      image.setType(fileEntry.getExtension());

      return image;
    } catch (PortalException pe) {
      throw pe;
    } catch (SystemException se) {
      throw se;
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static Image toModel(ImageSoap soapModel) {
    Image model = new ImageImpl();

    model.setImageId(soapModel.getImageId());
    model.setModifiedDate(soapModel.getModifiedDate());
    model.setText(soapModel.getText());
    model.setType(soapModel.getType());
    model.setHeight(soapModel.getHeight());
    model.setWidth(soapModel.getWidth());
    model.setSize(soapModel.getSize());

    return model;
  }
  protected Image getImage(InputStream is, byte[] bytes) throws PortalException, SystemException {

    try {
      if (is != null) {
        bytes = FileUtil.getBytes(is);
      }

      ImageBag imageBag = ImageProcessorUtil.read(bytes);

      RenderedImage renderedImage = imageBag.getRenderedImage();
      String type = imageBag.getType();

      if (renderedImage == null) {
        throw new ImageTypeException();
      }

      int height = renderedImage.getHeight();
      int width = renderedImage.getWidth();
      int size = bytes.length;

      Image image = new ImageImpl();

      image.setTextObj(bytes);
      image.setType(type);
      image.setHeight(height);
      image.setWidth(width);
      image.setSize(size);

      return image;
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException ioe) {
          if (_log.isWarnEnabled()) {
            _log.warn(ioe);
          }
        }
      }
    }
  }
  protected Image getImage(InputStream is, byte[] bytes, boolean cleanUpStream)
      throws PortalException, SystemException {

    try {
      if (is != null) {
        bytes = FileUtil.getBytes(is, -1, cleanUpStream);
      }

      if (bytes == null) {
        return null;
      }

      ImageBag imageBag = ImageToolUtil.read(bytes);

      RenderedImage renderedImage = imageBag.getRenderedImage();
      String type = imageBag.getType();

      if (renderedImage == null) {
        throw new ImageTypeException();
      }

      int height = renderedImage.getHeight();
      int width = renderedImage.getWidth();
      int size = bytes.length;

      Image image = new ImageImpl();

      image.setTextObj(bytes);
      image.setType(type);
      image.setHeight(height);
      image.setWidth(width);
      image.setSize(size);

      return image;
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    }
  }