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;
023
024import java.io.File;
025import java.io.IOException;
026import java.io.Serializable;
027import java.util.Map;
028
029import org.nuxeo.common.utils.Path;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.Blobs;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
034import org.nuxeo.ecm.platform.picture.api.ImageInfo;
035import org.nuxeo.ecm.platform.pictures.tiles.helpers.StringMaker;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Default implementation for the PictureTiles interface
040 *
041 * @author tiry
042 */
043public class PictureTilesImpl implements PictureTiles, Serializable {
044
045    /**
046     *
047     */
048    private static final long serialVersionUID = 1L;
049
050    public static final String TILE_OUTPUT_DIR_KEY = "outputDirPath";
051
052    public static final String TILE_INPUT_FILE_KEY = "inputFilePath";
053
054    public static final String X_TILES_KEY = "XTiles";
055
056    public static final String Y_TILES_KEY = "YTiles";
057
058    public static final String LAST_MODIFICATION_DATE_KEY = "lastModificationDate";
059
060    public static final String TILES_PREFIX_KEY = "TilesPrefix";
061
062    public static final String TILES_SUFFIX_KEY = "TilesSuffix";
063
064    public static final String TILES_WIDTH_KEY = "TilesWidth";
065
066    public static final String TILES_HEIGHT_KEY = "TilesHeight";
067
068    public static final String MAX_TILES_KEY = "MaxTiles";
069
070    public static final String PROGRESSIVE_TILING_KEY = "ProgressiveTiling";
071
072    protected Map<String, String> infoMap;
073
074    protected String tilesDirPath;
075
076    protected String cacheKey;
077
078    protected ImageInfo sourceImageInfo;
079
080    protected ImageInfo originalImageInfo;
081
082    public PictureTilesImpl(String tilesDirPath) {
083        this.tilesDirPath = tilesDirPath;
084    }
085
086    public PictureTilesImpl(Map<String, String> info) {
087        infoMap = info;
088        tilesDirPath = info.get(TILE_OUTPUT_DIR_KEY);
089    }
090
091    public String getCacheKey() {
092        return cacheKey;
093    }
094
095    public void setCacheKey(String cacheKey) {
096        this.cacheKey = cacheKey;
097    }
098
099    public Map<String, String> getInfo() {
100        return infoMap;
101    }
102
103    public boolean isTileComputed(int x, int y) {
104        long lastModificationTime = Long.parseLong(infoMap.get(PictureTilesImpl.LAST_MODIFICATION_DATE_KEY));
105        String tileFileName = StringMaker.getTileFileName(x, y, infoMap.get(TILES_PREFIX_KEY),
106                infoMap.get(TILES_SUFFIX_KEY), lastModificationTime);
107        File imageFile = new File(tilesDirPath + tileFileName);
108        return imageFile.exists();
109    }
110
111    public Blob getTile(int x, int y) throws IOException {
112        String imageFilePath = getTileFilePath(x, y);
113        File imageFile = new File(imageFilePath);
114        if (imageFile.exists())
115            return Blobs.createBlob(imageFile);
116        else {
117            PictureTilingService pts = Framework.getService(PictureTilingService.class);
118            pts.completeTiles(this, x, y);
119            imageFile = new File(imageFilePath);
120            if (imageFile.exists())
121                return Blobs.createBlob(imageFile);
122            else
123                throw new NuxeoException("Unable to get Tile");
124        }
125    }
126
127    public String getTileFilePath(int x, int y) {
128        long lastModificationTime = Long.parseLong(infoMap.get(PictureTilesImpl.LAST_MODIFICATION_DATE_KEY));
129        String tileFileName = StringMaker.getTileFileName(x, y, infoMap.get(TILES_PREFIX_KEY),
130                infoMap.get(TILES_SUFFIX_KEY), lastModificationTime);
131        String imageFilePath = new Path(tilesDirPath).append(tileFileName).toString();
132        return imageFilePath;
133    }
134
135    public int getMaxTiles() {
136        String MT = infoMap.get(MAX_TILES_KEY);
137        if (MT == null) {
138            return 0;
139        }
140        return Integer.parseInt(MT);
141    }
142
143    public int getTilesWidth() {
144        String TW = infoMap.get(TILES_WIDTH_KEY);
145        if (TW == null) {
146            return 0;
147        }
148        return Integer.parseInt(TW);
149    }
150
151    public int getTilesHeight() {
152        String TH = infoMap.get(TILES_HEIGHT_KEY);
153        if (TH == null) {
154            return 0;
155        }
156        return Integer.parseInt(TH);
157    }
158
159    public String getTilesPath() {
160        return tilesDirPath;
161    }
162
163    public int getXTiles() {
164        String XT = infoMap.get(X_TILES_KEY);
165        if (XT == null) {
166            return 0;
167        }
168        return Integer.parseInt(XT);
169    }
170
171    public int getYTiles() {
172        String YT = infoMap.get(Y_TILES_KEY);
173        if (YT == null) {
174            return 0;
175        }
176        return Integer.parseInt(YT);
177    }
178
179    public float getZoomfactor() {
180
181        float oWith = originalImageInfo.getWidth();
182        float tWith = getXTiles() * getTilesWidth();
183        float oHeight = originalImageInfo.getHeight();
184        float tHeight = getYTiles() * getTilesHeight();
185        return tWith / oWith < tHeight / oHeight ? tWith / oWith : tHeight / oHeight;
186    }
187
188    public void release() {
189        long lastModificationTime = Long.parseLong(infoMap.get(PictureTilesImpl.LAST_MODIFICATION_DATE_KEY));
190        for (int x = 0; x < getXTiles(); x++) {
191            for (int y = 0; y < getYTiles(); y++) {
192                String tileFileName = StringMaker.getTileFileName(x, y, infoMap.get(TILES_PREFIX_KEY),
193                        infoMap.get(TILES_SUFFIX_KEY), lastModificationTime);
194                File img = new File(tilesDirPath + tileFileName);
195                if (img.exists())
196                    img.delete();
197            }
198        }
199    }
200
201    public ImageInfo getSourceImageInfo() {
202        return sourceImageInfo;
203    }
204
205    public void setSourceImageInfo(ImageInfo imageInfo) {
206        this.sourceImageInfo = imageInfo;
207    }
208
209    public String getTileFormatCacheKey() {
210        return StringMaker.getTileFormatString(getTilesWidth(), getTilesHeight(), getMaxTiles());
211    }
212
213    public ImageInfo getOriginalImageInfo() {
214        return originalImageInfo;
215    }
216
217    public void setOriginalImageInfo(ImageInfo imageInfo) {
218        originalImageInfo = imageInfo;
219    }
220
221}