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