/*******************************************************************************
 *  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.mediacreation.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.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.utils.EnumFeatureList;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;

/**
 * This class handles database connection and queries generated for Media Creation Management Service Class.
 * MediaCreation_DBAgent is a singleton class.
 * MediaCreation_DBAgent provides the support to create, update and retrieve information of Media Creation SOP Instances.
 */
public class MediaCreation_DBAgent 
{
	
	private MediaCreation_DBQuery objMediaCreation_DBQuery = null;
	private static String dbURL = null;
	private static String emdriver = null;
	private static Connection objcon = null;
	private static MediaCreation_DBAgent objMediaCreation_DBAgent = null;
	private static Object mutex = new Object();

	//Default Private Constructor
	private MediaCreation_DBAgent(String strConnectionString, String strDriverName)throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		emdriver = new String(strDriverName);
		dbURL = new String(strConnectionString);
		Class.forName(emdriver).newInstance();
		objcon = DriverManager.getConnection(dbURL);
		objMediaCreation_DBQuery = new MediaCreation_DBQuery();
		objMediaCreation_DBQuery.setConnection(objcon);
	}

	/**
	 * Creates instance of MediaCreation_DBAgent.
	 * @return MediaCreation_DBAgent object of MediaCreation_DBAgent
	 * @throws SQLException Signals if SQL Exception occurs.
	 * @throws ClassNotFoundException if ClassNotFoundException occurs
	 * @throws InstantiationException if InstantiationException occurs
	 * @throws IllegalAccessException if IllegalAccessException occurs
	 */
	public static MediaCreation_DBAgent createInstance(String strConnectionString, String strDriverName)throws SQLException,ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		synchronized (mutex) 
		{
			if(objMediaCreation_DBAgent == null)
				objMediaCreation_DBAgent = new MediaCreation_DBAgent(strConnectionString,strDriverName);
		}
		return objMediaCreation_DBAgent;
	}

	/**
	 * Creates SOP Instance for provided SOPInstanceUID and Dataset.
	 * @param objIDataSet instance of Dataset
	 * @param strSOPInstanceUID value of SOPInstanceUID
	 * @return boolean - status as true or false
	 * @throws SQLException if SQL operation fails
	 */
	public boolean createMediaCreationInstance(IDataSet objIDataSet,String strSOPInstanceUID)throws SQLException
	{
		return (objMediaCreation_DBQuery.createMediaCreationInstance(objIDataSet, strSOPInstanceUID));
	}

	/**
	 * Retrieves information for a Media Creation SOP Instance according to the attributes provided in Dataset.
	 * @param objIDataSet instance of Dataset
	 * @param strSOPInstanceUID value of SOPInstanceUID
	 * @return IDataSet - instance of resultant Dataset 
	 * @throws SQLException if SQL operation fails
	 */
	public IDataSet retrieveMediaCreationInstanceInfo(ISingleCollection<ITag> objITagCollection,String strSOPInstanceUID)throws SQLException
	{
		return objMediaCreation_DBQuery.retrieveMediaCreationInstanceInfo(objITagCollection, strSOPInstanceUID);
	}
	
	/**
	 * Retrieves information for a scheduled MediaCreationSOPInstance according to the attributes provided in Dataset.
	 * @param objIDataSet instance of Dataset
	 * @param strSOPInstanceUID value of SOPInstanceUID
	 * @return IDataSet - instance of resultant Dataset 
	 * @throws SQLException if SQL operation fails
	 */
	public IDataSet retrieveScheduledInstanceInfo(ISingleCollection<ITag> objITagCollection,String strSOPInstanceUID)throws SQLException
	{
		return objMediaCreation_DBQuery.retrieveScheduledInstanceInfo(objITagCollection, strSOPInstanceUID);
	}
	
	/**
	 * Updates information for a scheduled MediaCreationSOPInstance according to the attributes provided in Dataset.
	 * @param objIDataSet instance of Dataset
	 * @param strSOPInstanceUID value of SOPInstanceUID
	 * @return IDataSet - instance of resultant Dataset 
	 * @throws SQLException if SQL operation fails
	 */
	public boolean updateScheduledInstanceInfo(IDataSet objIDataSet,String strSOPInstanceUID)throws SQLException
	{
		return objMediaCreation_DBQuery.updateScheduledInstanceInfo(objIDataSet, strSOPInstanceUID);
	}
	
	/**
	 * 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 objMediaCreation_DBQuery.isSOPInstanceExist(strSOPInstance);
	}
	
	/**
	 * Checks whether provided SOP Instance is scheduled or not.
	 * @param strSOPInstance value of SOP Instance UID 
	 * @return boolean shows status as success or failure
	 * @throws SQLException if sql operation fails
	 */
	public String isSOPInstanceScheduled(String strSOPInstance)throws SQLException
	{
		return objMediaCreation_DBQuery.isSOPInstanceScheduled(strSOPInstance);
	}

	/**
	 * Close data base connection.
	 */
	public void close() throws SQLException
	{
		objcon.close();
	}
}
