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.text.SimpleDateFormat;
026import java.util.Calendar;
027import java.util.List;
028
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
031import org.nuxeo.ecm.core.query.sql.NXQL;
032
033/**
034 * Retrieves stored Rendition associated to a DocumentModel.
035 * <p>
036 * Can run Unrestricted or not.
037 *
038 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
039 */
040public class RenditionFinder extends UnrestrictedSessionRunner {
041
042    protected final DocumentModel source;
043
044    protected DocumentModel storedRendition;
045
046    protected final String renditionName;
047
048    /**
049     * @since 8.1
050     */
051    protected RenditionDefinition renditionDefinition;
052
053    /**
054     * @since 8.1
055     */
056    protected final String renditionVariant;
057
058    /**
059     * @since 8.1
060     */
061    protected RenditionFinder(DocumentModel source, RenditionDefinition renditionDefinition) {
062        super(source.getCoreSession());
063        this.source = source;
064        this.renditionDefinition = renditionDefinition;
065        renditionName = renditionDefinition.getName();
066        renditionVariant = renditionDefinition.getProvider().getVariant(source, renditionDefinition);
067    }
068
069    @Override
070    public void run() {
071        boolean isVersionable = source.isVersionable();
072        String renditionSourceId = source.getId();
073        StringBuilder query = new StringBuilder();
074        query.append("SELECT * FROM Document WHERE ecm:isProxy = 0 AND ");
075        query.append(RENDITION_NAME_PROPERTY);
076        query.append(" = '");
077        query.append(NXQL.escapeStringInner(renditionName));
078        query.append("' AND ");
079        if (renditionVariant != null) {
080            query.append(RENDITION_VARIANT_PROPERTY);
081            query.append(" = '");
082            query.append(NXQL.escapeStringInner(renditionVariant));
083            query.append("' AND ");
084        }
085        if (isVersionable) {
086            if (!source.isVersion() && !source.isCheckedOut()) {
087                DocumentModel lastVersion = session.getLastDocumentVersion(source.getRef());
088                if (lastVersion != null) {
089                    renditionSourceId = lastVersion.getId();
090                } else {
091                    // no version at all
092                    return;
093                }
094            }
095            query.append("ecm:isCheckedInVersion = 1 AND ");
096        } else {
097            String modificationDatePropertyName = getSourceDocumentModificationDatePropertyName();
098            Calendar sourceLastModified = (Calendar) source.getPropertyValue(modificationDatePropertyName);
099            if (sourceLastModified != null) {
100                query.append(RENDITION_SOURCE_MODIFICATION_DATE_PROPERTY);
101                query.append(" >= ");
102                query.append(formatTimestamp(sourceLastModified));
103                query.append(" AND ");
104            }
105        }
106        query.append(RENDITION_SOURCE_ID_PROPERTY);
107        query.append(" = '");
108        query.append(renditionSourceId);
109        query.append("' ORDER BY dc:modified DESC");
110
111        List<DocumentModel> docs = session.query(query.toString());
112        if (docs.size() > 0) {
113            storedRendition = docs.get(0);
114            storedRendition.detach(true);
115        }
116    }
117
118    public DocumentModel getStoredRendition() {
119        return storedRendition;
120    }
121
122    protected String getSourceDocumentModificationDatePropertyName() {
123        return renditionDefinition.getSourceDocumentModificationDatePropertyName();
124    }
125
126    protected static String formatTimestamp(Calendar cal) {
127        return new SimpleDateFormat("'TIMESTAMP' ''yyyy-MM-dd HH:mm:ss.SSS''").format(cal.getTime());
128    }
129
130}