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.util.List;
029
030import org.apache.commons.csv.CSVPrinter;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.io.marshallers.csv.AbstractCSVWriter;
033import org.nuxeo.ecm.core.io.marshallers.csv.OutputStreamWithCSVWriter;
034import org.nuxeo.ecm.core.io.registry.Writer;
035import org.nuxeo.ecm.core.io.registry.reflect.Setup;
036
037/**
038 * @since 10.3
039 */
040@Setup(mode = SINGLETON, priority = REFERENCE)
041public class DocumentModelListCSVWriter extends AbstractCSVWriter<List<DocumentModel>> {
042
043    public DocumentModelListCSVWriter() {
044        super();
045    }
046
047    @Override
048    protected void write(List<DocumentModel> entity, CSVPrinter printer) throws IOException {
049        Writer<DocumentModel> writer = registry.getWriter(ctx, DocumentModel.class, TEXT_CSV_TYPE);
050        for (DocumentModel doc : entity) {
051            writer.write(doc, DocumentModel.class, DocumentModel.class, TEXT_CSV_TYPE,
052                    new OutputStreamWithCSVWriter(printer));
053            printer.println();
054        }
055    }
056
057    @Override
058    protected void writeHeader(List<DocumentModel> entity, CSVPrinter printer) throws IOException {
059        DocumentModelCSVHelper.printSystemPropertiesHeader(printer);
060        List<String> schemas = getList(ctx, SCHEMAS_CTX_DATA);
061        List<String> xpaths = getList(ctx, XPATHS_CTX_DATA);
062        DocumentModelCSVHelper.printPropertiesHeader(schemas, xpaths, printer);
063        printer.println();
064    }
065
066}