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.marshaling.io;
021
022import java.io.IOException;
023
024import org.nuxeo.common.utils.Path;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentLocation;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.io.DocumentTranslationMap;
030import org.nuxeo.ecm.core.io.ExportedDocument;
031import org.nuxeo.ecm.core.io.impl.AbstractDocumentModelWriter;
032import org.nuxeo.ecm.core.io.impl.DocumentTranslationMapImpl;
033import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter;
034
035/**
036 * {@link DocumentModelWriter} that creates a shallow DocumentModel (ie: no path and no uuid).
037 *
038 * @author tiry
039 */
040public class SingleShadowDocumentWriter extends AbstractDocumentModelWriter {
041
042    protected DocumentModel dm;
043
044    public SingleShadowDocumentWriter(CoreSession session, String parentPath) {
045        super(session, "/");
046    }
047
048    @Override
049    public DocumentTranslationMap write(ExportedDocument doc) throws IOException {
050        dm = createDocument(doc, null);
051        // keep location unchanged
052        DocumentLocation oldLoc = doc.getSourceLocation();
053        String oldServerName = oldLoc.getServerName();
054        DocumentRef oldDocRef = oldLoc.getDocRef();
055        DocumentTranslationMap map = new DocumentTranslationMapImpl(oldServerName, oldServerName);
056        map.put(oldDocRef, oldDocRef);
057        return map;
058    }
059
060    @Override
061    protected DocumentModel createDocument(ExportedDocument xdoc, Path toPath) {
062        String docType = xdoc.getType();
063        dm = session.createDocumentModel(docType);
064        // then load schemas data
065        loadSchemas(xdoc, dm, xdoc.getDocument());
066        return dm;
067    }
068
069    public DocumentModel getShadowDocument() {
070        return dm;
071    }
072}