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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 *
021 */
022package org.nuxeo.ecm.platform.pictures.tiles.api.adapter;
023
024import java.util.Map;
025import java.util.concurrent.ConcurrentHashMap;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTiles;
031import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTilingService;
032import org.nuxeo.ecm.platform.pictures.tiles.api.imageresource.DocumentImageResource;
033import org.nuxeo.ecm.platform.pictures.tiles.api.imageresource.ImageResource;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Default implementation for the PictureTilesAdapter. This implementation simply uses a xPath to get the target blob.
038 *
039 * @author tiry
040 */
041public class PictureTilesAdapterImpl implements PictureTilesAdapter {
042
043    protected String xPath;
044
045    protected DocumentModel doc;
046
047    protected String fileName;
048
049    protected Map<String, PictureTiles> tiles = new ConcurrentHashMap<>();
050
051    protected static PictureTilingService pts;
052
053    protected static final Log log = LogFactory.getLog(PictureTilesAdapterImpl.class);
054
055    public PictureTilesAdapterImpl(DocumentModel doc, String xPath) {
056        this.xPath = xPath;
057        this.doc = doc;
058    }
059
060    public PictureTilesAdapterImpl(DocumentModel doc) {
061        this(doc, null);
062    }
063
064    protected ImageResource getResource() {
065        DocumentImageResource res = new DocumentImageResource(doc, xPath);
066        if (fileName != null) {
067            res.setFileName(fileName);
068        }
069        return res;
070    }
071
072    protected PictureTilingService getService() {
073        if (pts == null) {
074            pts = Framework.getService(PictureTilingService.class);
075        }
076        return pts;
077    }
078
079    public PictureTiles getTiles(int tileWidth, int tileHeight, int maxTiles) {
080
081        String key = tileWidth + "-" + tileHeight + "-" + maxTiles;
082
083        if (!tiles.containsKey(key)) {
084            PictureTiles tile = getService().getTiles(getResource(), tileWidth, tileHeight, maxTiles, 0, 0, false);
085            tiles.put(key, tile);
086        }
087        return tiles.get(key);
088    }
089
090    public void cleanup() {
091        if (tiles == null) {
092            return;
093        }
094        for (String k : tiles.keySet()) {
095            tiles.get(k).release();
096        }
097        tiles = new ConcurrentHashMap<>();
098    }
099
100    public String getXPath() {
101        return xPath;
102    }
103
104    public void setXPath(String path) {
105        xPath = path;
106    }
107
108    public DocumentModel getDoc() {
109        return doc;
110    }
111
112    public void setDoc(DocumentModel doc) {
113        this.doc = doc;
114    }
115
116    /**
117     * @deprecated since 9.1 as filename is now hold by blob
118     */
119    @Deprecated
120    public void setFileName(String fileName) {
121        this.fileName = fileName;
122    }
123
124}