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 */
018
019package org.nuxeo.ecm.platform.rendition.publisher;
020
021import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_FACET;
022import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_SCHEMA;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.nuxeo.ecm.core.api.DocumentLocation;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentRef;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.core.query.sql.NXQL;
032import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
033import org.nuxeo.ecm.platform.publisher.impl.core.RootSectionsPublicationTree;
034import org.nuxeo.ecm.platform.rendition.Constants;
035
036/**
037 * Implementation of {@link org.nuxeo.ecm.platform.publisher.api.PublicationTree} that retrieve also any published
038 * Rendition documents for the given document.
039 *
040 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
041 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
042 * @since 5.4.1
043 */
044public class RenditionPublicationCoreTree extends RootSectionsPublicationTree {
045
046    private static final long serialVersionUID = 1L;
047
048    public static final String RENDITION_PUBLISHED_DOCUMENTS_FROM_PROXY_DOCUMENT = "SELECT * FROM Document WHERE rend:sourceId = '%s' "
049            + "AND ecm:path STARTSWITH '%s'" + " AND ecm:isProxy = 1";
050
051    public static final String RENDITION_PUBLISHED_DOCUMENTS_FROM_LIVE_DOCUMENT = "SELECT * FROM Document WHERE rend:sourceVersionableId = '%s' "
052            + "AND ecm:path STARTSWITH '%s' " + "AND ecm:isProxy = 1";
053
054    @Override
055    public List<PublishedDocument> getExistingPublishedDocument(DocumentLocation docLoc) {
056        List<PublishedDocument> publishedDocuments = super.getExistingPublishedDocument(docLoc);
057
058        DocumentModel sourceDocument = coreSession.getDocument(docLoc.getDocRef());
059        if (sourceDocument.isProxy()) {
060            // If on a proxy, we want all the others proxy pointing to the same
061            // version
062            if (!sourceDocument.hasFacet(RENDITION_FACET) && !sourceDocument.hasSchema(RENDITION_SCHEMA)) {
063                return publishedDocuments;
064            }
065            DocumentRef docRef = new IdRef(
066                    (String) sourceDocument.getPropertyValue(Constants.RENDITION_SOURCE_ID_PROPERTY));
067            publishedDocuments.addAll(getPublishedDocumentsFromProxyDocument(docRef, sourceDocument));
068        } else {
069            publishedDocuments.addAll(getPublishedDocumentsFromLiveDocument(docLoc.getDocRef()));
070        }
071        return publishedDocuments;
072    }
073
074    protected List<PublishedDocument> getPublishedDocumentsFromProxyDocument(DocumentRef docRef,
075            DocumentModel sourceDocument) {
076        List<PublishedDocument> publishedDocuments = new ArrayList<PublishedDocument>();
077        List<DocumentModel> docs = coreSession.query(String.format(RENDITION_PUBLISHED_DOCUMENTS_FROM_PROXY_DOCUMENT,
078                docRef, NXQL.escapeStringInner(rootPath)));
079        for (DocumentModel doc : docs) {
080            if (!doc.getRef().equals(sourceDocument.getRef())) {
081                publishedDocuments.add(factory.wrapDocumentModel(doc));
082            }
083        }
084        return publishedDocuments;
085    }
086
087    protected List<PublishedDocument> getPublishedDocumentsFromLiveDocument(DocumentRef docRef) {
088        List<PublishedDocument> publishedDocuments = new ArrayList<PublishedDocument>();
089        List<DocumentModel> docs = coreSession.query(String.format(RENDITION_PUBLISHED_DOCUMENTS_FROM_LIVE_DOCUMENT,
090                docRef, NXQL.escapeStringInner(rootPath)));
091        for (DocumentModel doc : docs) {
092            publishedDocuments.add(factory.wrapDocumentModel(doc));
093        }
094        return publishedDocuments;
095    }
096
097}