<commit_before>package org.demyo.service.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipOutputStream;

import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.demyo.common.config.SystemConfiguration;
import org.demyo.common.exception.DemyoErrorCode;
import org.demyo.common.exception.DemyoException;
import org.demyo.service.IExportService;
import org.demyo.service.exporting.IExporter;
import org.demyo.utils.io.ZipUtils;

/**
 * Implements the contract defined by {@link IExportService}.
 */
@Service
public class ExportService implements IExportService {
	/**
	 * Defines the result of an export.
	 */
	public static final class Output {
		/** The {@link File} containing the data. */
		private final File file;
		/** The name of the file to provide to the user. */
		private final String fileName;

		Output(File file, String fileName) {
			this.file = file;
			this.fileName = fileName;
		}

		public File getFile() {
			return file;
		}

		public String getFileName() {
			return fileName;
		}
	}

	private static final String EXPORT_DIRECTORY_NAME = "exports";
	private static final Logger LOGGER = LoggerFactory.getLogger(ExportService.class);

	/**
	 * Returns a date format to use in the exported file name.
	 * <p>
	 * Doesn't use a ThreadLocal to avoid memory leaks.
	 * </p>
	 * 
	 * @return The date format
	 */
	private static DateFormat getDateFormat() {
		return new SimpleDateFormat("yyyy-MM-dd");
	}

	private final List<IExporter> exporters = new ArrayList<>();
	private final File exportDirectory;

	public ExportService() {
		exportDirectory = new File(SystemConfiguration.getInstance().getUserDirectory(), EXPORT_DIRECTORY_NAME);
		exportDirectory.mkdirs();
	}

	@Override
	public void registerExporter(@NotNull IExporter exporter) {
		LOGGER.debug("Registering exporter of type: {}", exporter.getClass().getCanonicalName());
		exporters.add(exporter);
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public Output export(boolean withResources) throws DemyoException {
		// Note that we don't check if withResources makes sense for the selected exporter.
		// It's not a issue at the moment.

		IExporter exporter = exporters.get(0);
		String baseExportFileName = "demyo_" + getDateFormat().format(new Date()) + ".";
<commit_msg>Use the Java 8 time API.<commit_after>package org.demyo.service.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipOutputStream;

import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.demyo.common.config.SystemConfiguration;
import org.demyo.common.exception.DemyoErrorCode;
import org.demyo.common.exception.DemyoException;
import org.demyo.service.IExportService;
import org.demyo.service.exporting.IExporter;
import org.demyo.utils.io.ZipUtils;

/**
 * Implements the contract defined by {@link IExportService}.
 */
@Service
public class ExportService implements IExportService {
	/**
	 * Defines the result of an export.
	 */
	public static final class Output {
		/** The {@link File} containing the data. */
		private final File file;
		/** The name of the file to provide to the user. */
		private final String fileName;

		Output(File file, String fileName) {
			this.file = file;
			this.fileName = fileName;
		}

		public File getFile() {
			return file;
		}

		public String getFileName() {
			return fileName;
		}
	}

	private static final String EXPORT_DIRECTORY_NAME = "exports";
	private static final Logger LOGGER = LoggerFactory.getLogger(ExportService.class);
	private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

	private final List<IExporter> exporters = new ArrayList<>();
	private final File exportDirectory;

	public ExportService() {
		exportDirectory = new File(SystemConfiguration.getInstance().getUserDirectory(), EXPORT_DIRECTORY_NAME);
		exportDirectory.mkdirs();
	}

	@Override
	public void registerExporter(@NotNull IExporter exporter) {
		LOGGER.debug("Registering exporter of type: {}", exporter.getClass().getCanonicalName());
		exporters.add(exporter);
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public Output export(boolean withResources) throws DemyoException {
		// Note that we don't check if withResources makes sense for the selected exporter.
		// It's not a issue at the moment.

		IExporter exporter = exporters.get(0);
		String baseExportFileName = "demyo_" + LocalDate.now().format(DATE_FORMAT) + ".";

{'fraction_non_alphanumeric': 0.10262345679012345, 'fraction_numerical': 0.0013503086419753086, 'mean_word_length': 4.806270996640538, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3230925', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1496, 'n_words': 416}