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 {
061            CoreSession coreSession = CoreInstance.getCoreSessionSystem(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 {
115            CoreSession coreSession = CoreInstance.getCoreSessionSystem(repo);
116            DocumentPipe pipe = new DocumentPipeImpl(10);
117            // XXX check format before creating writer
118            writer = new NuxeoArchiveWriter(out);
119            pipe.setWriter(writer);
120            if (!recurse) {
121                reader = DocumentsListReader.createDocumentsListReader(coreSession, sources);
122                pipe.setReader(reader);
123                return pipe.run();
124            } else {
125                List<DocumentTranslationMap> maps = new ArrayList<>();
126                for (DocumentRef rootSource : sources) {
127                    // create a tree reader for each doc
128                    reader = new DocumentTreeReader(coreSession, rootSource);
129                    pipe.setReader(reader);
130                    DocumentTranslationMap map = pipe.run();
131                    if (map != null) {
132                        maps.add(map);
133                    }
134                }
135                return DocumentTranslationMapImpl.merge(maps);
136            }
137        } catch (IOException e) {
138            throw new NuxeoException(e);
139        } finally {
140            if (reader != null) {
141                reader.close();
142            }
143            if (writer != null) {
144                writer.close();
145            }
146        }
147    }
148
149    @Override
150    public DocumentTranslationMap exportDocuments(OutputStream out, DocumentReader customDocReader, String format) {
151
152        DocumentWriter writer = null;
153
154        try {
155            DocumentPipe pipe = new DocumentPipeImpl(10);
156            // XXX check format before creating writer
157            writer = new NuxeoArchiveWriter(out);
158            pipe.setWriter(writer);
159            pipe.setReader(customDocReader);
160
161            List<DocumentTranslationMap> maps = new ArrayList<>();
162            DocumentTranslationMap map = pipe.run();
163            if (map != null) {
164                maps.add(map);
165            }
166
167            return DocumentTranslationMapImpl.merge(maps);
168        } catch (IOException e) {
169            throw new NuxeoException(e);
170        } finally {
171            // reader will be closed by caller
172            if (writer != null) {
173                writer.close();
174            }
175        }
176    }
177
178    @Override
179    public DocumentTranslationMap importDocuments(DocumentReader customDocReader, DocumentWriter customDocWriter) {
180
181        try {
182            DocumentPipe pipe = new DocumentPipeImpl(10);
183            pipe.setReader(customDocReader);
184            pipe.setWriter(customDocWriter);
185            DocumentTranslationMap map = pipe.run();
186
187            // will need to save session before notifying events, otherwise docs
188            // won't be found
189            // writer.close();
190
191            return map;
192        } catch (IOException e) {
193            throw new NuxeoException(e);
194        }
195    }
196
197}