/*******************************************************************************
 *  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.cstore.scp.CStoreSCP;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumDataSink;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.IDataSink;
import cdac.medinfo.sdk.dcm30_04.services.javadbhelper.DataSinkImpl;
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 CStore SCP.
 * "Stand Alone" means that the User is required to write a minimal set of code to create C-Store SCP
 * Once the required parameters are given to the CStoreSCP object and is started , it is 
 * capable of accepting connection, negotiation association and creating appropriate response(AssociateAC or Reject)
 * processing CStore 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 "CStore_SCP.java". 
 * To create DB for C-Services refer SQL Script DICOMServerDB.sql provided in install. 
 */
public class CStore_SCP_Standalone
{
	public static void main(String[] args)
	{
		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 CStore_SCP_Standalone and Start C-Store Server.
			 */
			CStore_SCP_Standalone objCStoreSCPTest = new CStore_SCP_Standalone();

			objCStoreSCPTest.doCStore();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * This function starts C-Store SCP.
	 * @throws IOException Signals if IO Exception occurs.
	 * @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 doCStore() throws IOException, SQLException,ClassNotFoundException,IllegalAccessException,InstantiationException
	{

		/*
		 * Create CStore SCP.
		 */
		CStoreSCP objCStoreSCP = CStoreSCP.createInstance();

		/*
		 * Create DataSinkImpl with ConnectionString and driver String.
		 * DataSinkImpl is default implementation of library which uses JavaDB to 
		 * Store Data Elements.The user has to start javaDB server before connection
		 * if Client Server model in java DB is used.
		 * The SQL Script needed to create database is provided at %InstallDir%/SQLScripts/DICOMServerDB.sql
		 * User has to first create the Database then it should be set in the C-Store SCP.
		 * This DataSink should be set in CStore SCP.
		 * User can also provide its own implementation of IDataSink interface
		 * to store the attributes.
		 */
		String dataBasePath = "C:/Database/SDKdb";
		String ConnectionString = "jdbc:derby://localhost:5050/" + dataBasePath;
		String driverString = "org.apache.derby.jdbc.ClientDriver";
		IDataSink objIDataSink = new DataSinkImpl(ConnectionString,driverString);
		IPairedCollection<EnumDataSink, IDataSink> objColl = new PairedCollection<EnumDataSink, IDataSink>();
		objColl.put(EnumDataSink.CSERVICES, objIDataSink);
		objCStoreSCP.setDataSinkList(objColl);

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