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.api.PictureTilingService;
036import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.TilingPreviewConstant;
037import org.nuxeo.ecm.platform.preview.adapter.AbstractPreviewer;
038import org.nuxeo.ecm.platform.preview.adapter.ImagePreviewer;
039import org.nuxeo.ecm.platform.preview.adapter.MimeTypePreviewer;
040import org.nuxeo.ecm.platform.preview.adapter.PlainImagePreviewer;
041import org.nuxeo.ecm.platform.preview.adapter.base.ConverterBasedHtmlPreviewAdapter;
042import org.nuxeo.ecm.platform.preview.api.PreviewException;
043import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
044import org.nuxeo.runtime.api.Framework;
045import org.nuxeo.runtime.services.config.ConfigurationService;
046
047/**
048 * @author Alexandre Russel
049 */
050public class TiledImagePreviewer extends AbstractPreviewer implements MimeTypePreviewer {
051
052    private static final Log log = LogFactory.getLog(TiledImagePreviewer.class);
053
054    protected static final String ORIGINAL_JPEG_VIEW_NAME = "OriginalJpeg";
055
056    /**
057     * @deprecated since 7.2. The Original view does not exist anymore. See NXP-16070.
058     */
059    @Deprecated
060    protected static final String ORIGINAL_VIEW_NAME = "Original";
061
062    public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException {
063        if (useTiling(blob)) {
064            List<Blob> blobResults = new ArrayList<Blob>();
065            String htmlFile = getString().replace("$repoId$", dm.getRepositoryName());
066            htmlFile = htmlFile.replace("$docId$", dm.getId());
067            htmlFile = htmlFile.replace("$tileWidth$", "" + 200);
068            htmlFile = htmlFile.replace("$tileHeight$", "" + 200);
069            htmlFile = htmlFile.replace("$maxTiles$", "" + 2);
070            Blob mainBlob = Blobs.createBlob(htmlFile, "text/html", null, "index.html");
071            blob.setFilename("image");
072
073            blobResults.add(mainBlob);
074            blobResults.add(blob);
075
076            return blobResults;
077        }
078
079        ConfigurationService service = Framework.getService(ConfigurationService.class);
080        return service.isBooleanPropertyTrue(ConverterBasedHtmlPreviewAdapter.OLD_PREVIEW_PROPERTY)
081                ? new PlainImagePreviewer().getPreview(blob, dm)
082                : new ImagePreviewer().getPreview(blob, dm);
083    }
084
085    protected boolean useTiling(Blob blob) {
086        ImagingService imagingService = Framework.getService(ImagingService.class);
087        if (imagingService != null) {
088            ImageInfo info = imagingService.getImageInfo(blob);
089            if (info != null) {
090                int width = info.getWidth();
091                int height = info.getHeight();
092                PictureTilingComponent ptc = (PictureTilingComponent) Framework.getService(PictureTilingService.class);
093                Integer widthThreshold = Integer.valueOf(ptc.getEnvValue("WidthThreshold", "1200"));
094                Integer heightThreshold = Integer.valueOf(ptc.getEnvValue("HeightThreshold", "1200"));
095                return width > widthThreshold || height > heightThreshold;
096            }
097        }
098        return false;
099    }
100
101    /**
102     * @deprecated since 5.9.2. Use {@link #useTiling(org.nuxeo.ecm.core.api.Blob)}.
103     */
104    @Deprecated
105    protected boolean useTiling(Blob blob, DocumentModel dm) {
106        return useTiling(blob);
107    }
108
109    private String getString() {
110        StringWriter writer = new StringWriter();
111        writer.write("<html><head></head><body>");
112        writer.write("<script type=\"text/javascript\">");
113        writer.write("var serverSetting = {");
114        writer.write("repoId : '$repoId$' ,");
115        writer.write("docId : '$docId$' ,");
116        writer.write("contextPath : '" + VirtualHostHelper.getContextPathProperty() + "'");
117        writer.write("};");
118        writer.write("</script>");
119        writer.write("<script type=\"text/javascript\"");
120        writer.write("src=\""
121                + VirtualHostHelper.getContextPathProperty()
122                + "/org.nuxeo.ecm.platform.pictures.tiles.gwt.TilingPreview/org.nuxeo.ecm.platform.pictures.tiles.gwt.TilingPreview.nocache.js\">");
123        writer.write("</script>");
124        appendPreviewSettings(writer);
125        writer.write("<div id=\"display\"></div>");
126        writer.write("</body></html>");
127        return writer.toString();
128    }
129
130    private static void appendPreviewSettings(StringWriter sb) {
131        sb.append("<script type=\"text/javascript\">");
132        sb.append("var previewSettings = { ");
133        sb.append("imageOnly: \"true\", ");
134        sb.append("multiImageAnnotation: \"true\", ");
135        sb.append("xPointerFilterPath: \""
136                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_XPOINTER_FILTER + "\", ");
137        sb.append("pointerAdapter: \""
138                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_POINTER_ADAPTER + "\", ");
139        sb.append("annotationDecoratorFunction: \""
140                + TilingPreviewConstant.ORG_NUXEO_ECM_PLATFORM_PICTURES_TILES_GWT_CLIENT_UPDATE_ANNOTATED_DOCUMENT
141                + "\"");
142        sb.append("}");
143        sb.append("</script>");
144    }
145
146}