001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 * 019 */ 020package org.nuxeo.ecm.platform.pictures.tiles.api.adapter; 021 022import java.util.Map; 023import java.util.concurrent.ConcurrentHashMap; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTiles; 029import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTilingService; 030import org.nuxeo.ecm.platform.pictures.tiles.api.imageresource.DocumentImageResource; 031import org.nuxeo.ecm.platform.pictures.tiles.api.imageresource.ImageResource; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * Default implementation for the PictureTilesAdapter. This implementation simply uses a xPath to get the target blob. 036 * 037 * @author tiry 038 */ 039public class PictureTilesAdapterImpl implements PictureTilesAdapter { 040 041 protected String xPath; 042 043 protected DocumentModel doc; 044 045 protected String fileName; 046 047 protected Map<String, PictureTiles> tiles = new ConcurrentHashMap<String, PictureTiles>(); 048 049 protected static PictureTilingService pts; 050 051 protected static final Log log = LogFactory.getLog(PictureTilesAdapterImpl.class); 052 053 public PictureTilesAdapterImpl(DocumentModel doc, String xPath) { 054 this.xPath = xPath; 055 this.doc = doc; 056 } 057 058 public PictureTilesAdapterImpl(DocumentModel doc) { 059 this(doc, null); 060 } 061 062 protected ImageResource getResource() { 063 DocumentImageResource res = new DocumentImageResource(doc, xPath); 064 if (fileName != null) { 065 res.setFileName(fileName); 066 } 067 return res; 068 } 069 070 protected PictureTilingService getService() { 071 if (pts == null) { 072 pts = Framework.getLocalService(PictureTilingService.class); 073 } 074 return pts; 075 } 076 077 public PictureTiles getTiles(int tileWidth, int tileHeight, int maxTiles) { 078 079 String key = tileWidth + "-" + tileHeight + "-" + maxTiles; 080 081 if (!tiles.containsKey(key)) { 082 PictureTiles tile = getService().getTiles(getResource(), tileWidth, tileHeight, maxTiles, 0, 0, false); 083 tiles.put(key, tile); 084 } 085 return tiles.get(key); 086 } 087 088 public void cleanup() { 089 if (tiles == null) { 090 return; 091 } 092 for (String k : tiles.keySet()) { 093 tiles.get(k).release(); 094 } 095 tiles = new ConcurrentHashMap<String, PictureTiles>(); 096 } 097 098 public String getXPath() { 099 return xPath; 100 } 101 102 public void setXPath(String path) { 103 xPath = path; 104 } 105 106 public DocumentModel getDoc() { 107 return doc; 108 } 109 110 public void setDoc(DocumentModel doc) { 111 this.doc = doc; 112 } 113 114 public void setFileName(String fileName) { 115 this.fileName = fileName; 116 } 117 118}