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

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


import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.IQueryModel;
import cdac.medinfo.sdk.dcm30_04.services.javadbhelper.DicomImage;
import cdac.medinfo.sdk.dcm30_04.services.javadbhelper.Patient;
import cdac.medinfo.sdk.dcm30_04.services.javadbhelper.Series;
import cdac.medinfo.sdk.dcm30_04.services.javadbhelper.Study;

/**
 * This class handles database connection and queries generated for Composite Instance Retrieve Without Bulk Data Service Class.
 * CIRWBD_DBAgent is a singleton class.
 */
public class CIRWBD_DBAgent {

	CIRWBD_DBQuery objCIRWBD_DBQuery = null;
	private static String dbURL = null;
	private static String emdriver = null;
	private static Connection objcon = null;
	private static CIRWBD_DBAgent objDCMDBAgent = null;
	private static Object mutex = new Object();

	/**
	 * Default Private Constructor.
	 */
	private CIRWBD_DBAgent(String strConnectionString, String strDriverName)throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		emdriver = new String(strDriverName);
		dbURL = new String(strConnectionString);
	{
		// System.out.println("Loading the driver...");
	}
		Class.forName(emdriver).newInstance();
	{
		// System.out.println("Getting  database connection...");
	}
		objcon = DriverManager.getConnection(dbURL);
	{
		// System.out.println("Connection established");
	}
		objCIRWBD_DBQuery = new CIRWBD_DBQuery();
		objCIRWBD_DBQuery.setConnection(objcon);

	}

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

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

	/**
	 * Retrieves collection of PatientInfo.
	 * @return ISingleCollection<Patient> Collection of Patient.
	 * @throws SQLException Signals if SQL Exception occurs.
	 */
	public ISingleCollection<Patient> getPatientInfo()throws SQLException
	{
		return objCIRWBD_DBQuery.getPatientInfo();
	}

	/**
	 * Retrieves collection of SeriesInfo.
	 * @return ISingleCollection<Series> Collection of Series.
	 * @throws SQLException Signals if SQL Exception occurs.
	 */
	public ISingleCollection<Series> getSeriesInfo()throws SQLException
	{
		return objCIRWBD_DBQuery.getSeriesInfo();
	}

	/**
	 * Retrieves collection of StudyInfo.
	 * @return ISingleCollection<Study> Collection of Study.
	 * @throws SQLException Signals if SQL Exception occurs.
	 */
	public ISingleCollection<Study> getStudyInfo()throws SQLException
	{
		return objCIRWBD_DBQuery.getStudyInfo();
	}

	/**
	 * Retrieves collection of DicomImage.
	 * @return ISingleCollection<DicomImage> Collection of DicomImage.
	 * @throws SQLException Signals if SQL Exception occurs.
	 */
	public ISingleCollection<DicomImage> getDicomImageInfo()throws SQLException
	{
		return objCIRWBD_DBQuery.getDicomImageInfo();
	}

	/**
	 * This method populates resultant CIRWBD_QueryModel according to the given find parameter in CIRWBD_QueryModel.
	 * @param objQueryModel object of CIRWBD_QueryModel.
	 * @param objRstQueryModel object of resultant CIRWBD_QueryModel 
	 * @return boolean true if matches are found.
	 * 				   false if no match is found.
	 */
	public boolean find(IQueryModel objQueryModel, IQueryModel objRstQueryModel) throws SQLException
	{
		return objCIRWBD_DBQuery.find(objQueryModel, objRstQueryModel);
	}

	/**
	 * Retrieves  collection of dicom files path according to the given find parameter in CIRWBD_QueryModel.
	 * @param objQueryModel object of CIRWBD_QueryModel.
	 * @return ISingleCollection<String>  collection of dicom files path.
	 */
	public ISingleCollection<String> retrive(IQueryModel objQueryModel) throws SQLException
	{
		return objCIRWBD_DBQuery.retrieve(objQueryModel);
	}
}
