/**
 * 
 */
package org.pmdreports.manager;

import org.apache.log4j.Logger;
import org.pmdreports.util.PMDReportsException;
import org.pmdreports.util.StringUtil;

/**
 * Manage the configuration of the PMDReports tool (i.e. properties that define
 * how it will run.
 * 
 * @author Ben Northrop
 */
public class ConfigManager {

	// DATA MEMBERS
	private static ConfigManager instance;

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

	private final static String DEFAULT_CONFIG_DIR = "./";

	private final static String DEFAULT_OUTPUT_DIR = "pmdreports-output/";

	private static final String RULESETS_FILE = "rulesets.xml";

	private static final String PROJECTS_FILE = "projects.xml";

	private static final String PMD_XSL_FILE = "pmd.xslt";

	private static final String DATABASE = "DATABASE.txt";

	private final String configDir;

	private final String outputDir;

	// CONSTRUCTOR
	/**
	 * Private constructor for Singleton.
	 */
	private ConfigManager(String configDir, String outputDir) {
		this.configDir = StringUtil.isEmpty(configDir) ? DEFAULT_CONFIG_DIR
				: configDir;
		LOG.info("config directory: " + this.configDir);

		this.outputDir = StringUtil.isEmpty(outputDir) ? DEFAULT_OUTPUT_DIR
				: outputDir;
		LOG.info("output directory: " + this.outputDir);
	}

	// METHODS
	/**
	 * Factory method for retrieving instance.
	 */
	public static ConfigManager getInstance() {
		if (instance == null) {
			throw new PMDReportsException(
					"ConfigManager has not been initialized!");
		}
		return instance;
	}

	/**
	 * Create a new instance - to be called by main method.
	 */
	public static void newInstance(String configDir, String outputDir) {
		instance = new ConfigManager(configDir, outputDir);
	}

	/**
	 * Create a new instance - to be called by main method.
	 */
	public static void newInstance() {
		instance = new ConfigManager("", "");
	}

	public String getRulesetsFile() {
		return getConfigDir() + RULESETS_FILE;
	}

	public String getProjectsFile() {
		return getConfigDir() + PROJECTS_FILE;
	}

	public String getPmdXslFile() {
		return getOutputDir() + PMD_XSL_FILE;
	}

	public String getConfigDir() {
		return configDir + "/";
	}

	public String getOutputDir() {
		return outputDir + "/";
	}

	public String getDatabase() {
		return getOutputDir() + DATABASE;
	}
}
