<commit_before>package io.dummymaker.export.impl;

import io.dummymaker.export.Format;
import io.dummymaker.export.IExporter;
import io.dummymaker.export.container.IClassContainer;
import io.dummymaker.export.container.impl.ClassContainer;
import io.dummymaker.export.container.impl.ExportContainer;
import io.dummymaker.export.naming.IStrategy;
import io.dummymaker.util.BasicCollectionUtils;
import io.dummymaker.writer.IWriter;
import io.dummymaker.writer.impl.BufferedFileWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import static io.dummymaker.util.BasicStringUtils.isBlank;

/**
 * Basic abstract exporter implementation
 *
 * @see IExporter
 *
 * @author GoodforGod
 * @since 25.02.2018
 */
abstract class BasicExporter implements IExporter {

    private final Logger logger = Logger.getLogger(BasicExporter.class.getName());

    private String path;
    private Format format;
    private IStrategy strategy;

    /**
     * @param strategy naming strategy
     * @param path     path where to export (NULL IF HOME DIR)
     * @param format   export format
     *
     * @see IStrategy
     * @see Format
     */
    BasicExporter(final String path,
                  final Format format,
                  final IStrategy strategy) {
        setPath(path);
        this.format = format;
        this.strategy = strategy;
    }

    void setPath(final String path) {
        this.path = (isBlank(path))
                ? null
                : path;
    }

    void setStrategy(final IStrategy strategy) {
        if(strategy != null)
            this.strategy = strategy;
    }

    /**
     * Build class container with export entity parameters
     */
    <T> IClassContainer buildClassContainer(final T t) {
        return new ClassContainer(t, strategy);
    }

    /**
     * Build class container with export entity parameters
     */
    <T> IClassContainer buildClassContainer(final List<T> list) {
        return (BasicCollectionUtils.isNotEmpty(list))
                ? buildClassContainer(list.get(0))
                : null;
    }

    /**
     * Build buffered writer for export
     *
     * @see #export(Object)
     * @see #export(List)
     */
    IWriter buildWriter(final IClassContainer classContainer) {
        try {
            return new BufferedFileWriter(classContainer.exportClassName(), path, format.getExtension());
        } catch (IOException e) {
            logger.warning(e.getMessage());
            return null;
        }
    }

    /**
     * Generates class export field-value map
     *
     * @param t class to export
     * @return export containers
     * @see ExportContainer
     */
    <T> List<ExportContainer> extractExportContainers(final T t,
                                                      final IClassContainer classContainer) {

        final List<ExportContainer> exports = new ArrayList<>();
        classContainer.getContainers().forEach((key, value) -> {
            try {
                value.getField().setAccessible(true);

                final String exportFieldName = value.getExportName();
                final Object exportFieldValue = value.getField().get(t);

                exports.add(buildContainer(exportFieldName, exportFieldValue));

                value.getField().setAccessible(false);
            } catch (Exception e) {
                logger.warning(e.getMessage());
            }
        });

        return exports;
    }

    ExportContainer buildContainer(final String exportFieldName,
                                   final Object exportFieldValue) {
        if(exportFieldValue == null)
           return ExportContainer.buildValue(exportFieldName, "");

        if (exportFieldValue.getClass().equals(Date.class))
            return ExportContainer.buildValue(exportFieldName, String.valueOf(((Date) exportFieldValue).getTime()));

        if(exportFieldValue.getClass().isAssignableFrom(Collection.class)) {
            final ExportContainer container = ExportContainer.buildCollection(exportFieldName);
        }


        return ExportContainer.buildValue(exportFieldName, String.valueOf(exportFieldValue));
    }

