package edu.emory.cci.aiw.cvrg.eureka.etl.ksb;

import java.io.File;

import org.protempa.KnowledgeSource;
import org.protempa.KnowledgeSourceReadException;
import org.protempa.PropositionDefinition;
import org.protempa.SourceFactory;
import org.protempa.backend.BackendInitializationException;
import org.protempa.backend.BackendNewInstanceException;
import org.protempa.backend.BackendProviderSpecLoaderException;
import org.protempa.backend.Configurations;
import org.protempa.backend.ConfigurationsLoadException;
import org.protempa.backend.InvalidConfigurationException;
import org.protempa.bconfigs.commons.INICommonsConfigurations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.emory.cci.aiw.cvrg.eureka.common.entity.Configuration;

public class PropositionFinder {

	private static final Logger LOGGER =
			LoggerFactory.getLogger(PropositionFinder.class);
	private static final File CONF_DIR = new File("/etc/eureka/etlconfig");
	private static final String CONF_PREFIX = "config";
	private final KnowledgeSource knowledgeSource;

	public PropositionFinder (Configuration inConfiguration) throws
		PropositionFinderException {
		Long confId = inConfiguration.getId();
		try {
			String idStr = String.valueOf(confId.longValue());
			String confFileName = CONF_PREFIX + idStr + ".ini";
			Configurations configurations =
				new INICommonsConfigurations(CONF_DIR);
			SourceFactory sf =
				new SourceFactory(configurations,
					confFileName);
			this.knowledgeSource = sf.newKnowledgeSourceInstance();
		} catch (BackendProviderSpecLoaderException e) {
			throw new PropositionFinderException(e);
		} catch (InvalidConfigurationException e) {
			throw new PropositionFinderException(e);
		} catch (ConfigurationsLoadException e) {
			throw new PropositionFinderException(e);
		} catch (BackendNewInstanceException e) {
			throw new PropositionFinderException(e);
		} catch (BackendInitializationException e) {
			throw new PropositionFinderException(e);
		}
	}

	public PropositionDefinition find(String inKey)
			throws PropositionFinderException {
		PropositionDefinition definition = null;
		try {
			definition = this.knowledgeSource.readPropositionDefinition(inKey);
		} catch (KnowledgeSourceReadException e) {
			throw new PropositionFinderException(e);
		}
		return definition;
	}
}

@@ -959,58 +959,683 @@
 %22);%0A
-%09private static final String CONF_PREFIX = %22config
+%0A%09static %7B%0A%09%09if (LOGGER.isErrorEnabled()) %7B%0A%09%09%09if (!CONF_DIR.exists()) %7B%0A%09%09%09%09LOGGER.error(%0A%09%09%09%09%09%09%22Configuration directory %22%0A%09%09%09%09%09%09+ CONF_DIR.getAbsolutePath()%0A%09%09%09%09%09%09+ %22 does not exist. Proposition finding will not work without it. Please create it and try again.%22);%0A%09%09%09%7D else if (!CONF_DIR.isDirectory()) %7B%0A%09%09%09%09LOGGER.error(%0A%09%09%09%09%09%09%22Path %22%0A%09%09%09%09%09%09+ CONF_DIR.getAbsolutePath()%0A%09%09%09%09%09%09+ %22 is not a directory. Proposition finding requires it to be a directory.%22);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09private static final File DEFAULT_CONF_DIR =%0A%09%09%09new File(CONF_DIR, %22etlconfigdefaults%22);%0A%09private static final String CONF_PREFIX = %22config%22;%0A%09private static final String DEFAULT_CONF_FILE = %22defaults.ini
 %22;%0A%09
@@ -1707,17 +1707,16 @@
 onFinder
- 
 (Configu
@@ -1746,16 +1746,17 @@
  throws%0A
+%09
 %09%09Propos
@@ -1971,24 +1971,25 @@
 tions =%0A%09%09%09%09
+%09
 new INICommo
@@ -2038,24 +2038,25 @@
 ry sf =%0A%09%09%09%09
+%09
 new SourceFa
@@ -2104,62 +2104,501 @@
 %0A%09%09%09
-this.knowledgeSource = sf.newKnowledgeSourceInstance()
+KnowledgeSource ks = sf.newKnowledgeSourceInstance();%0A%09%09%09if (ks == null && DEFAULT_CONF_DIR.exists()) %7B%0A%09%09%09%09File defaultConfFile = new File(DEFAULT_CONF_DIR,%0A%09%09%09%09%09%09DEFAULT_CONF_FILE);%0A%09%09%09%09if (defaultConfFile.exists()) %7B%0A%09%09%09%09%09Configurations defaultConfigs =%0A%09%09%09%09%09%09%09new INICommonsConfigurations(DEFAULT_CONF_DIR);%0A%09%09%09%09%09SourceFactory defaultSF = new SourceFactory(defaultConfigs,%0A%09%09%09%09%09%09%09DEFAULT_CONF_FILE);%0A%09%09%09%09%09ks = defaultSF.newKnowledgeSourceInstance();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09this.knowledgeSource = ks
 ;%0A%09%09

Use the default config file if it exists.