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: DocumentPipeImpl.java 29029 2008-01-14 18:38:14Z ldoguin $
020 */
021
022package org.nuxeo.ecm.core.io.impl;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.nuxeo.ecm.core.io.DocumentPipe;
029import org.nuxeo.ecm.core.io.DocumentReader;
030import org.nuxeo.ecm.core.io.DocumentTransformer;
031import org.nuxeo.ecm.core.io.DocumentTranslationMap;
032import org.nuxeo.ecm.core.io.DocumentWriter;
033import org.nuxeo.ecm.core.io.ExportedDocument;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038public class DocumentPipeImpl implements DocumentPipe {
039
040    private final List<DocumentTransformer> transformers;
041
042    private final int pageSize;
043
044    private DocumentReader reader;
045
046    private DocumentWriter writer;
047
048    public DocumentPipeImpl(int pageSize) {
049        this.pageSize = pageSize;
050        transformers = new ArrayList<DocumentTransformer>();
051    }
052
053    public DocumentPipeImpl() {
054        this(0);
055    }
056
057    @Override
058    public void addTransformer(DocumentTransformer transformer) {
059        transformers.add(transformer);
060    }
061
062    @Override
063    public DocumentReader getReader() {
064        return reader;
065    }
066
067    @Override
068    public List<DocumentTransformer> getTransformers() {
069        return transformers;
070    }
071
072    @Override
073    public DocumentWriter getWriter() {
074        return writer;
075    }
076
077    @Override
078    public void removeTransformer(DocumentTransformer transformer) {
079        transformers.remove(transformer);
080    }
081
082    @Override
083    public void setReader(DocumentReader reader) {
084        this.reader = reader;
085    }
086
087    @Override
088    public void setWriter(DocumentWriter writer) {
089        this.writer = writer;
090    }
091
092    @Override
093    public DocumentTranslationMap run() throws IOException {
094        if (reader == null) {
095            throw new IllegalArgumentException("Pipe reader cannot be null");
096        }
097        if (writer == null) {
098            throw new IllegalArgumentException("Pipe writer cannot be null");
099        }
100
101        List<DocumentTranslationMap> maps = new ArrayList<DocumentTranslationMap>();
102        readAndWriteDocs(maps);
103        return DocumentTranslationMapImpl.merge(maps);
104    }
105
106    protected void handleBatchEnd() {
107        // NOP
108    }
109
110    protected void readAndWriteDocs(List<DocumentTranslationMap> maps) throws IOException {
111        if (pageSize == 0) {
112            // handle single doc case
113            ExportedDocument doc;
114            while ((doc = reader.read()) != null) {
115                applyTransforms(doc);
116                DocumentTranslationMap map = writer.write(doc);
117                maps.add(map);
118                handleBatchEnd();
119            }
120
121        } else {
122            // handle multiple doc case
123            ExportedDocument[] docs;
124            while ((docs = reader.read(pageSize)) != null) {
125                if (docs.length != 0) {
126                    applyTransforms(docs);
127                    DocumentTranslationMap map = writer.write(docs);
128                    if (map != null) {
129                        maps.add(map);
130                    }
131                    handleBatchEnd();
132                }
133            }
134        }
135    }
136
137    public void applyTransforms(ExportedDocument doc) throws IOException {
138        for (DocumentTransformer tr : transformers) {
139            tr.transform(doc);
140        }
141    }
142
143    public void applyTransforms(ExportedDocument[] docs) throws IOException {
144        for (DocumentTransformer tr : transformers) {
145            for (ExportedDocument doc : docs) {
146                tr.transform(doc);
147            }
148        }
149    }
150
151}