/*******************************************************************************
 *  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.dicomnetcommons.EnumDataSink;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumDataSource;
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.dimsecommons.IDataSource;
import cdac.medinfo.sdk.dcm30_04.services.javadbhelper.DataSinkImpl;
import cdac.medinfo.sdk.dcm30_04.services.javadbhelper.DataSourceImpl;
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 master test case for SCP which works as SCP for All C-Services.
 * This test case extends ProcessHandler class which is default implementation 
 * of SDK to process all kinds of Association and Service Request.
 * Once the required parameters are given to the ProcessHandler and is started , 
 * it is capable of process all Association and service requests and sending their response
 * To create DB for C-Services refer SQL Script DICOMServerDB.sql provided in install.  
 */
public class CServices_SCP_WithDB_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 CServices_SCP.
			 */
			CServices_SCP_Handler objCServices_SCP = new CServices_SCP_Handler();

			/*
			 * Create IDataSink and IDataSource with ConnectionString and driver String.
			 * DataSinkImpl and DataSourceImpl is default implementation of library which uses JavaDB to 
			 * store and find Data Elements.The user has to start javaDB server before connection
			 * if Client Server model in java DB is used.
			 * This IDataSource should be set in Process Handler.
			 * User can also provide its own implementation of IDataSink interface
			 * to find the attributes.
			 */
			String dataBasePath = "D:/Database/SDKdb";
			String ConnectionString = "jdbc:derby://localhost:5050/" + dataBasePath;
			String driverString = "org.apache.derby.jdbc.ClientDriver";
			IDataSink objIDataSink = new DataSinkImpl(ConnectionString,driverString);

			/*
			 * add data sink to Process handler.
			 */
			objCServices_SCP.addDataSink(EnumDataSink.CSERVICES, objIDataSink);

			/*
			 * Add DataSource to Process Handler
			 */
			IDataSource objIDataSource = new DataSourceImpl(ConnectionString,driverString);
			objCServices_SCP.addDataSource(EnumDataSource.CSERVICES, objIDataSource);


			/*
			 * For C-Move add Third party AE Titles in the Process Handler which will act as
			 * C-Store SCP for C-Move sub-operation.
			 */
			objCServices_SCP.addSupportedAETitles("SCUTUM", "192.168.8.101", 9000);
			objCServices_SCP.addSupportedAETitles("DCMRCV", "192.168.8.94", 9002);
			objCServices_SCP.start();

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


/**
 * This is master test case for SCP which works as SCP for All Services.
 * This test case extends ProcessHandler class which is default implementation 
 * of SDK to process all kinds of Association and Service Request.
 * Once the required parameters are given to the ProcessHandler and is started , 
 * it is capable of process all Association and service requests and sending their response 
 */
class CServices_SCP_Handler extends ProcessHandler
{

	/**
	 * Starts SCP server at iPort and local host.
	 * @throws IOException Signals IOException if IO operation fails.
	 * @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 start() throws IOException,SQLException,ClassNotFoundException,IllegalAccessException,InstantiationException
	{
		/*
		 * Create SCP session.
		 */
		SCPSession objSCPSession = new SCPSession();

		/*
		 * Set Process handler.
		 */
		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);

		/*
		 * Initializes the 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[5];
		arrenumServiceType[0] = EnumServiceType.CECHO;
		arrenumServiceType[1] = EnumServiceType.CSTORE_DEFAULT;
		arrenumServiceType[2] = EnumServiceType.CFIND_DEFAULT;
		arrenumServiceType[3] = EnumServiceType.CMOVE;
		arrenumServiceType[4] = EnumServiceType.CGET;
		objSCPSession.selectServices(arrenumServiceType);

		/*
		 * Start the server.
		 */
		objSCPSession.start("SDK",9000);
	} 
}
