package eu.dnetlib.xml.database.exist;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Required;

import eu.dnetlib.enabling.is.store.BulkResourceImporter;

/**
 * Persistent exist.
 *
 * TODO: this is copy&paste from TemporaryExistDatase. Refactor common stuff.
 *
 * @author marko
 *
 */
public class PersistentExistDatabase extends SyncExistDatabase {

	/**
	 * logger.
	 */
	private static final Log log = LogFactory.getLog(PersistentExistDatabase.class); // NOPMD by marko on 11/24/08 5:02 PM

	/**
	 * db directory.
	 */
	private transient File dbDirectory;

	/**
	 * this config file will be copied to a newly created temporary directory.
	 */
	private String configTemplate = "default-exist-conf.xml";

	/**
	 * db root path.
	 */
	private String dbRootPath;

	/**
	 * bulk importer.
	 */
	private BulkResourceImporter bulkImporter;

	/**
	 * exist config file.
	 */
	private File existConfigFile;

	/**
	 * {@inheritDoc}
	 *
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#start()
	 */
	@Override
	public void start() {
		log.warn("STARTING PERSISTENT EXIST DATABASE");
		dbDirectory = new File(dbRootPath);

		if (dbDirectory.exists()) {
			bulkImporter.setEnabled(false);
			existConfigFile = new File(dbDirectory, "conf.xml");
			setConfigFile(existConfigFile.getAbsolutePath());
		} else {
			createPersistentDatabase();
		}

		super.start();
	}

	/**
	 * create a temporary directory and copy the default configuration file.
	 */
	protected void createPersistentDatabase() {
		log.debug("creating persistent database");
		try {
			new File(dbDirectory, "data").mkdirs();

			final InputStream defaultConf = getClass().getResourceAsStream(getConfigTemplate());
			if (defaultConf == null)
				throw new IOException("cannot find " + getConfigTemplate());

			existConfigFile = new File(dbDirectory, "conf.xml");
			final FileOutputStream confOutput = new FileOutputStream(existConfigFile);

			try {
				IOUtils.copy(defaultConf, confOutput);
			} finally {
				confOutput.close();
			}

			setConfigFile(existConfigFile.getAbsolutePath());
		} catch (final IOException e) {
			log.fatal("creating database dir", e);
			throw new IllegalStateException(e);
		}
		log.debug("created temp database");
	}

	public File getDbDirectory() {
		return dbDirectory;
	}

	public void setDbDirectory(final File dbDirectory) {
		this.dbDirectory = dbDirectory;
	}

	public String getConfigTemplate() {
		return configTemplate;
	}

	public void setConfigTemplate(final String configTemplate) {
		this.configTemplate = configTemplate;
	}

	public BulkResourceImporter getBulkImporter() {
		return bulkImporter;
	}

	@Required
	public void setBulkImporter(final BulkResourceImporter bulkImporter) {
		this.bulkImporter = bulkImporter;
	}

	public File getExistConfigFile() {
		return existConfigFile;
	}

	public void setExistConfigFile(final File existConfigFile) {
		this.existConfigFile = existConfigFile;
	}

	@Required
	public String getDbRootPath() {
		return dbRootPath;
	}

	public void setDbRootPath(final String dbRootPath) {
		this.dbRootPath = dbRootPath;
	}

}