    /**
     * Validate export argument
     *<commit_msg>Basic exporter JSON extension support<commit_after>package io.dummymaker.export.impl;

import io.dummymaker.export.Format;
import io.dummymaker.export.IExporter;
import io.dummymaker.export.container.IClassContainer;
import io.dummymaker.export.container.impl.ClassContainer;
import io.dummymaker.export.container.impl.ExportContainer;
import io.dummymaker.export.naming.IStrategy;
import io.dummymaker.util.BasicCollectionUtils;
import io.dummymaker.writer.IWriter;
import io.dummymaker.writer.impl.BufferedFileWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import static io.dummymaker.util.BasicStringUtils.isBlank;

/**
 * Basic abstract exporter implementation
 *
 * @see IExporter
 *
 * @author GoodforGod
 * @since 25.02.2018
 */
abstract class BasicExporter implements IExporter {

    private final Logger logger = Logger.getLogger(BasicExporter.class.getName());

    private String path;
    private Format format;
    private IStrategy strategy;

    /**
     * @param strategy naming strategy
     * @param path     path where to export (NULL IF HOME DIR)
     * @param format   export format
     *
     * @see IStrategy
     * @see Format
     */
    BasicExporter(final String path,
                  final Format format,
                  final IStrategy strategy) {
        setPath(path);
        this.format = format;
        this.strategy = strategy;
    }

    void setPath(final String path) {
        this.path = (isBlank(path))
                ? null
                : path;
    }

    void setStrategy(final IStrategy strategy) {
        if(strategy != null)
            this.strategy = strategy;
    }

    /**
     * Build class container with export entity parameters
     */
    <T> IClassContainer buildClassContainer(final T t) {
        return new ClassContainer(t, strategy);
    }

    /**
     * Build class container with export entity parameters
     */
    <T> IClassContainer buildClassContainer(final List<T> list) {
        return (BasicCollectionUtils.isNotEmpty(list))
                ? buildClassContainer(list.get(0))
                : null;
    }

    /**
     * Build buffered writer for export
     *
     * @see #export(Object)
     * @see #export(List)
     */
    IWriter buildWriter(final IClassContainer classContainer) {
        try {
            return new BufferedFileWriter(classContainer.exportClassName(), path, format.getExtension());
        } catch (IOException e) {
            logger.warning(e.getMessage());
            return null;
        }
    }

    /**
     * Generates class export field-value map
     *
     * @param t class to export
     * @return export containers
     * @see ExportContainer
     */
    <T> List<ExportContainer> extractExportContainers(final T t,
                                                      final IClassContainer classContainer) {

        final List<ExportContainer> exports = new ArrayList<>();
        classContainer.getContainers().forEach((key, value) -> {
            try {
                value.getField().setAccessible(true);

                final String exportFieldName = value.getExportName();
                final Object exportFieldValue = value.getField().get(t);

                exports.add(buildContainer(exportFieldName, exportFieldValue));

                value.getField().setAccessible(false);
            } catch (Exception e) {
                logger.warning(e.getMessage());
            }
        });

        return exports;
    }

    ExportContainer buildContainer(final String exportFieldName,
                                   final Object exportFieldValue) {
        if(exportFieldValue == null)
           return ExportContainer.buildValue(exportFieldName, "");

        if (exportFieldValue.getClass().equals(Date.class))
            return ExportContainer.buildValue(exportFieldName, String.valueOf(((Date) exportFieldValue).getTime()));

        if(this.format == Format.JSON) {
            if (exportFieldValue.getClass().isAssignableFrom(Collection.class)) {
                final ExportContainer container = ExportContainer.buildCollection(exportFieldName);
            }

            final IClassContainer classContainer = buildClassContainer(exportFieldValue.getClass());
            if(classContainer.isExportable()) {
                final List<ExportContainer> embeddedContainers = extractExportContainers(exportFieldValue, classContainer);
            }
        }


        return ExportContainer.buildValue(exportFieldName, String.valueOf(exportFieldValue));
    }

    /**
     * Validate export argument
     *
{'fraction_non_alphanumeric': 0.09455555555555556, 'fraction_numerical': 0.002, 'mean_word_length': 2.4250380517503807, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3106404', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2306, 'n_words': 517}