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