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