001/* 002 * (C) Copyright 2010 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 * Contributors: 014 * Nuxeo - initial API and implementation 015 */ 016package org.nuxeo.ecm.platform.rendition.service; 017 018import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_NAME_PROPERTY; 019import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_SOURCE_ID_PROPERTY; 020import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_SOURCE_MODIFICATION_DATE_PROPERTY; 021 022import java.text.SimpleDateFormat; 023import java.util.Calendar; 024import java.util.List; 025 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 028import org.nuxeo.runtime.api.Framework; 029 030/** 031 * Retrieves stored Rendition associated to a DocumentModel. 032 * <p> 033 * Can run Unrestricted or not. 034 * 035 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 036 */ 037public class RenditionFinder extends UnrestrictedSessionRunner { 038 039 protected final DocumentModel source; 040 041 protected DocumentModel storedRendition; 042 043 protected final String renditionName; 044 045 protected RenditionFinder(DocumentModel source, String renditionName) { 046 super(source.getCoreSession()); 047 this.source = source; 048 this.renditionName = renditionName; 049 } 050 051 @Override 052 public void run() { 053 StringBuilder query = new StringBuilder(); 054 query.append("SELECT * FROM Document WHERE ecm:isProxy = 0 AND "); 055 query.append(RENDITION_NAME_PROPERTY); 056 query.append(" = '"); 057 query.append(renditionName); 058 query.append("' AND "); 059 boolean isVersionable = source.isVersionable(); 060 String renditionSourceId = source.getId(); 061 if (isVersionable) { 062 if (!source.isVersion() && !source.isCheckedOut()) { 063 DocumentModel lastVersion = session.getLastDocumentVersion(source.getRef()); 064 if (lastVersion != null) { 065 renditionSourceId = lastVersion.getId(); 066 } else { 067 // no version at all 068 return; 069 } 070 } 071 query.append("ecm:isCheckedInVersion = 1 AND "); 072 } else { 073 String modificationDatePropertyName = getSourceDocumentModificationDatePropertyName(); 074 Calendar sourceLastModified = (Calendar) source.getPropertyValue(modificationDatePropertyName); 075 if (sourceLastModified != null) { 076 query.append(RENDITION_SOURCE_MODIFICATION_DATE_PROPERTY); 077 query.append(" >= "); 078 query.append(formatTimestamp(sourceLastModified)); 079 query.append(" AND "); 080 } 081 } 082 query.append(RENDITION_SOURCE_ID_PROPERTY); 083 query.append(" = '"); 084 query.append(renditionSourceId); 085 query.append("' ORDER BY dc:modified DESC"); 086 087 List<DocumentModel> docs = session.query(query.toString()); 088 if (docs.size() > 0) { 089 storedRendition = docs.get(0); 090 storedRendition.detach(true); 091 } 092 } 093 094 public DocumentModel getStoredRendition() { 095 return storedRendition; 096 } 097 098 protected String getSourceDocumentModificationDatePropertyName() { 099 RenditionService rs = Framework.getService(RenditionService.class); 100 RenditionDefinition def = ((RenditionServiceImpl) rs).getRenditionDefinition(renditionName); 101 return def.getSourceDocumentModificationDatePropertyName(); 102 } 103 104 protected static String formatTimestamp(Calendar cal) { 105 return new SimpleDateFormat("'TIMESTAMP' ''yyyy-MM-dd HH:mm:ss.SSS''").format(cal.getTime()); 106 } 107 108}