/*******************************************************************************
 *  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.colorpalette.dbhelper;

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;

/**
 * Color Palette DBAgent provides implementation for
 * processing of queries on DataBase.
 */
public class ColorPaletteDBAgent 
{
	private static Connection objConnection = null;
	private static ColorPaletteDBAgent objColorPaletteDBAgent= null;
	private static Object mutex = new Object();
	private ColorPalette_DBQuery objColorPalette_DBQuery= null;
	private static String dbURL = null;
	private static String emdriver = null;

	/**
	 * Default Constructor of {@link ColorPaletteDBAgent}
	 * @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 ColorPaletteDBAgent(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);
		objColorPalette_DBQuery = new ColorPalette_DBQuery();
		objColorPalette_DBQuery.setConnection(objConnection);
	}

	/**
	 * Creates instance of ColorPaletteDBAgent.
	 * @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.
	 */
	public static ColorPaletteDBAgent createInstance(String strConnectionString, String strDriverName)throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		synchronized (mutex) 
		{
			if(objColorPaletteDBAgent == null)
				objColorPaletteDBAgent= new ColorPaletteDBAgent(strConnectionString,strDriverName);
		}
		return objColorPaletteDBAgent;
	}

	/**
	 * 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.
	 */
	public boolean storeInstanceInfo(IDataSet objIDataSet)throws SQLException
	{
		return objColorPalette_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.
	 */
	public boolean find(IDataSet objIDataSet, ISingleCollection<IDataSet> objRstDataCollection) throws SQLException
	{
		return objColorPalette_DBQuery.find(objIDataSet, objRstDataCollection);
	}
	/**
	 * This function retrieves all DataSets related to strSOPInstanceUID.
	 * @param enumSOPClassUIDs
	 * @param objITagCollection  list of requested attributes
	 * @param strSOPInstanceUID String value of SOP instance UID to be retrieved.
	 * @return IDataSet retrieve data set.
	 * @throws SQLException Signals if the SQL operation fails.
	 * @throws IOException Signals if IO Operation fails
	 */
	public short retrieveColorPalette(IDataSet dataSet,ISingleCollection<IDataSet> objRstDatasetList) throws SQLException 
	{		
		return objColorPalette_DBQuery.retrieve(dataSet,objRstDatasetList);
		
	}
	
	/**
	 * Checks whether provided SOP Instance is created or not.
	 * @param strSOPInstance value of SOP Instance UID 
	 * @return boolean shows status as success or failure
	 * @throws SQLException if sql operation fails
	 */
	public boolean isSOPInstanceExist(String strSOPInstance)throws SQLException
	{
		return objColorPalette_DBQuery.isSOPInstanceExist(strSOPInstance);
	}

}

	
	

