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