package eu.dnetlib.enabling.manager.msro;

import javax.annotation.Resource;

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

import com.googlecode.sarasvati.Engine;
import com.googlecode.sarasvati.Env;
import com.googlecode.sarasvati.Graph;
import com.googlecode.sarasvati.GraphProcess;
import com.googlecode.sarasvati.mem.MemGraphProcess;

import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import eu.dnetlib.enabling.tools.ServiceLocator;
import eu.dnetlib.enabling.tools.blackboard.NotificationHandler;
import eu.dnetlib.workflow.GraphProcessRegistry;

/**
 * reindex indices when the mdstores change.
 *
 * @author marko
 *
 */
public class ReindexNotificationHandler implements NotificationHandler {

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

	/**
	 * workflow process registry.
	 */
	private GraphProcessRegistry processRegistry;

	/**
	 * workflow graph.
	 */
	private Graph graph;

	/**
	 * workflow engine.
	 */
	@Resource
	private transient Engine engine;

	/**
	 * locator.
	 */
	private ServiceLocator<ISLookUpService> lookupLocator;

	/**
	 * {@inheritDoc}
	 *
	 * @see eu.dnetlib.enabling.tools.blackboard.NotificationHandler#notified(java.lang.String, java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public void notified(final String subscrId, final String topic, final String rsId, final String profile) {
		if (!topic.startsWith("UPDATE.MDStoreDSResourceType"))
			return;

		final GraphProcess process = new MemGraphProcess(graph);
		processRegistry.associateProcessWithResource(process, rsId);

		// TODO: use utility method for this query because it's used in UpdateRepositorySizeJob.java too
		final String query = "collection('')/RESOURCE_PROFILE[.//MDSTORE_DS_IDENTIFIER='" + rsId + "']//REPOSITORY_SERVICE_IDENTIFIER/text()";

		try {
			final String repId = lookupLocator.getService().getResourceProfileByQuery(query);
			processRegistry.associateProcessWithResource(process, repId);
		} catch (ISLookUpDocumentNotFoundException e) {
			log.warn("cannot locate the repository associated with the mdstore " + rsId, e);
		} catch (ISLookUpException e) {
			log.warn("cannot locate the repository associated with the mdstore " + rsId, e);
		}

		final Env env = process.getEnv();

		env.setStringAttribute("mdId", rsId);

		engine.startProcess(process);
	}

	public GraphProcessRegistry getProcessRegistry() {
		return processRegistry;
	}

	@Required
	public void setProcessRegistry(final GraphProcessRegistry processRegistry) {
		this.processRegistry = processRegistry;
	}

	public Graph getGraph() {
		return graph;
	}

	@Required
	public void setGraph(final Graph graph) {
		this.graph = graph;
	}

	public ServiceLocator<ISLookUpService> getLookupLocator() {
		return lookupLocator;
	}

	public void setLookupLocator(final ServiceLocator<ISLookUpService> lookupLocator) {
		this.lookupLocator = lookupLocator;
	}

}
