001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (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 *
014 * Contributors:
015 *     Nuxeo
016 */
017
018package org.nuxeo.ecm.platform.publisher.impl.core;
019
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.NuxeoException;
022import org.nuxeo.ecm.core.api.PathRef;
023import org.nuxeo.ecm.platform.publisher.api.AbstractBasePublishedDocumentFactory;
024import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
025import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
026import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
027import org.nuxeo.ecm.platform.publisher.api.PublishingEvent;
028
029import java.util.Map;
030
031/**
032 * Implementation of the {@link PublishedDocumentFactory} for simple core implementation using native proxy system.
033 *
034 * @author tiry
035 */
036public class CoreProxyFactory extends AbstractBasePublishedDocumentFactory implements PublishedDocumentFactory {
037
038    public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
039            {
040
041        DocumentModel targetDocModel;
042        if (targetNode instanceof CoreFolderPublicationNode) {
043            CoreFolderPublicationNode coreNode = (CoreFolderPublicationNode) targetNode;
044            targetDocModel = coreNode.getTargetDocumentModel();
045        } else {
046            targetDocModel = coreSession.getDocument(new PathRef(targetNode.getPath()));
047        }
048
049        DocumentModel proxy;
050        if ((params != null) && (params.containsKey("overwriteExistingProxy"))) {
051            proxy = coreSession.publishDocument(doc, targetDocModel,
052                    Boolean.parseBoolean(params.get("overwriteExistingProxy")));
053        } else {
054            proxy = coreSession.publishDocument(doc, targetDocModel);
055        }
056        if ((params != null) && ("true".equalsIgnoreCase(params.get("batchSave")))) {
057            // no save
058        } else {
059            coreSession.save();
060        }
061        notifyEvent(PublishingEvent.documentPublished, proxy, coreSession);
062        return new SimpleCorePublishedDocument(proxy);
063    }
064
065    public DocumentModel snapshotDocumentBeforePublish(DocumentModel doc) {
066        // snapshoting is done as part of the publishing
067        return doc;
068    }
069
070    public DocumentModel unwrapPublishedDocument(PublishedDocument pubDoc) {
071        if (pubDoc instanceof SimpleCorePublishedDocument) {
072            SimpleCorePublishedDocument pubProxy = (SimpleCorePublishedDocument) pubDoc;
073            return pubProxy.getProxy();
074        }
075        throw new NuxeoException("factory can not unwrap this PublishedDocument");
076    }
077
078    public PublishedDocument wrapDocumentModel(DocumentModel doc) {
079        return new SimpleCorePublishedDocument(doc);
080    }
081
082}