001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.pictures.tiles.service;
021
022import java.io.StringWriter;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.Blobs;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.platform.picture.api.ImageInfo;
032import org.nuxeo.ecm.platform.picture.api.ImagingService;
033import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant;
034import org.nuxeo.ecm.platform.preview.adapter.AbstractPreviewer;
035import org.nuxeo.ecm.platform.preview.adapter.ImagePreviewer;
036import org.nuxeo.ecm.platform.preview.adapter.MimeTypePreviewer;
037import org.nuxeo.ecm.platform.preview.api.PreviewException;
038import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * @author Alexandre Russel
043 */
044public class TiledImagePreviewer extends AbstractPreviewer implements MimeTypePreviewer {
045
046    private static final Log log = LogFactory.getLog(TiledImagePreviewer.class);
047
048    protected static final String ORIGINAL_JPEG_VIEW_NAME = "OriginalJpeg";
049
050    /**
051     * @deprecated since 7.2. The Original view does not exist anymore. See NXP-16070.
052     */
053    @Deprecated
054    protected static final String ORIGINAL_VIEW_NAME = "Original";
055
056    public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException {
057        if (useTiling(blob)) {
058            List<Blob> blobResults = new ArrayList<Blob>();
059            String htmlFile = getString().replace("$repoId$", dm.getRepositoryName());
060            htmlFile = htmlFile.replace("$docId$", dm.getId());
061            htmlFile = htmlFile.replace("$tileWidth$", "" + 200);
062            htmlFile = htmlFile.replace("$tileHeight$", "" + 200);
063            htmlFile = htmlFile.replace("$maxTiles$", "" + 2);
064            Blob mainBlob = Blobs.createBlob(htmlFile, "text/html", null, "index.html");
065            blob.setFilename("image");
066
067            blobResults.add(mainBlob);
068            blobResults.add(blob);
069
070            return blobResults;
071        }
072
073        return new ImagePreviewer().getPreview(blob, dm);
074    }
075
076    protected boolean useTiling(Blob blob) {
077        ImagingService imagingService = Framework.getLocalService(ImagingService.class);
078        if (imagingService != null) {
079            ImageInfo info = imagingService.getImageInfo(blob);
080            if (info != null) {
081                int width = info.getWidth();
082                int height = info.getHeight();
083                Integer widthThreshold = Integer.valueOf(PictureTilingComponent.getEnvValue("WidthThreshold", "1200"));
084                Integer heightThreshold = Integer.valueOf(PictureTilingComponent.getEnvValue("HeightThreshold", "1200"));
085                return width > widthThreshold || height > heightThreshold;
086            }
087        }
088        return false;
089    }
090
091    /**
092     * @deprecated since 5.9.2. Use {@link #useTiling(org.nuxeo.ecm.core.api.Blob)}.
093     */
094    @Deprecated
095    protected boolean useTiling(Blob blob, DocumentModel dm) {
096        return useTiling(blob);
097    }
098
099    private String getString() {
100        StringWriter writer = new StringWriter();
101        writer.write("<html><head></head><body>");
102        writer.write("<script type=\"text/javascript\">");
103        writer.write("var serverSetting = {");
104        writer.write("repoId : '$repoId$' ,");
105        writer.write("docId : '$docId$' ,");
106        writer.write("contextPath : '" + VirtualHostHelper.getContextPathProperty() + "'");
107        writer.write("};");
108        writer.write("</script>");
109        writer.write("<script type=\"text/javascript\"");
110        writer.write("src=\""
111                + VirtualHostHelper.getContextPathProperty()
112                + "/org.nuxeo.ecm.platform.pictures.tiles.gwt.TilingPreview/org.nuxeo.ecm.platform.pictures.tiles.gwt.TilingPreview.nocache.js\">");
113        writer.write("</script>");
114        appendPreviewSettings(writer);
115        writer.write("<div id=\"display\"></div>");
116        writer.write("</body></html>");
117        return writer.toString();
118    }
119
120    private static void appendPreviewSettings(StringWriter sb) {
121        sb.append("<script type=\"text/javascript\">");
122        sb.append("var previewSettings = { ");
123        sb.append("imageOnly: \"true\", ");
124        sb.append("multiImageAnnotation: \"true\", ");
125        sb.append("xPointerFilterPath: \""
126                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_XPOINTER_FILTER + "\", ");
127        sb.append("pointerAdapter: \""
128                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_POINTER_ADAPTER + "\", ");
129        sb.append("annotationDecoratorFunction: \""
130                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_UPDATE_ANNOTATED_DOCUMENT
131                + "\"");
132        sb.append("}");
133        sb.append("</script>");
134    }
135
136}