001/*
002 * (C) Copyright 2018 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 *     pierre
018 */
019package org.nuxeo.ecm.platform.csv.export.io;
020
021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
023import static org.nuxeo.ecm.platform.csv.export.io.DocumentModelCSVHelper.getList;
024import static org.nuxeo.ecm.platform.csv.export.io.DocumentModelCSVWriter.SCHEMAS_CTX_DATA;
025import static org.nuxeo.ecm.platform.csv.export.io.DocumentModelCSVWriter.XPATHS_CTX_DATA;
026
027import java.io.IOException;
028import java.io.OutputStream;
029import java.util.List;
030
031import org.apache.commons.csv.CSVPrinter;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.io.marshallers.csv.AbstractCSVWriter;
034import org.nuxeo.ecm.core.io.marshallers.csv.OutputStreamWithCSVWriter;
035import org.nuxeo.ecm.core.io.registry.Writer;
036import org.nuxeo.ecm.core.io.registry.reflect.Setup;
037
038/**
039 * @since 10.3
040 */
041@Setup(mode = SINGLETON, priority = REFERENCE)
042public class DocumentModelListCSVWriter extends AbstractCSVWriter<List<DocumentModel>> {
043
044    public DocumentModelListCSVWriter() {
045        super();
046    }
047
048    @Override
049    protected void write(List<DocumentModel> entity, CSVPrinter printer) throws IOException {
050        Writer<DocumentModel> writer = registry.getWriter(ctx, DocumentModel.class, TEXT_CSV_TYPE);
051        for (DocumentModel doc : entity) {
052            try (OutputStream out = new OutputStreamWithCSVWriter(printer)) {
053                writer.write(doc, DocumentModel.class, DocumentModel.class, TEXT_CSV_TYPE, out);
054            }
055            printer.println();
056        }
057    }
058
059    @Override
060    protected void writeHeader(List<DocumentModel> entity, CSVPrinter printer) throws IOException {
061        DocumentModelCSVHelper.printSystemPropertiesHeader(printer);
062        List<String> schemas = getList(ctx, SCHEMAS_CTX_DATA);
063        List<String> xpaths = getList(ctx, XPATHS_CTX_DATA);
064        DocumentModelCSVHelper.printPropertiesHeader(schemas, xpaths, printer);
065        printer.println();
066    }
067
068}