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 * Nuxeo - initial API and implementation 011 * 012 * $Id: IODocumentManagerImpl.java 29979 2008-02-07 16:00:26Z dmihalache $ 013 */ 014 015package org.nuxeo.ecm.core.io.impl; 016 017import java.io.IOException; 018import java.io.InputStream; 019import java.io.OutputStream; 020import java.util.ArrayList; 021import java.util.Collection; 022import java.util.List; 023 024import org.nuxeo.ecm.core.api.CoreInstance; 025import org.nuxeo.ecm.core.api.CoreSession; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.DocumentRef; 028import org.nuxeo.ecm.core.api.NuxeoException; 029import org.nuxeo.ecm.core.io.DocumentPipe; 030import org.nuxeo.ecm.core.io.DocumentReader; 031import org.nuxeo.ecm.core.io.DocumentTranslationMap; 032import org.nuxeo.ecm.core.io.DocumentWriter; 033import org.nuxeo.ecm.core.io.IODocumentManager; 034import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter; 035import org.nuxeo.ecm.core.io.impl.plugins.DocumentTreeReader; 036import org.nuxeo.ecm.core.io.impl.plugins.DocumentsListReader; 037import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveReader; 038import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter; 039 040/** 041 * IODocumentManager basic implementation. 042 * 043 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 044 */ 045public class IODocumentManagerImpl implements IODocumentManager { 046 047 private static final long serialVersionUID = -3131999198524020179L; 048 049 @Override 050 public DocumentTranslationMap importDocuments(InputStream in, String repo, DocumentRef root) { 051 DocumentReader reader = null; 052 DocumentModelWriter writer = null; 053 try (CoreSession coreSession = CoreInstance.openCoreSessionSystem(repo)) { 054 final DocumentModel dst = coreSession.getDocument(root); 055 reader = new NuxeoArchiveReader(in); 056 writer = new DocumentModelWriter(coreSession, dst.getPathAsString()); 057 DocumentPipe pipe = new DocumentPipeImpl(10); 058 pipe.setReader(reader); 059 pipe.setWriter(writer); 060 DocumentTranslationMap map = pipe.run(); 061 coreSession.save(); 062 return map; 063 } catch (IOException e) { 064 throw new NuxeoException(e); 065 } finally { 066 if (reader != null) { 067 reader.close(); 068 } 069 if (writer != null) { 070 writer.close(); 071 } 072 } 073 } 074 075 @Override 076 public DocumentTranslationMap importDocuments(InputStream in, DocumentWriter customDocWriter) { 077 078 DocumentReader reader = null; 079 080 try { 081 reader = new NuxeoArchiveReader(in); 082 DocumentPipe pipe = new DocumentPipeImpl(10); 083 pipe.setReader(reader); 084 pipe.setWriter(customDocWriter); 085 DocumentTranslationMap map = pipe.run(); 086 087 // will need to save session before notifying events, otherwise docs won't be found 088 customDocWriter.close(); 089 090 return map; 091 } catch (IOException e) { 092 throw new NuxeoException(e); 093 } finally { 094 if (reader != null) { 095 reader.close(); 096 } 097 // writer will be closed by caller 098 } 099 } 100 101 @Override 102 public DocumentTranslationMap exportDocuments(OutputStream out, String repo, Collection<DocumentRef> sources, 103 boolean recurse, String format) { 104 DocumentReader reader = null; 105 DocumentWriter writer = null; 106 try (CoreSession coreSession = CoreInstance.openCoreSessionSystem(repo)) { 107 DocumentPipe pipe = new DocumentPipeImpl(10); 108 // XXX check format before creating writer 109 writer = new NuxeoArchiveWriter(out); 110 pipe.setWriter(writer); 111 if (!recurse) { 112 reader = DocumentsListReader.createDocumentsListReader(coreSession, sources); 113 pipe.setReader(reader); 114 return pipe.run(); 115 } else { 116 List<DocumentTranslationMap> maps = new ArrayList<DocumentTranslationMap>(); 117 for (DocumentRef rootSource : sources) { 118 // create a tree reader for each doc 119 reader = new DocumentTreeReader(coreSession, rootSource); 120 pipe.setReader(reader); 121 DocumentTranslationMap map = pipe.run(); 122 if (map != null) { 123 maps.add(map); 124 } 125 } 126 return DocumentTranslationMapImpl.merge(maps); 127 } 128 } catch (IOException e) { 129 throw new NuxeoException(e); 130 } finally { 131 if (reader != null) { 132 reader.close(); 133 } 134 if (writer != null) { 135 writer.close(); 136 } 137 } 138 } 139 140 @Override 141 public DocumentTranslationMap exportDocuments(OutputStream out, DocumentReader customDocReader, String format) { 142 143 DocumentWriter writer = null; 144 145 try { 146 DocumentPipe pipe = new DocumentPipeImpl(10); 147 // XXX check format before creating writer 148 writer = new NuxeoArchiveWriter(out); 149 pipe.setWriter(writer); 150 pipe.setReader(customDocReader); 151 152 List<DocumentTranslationMap> maps = new ArrayList<DocumentTranslationMap>(); 153 DocumentTranslationMap map = pipe.run(); 154 if (map != null) { 155 maps.add(map); 156 } 157 158 return DocumentTranslationMapImpl.merge(maps); 159 } catch (IOException e) { 160 throw new NuxeoException(e); 161 } finally { 162 // reader will be closed by caller 163 if (writer != null) { 164 writer.close(); 165 } 166 } 167 } 168 169 @Override 170 public DocumentTranslationMap importDocuments(DocumentReader customDocReader, DocumentWriter customDocWriter) { 171 172 try { 173 DocumentPipe pipe = new DocumentPipeImpl(10); 174 pipe.setReader(customDocReader); 175 pipe.setWriter(customDocWriter); 176 DocumentTranslationMap map = pipe.run(); 177 178 // will need to save session before notifying events, otherwise docs 179 // won't be found 180 // writer.close(); 181 182 return map; 183 } catch (IOException e) { 184 throw new NuxeoException(e); 185 } 186 } 187 188}