<commit_before>/*******************************************************************************
 * Copyright (C) 2015 Black Duck Software, Inc.
 * http://www.blackducksoftware.com/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2 only
 * as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License version 2
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *******************************************************************************/
package com.blackducksoftware.tools.commonframework.standard.protex.report.template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.FormulaParser;
import org.apache.poi.ss.formula.FormulaRenderer;
import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.ptg.AreaPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.formula.ptg.RefPtgBase;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blackducksoftware.tools.commonframework.standard.protex.report.model.TemplateColumn;
import com.blackducksoftware.tools.commonframework.standard.protex.report.model.TemplatePojo;
import com.blackducksoftware.tools.commonframework.standard.protex.report.model.TemplateSheet;

/**
 * Writes out rows to an existing Template created by the {@link TemplateReader } .
 * 
 * @author <NAME>
 * @param <T>
 *            This is the extension of the TemplatePojo
 */
public class TemplateWriter<T extends TemplatePojo> {

    /** The log. */
    private final Logger log = LoggerFactory.getLogger(this.getClass()
            .getName());

    /**
     * Flags to instruct the TemplateWriter to either ignore or proceed on mapping errors
     * Mapping errors are where the Pojo methods do not match to the columns
     */
    public static final Boolean IGNORE_MAPPING_ERRORS = true;

    public static final Boolean EXIT_ON_MAPPING_ERRORS = false;

    /** The template reader. */
    private final TemplateReader templateReader;

    /** The pojo class. */
    private Class<T> pojoClass;

    /** The book. */
    private Workbook book;

    /**
     * Instantiates a new template writer.
     * 
     * @param templateReader
     *            the template reader
     */
    public TemplateWriter(TemplateReader templateReader) {
        this.templateReader = templateReader;
    }

    /**
     * Writes out pojos to specific sheet by using internal mappings specified
     * by the user and generates the work book
     * 
     * @param pojoList
     *            the pojo list
     * @param templateSheet
     *            the template sheet
     * @param pojoClass
     *            the pojo class
     * @param ignoreMissingColumns
     *            - if true, proceeds despite missing mappings (will write out what it can)
     * @throws Exception
     *             the exception
     */
    public void writeOutPojo(List<T> pojoList, TemplateSheet templateSheet,
            Class<T> pojoClass, boolean ignoreMissingColumns) throws Exception {
        writeOutPojo(pojoList,
                templateSheet,
                pojoClass,
                ignoreMissingColumns,
                true);
    }

    /**
     * Writes out pojos to specific sheet by using internal mappings specified
     * by the user and generated the workbook if the flag is set to true
     * 
     * @param pojoList
     *            the pojo list
     * @param templateSheet
     *            the template sheet
     * @param pojoClass
     *            the pojo class
     * @param ignoreMissingColumns
     *            - if true, proceeds despite missing mappings (will write out what it can)
     * @param generateWorkBook
     *            - if true, generates the workbook
     * @throws Exception
     */
    public void writeOutPojo(List<T> pojoList, TemplateSheet templateSheet,
            Class<T> pojoClass, boolean ignoreMissingColumns, boolean generateWorkBook) throws Exception {
        this.pojoClass = pojoClass;
        testReflectionMappings(templateSheet, pojoList.get(0), ignoreMissingColumns);

        book = templateReader.getInternalWorkBook();

        Map<String, TemplateColumn> columnMap = templateSheet.getColumnMap();

        Sheet activeSheet = book.getSheet(templateSheet.getSheetName());

        int i = 1;
        for (TemplatePojo pojo : pojoList) {
            Row activeRow = activeSheet.createRow(i);
            writePojoValuesToRow(activeSheet, activeRow, pojo, columnMap);
            i++;
        }

        if (generateWorkBook) {
            writeWorkBook(book);
        }
    }

    /**
     * Write pojo values to row for the provided sheet.
     * 
     * @param activeSheet
     *            the provided sheet for pojo values write out to the rows
     * @param activeRow
     *            the active row
     * @param pojo
     *            the pojo<commit_msg>#13: Rollback the change for TemplateWriter - Workbook generation on demand;<commit_after>/*******************************************************************************
 * Copyright (C) 2015 Black Duck Software, Inc.
 * http://www.blackducksoftware.com/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2 only
 * as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License version 2
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *******************************************************************************/
package com.blackducksoftware.tools.commonframework.standard.protex.report.template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.FormulaParser;
import org.apache.poi.ss.formula.FormulaRenderer;
import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.ptg.AreaPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.formula.ptg.RefPtgBase;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blackducksoftware.tools.commonframework.standard.protex.report.model.TemplateColumn;
import com.blackducksoftware.tools.commonframework.standard.protex.report.model.TemplatePojo;
import com.blackducksoftware.tools.commonframework.standard.protex.report.model.TemplateSheet;

/**
 * Writes out rows to an existing Template created by the {@link TemplateReader } .
 * 
 * @author <NAME>
 * @param <T>
 *            This is the extension of the TemplatePojo
 */
public class TemplateWriter<T extends TemplatePojo> {

    /** The log. */
    private final Logger log = LoggerFactory.getLogger(this.getClass()
            .getName());

    /**
     * Flags to instruct the TemplateWriter to either ignore or proceed on mapping errors
     * Mapping errors are where the Pojo methods do not match to the columns
     */
    public static final Boolean IGNORE_MAPPING_ERRORS = true;

    public static final Boolean EXIT_ON_MAPPING_ERRORS = false;

    /** The template reader. */
    private final TemplateReader templateReader;

    /** The pojo class. */
    private Class<T> pojoClass;

    /** The book. */
    private Workbook book;

    /**
     * Instantiates a new template writer.
     * 
     * @param templateReader
     *            the template reader
     */
    public TemplateWriter(TemplateReader templateReader) {
        this.templateReader = templateReader;
    }

    /**
     * Writes out pojos to specific sheet by using internal mappings specified
     * by the user.
     * 
     * @param pojoList
     *            the pojo list
     * @param templateSheet
     *            the template sheet
     * @param pojoClass
     *            the pojo class
     * @param ignoreMissingColumns
     *            - if true, proceeds despite missing mappings (will write out what it can)
     * @throws Exception
     *             the exception
     */
    public void writeOutPojo(List<T> pojoList, TemplateSheet templateSheet,
            Class<T> pojoClass, boolean ignoreMissingColumns) throws Exception {
        this.pojoClass = pojoClass;
        testReflectionMappings(templateSheet, pojoList.get(0), ignoreMissingColumns);

        book = templateReader.getInternalWorkBook();

        Map<String, TemplateColumn> columnMap = templateSheet.getColumnMap();

        Sheet activeSheet = book.getSheet(templateSheet.getSheetName());

        int i = 1;
        for (TemplatePojo pojo : pojoList) {
            Row activeRow = activeSheet.createRow(i);
            writePojoValuesToRow(activeSheet, activeRow, pojo, columnMap);
            i++;
        }

        writeWorkBook(book);
    }

    /**
     * Write pojo values to row for the provided sheet.
     * 
     * @param activeSheet
     *            the provided sheet for pojo values write out to the rows
     * @param activeRow
     *            the active row
     * @param pojo
     *            the pojo
{'fraction_non_alphanumeric': 0.10916666666666666, 'fraction_numerical': 0.004074074074074074, 'mean_word_length': 2.9506217995610826, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7666215', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2892, 'n_words': 895}