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()) + ".";

		File libraryExport = exporter.export();
		LOGGER.debug("Data export complete");

		if (!withResources) {
			return new Output(libraryExport, baseExportFileName + exporter.getExtension(false));
		}

		LOGGER.debug("Adding resources to export");
		// Build the ZIP file including all resources
		File zipFile = SystemConfiguration.getInstance().createTempFile("demyo-export-archive-",
				"." + exporter.getExtension(true), exportDirectory);

		try (FileOutputStream fos = new FileOutputStream(zipFile);
				BufferedOutputStream bos = new BufferedOutputStream(fos);
				ZipOutputStream zos = new ZipOutputStream(bos);) {
			// The file inside the archive must always have the same name to be imported back
			ZipUtils.compress(libraryExport, "demyo." + exporter.getExtension(false), zos);
			ZipUtils.compress(SystemConfiguration.getInstance().getImagesDirectory(), "images", zos);
		} catch (IOException e) {
			LOGGER.warn("Failed to export", e);
			throw new DemyoException(DemyoErrorCode.EXPORT_IO_ERROR, e);
		}

		LOGGER.debug("All resources added, export is fully complete");

		return new Output(zipFile, baseExportFileName + exporter.getExtension(true));
	}
}

@@ -162,22 +162,21 @@
 va.t
-ext.DateFormat
+ime.LocalDate
 ;%0Aim
@@ -190,28 +190,36 @@
 va.t
-ext.SimpleDat
+ime.format.DateTim
 eFormat
+ter
 ;%0Aim
@@ -248,31 +248,8 @@
 st;%0A
-import java.util.Date;%0A
 impo
@@ -1530,256 +1530,89 @@
 s);%0A
-%0A%09/**%0A%09 * Returns a date format to use in the exported file name.%0A%09 * %3Cp%3E%0A%09 * Doesn't use a ThreadLocal to avoid memory leaks.%0A%09 * %3C/p%3E%0A%09 * %0A%09 * @return The date format%0A%09 */%0A%09private static DateFormat getDateFormat() %7B%0A%09%09return new SimpleDateFormat
+%09private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern
 (%22yy
@@ -1622,19 +1622,16 @@
 MM-dd%22);
-%0A%09%7D
 %0A%0A%09priva
@@ -2421,41 +2421,42 @@
 %22 + 
-getDateFormat().format(new Date()
+LocalDate.now().format(DATE_FORMAT
 ) + 

Use the Java 8 time API.