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