001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Alexandre Russel
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.pictures.tiles.service;
023
024import java.io.StringWriter;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.Blobs;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.platform.picture.api.ImageInfo;
034import org.nuxeo.ecm.platform.picture.api.ImagingService;
035import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant;
036import org.nuxeo.ecm.platform.preview.adapter.AbstractPreviewer;
037import org.nuxeo.ecm.platform.preview.adapter.ImagePreviewer;
038import org.nuxeo.ecm.platform.preview.adapter.MimeTypePreviewer;
039import org.nuxeo.ecm.platform.preview.api.PreviewException;
040import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
041import org.nuxeo.runtime.api.Framework;
042
043/**
044 * @author Alexandre Russel
045 */
046public class TiledImagePreviewer extends AbstractPreviewer implements MimeTypePreviewer {
047
048    private static final Log log = LogFactory.getLog(TiledImagePreviewer.class);
049
050    protected static final String ORIGINAL_JPEG_VIEW_NAME = "OriginalJpeg";
051
052    /**
053     * @deprecated since 7.2. The Original view does not exist anymore. See NXP-16070.
054     */
055    @Deprecated
056    protected static final String ORIGINAL_VIEW_NAME = "Original";
057
058    public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException {
059        if (useTiling(blob)) {
060            List<Blob> blobResults = new ArrayList<Blob>();
061            String htmlFile = getString().replace("$repoId$", dm.getRepositoryName());
062            htmlFile = htmlFile.replace("$docId$", dm.getId());
063            htmlFile = htmlFile.replace("$tileWidth$", "" + 200);
064            htmlFile = htmlFile.replace("$tileHeight$", "" + 200);
065            htmlFile = htmlFile.replace("$maxTiles$", "" + 2);
066            Blob mainBlob = Blobs.createBlob(htmlFile, "text/html", null, "index.html");
067            blob.setFilename("image");
068
069            blobResults.add(mainBlob);
070            blobResults.add(blob);
071
072            return blobResults;
073        }
074
075        return new ImagePreviewer().getPreview(blob, dm);
076    }
077
078    protected boolean useTiling(Blob blob) {
079        ImagingService imagingService = Framework.getLocalService(ImagingService.class);
080        if (imagingService != null) {
081            ImageInfo info = imagingService.getImageInfo(blob);
082            if (info != null) {
083                int width = info.getWidth();
084                int height = info.getHeight();
085                Integer widthThreshold = Integer.valueOf(PictureTilingComponent.getEnvValue("WidthThreshold", "1200"));
086                Integer heightThreshold = Integer.valueOf(PictureTilingComponent.getEnvValue("HeightThreshold", "1200"));
087                return width > widthThreshold || height > heightThreshold;
088            }
089        }
090        return false;
091    }
092
093    /**
094     * @deprecated since 5.9.2. Use {@link #useTiling(org.nuxeo.ecm.core.api.Blob)}.
095     */
096    @Deprecated
097    protected boolean useTiling(Blob blob, DocumentModel dm) {
098        return useTiling(blob);
099    }
100
101    private String getString() {
102        StringWriter writer = new StringWriter();
103        writer.write("<html><head></head><body>");
104        writer.write("<script type=\"text/javascript\">");
105        writer.write("var serverSetting = {");
106        writer.write("repoId : '$repoId$' ,");
107        writer.write("docId : '$docId$' ,");
108        writer.write("contextPath : '" + VirtualHostHelper.getContextPathProperty() + "'");
109        writer.write("};");
110        writer.write("</script>");
111        writer.write("<script type=\"text/javascript\"");
112        writer.write("src=\""
113                + VirtualHostHelper.getContextPathProperty()
114                + "/org.nuxeo.ecm.platform.pictures.tiles.gwt.TilingPreview/org.nuxeo.ecm.platform.pictures.tiles.gwt.TilingPreview.nocache.js\">");
115        writer.write("</script>");
116        appendPreviewSettings(writer);
117        writer.write("<div id=\"display\"></div>");
118        writer.write("</body></html>");
119        return writer.toString();
120    }
121
122    private static void appendPreviewSettings(StringWriter sb) {
123        sb.append("<script type=\"text/javascript\">");
124        sb.append("var previewSettings = { ");
125        sb.append("imageOnly: \"true\", ");
126        sb.append("multiImageAnnotation: \"true\", ");
127        sb.append("xPointerFilterPath: \""
128                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_XPOINTER_FILTER + "\", ");
129        sb.append("pointerAdapter: \""
130                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_POINTER_ADAPTER + "\", ");
131        sb.append("annotationDecoratorFunction: \""
132                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_UPDATE_ANNOTATED_DOCUMENT
133                + "\"");
134        sb.append("}");
135        sb.append("</script>");
136    }
137
138}