/**
 * Copyright (c) 2015 Lemur Consulting Ltd.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.flax.biosolr.ontology.plugins.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import uk.co.flax.biosolr.ontology.config.OntologyConfiguration;
import uk.co.flax.biosolr.ontology.plugins.OntologyPlugin;
import uk.co.flax.biosolr.ontology.plugins.Plugin;
import uk.co.flax.biosolr.ontology.plugins.PluginException;
import uk.co.flax.biosolr.ontology.plugins.PluginInitialisationException;

import com.hp.hpl.jena.sparql.core.DatasetGraph;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.tdb.TDBLoader;
import com.hp.hpl.jena.tdb.sys.TDBInternal;

/**
 * @author Matt Pearce
 */
public class TDBOntologyPlugin implements OntologyPlugin {
	
	public static final String PLUGIN_NAME = "tripleStore";
	
	static final String ENABLED_CFGKEY = "enabled";
	static final String TDB_PATH_CFGKEY = "tdbPath";
	
	private static final Logger LOGGER = LoggerFactory.getLogger(TDBOntologyPlugin.class);
	
	private DatasetGraph dataset;
	
	public TDBOntologyPlugin() {
		// Do nothing - no configuration available until initialise() called
	}

	@Override
	public Plugin createPlugin() {
		return new TDBOntologyPlugin();
	}

	@Override
	public void initialise(Map<String, Object> configuration) throws PluginInitialisationException {
		LOGGER.debug("Initialising ontology plugin: {}", PLUGIN_NAME);
		if (!configuration.containsKey(ENABLED_CFGKEY)) {
			LOGGER.info("No '{}' config key - assuming plugin disabled", ENABLED_CFGKEY);
		} else if ((Boolean) configuration.get(ENABLED_CFGKEY)) {
			if (!configuration.containsKey(TDB_PATH_CFGKEY)) {
				throw new PluginInitialisationException("No " + TDB_PATH_CFGKEY + " specified - cannot create TDB dataset.");
			} else {
				this.dataset = TDBFactory.createDatasetGraph((String) configuration.get(TDB_PATH_CFGKEY));
			}
		}
	}

	@Override
	public void shutdown() throws PluginException {
		LOGGER.debug("Shutting down ontology plugin: {}", PLUGIN_NAME);
		if (dataset != null) {
			dataset.close();
		}
	}

	@Override
	public void process(String sourceName, OntologyConfiguration ontologyConfiguration) throws PluginException {
		LOGGER.debug("Loading dataset {} into triple store from {}", sourceName, ontologyConfiguration.getAccessURI());
		TDBLoader.load(TDBInternal.getDatasetGraphTDB(dataset), ontologyConfiguration.getAccessURI(), true);
	}

}

@@ -703,24 +703,83 @@
 til.Map;%0D%0A%0D%0A
+import com.sun.org.apache.xpath.internal.operations.Bool;%0D%0A
 import org.s
@@ -1670,17 +1670,46 @@
 lass);%0D%0A
-%09
+%0D%0A    private boolean enabled;
 %0D%0A%09priva
@@ -2253,20 +2253,41 @@
 %09%7D else 
-if (
+%7B%0D%0A            enabled = 
 (Boolean
@@ -2287,17 +2287,16 @@
 Boolean)
- 
 configur
@@ -2320,24 +2320,63 @@
 _CFGKEY)
-) %7B%0D%0A%09%09%09
+;%0D%0A            if (enabled) %7B%0D%0A                
 if (!con
@@ -2419,20 +2419,36 @@
 EY)) %7B%0D%0A
-%09%09%09%09
+                    
 throw ne
@@ -2554,25 +2554,54 @@
 );%0D%0A
-%09%09%09%7D else %7B%0D%0A%09%09%09%09
+                %7D else %7B%0D%0A                    
 this
@@ -2688,19 +2688,47 @@
 KEY));%0D%0A
-%09%09%09
+                %7D%0D%0A            
 %7D%0D%0A%09%09%7D%0D%0A
@@ -3035,34 +3035,68 @@
 ginException %7B%0D%0A
-%09%09
+        if (enabled) %7B%0D%0A            
 LOGGER.debug(%22Lo
@@ -3192,18 +3192,28 @@
 RI());%0D%0A
-%09%09
+            
 TDBLoade
@@ -3302,21 +3302,111 @@
 (), true);%0D%0A
+        %7D else %7B%0D%0A            LOGGER.debug(%22TDB plugin disabled - skipping%22);%0D%0A        %7D%0D%0A
 %09%7D%0D%0A%0D%0A%7D%0D%0A

Fix NPE when TDB plugin is disabled.