001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id: DocumentModelInjector.java 29029 2008-01-14 18:38:14Z ldoguin $
013 */
014
015package org.nuxeo.ecm.core.io.impl.plugins;
016
017import java.io.IOException;
018
019import org.nuxeo.common.utils.Path;
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentLocation;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.io.DocumentTranslationMap;
024import org.nuxeo.ecm.core.io.ExportedDocument;
025import org.nuxeo.ecm.core.io.impl.AbstractDocumentModelWriter;
026import org.nuxeo.ecm.core.io.impl.DocumentTranslationMapImpl;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031// TODO: improve it ->
032// modify core session to add a batch create method and use it
033public class DocumentModelInjector extends AbstractDocumentModelWriter {
034
035    /**
036     * @param session the session to the repository where to write
037     * @param parentPath where to write the tree. this document will be used as the parent of all top level documents
038     *            passed as input. Note that you may have
039     */
040    public DocumentModelInjector(CoreSession session, String parentPath) {
041        super(session, parentPath);
042
043    }
044
045    public DocumentModelInjector(CoreSession session, String parentPath, int saveInterval) {
046        super(session, parentPath, saveInterval);
047    }
048
049    @Override
050    public DocumentTranslationMap write(ExportedDocument xdoc) throws IOException {
051        Path path = xdoc.getPath();
052        if (path.isEmpty() || path.isRoot()) {
053            return null; // TODO avoid to import the root
054        }
055        path = root.append(path); // compute target path
056
057        DocumentModel doc = createDocument(xdoc, path);
058        DocumentLocation source = xdoc.getSourceLocation();
059        DocumentTranslationMap map = new DocumentTranslationMapImpl(source.getServerName(), doc.getRepositoryName());
060        map.put(source.getDocRef(), doc.getRef());
061        return map;
062    }
063
064}