<commit_before>/*
 jBilling - The Enterprise Open Source Billing System
 Copyright (C) 2003-2011 Enterprise jBilling Software Ltd. and <NAME>

 This file is part of jbilling.

 jbilling is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 jbilling 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 Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with jbilling.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sapienter.jbilling.server.util.csv;

import au.com.bytecode.opencsv.CSVWriter;
import com.sapienter.jbilling.server.util.converter.BigDecimalConverter;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.List;

/**
 * CsvExporter
 *
 * @author <NAME>
 * @since 03/03/11
 */
public class CsvExporter<T extends Exportable> implements Exporter<T> {

    private static final Logger LOG = Logger.getLogger(CsvExporter.class);

    /** The maximum safe number of exportable elements to processes.  */
    public static final Integer MAX_RESULTS = 10000;

    static {
        ConvertUtils.register(new BigDecimalConverter(), BigDecimal.class);
    }

    private Class<T> type;

    private CsvExporter(Class<T> type) {
        this.type = type;
    }

    /**
     * Factory method to produce a new instance of CsvExporter for the given type.
     *
     * @param type type of exporter
     * @param <T> type T
     * @return new exporter of type T
     */
    public static <T extends Exportable> CsvExporter<T> createExporter(Class<T> type) {
        return new CsvExporter<T>(type);
    }

    public Class<T> getType() {
        return type;
    }

    public String export(List<? extends Exportable> list) {
        String[] header;

        // list can be empty, instantiate a new instance of type to
        // extract the field names for the CSV header
        try {
            header = type.newInstance().getFieldNames();
        } catch (InstantiationException e) {
            LOG.debug("Could not produce a new instance of " + type.getSimpleName() + " to build CSV header.");
            return null;

        } catch (IllegalAccessException e) {
            LOG.debug("Constructor of " + type.getSimpleName() + " is not accessible to build CSV header.");
            return null;
        }

        StringWriter out = new StringWriter();
        CSVWriter writer = new CSVWriter(out);
        writer.writeNext(header);

        for (Exportable exportable : list) {
            for (Object[] values : exportable.getFieldValues()) {
                writer.writeNext(convertToString(values));
            }
        }

        try {
            writer.close();
            out.close();
        } catch (IOException e) {
            LOG.debug("Writer cannot be closed, exported CSV may be missing data.");
        }

        return out.toString();
    }

    public String[] convertToString(Object[] objects) {
        String[] strings = new String[objects.length];

        int i = 0;
        for (Object object : objects) {
            if (object != null) {
                Converter converter = ConvertUtils.lookup(object.getClass());
                strings[i++] = converter.convert(object.getClass(), object).toString();
            } else {
                strings[i++] = "";
            }
        }

        return strings;<commit_msg>Bugs #1581 - 'File Not Found' page is opened when user clicks on 'Download CSV' link.<commit_after>/*
 jBilling - The Enterprise Open Source Billing System
 Copyright (C) 2003-2011 Enterprise jBilling Software Ltd. and <NAME>

 This file is part of jbilling.

 jbilling is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 jbilling 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 Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with jbilling.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sapienter.jbilling.server.util.csv;

import au.com.bytecode.opencsv.CSVWriter;
import com.sapienter.jbilling.server.util.converter.BigDecimalConverter;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.List;

/**
 * CsvExporter
 *
 * @author <NAME>
 * @since 03/03/11
 */
public class CsvExporter<T extends Exportable> implements Exporter<T> {

    private static final Logger LOG = Logger.getLogger(CsvExporter.class);

    /** The maximum safe number of exportable elements to processes.  */
    public static final Integer MAX_RESULTS = 10000;

    static {
        ConvertUtils.register(new BigDecimalConverter(), BigDecimal.class);
    }

    private Class<T> type;

    private CsvExporter(Class<T> type) {
        this.type = type;
    }

    /**
     * Factory method to produce a new instance of CsvExporter for the given type.
     *
     * @param type type of exporter
     * @param <T> type T
     * @return new exporter of type T
     */
    public static <T extends Exportable> CsvExporter<T> createExporter(Class<T> type) {
        return new CsvExporter<T>(type);
    }

    public Class<T> getType() {
        return type;
    }

    public String export(List<? extends Exportable> list) {
        String[] header;

        // list can be empty, instantiate a new instance of type to
        // extract the field names for the CSV header
        try {
            header = type.newInstance().getFieldNames();
        } catch (InstantiationException e) {
            LOG.debug("Could not produce a new instance of " + type.getSimpleName() + " to build CSV header.");
            return null;

        } catch (IllegalAccessException e) {
            LOG.debug("Constructor of " + type.getSimpleName() + " is not accessible to build CSV header.");
            return null;
        }

        StringWriter out = new StringWriter();
        CSVWriter writer = new CSVWriter(out);
        writer.writeNext(header);

        for (Exportable exportable : list) {
            for (Object[] values : exportable.getFieldValues()) {
                writer.writeNext(convertToString(values));
            }
        }

        try {
            writer.close();
            out.close();
        } catch (IOException e) {
            LOG.debug("Writer cannot be closed, exported CSV may be missing data.");
        }

        return out.toString();
    }

    public String[] convertToString(Object[] objects) {
        String[] strings = new String[objects.length];

        int i = 0;
        for (Object object : objects) {
            if (object != null) {
                Converter converter = ConvertUtils.lookup(object.getClass());
                if (converter != null) {
                    strings[i++] = converter.convert(object.getClass(), object).toString();
                } else {
                    strings[i++] = object.toString();
                }
            } else {
                strings[i++] = "";
            }
        }

        return strings;
{'fraction_non_alphanumeric': 0.09396312200050518, 'fraction_numerical': 0.006062136903258399, 'mean_word_length': 2.4839419269687637, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5395587', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2070, 'n_words': 735}