001/* 002 * (C) Copyright 2010 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 * Contributors: 016 * Nuxeo - initial API and implementation 017 */ 018package org.nuxeo.ecm.platform.rendition.service; 019 020import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_NAME_PROPERTY; 021import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_SOURCE_ID_PROPERTY; 022import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_SOURCE_MODIFICATION_DATE_PROPERTY; 023import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_VARIANT_PROPERTY; 024 025import java.util.Calendar; 026import java.util.List; 027 028import org.apache.logging.log4j.LogManager; 029import org.apache.logging.log4j.Logger; 030import org.nuxeo.common.utils.DateUtils; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 033import org.nuxeo.ecm.core.query.sql.NXQL; 034 035/** 036 * Retrieves stored Rendition associated to a DocumentModel. 037 * <p> 038 * Can run Unrestricted or not. 039 * 040 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 041 */ 042public class RenditionFinder extends UnrestrictedSessionRunner { 043 044 private static final Logger log = LogManager.getLogger(RenditionFinder.class); 045 046 protected final DocumentModel source; 047 048 protected DocumentModel storedRendition; 049 050 protected final String renditionName; 051 052 /** 053 * @since 8.1 054 */ 055 protected RenditionDefinition renditionDefinition; 056 057 /** 058 * @since 8.1 059 */ 060 protected final String renditionVariant; 061 062 /** 063 * @since 8.1 064 */ 065 protected RenditionFinder(DocumentModel source, RenditionDefinition renditionDefinition) { 066 super(source.getCoreSession()); 067 this.source = source; 068 this.renditionDefinition = renditionDefinition; 069 renditionName = renditionDefinition.getName(); 070 renditionVariant = renditionDefinition.getProvider().getVariant(source, renditionDefinition); 071 } 072 073 @Override 074 public void run() { 075 boolean isVersionable = source.isVersionable(); 076 String renditionSourceId = source.getId(); 077 StringBuilder query = new StringBuilder(); 078 query.append("SELECT * FROM Document WHERE ecm:isProxy = 0 AND "); 079 query.append(RENDITION_NAME_PROPERTY); 080 query.append(" = '"); 081 query.append(NXQL.escapeStringInner(renditionName)); 082 query.append("' AND "); 083 if (renditionVariant != null) { 084 query.append(RENDITION_VARIANT_PROPERTY); 085 query.append(" = '"); 086 query.append(NXQL.escapeStringInner(renditionVariant)); 087 query.append("' AND "); 088 } 089 if (isVersionable) { 090 if (!source.isVersion() && !source.isCheckedOut()) { 091 DocumentModel lastVersion = session.getLastDocumentVersion(source.getRef()); 092 if (lastVersion != null) { 093 renditionSourceId = lastVersion.getId(); 094 } else { 095 // no version at all 096 return; 097 } 098 } 099 query.append("ecm:isVersion = 1 AND "); 100 } else { 101 String modificationDatePropertyName = getSourceDocumentModificationDatePropertyName(); 102 Calendar sourceLastModified = (Calendar) source.getPropertyValue(modificationDatePropertyName); 103 if (sourceLastModified != null) { 104 query.append(RENDITION_SOURCE_MODIFICATION_DATE_PROPERTY); 105 query.append(" >= TIMESTAMP '"); 106 query.append(DateUtils.formatISODateTime(sourceLastModified)); 107 query.append("' AND "); 108 } 109 } 110 query.append(RENDITION_SOURCE_ID_PROPERTY); 111 query.append(" = '"); 112 query.append(renditionSourceId); 113 query.append("' ORDER BY dc:modified DESC"); 114 String queryStr = query.toString(); 115 116 log.debug("Finding stored renditions for document {} with query {}.", source, queryStr); 117 List<DocumentModel> docs = session.query(queryStr); 118 log.debug("Stored renditions found for document {}: {}", source, docs); 119 if (!docs.isEmpty()) { 120 storedRendition = docs.get(0); 121 storedRendition.detach(true); 122 } 123 } 124 125 public DocumentModel getStoredRendition() { 126 return storedRendition; 127 } 128 129 protected String getSourceDocumentModificationDatePropertyName() { 130 return renditionDefinition.getSourceDocumentModificationDatePropertyName(); 131 } 132 133}