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