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