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