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    @Override
041    public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
042            {
043
044        DocumentModel targetDocModel;
045        if (targetNode instanceof CoreFolderPublicationNode) {
046            CoreFolderPublicationNode coreNode = (CoreFolderPublicationNode) targetNode;
047            targetDocModel = coreNode.getTargetDocumentModel();
048        } else {
049            targetDocModel = coreSession.getDocument(new PathRef(targetNode.getPath()));
050        }
051
052        DocumentModel proxy;
053        if ((params != null) && (params.containsKey("overwriteExistingProxy"))) {
054            proxy = coreSession.publishDocument(doc, targetDocModel,
055                    Boolean.parseBoolean(params.get("overwriteExistingProxy")));
056        } else {
057            proxy = coreSession.publishDocument(doc, targetDocModel);
058        }
059        if ((params != null) && ("true".equalsIgnoreCase(params.get("batchSave")))) {
060            // no save
061        } else {
062            coreSession.save();
063        }
064        notifyEvent(PublishingEvent.documentPublished, proxy, coreSession);
065        return new SimpleCorePublishedDocument(proxy);
066    }
067
068    @Override
069    public DocumentModel snapshotDocumentBeforePublish(DocumentModel doc) {
070        // snapshoting is done as part of the publishing
071        return doc;
072    }
073
074    public DocumentModel unwrapPublishedDocument(PublishedDocument pubDoc) {
075        if (pubDoc instanceof SimpleCorePublishedDocument) {
076            SimpleCorePublishedDocument pubProxy = (SimpleCorePublishedDocument) pubDoc;
077            return pubProxy.getProxy();
078        }
079        throw new NuxeoException("factory can not unwrap this PublishedDocument");
080    }
081
082    @Override
083    public PublishedDocument wrapDocumentModel(DocumentModel doc) {
084        return new SimpleCorePublishedDocument(doc);
085    }
086
087}