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.remoting.server;
019
020import org.nuxeo.common.collections.ScopeType;
021import org.nuxeo.common.collections.ScopedMap;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService;
024import org.nuxeo.ecm.platform.publisher.api.AbstractBasePublishedDocumentFactory;
025import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
026import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
027import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
028import org.nuxeo.ecm.platform.publisher.impl.core.SimpleCorePublishedDocument;
029import org.nuxeo.ecm.platform.versioning.api.VersioningActions;
030import org.nuxeo.runtime.api.Framework;
031
032import java.util.Map;
033
034/**
035 * {@link PublishedDocumentFactory} implementation that creates {@link DocumentModel} instead of simple proxies.
036 *
037 * @author tiry
038 */
039public class SimpleExternalDocumentModelFactory extends AbstractBasePublishedDocumentFactory implements
040        PublishedDocumentFactory {
041
042    public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
043            {
044
045        PathSegmentService pss = Framework.getService(PathSegmentService.class);
046        doc.setPathInfo(targetNode.getPath(), "remote_doc_" + pss.generatePathSegment(doc));
047        // We don't want to erase the current version
048        final ScopedMap ctxData = doc.getContextData();
049        ctxData.putScopedValue(ScopeType.REQUEST, VersioningActions.SKIP_VERSIONING, true);
050        doc = coreSession.createDocument(doc);
051        coreSession.save();
052
053        return new ExternalCorePublishedDocument(doc);
054    }
055
056    @Override
057    protected boolean needToVersionDocument(DocumentModel doc) {
058        if (!doc.getRepositoryName().equalsIgnoreCase(coreSession.getRepositoryName())) {
059            return false;
060        } else {
061            return super.needToVersionDocument(doc);
062        }
063    }
064
065    public PublishedDocument wrapDocumentModel(DocumentModel doc) {
066        if (doc.isProxy()) {
067            return new SimpleCorePublishedDocument(doc);
068        }
069        return new ExternalCorePublishedDocument(doc);
070    }
071}