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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;


import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.dicommodel.xray.XRayAngiographicIOD;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;

import cdac.medinfo.sdk.dcm30_04.services.cstore.commons.CStoreRSP;
import cdac.medinfo.sdk.dcm30_04.services.cstore.scu.CStoreSCU;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

/**
 * This class tests a Stand Alone CStore SCU.
 * "Stand Alone" means that the User is required to write a minimal set of code to actually send a 
 * CStore Request. Once the required parameters are given to the CStoreSCU object it is 
 * capable of making a connection to the SCP, establishing association, sending the CStore Request and receiving the 
 * DIMSE Response for the same. If User wants to handle the request sending scenarios by his/her code 
 * then please refer to sample code "CStore_SCU.java". 
 */
public class CStore_SCU_Standalone
{
	/**
	 * Creates CSTORE SCU by specifying address and port of the destination CStore SCP.
	 * Sends the available dataset to SCP.
	 * @throws IOException Signals IOException if IO operation fails.
	 */
	public void doCStore() throws IOException
	{
		try
		{
			//Enter the X-Ray Angiogram DICOM File with full path
			String strFilePath = null;
			System.out.println("Enter the X-Ray Angiogram Dicom File with complete path");
			BufferedReader b_reader =new BufferedReader( new InputStreamReader( System.in ) );
			strFilePath = b_reader.readLine();

			// Creates the object of corresponding IOD by calling its constructor which takes File Name as a parameter and give its DataSet.
			XRayAngiographicIOD objXRayAngiographicIOD = new XRayAngiographicIOD(strFilePath);
			IDataSet objIDataSet = objXRayAngiographicIOD.getDataSet();

			InetSocketAddress addr = new InetSocketAddress("192.168.8.94",9000);

			/* 
			 * Creates SCU with specified destination .
			 */
			CStoreSCU objCStoreSCU = new CStoreSCU(addr);

			/*
			 * Create Data Set collection.
			 */
			ISingleCollection<IDataSet> objIDataSetCollection = new SingleCollection<IDataSet>();

			/*
			 * sends the available datasets to the destination SCP for storage.
			 */
			objIDataSetCollection.add(objIDataSet);

			/*
			 * sends the available datasets to the destination SCP for storage.
			 */
			objCStoreSCU.send(objIDataSetCollection,"SDK","SDK");

			System.out.println("CStore operation is performed and Response size collection is "+ objCStoreSCU.getDIMSEResponse().size());

			if(objCStoreSCU.getDIMSEResponse().size() >0)
			{
				CStoreRSP objCStoreRSP = new CStoreRSP(objCStoreSCU.getDIMSEResponse().get(0));
				System.out.println("C-Store Response Status is :"+objCStoreRSP.getStatus());
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * The main method.
	 * @param args the arguments
	 * @throws IOException Signals if IO Exception occurs.
	 */
	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_SCU_Standalone.
			 */
			CStore_SCU_Standalone objCStoreSCUTest = new CStore_SCU_Standalone();
			objCStoreSCUTest.doCStore();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}