com.rodrigodev.xgen4j_table_generator.model.table.TableWriter.java Source code

Java tutorial

Introduction

Here is the source code for com.rodrigodev.xgen4j_table_generator.model.table.TableWriter.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Rodrigo Quesada <rodrigoquesada.dev@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.rodrigodev.xgen4j_table_generator.model.table;

import com.rodrigodev.xgen4j_table_generator.model.information.ErrorInfoWrapper;
import com.rodrigodev.xgen4j_table_generator.model.information.InformationWrapper;
import com.rodrigodev.xgen4j_table_generator.model.table.cell.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by Rodrigo Quesada on 15/07/15.
 */
@Singleton
public class TableWriter {

    public static class InjectedFields {
        @Inject
        IdCellWriter idWriter;
        @Inject
        NumericIdCellWriter numericIdWriter;
        @Inject
        DescriptionCellWriter descriptionWriter;
        @Inject
        ExceptionCellWriter exceptionCellWriter;
        @Inject
        CommonCellWriter commonCellWriter;

        @Inject
        public InjectedFields() {
        }
    }

    private CellWriter[] cellWriters;

    @Inject
    public TableWriter(InjectedFields inj) {
        cellWriters = new CellWriter[] { inj.idWriter, inj.numericIdWriter, inj.descriptionWriter,
                inj.exceptionCellWriter, inj.commonCellWriter };
    }

    public void write(InformationWrapper information, OutputStream output) {
        try {
            Workbook wb = new XSSFWorkbook();
            Table table = new Table(wb.createSheet());

            writeHeaders(information, table.newRow());
            for (ErrorInfoWrapper errorInfo : information.list()) {
                writeContent(errorInfo, table.newRow());
            }

            wb.write(output);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void writeHeaders(InformationWrapper information, Row row) {
        List<ErrorInfoWrapper> infoList = information.list();
        if (!infoList.isEmpty()) {
            for (CellWriter cellWriter : cellWriters) {
                cellWriter.writeHeader(row, infoList.get(0));
            }
        }
    }

    private void writeContent(ErrorInfoWrapper errorInfo, Row row) {
        for (CellWriter cellWriter : cellWriters) {
            cellWriter.writeContent(row, errorInfo);
        }
    }
}