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