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

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.mri.EnhancedMRImageIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.mri.MRIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.nm.NMIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.us.USIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.us.USMultiFrameIOD;
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 is implemented to test the Instance and Frame Level Retrieve SOP Classes's C-Store SCU.
 * In order to run the following class , please refer to this link($Link) to create the environment.
 * To implement an SCU of a particular we need to do the following things:
 * Create the SCUSession object which maintains the current session of the SCU. Its "start" method accepts
 * the SCP's IP Address and Port and tries to establish a connection with the SCP.
 * 
 * Next step is to establish association .Some ways to create an Association Request: 
 * 1)Use Default Association class to Load the association for requested service. User can load the association 
 * by specifying the Service Type in loadAssociation method of Default association. For e.g. here EnumServiceType.CSTORE
 * should be specified to load Association for C-Store Services. The association request for C-Store 
 * is made according to  jars in the class path.
 * 2) Manually create object of AssociationRQ class and set all presentation contexts collection on it. 
 * More details provided in the API DOCS of AssociationRQ class.
 * 
 * After successful association is established the SCUSession stores the status of the Association with it.  
 * Create CStoreSCU object to send the C-Store Request to the respective SCP. Currently the "sendMessage" method 
 * of the CStoreSCU would wait after sending the C-Store-RQ until a suitable DIMSE response is received. 
 * This operation is part of Synchronous mode request handling.  
 * Once the DIMSE response is received by the CStoreSCU it stores it into its collection
 * which then can be retrieved.
 * 
 * After this, create a A-Release-RQ in order to request for association release and send it using the
 * SCUSession object. This again is a blocking call and the SCUSession waits until a A-Release-RP is received.
 * 
 * At the end of an operation calling "close" on SCUSession would disconnect the connection made with the 
 * SCP and release all the acquired resources.
 * Instead of closing the session after a C-Store operation is complete,we can create another association
 * and send more requests. 
 * Also it is possible to create multiple C-Store-RQs and send them one by one on the same Association.
 */
public class IFLR_SCU 
{

	/**
	 * 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 Multi-Frame DICOM File with full path
			String strFilePath = null;
			System.out.println("Enter the  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);
			//EnhancedMRImageIOD objMRIOD= new EnhancedMRImageIOD(strFilePath);	
			//USMultiFrameIOD objUSMultiFrameIOD=new USMultiFrameIOD(strFilePath);

			MRIOD objMRIOD=new MRIOD(strFilePath);		

			IDataSet objIDataSet = objMRIOD.getDataSet();

			InetSocketAddress addr = new InetSocketAddress("localhost",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.
			 */
			IFLR_SCU objIFLR_SCU = new IFLR_SCU();
			objIFLR_SCU.doCStore();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
