/*******************************************************************************
 *  Copyright 2017-2018 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 * 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
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 * 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 cdac.medinfo.sdk.dcm30_04.services.relevantpatientinfoqry.sourceimpl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;

import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;

/**
 * Modality Worklist DataBase Agent provides implementation for
 * processing of queries on DataBase.
 */
class RelevantPatientInfoQryDBAgent 
{
	private static Connection objConnection = null;
	private static RelevantPatientInfoQryDBAgent objRelevantPatientInfoQryDBAgent= null;
	private static Object mutex = new Object();
	private RelevantPatientInfoQry_DBQuery objRelevantPatientInfoQry_DBQuery= null;
	private static String dbURL = null;
	private static String emdriver = null;

	/**
	 * Default Constructor of {@link RelevantPatientInfoQryDBAgent}
	 * @param strConnectionString Connection String to connect DataBase
	 * @param strDriverName Database Driver Name
	 * @throws InstantiationException - thrown in case failure when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated.
	 * @throws IllegalAccessException - thrown in case failure when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor. 
	 * @throws ClassNotFoundException - thrown in case failure when an application tries to load in a class. 
	 * @throws SQLException - thrown in case of database access error or other errors.
	 */
	private RelevantPatientInfoQryDBAgent(String strConnectionString, String strDriverName) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException 
	{
		emdriver = new String(strDriverName);
		dbURL = new String(strConnectionString);		
		Class.forName(emdriver).newInstance();
		objConnection = DriverManager.getConnection(dbURL);
		objRelevantPatientInfoQry_DBQuery = new RelevantPatientInfoQry_DBQuery();
		objRelevantPatientInfoQry_DBQuery.setConnection(objConnection);
	}

	/**
	 * Creates instance of RelevantPatientInfoQryDBAgent.
	 * @param strConnectionString Database Connection String
	 * @param strDriverName Database driver fully qualified name
	 * @return instance of RelevantPatientInfoQryDBAgent.
	 * @throws SQLException  - thrown in case of database access error or other errors.
	 * @throws ClassNotFoundException  - thrown in case failure when an application tries to load in a class.
	 * @throws InstantiationException - thrown in case failure when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated.
	 * @throws IllegalAccessException - thrown in case failure when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.
	 */
	static RelevantPatientInfoQryDBAgent createInstance(String strConnectionString, String strDriverName)throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		synchronized (mutex) 
		{
			if(objRelevantPatientInfoQryDBAgent == null)
				objRelevantPatientInfoQryDBAgent = new RelevantPatientInfoQryDBAgent(strConnectionString,strDriverName);
		}
		return objRelevantPatientInfoQryDBAgent;
	}

	/**
	 * Stores specified Dataset to Database. 
	 * @param objIDataSet - instance of {@link IDataSet}
	 * @return  <tt>true</tt> if Success, otherwise
	 * 			<tt>false</tt>
	 * @throws SQLException if there is database access error or other errors.
	 */
	boolean storeInstanceInfo(IDataSet objIDataSet)throws SQLException
	{
		return objRelevantPatientInfoQry_DBQuery.storeInstanceInfo(objIDataSet);
	}

	/**
	 * Depending on the keys in the IDataSet it finds the matching DataSets and returns status.
	 * According to the matched DataSets populate the collection taken as out parameter.
	 * @param objIDataSet The data set containing identifiers.
	 * @param objRstDataCollection the matched data set collection
	 * @return boolean true - if one or more matches are found
	 * 				   false - if no matches are found.
	 * @throws SQLException if there is database access error or other errors.
	 */
	boolean find(IDataSet objIDataSet, ISingleCollection<IDataSet> objRstDataCollection) throws SQLException
	{
		return objRelevantPatientInfoQry_DBQuery.find(objIDataSet, objRstDataCollection);
	}
}
