/*******************************************************************************
 *  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.cservices.withdb.testcases.defaultscp;
import java.io.IOException;
import java.sql.SQLException;


import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;

import cdac.medinfo.sdk.dcm30_04.services.cfind.scp.CFindSCP;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumDataSource;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.IDataSource;
import cdac.medinfo.sdk.dcm30_04.services.javadbhelper.DataSourceImpl;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.IPairedCollection;
import cdac.medinfo.sdk.dcm30_04.utils.PairedCollection;

/**
 * This class tests a Stand Alone CFindSCP.
 * "Stand Alone" means that the User is required to write a minimal set of code to create C-CFindSCP
 * Once the required parameters are given to the CFindSCP object and is started , it is 
 * capable of accepting connection, negotiation association and creating appropriate response(AssociateAC or Reject)
 * processing CFind Request and sending DIMSE Response for the same, processing Release Request and sending Release Response.
 * If User wants to handle the request sending scenarios by his/her code 
 * then please refer to sample code "CFind_SCP.java". 
 * To create DB for C-Services refer SQL Script DICOMServerDB.sql provided in install. 
 */
public class CFind_SCP_Standalone 
{
	public static void main(String[] args) throws IOException, SQLException
	{
		try
		{
			DicomConfig objDicomConfig = DicomConfig.createInstance();

			//enable logging,specify the path of the directory where log files are created.
			//User can specify any directory in place of 'c:\DCM_Logs'.
			//If no directory is defined by user to create logs then it is created in User Temp DIR.
			objDicomConfig.enableLogging(EnumLoggingLevel.INFO, "C:\\DCM_Logs");
			/*
			 * Creates CFind_SCP_Standalone and Start C-Find Server.
			 */
			CFind_SCP_Standalone objCFindSCPTest = new CFind_SCP_Standalone();
			objCFindSCPTest.doCFind();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * This method Starts CFind SCP.
	 * @throws IOException Signals that an IO Exception has occurred.
	 * @throws SQLException the SQL exception
	 * @throws ClassNotFoundException the class not found exception
	 * @throws IllegalAccessException the illegal access exception
	 * @throws InstantiationException the instantiation exception
	 */
	public void doCFind() throws IOException, SQLException,ClassNotFoundException,IllegalAccessException,InstantiationException
	{
		/*
		 * Create CFindSCP.
		 */
		CFindSCP objCFindSCP = CFindSCP.createInstance();

		/*
		 * Create DataSourceImpl with ConnectionString and driver String.
		 * DataSourceImpl is default implementation of library which uses JavaDB to 
		 * Find Data Elements.The user has to start javaDB server before connection
		 * if Client Server model in java DB is used.
		 * This DataSourceImpl should be set in CFind SCP.
		 * User can also provide its own implementation of IDataSink interface
		 * to FIND the attributes.
		 */
		String dataBasePath = "C:/Database/SDKdb";
		String ConnectionString = "jdbc:derby://localhost:5050/" + dataBasePath;
		String driverString = "org.apache.derby.jdbc.ClientDriver";
		IDataSource objIDataSource = new DataSourceImpl(ConnectionString,driverString);
		IPairedCollection<EnumDataSource,IDataSource> objDataSourceList = new PairedCollection<EnumDataSource, IDataSource>();
		objDataSourceList.put(EnumDataSource.CSERVICES,objIDataSource);
		objCFindSCP.setDataSourceList(objDataSourceList);

		/*
		 * Start C-Find SCP.
		 */
		objCFindSCP.start("SDK",9000);
	}
}
