package com.github.aureliano.edocs.common.helper;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import com.github.aureliano.edocs.common.config.AppConfiguration;
import com.github.aureliano.edocs.common.config.FileRepositoryConfiguration;
import com.github.aureliano.edocs.common.config.SecureConfiguration;
import com.github.aureliano.edocs.common.exception.EDocsException;

public final class ConfigurationHelper {

	private static final String DEFAULT_CONFIGURATION_PATH = "conf/app-configuration.properties";

	private ConfigurationHelper() {}
	
	public static AppConfiguration parseConfiguration() {
		return parseConfiguration(DEFAULT_CONFIGURATION_PATH);
	}
	
	public static AppConfiguration parseConfiguration(String path) {
		Properties properties = loadProperties(path);
		return new AppConfiguration()
			.withSecureConfiguration(buildSecureModel(properties))
			.withFileRepositoryConfiguration(buildFileRepositoryModel(properties));
	}
	
	private static SecureConfiguration buildSecureModel(Properties properties) {
		SecureConfiguration conf = new SecureConfiguration()
			.withAlgorithm(properties.getProperty("app.secure.algorithm"))
			.withSalt(properties.getProperty("app.secure.salt"));
		
		if (StringHelper.isEmpty(properties.getProperty("app.secure.hash.iterations"))) {
			conf.withHashIterations(null);
		} else {
			conf.withHashIterations(Integer.parseInt(properties.getProperty("app.secure.hash.iterations")));
		}
		
		return conf;
	}
	
	private static FileRepositoryConfiguration buildFileRepositoryModel(Properties properties) {
		FileRepositoryConfiguration conf = new FileRepositoryConfiguration()
			.withRepositoryType(properties.getProperty("app.repository.type"))
			.withRootPath(properties.getProperty("app.repository.file.path"))
			.withLimboPath(properties.getProperty("app.repository.limbo.path"));
		
		return conf;
	}
	
	private static Properties loadProperties(String path) {
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(path));
		} catch (IOException ex) {
			throw new EDocsException(ex);
		}

		return properties;
	}
}
@@ -59,26 +59,80 @@
 java.io.File
-In
+;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOut
 putStream;%0Ai
@@ -963,16 +963,16 @@
 rties))%0A
-
 %09%09%09.with
@@ -1039,24 +1039,1086 @@
 ies));%0A%09%7D%0A%09%0A
+%09public static void saveConfigurationFile(AppConfiguration configuration, String comments) %7B%0A%09%09Properties properties = new Properties();%0A%09%09SecureConfiguration secure = configuration.getSecureConfiguration();%0A%09%09FileRepositoryConfiguration repo = configuration.getFileRepositoryConfiguration();%0A%09%09%0A%09%09properties.setProperty(%22app.secure.algorithm%22, secure.getAlgorithm());%0A%09%09properties.setProperty(%22app.secure.salt%22, secure.getSalt());%0A%09%09properties.setProperty(%22app.secure.hash.iterations%22, secure.getHashIterations().toString());%0A%09%09%0A%09%09properties.setProperty(%22app.repository.type%22, repo.getRepositoryType());%0A%09%09properties.setProperty(%22app.repository.file.path%22, repo.getRootPath());%0A%09%09properties.setProperty(%22app.repository.limbo.path%22, repo.getLimboPath());%0A%09%09%0A%09%09File confDir = new File(%22conf%22);%0A%09%09if (!confDir.exists()) %7B%0A%09%09%09FileHelper.createDirectory(confDir, false);%0A%09%09%7D%0A%09%09%0A%09%09try(FileOutputStream fos = new FileOutputStream(DEFAULT_CONFIGURATION_PATH)) %7B%0A%09%09%09properties.store(fos, comments);%0A%09%09%7D catch (IOException ex) %7B%0A%09%09%09throw new EDocsException(ex);%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09private sta

Add method to write configuration file.