/*******************************************************************************
 *  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.HangingProtocol.testcases.defaultscp;

import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumDataSink;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumServiceType;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.IDataSink;
import cdac.medinfo.sdk.dcm30_04.services.hangingprotocol.sinkimpl.HangingProtocolDataSinkImpl;
import cdac.medinfo.sdk.dcm30_04.services.scp.commons.ProcessHandler;
import cdac.medinfo.sdk.dcm30_04.services.scp.commons.SCPSession;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;


/**
 * This is the default implementation of the sample test code for Hanging Protocol Services.
 * This sample codes is to show the handling of connection, association and service request 
 * by the default Process Handler Implementation of the SDK.
 * The Sample Test code should be extended from the ProcessHandler class of SDK for DICOM.
 * Then all the connection , association and service requests are handled by the default implementation
 * of the SDK.
 * To create DB for HangingProtocol refer SQL Script HangingProtocolServerDB.sql provided in install.
 */
public class HangingProtocol_CStore_SCP_Def_ProcHandler {

	
	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 HangingProtocolService_SCP and Start C-Store Server.
			 */
			HangingProtocolService_SCP objHangingProtocolServiceSCP = new HangingProtocolService_SCP();
			objHangingProtocolServiceSCP.start();
			
			/*
			 * Create SCP session.
			 */
			SCPSession objSCPSession = new SCPSession();
			/*
			 * Initializes the supporting service types and sets it on SCP Session
			 * EnumService Type configures SCP Session to load corresponding association context and start
			 * corresponding DIMSE Services.
			 */
			EnumServiceType[] arrenumServiceType = new EnumServiceType[1];
			arrenumServiceType[0] = EnumServiceType.CSTORE_HANGINGPROTOCOL;
			objSCPSession.selectServices(arrenumServiceType);

			//Set HangingProtocolServices_SCP as ProcessHandler.
			objSCPSession.setIProcessHandler(objHangingProtocolServiceSCP);
			
			//Start the Session for Hanging Protocol SCP
			objSCPSession.start("SDK",9000);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
/**
 * This class inherits all the life cycle methods involved in a communication with client.
 * This class is extended from the ProcessHandler class of the SDK for DICOM.
 * ProcessHandler class is the default processor of SDK for DICOM which is capable
 * of handling connection , association and service requests. 
 * It process the request ,generates response and send it back to SCU.
 * Once the required parameters are set on ProcessHandler class of SDK for DICOM
 * then it will handle all the Association and Service requests.
 */
class HangingProtocolService_SCP extends ProcessHandler
{
	/**
	 * Starts Hanging Protocol SCP at iPort and local host.
	 * @throws IOException Signals IOException if IO operation fails.
	 */
	public void start()
	{
		try
		{

			/*
			 * Create Sink Impl with ConnectionString and driver String.
			 * HangingProtocolDataSinkImpl is default implementation of library which uses JavaDB. 
			 * Sink Impl process the storage and retrieval process for all IAN supported service. 
			 * The user has to start javaDB server before connection creation if Client Server model in java DB is used.
			 * The SQL Script needed to create database is provided at %InstallDir%/SQLScripts/HangingProtocolServerDB.sql
			 * User has to first create the Database then it should be set in the Hanging Protocol SCP.
			 * This DataSink should be set on process handler class working as Hanging Protocol SCP.
			 * User can also provide its own implementation of IDataSink interface for Hanging Protocol.
			 */
			String dataBasePath = "D:/DataBase/HangingProtocolServerDB";
			String ConnectionString = "jdbc:derby://localhost:5050/" + dataBasePath;
			String driverString = "org.apache.derby.jdbc.ClientDriver";

			/*
			 * The DataSet is serialized at the Storage Path provided on C-Store SCP.
			 * If No Storage Path is provide on C-Store SCP then File is serialized at "%temp%/CDAC-SDK/DCM-J/DicomDump"
			 * in User Temp Folder.
			 */
			setStoragePath("C:/Storage");
			
			IDataSink objHangingProtocolDataSink = new HangingProtocolDataSinkImpl(ConnectionString,driverString);
			addDataSink(EnumDataSink.HANGING_PROTOCOL,objHangingProtocolDataSink);

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