package eu.dnetlib.dlms.jdbc.server;

import java.sql.SQLException;

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

import eu.dnetlib.dlms.jdbc.ParamInfo;
import eu.dnetlib.dlms.jdbc.parser.AbstractParserFactory;

/**
 * Abstract class of the main engine of the system. The instance of this class should be able to parse and execute every
 * String in Doroty Language
 * 
 * @author lexis
 */
public abstract class AbstractDOLEngine {

	/**
	 * logger.
	 */
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(AbstractDOLEngine.class);
	/**
	 * Context of the parsing and executions led by this engine. TODO: remove this property if we decided to associate
	 * the context to the connection instead of the engine.
	 */
	private EngineContext theContext;
	/** Factory to create instances of AbstractDOLParser class that will parse statement in DOL. */
	private AbstractParserFactory parserFactory;
	/** Factory to create instances of AbstractDOLExecuter class that will execute statements in DOL. */
	private AbstractExecuterFactory executerFactory;

	public void setTheContext(final EngineContext theContext) {
		this.theContext = theContext;
	}

	public EngineContext getTheContext() {
		return this.theContext;
	}

	public AbstractParserFactory getParserFactory() {
		return this.parserFactory;
	}

	@Required
	public void setParserFactory(final AbstractParserFactory parserFactory) {
		this.parserFactory = parserFactory;
	}

	public AbstractExecuterFactory getExecuterFactory() {
		return this.executerFactory;
	}

	@Required
	public void setExecuterFactory(final AbstractExecuterFactory executerFactory) {
		this.executerFactory = executerFactory;
	}

	/**
	 * Parses and executes the given string in Doroty language.
	 * 
	 * @param dolString
	 *            the DOL String to parse and execute
	 * @param paramList
	 *            list of parameter values to be used for the execution of dolString. Can be null if dolString does not
	 *            have any parameter.
	 * @throws SQLException
	 *             parsing or executing the DOL command
	 * @return an AbstractDOLExecuter instance that can be use to retrieve the execution results.
	 */
	public abstract AbstractDOLExecuter execute(String dolString, ParamInfo[] paramList) throws SQLException;

}
