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;
020
021import java.util.Map;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentRef;
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
029import org.nuxeo.ecm.core.api.security.ACP;
030import org.nuxeo.ecm.core.api.security.impl.ACPImpl;
031import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
032import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
033import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
034import org.nuxeo.ecm.platform.publisher.impl.core.SimpleCorePublishedDocument;
035import org.nuxeo.ecm.platform.publisher.task.CoreProxyWithWorkflowFactory;
036import org.nuxeo.ecm.platform.rendition.Rendition;
037import org.nuxeo.ecm.platform.rendition.service.RenditionService;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Implementation of {@link PublishedDocumentFactory} that uses the {@link RenditionService} to publish a Rendition of
042 * the given document.
043 *
044 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
045 * @since 5.4.1
046 */
047public class RenditionPublicationFactory extends CoreProxyWithWorkflowFactory implements PublishedDocumentFactory {
048
049    public static final String RENDITION_NAME_PARAMETER_KEY = "renditionName";
050
051    @Override
052    public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
053            {
054        if (params != null && params.containsKey(RENDITION_NAME_PARAMETER_KEY)) {
055            String renditionName = params.get(RENDITION_NAME_PARAMETER_KEY);
056            if (!StringUtils.isEmpty(renditionName)) {
057                DocumentModel renditionDocument = null;
058                Rendition rendition = null;
059                rendition = getRenditionService().getRendition(doc, renditionName, true);
060                if (rendition != null) {
061                    renditionDocument = rendition.getHostDocument();
062                } else {
063                    throw new NuxeoException("Unable to render the document");
064                }
065                PublishedDocument publishedDocument = super.publishDocument(renditionDocument, targetNode, params);
066                DocumentModel proxy = ((SimpleCorePublishedDocument) publishedDocument).getProxy();
067                proxy.attach(doc.getSessionId());
068                if (!hasValidationTask(publishedDocument)) {
069                    removeExistingProxiesOnPreviousVersions(proxy);
070                }
071                return publishedDocument;
072            }
073
074        }
075        return super.publishDocument(doc, targetNode, params);
076    }
077
078    @Override
079    protected DocumentModel getLiveDocument(CoreSession session, DocumentModel proxy) {
080        if (!proxy.hasFacet(RENDITION_FACET)) {
081            return super.getLiveDocument(session, proxy);
082        }
083        RenditionLiveDocFetcher fetcher = new RenditionLiveDocFetcher(session, proxy);
084        fetcher.runUnrestricted();
085        return fetcher.getLiveDocument();
086    }
087
088    @Override
089    protected void removeExistingProxiesOnPreviousVersions(DocumentModel newProxy) {
090        if (!newProxy.hasFacet(RENDITION_FACET)) {
091            super.removeExistingProxiesOnPreviousVersions(newProxy);
092            return;
093        }
094        RenditionsRemover remover = new RenditionsRemover(newProxy);
095        remover.runUnrestricted();
096    }
097
098    protected RenditionService getRenditionService() {
099        return Framework.getService(RenditionService.class);
100    }
101
102    public static class RemoveACP extends UnrestrictedSessionRunner {
103
104        protected DocumentRef docRef;
105
106        public RemoveACP(CoreSession session, DocumentRef docRef) {
107            super(session);
108            this.docRef = docRef;
109        }
110
111        @Override
112        public void run() {
113            ACP acp = new ACPImpl();
114            session.setACP(docRef, acp, true);
115        }
116
117    }
118
119}