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