/*******************************************************************************
 *  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.ianservice.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.ian.iansinkimpl.IANDataSinkImpl;
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 sample code defines the default implementation of SCP for Instance Availability Notification Service Class.
 * This code process the request for service belonging to IAN service group.
 * Supported service for IAN is N-Create.
 * This sample code,working as SCP, extends from ProcessHandler.
 * It uses default implementation provided for connection establishment,
 * association establishment and services processing.
 * To create DB for IAN refer SQL Script NServiceDB.sql provided in install. 
 */
public class IAN_Service_SCP_Def_ProcHandler 
{
	public static void main(String args[])
	{
		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 objIAN_SCP.
		 */
		IANService_SCP objIAN_SCP = new IANService_SCP();

		/*
		 * Start the IAN SCP.
		 */
		objIAN_SCP.start();
	}
}

/**
 * This class inherits all the lifecycle methods involved in a communication with client.
 * This class is extened 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 serive 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 IANService_SCP extends ProcessHandler
{
	/**
	 * Starts IAN SCP at iPort and local host.
	 * @throws IOException Signals IOException if IO operation fails.
	 */
	public void start()
	{
		try
		{
			/*
			 * Create SCP session.
			 */
			SCPSession objSCPSession = new SCPSession();

			/*
			 * Create Sink Impl with ConnectionString and driver String.
			 * IANDataSinkImpl 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/NServiceDB.sql
			 * User has to first create the Database then it should be set in the IAN SCP.
			 * This DataSink should be set on process handler class working as IAN SCP.
			 * User can also provide its own implementation of IDataSink interface for IAN.
			 */
			String dataBasePath = "D:/DataBase/NServiceSDKdb";
			String ConnectionString = "jdbc:derby://localhost:5050/" + dataBasePath;
			String driverString = "org.apache.derby.jdbc.ClientDriver";

			IDataSink objIANDataSink = new IANDataSinkImpl(ConnectionString,driverString);

			/*
			 * Add Datasink on Process Handler.
			 * Here IANServices_SCP is acting as the Process handler,hence adding Data Sink and Source to Process Handler.
			 */
			addDataSink(EnumDataSink.IAN,objIANDataSink);

			/*
			 * 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.NCREATE_IAN;
			objSCPSession.selectServices(arrenumServiceType);

			//Set IANServices_SCP as ProcessHandler.
			objSCPSession.setIProcessHandler(this);
			
			/*
			 * Setting time out as 5000 milli second. It will wait 5000 mili seconds for read and write operation.
			 * If in the time specified (5000 ms) read or write operation is not performed then 
			 * session is time out.
			 */ 
		//	objSCPSession.setTimeOut(5000);

			//Start the Session for IAN SCP
			objSCPSession.start("SDK",9000);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	} 
}


