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: DocumentModelWriter.java 30413 2008-02-21 18:38:54Z sfermigier $ 013 */ 014 015package org.nuxeo.ecm.core.io.impl.plugins; 016 017import java.io.IOException; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.nuxeo.common.utils.Path; 022import org.nuxeo.ecm.core.api.CoreSession; 023import org.nuxeo.ecm.core.api.DocumentLocation; 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.ecm.core.api.PathRef; 026import org.nuxeo.ecm.core.io.DocumentTranslationMap; 027import org.nuxeo.ecm.core.io.ExportedDocument; 028import org.nuxeo.ecm.core.io.impl.AbstractDocumentModelWriter; 029import org.nuxeo.ecm.core.io.impl.DocumentTranslationMapImpl; 030 031/** 032 * A writer which is creating new docs or updating existing docs. 033 * 034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 035 */ 036// TODO: improve it -> 037// modify core session to add a batch create method and use it 038public class DocumentModelWriter extends AbstractDocumentModelWriter { 039 040 private static final Log log = LogFactory.getLog(DocumentModelWriter.class); 041 042 /** 043 * @param session the session to the repository where to write 044 * @param parentPath where to write the tree. this document will be used as the parent of all top level documents 045 * passed as input. Note that you may have 046 */ 047 public DocumentModelWriter(CoreSession session, String parentPath) { 048 super(session, parentPath); 049 } 050 051 public DocumentModelWriter(CoreSession session, String parentPath, int saveInterval) { 052 super(session, parentPath, saveInterval); 053 } 054 055 @Override 056 public DocumentTranslationMap write(ExportedDocument xdoc) throws IOException { 057 if (xdoc.getDocument() == null) { 058 // not a valid doc -> this may be a regular folder for example the 059 // root of the tree 060 return null; 061 } 062 Path path = xdoc.getPath(); 063 // if (path.isEmpty() || path.isRoot()) { 064 // return; // TODO avoid to import the root 065 // } 066 path = root.append(path); // compute target path 067 068 return doWrite(xdoc, path); 069 } 070 071 private DocumentTranslationMap doWrite(ExportedDocument xdoc, Path targetPath) { 072 073 DocumentModel previousDoc = null; 074 PathRef pathRef = new PathRef(targetPath.toString()); 075 if (session.exists(pathRef)) { 076 previousDoc = session.getDocument(pathRef); 077 } 078 079 DocumentModel doc; 080 if (previousDoc == null) { 081 doc = createDocument(xdoc, targetPath); 082 } else { 083 doc = updateDocument(xdoc, previousDoc); 084 } 085 086 DocumentLocation source = xdoc.getSourceLocation(); 087 DocumentTranslationMap map = new DocumentTranslationMapImpl(source.getServerName(), doc.getRepositoryName()); 088 if (source.getDocRef() != null && source.getDocRef().reference() != null) { 089 map.put(source.getDocRef(), doc.getRef()); 090 } 091 return map; 092 } 093 094}