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: DocumentModelUpdater.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.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.nuxeo.ecm.core.api.CoreSession; 022import org.nuxeo.ecm.core.api.DocumentLocation; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.api.DocumentNotFoundException; 025import org.nuxeo.ecm.core.api.IdRef; 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 that only updates existing documents. The doc ID is used to identity documents. The imported tree structure 033 * is ignored. 034 * 035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 036 */ 037// TODO: improve it -> 038// modify core session to add a batch create method and use it 039public class DocumentModelUpdater extends AbstractDocumentModelWriter { 040 041 private static final Log log = LogFactory.getLog(DocumentModelUpdater.class); 042 043 /** 044 * @param session the session to the repository where to write 045 * @param parentPath where to write the tree. this document will be used as the parent of all top level documents 046 * passed as input. Note that you may have 047 */ 048 public DocumentModelUpdater(CoreSession session, String parentPath) { 049 super(session, parentPath); 050 } 051 052 public DocumentModelUpdater(CoreSession session, String parentPath, int saveInterval) { 053 super(session, parentPath, saveInterval); 054 } 055 056 @Override 057 public DocumentTranslationMap write(ExportedDocument xdoc) throws IOException { 058 if (xdoc.getDocument() == null) { 059 // not a valid doc -> this may be a regular folder for example the 060 // root of the tree 061 return null; 062 } 063 064 DocumentModel doc = null; 065 String id = xdoc.getId(); 066 try { 067 doc = session.getDocument(new IdRef(id)); 068 } catch (DocumentNotFoundException e) { 069 log.error("Cannot update document. No such document: " + id); 070 return null; 071 } 072 073 doc = updateDocument(xdoc, doc); 074 DocumentLocation source = xdoc.getSourceLocation(); 075 DocumentTranslationMap map = new DocumentTranslationMapImpl(source.getServerName(), doc.getRepositoryName()); 076 map.put(source.getDocRef(), doc.getRef()); 077 return map; 078 } 079 080}