Massive Argeo APIs refactoring
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / util / DefaultImageManager.java
index 1c6f7198413942206a04c5694a06b5073e0036b1..1fc9bd1be625e90d5d16424bf0917e498357593f 100644 (file)
@@ -4,7 +4,7 @@ import static javax.jcr.Node.JCR_CONTENT;
 import static javax.jcr.Property.JCR_DATA;
 import static javax.jcr.nodetype.NodeType.NT_FILE;
 import static javax.jcr.nodetype.NodeType.NT_RESOURCE;
-import static org.argeo.cms.ui.CmsConstants.NO_IMAGE_SIZE;
+import static org.argeo.cms.ui.CmsUiConstants.NO_IMAGE_SIZE;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -18,10 +18,10 @@ import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 
 import org.apache.commons.io.IOUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.CmsException;
-import org.argeo.cms.ui.CmsImageManager;
+import org.argeo.api.cms.Cms2DSize;
+import org.argeo.api.cms.CmsImageManager;
+import org.argeo.api.cms.CmsLog;
+import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrUtils;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.service.ResourceManager;
@@ -34,17 +34,17 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 
 /** Manages only public images so far. */
-public class DefaultImageManager implements CmsImageManager {
-       private final static Log log = LogFactory.getLog(DefaultImageManager.class);
+public class DefaultImageManager implements CmsImageManager<Control, Node> {
+       private final static CmsLog log = CmsLog.getLog(DefaultImageManager.class);
 //     private MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
 
-       public Boolean load(Node node, Control control, Point preferredSize) throws RepositoryException {
-               Point imageSize = getImageSize(node);
-               Point size;
+       public Boolean load(Node node, Control control, Cms2DSize preferredSize) {
+               Cms2DSize imageSize = getImageSize(node);
+               Cms2DSize size;
                String imgTag = null;
-               if (preferredSize == null || imageSize.x == 0 || imageSize.y == 0
-                               || (preferredSize.x == 0 && preferredSize.y == 0)) {
-                       if (imageSize.x != 0 && imageSize.y != 0) {
+               if (preferredSize == null || imageSize.getWidth() == 0 || imageSize.getHeight() == 0
+                               || (preferredSize.getWidth() == 0 && preferredSize.getHeight() == 0)) {
+                       if (imageSize.getWidth() != 0 && imageSize.getHeight() != 0) {
                                // actual image size if completely known
                                size = imageSize;
                        } else {
@@ -53,15 +53,15 @@ public class DefaultImageManager implements CmsImageManager {
                                imgTag = CmsUiUtils.noImg(size);
                        }
 
-               } else if (preferredSize.x != 0 && preferredSize.y != 0) {
+               } else if (preferredSize.getWidth() != 0 && preferredSize.getHeight() != 0) {
                        // given size if completely provided
                        size = preferredSize;
                } else {
                        // at this stage :
                        // image is completely known
-                       assert imageSize.x != 0 && imageSize.y != 0;
+                       assert imageSize.getWidth() != 0 && imageSize.getHeight() != 0;
                        // one and only one of the dimension as been specified
-                       assert preferredSize.x == 0 || preferredSize.y == 0;
+                       assert preferredSize.getWidth() == 0 || preferredSize.getHeight() == 0;
                        size = resizeTo(imageSize, preferredSize);
                }
 
@@ -86,7 +86,7 @@ public class DefaultImageManager implements CmsImageManager {
                } else if (control instanceof FileUpload) {
                        FileUpload lbl = (FileUpload) control;
                        lbl.setImage(CmsUiUtils.noImage(size));
-                       lbl.setSize(size);
+                       lbl.setSize(new Point(size.getWidth(), size.getHeight()));
                        return loaded;
                } else
                        loaded = false;
@@ -94,17 +94,19 @@ public class DefaultImageManager implements CmsImageManager {
                return loaded;
        }
 
-       private Point resizeTo(Point orig, Point constraints) {
-               if (constraints.x != 0 && constraints.y != 0) {
+       private Cms2DSize resizeTo(Cms2DSize orig, Cms2DSize constraints) {
+               if (constraints.getWidth() != 0 && constraints.getHeight() != 0) {
                        return constraints;
-               } else if (constraints.x == 0 && constraints.y == 0) {
+               } else if (constraints.getWidth() == 0 && constraints.getHeight() == 0) {
                        return orig;
-               } else if (constraints.y == 0) {// force width
-                       return new Point(constraints.x, scale(orig.y, orig.x, constraints.x));
-               } else if (constraints.x == 0) {// force height
-                       return new Point(scale(orig.x, orig.y, constraints.y), constraints.y);
+               } else if (constraints.getHeight() == 0) {// force width
+                       return new Cms2DSize(constraints.getWidth(),
+                                       scale(orig.getHeight(), orig.getWidth(), constraints.getWidth()));
+               } else if (constraints.getWidth() == 0) {// force height
+                       return new Cms2DSize(scale(orig.getWidth(), orig.getHeight(), constraints.getHeight()),
+                                       constraints.getHeight());
                }
-               throw new CmsException("Cannot resize " + orig + " to " + constraints);
+               throw new IllegalArgumentException("Cannot resize " + orig + " to " + constraints);
        }
 
        private int scale(int origDimension, int otherDimension, int otherConstraint) {
@@ -115,19 +117,19 @@ public class DefaultImageManager implements CmsImageManager {
                return ((float) a) / ((float) b);
        }
 
-       public Point getImageSize(Node node) throws RepositoryException {
+       public Cms2DSize getImageSize(Node node) {
                // TODO optimise
                Image image = getSwtImage(node);
-               return new Point(image.getBounds().width,image.getBounds().height);
+               return new Cms2DSize(image.getBounds().width, image.getBounds().height);
        }
 
        /** @return null if not available */
        @Override
-       public String getImageTag(Node node) throws RepositoryException {
+       public String getImageTag(Node node) {
                return getImageTag(node, getImageSize(node));
        }
 
-       private String getImageTag(Node node, Point size) throws RepositoryException {
+       private String getImageTag(Node node, Cms2DSize size) {
                StringBuilder buf = getImageTagBuilder(node, size);
                if (buf == null)
                        return null;
@@ -136,12 +138,12 @@ public class DefaultImageManager implements CmsImageManager {
 
        /** @return null if not available */
        @Override
-       public StringBuilder getImageTagBuilder(Node node, Point size) throws RepositoryException {
-               return getImageTagBuilder(node, Integer.toString(size.x), Integer.toString(size.y));
+       public StringBuilder getImageTagBuilder(Node node, Cms2DSize size) {
+               return getImageTagBuilder(node, Integer.toString(size.getWidth()), Integer.toString(size.getHeight()));
        }
 
        /** @return null if not available */
-       private StringBuilder getImageTagBuilder(Node node, String width, String height) throws RepositoryException {
+       private StringBuilder getImageTagBuilder(Node node, String width, String height) {
                String url = getImageUrl(node);
                if (url == null)
                        return null;
@@ -150,45 +152,35 @@ public class DefaultImageManager implements CmsImageManager {
 
        /** @return null if not available */
        @Override
-       public String getImageUrl(Node node) throws RepositoryException {
-               return CmsUiUtils.getDataPath(node);
-               // String name = getResourceName(node);
-               // ResourceManager resourceManager = RWT.getResourceManager();
-               // if (!resourceManager.isRegistered(name)) {
-               // InputStream inputStream = null;
-               // Binary binary = getImageBinary(node);
-               // if (binary == null)
-               // return null;
-               // try {
-               // inputStream = binary.getStream();
-               // resourceManager.register(name, inputStream);
-               // } finally {
-               // IOUtils.closeQuietly(inputStream);
-               // JcrUtils.closeQuietly(binary);
-               // }
-               // if (log.isTraceEnabled())
-               // log.trace("Registered image " + name);
-               // }
-               // return resourceManager.getLocation(name);
+       public String getImageUrl(Node node) {
+               return CmsUiUtils.getDataPathForUrl(node);
        }
 
-       protected String getResourceName(Node node) throws RepositoryException {
-               String workspace = node.getSession().getWorkspace().getName();
-               if (node.hasNode(JCR_CONTENT))
-                       return workspace + '_' + node.getNode(JCR_CONTENT).getIdentifier();
-               else
-                       return workspace + '_' + node.getIdentifier();
+       protected String getResourceName(Node node) {
+               try {
+                       String workspace = node.getSession().getWorkspace().getName();
+                       if (node.hasNode(JCR_CONTENT))
+                               return workspace + '_' + node.getNode(JCR_CONTENT).getIdentifier();
+                       else
+                               return workspace + '_' + node.getIdentifier();
+               } catch (RepositoryException e) {
+                       throw new JcrException(e);
+               }
        }
 
-       public Binary getImageBinary(Node node) throws RepositoryException {
-               if (node.isNodeType(NT_FILE)) {
-                       return node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary();
-               } else {
-                       return null;
+       public Binary getImageBinary(Node node) {
+               try {
+                       if (node.isNodeType(NT_FILE)) {
+                               return node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary();
+                       } else {
+                               return null;
+                       }
+               } catch (RepositoryException e) {
+                       throw new JcrException(e);
                }
        }
 
-       public Image getSwtImage(Node node) throws RepositoryException {
+       public Image getSwtImage(Node node) {
                InputStream inputStream = null;
                Binary binary = getImageBinary(node);
                if (binary == null)
@@ -196,6 +188,8 @@ public class DefaultImageManager implements CmsImageManager {
                try {
                        inputStream = binary.getStream();
                        return new Image(Display.getCurrent(), inputStream);
+               } catch (RepositoryException e) {
+                       throw new JcrException(e);
                } finally {
                        IOUtils.closeQuietly(inputStream);
                        JcrUtils.closeQuietly(binary);
@@ -203,7 +197,7 @@ public class DefaultImageManager implements CmsImageManager {
        }
 
        @Override
-       public String uploadImage(Node parentNode, String fileName, InputStream in) throws RepositoryException {
+       public String uploadImage(Node context, Node parentNode, String fileName, InputStream in, String contentType) {
                InputStream inputStream = null;
                try {
                        String previousResourceName = null;
@@ -220,10 +214,12 @@ public class DefaultImageManager implements CmsImageManager {
                        Node fileNode = JcrUtils.copyBytesAsFile(parentNode, fileName, arr);
                        inputStream = new ByteArrayInputStream(arr);
                        ImageData id = new ImageData(inputStream);
-                       processNewImageFile(fileNode, id);
+                       processNewImageFile(context, fileNode, id);
 
-                       String mime = Files.probeContentType(Paths.get(fileName));
-                       fileNode.setProperty(Property.JCR_MIMETYPE, mime);
+                       String mime = contentType != null ? contentType : Files.probeContentType(Paths.get(fileName));
+                       if (mime != null) {
+                               fileNode.getNode(JCR_CONTENT).setProperty(Property.JCR_MIMETYPE, mime);
+                       }
                        fileNode.getSession().save();
 
                        // reset resource manager
@@ -233,15 +229,18 @@ public class DefaultImageManager implements CmsImageManager {
                                if (log.isDebugEnabled())
                                        log.debug("Unregistered image " + previousResourceName);
                        }
-                       return getImageUrl(fileNode);
+                       return CmsUiUtils.getDataPath(fileNode);
                } catch (IOException e) {
-                       throw new CmsException("Cannot upload image " + fileName + " in " + parentNode, e);
+                       throw new RuntimeException("Cannot upload image " + fileName + " in " + parentNode, e);
+               } catch (RepositoryException e) {
+                       throw new JcrException(e);
                } finally {
                        IOUtils.closeQuietly(inputStream);
                }
        }
 
-       /** Does nothign by default. */
-       protected void processNewImageFile(Node fileNode, ImageData id) throws RepositoryException, IOException {
+       /** Does nothing by default. */
+       protected void processNewImageFile(Node context, Node fileNode, ImageData id)
+                       throws RepositoryException, IOException {
        }
 }