/*******************************************************************************
 *  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 cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

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


import cdac.medinfo.sdk.dcm30_04.baselibs.DataSet;
import cdac.medinfo.sdk.dcm30_04.baselibs.ImplicitDE;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;

import cdac.medinfo.sdk.dcm30_04.services.cmove.commons.CMoveRSP;
import cdac.medinfo.sdk.dcm30_04.services.cmove.scu.CMoveSCU;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DicomMessage;
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 CMove SCU.
 * "Stand Alone" means that the User is required to write a minimal set of code to actually send a 
 * CMove Request. Once the required parameters are given to the CMoveSCU object it is 
 * capable of making a connection to the SCP, establishing association, sending the CMove Request and receiveing 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 "TestCMoveSCU.java". 
 */
public class CMove_SCU_Standalone 
{
	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 object of CMove_SCU_Standalone and start CMove operation.
			 */
			CMove_SCU_Standalone objCMoveSCUTest = new CMove_SCU_Standalone();
			objCMoveSCUTest.doCMove();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * This function initiates C-Move SCU and sends C-Move Request.
	 * @throws IOException signals IOException if IO Operation fails.
	 */
	public void doCMove() throws IOException
	{
		InetSocketAddress addr = new InetSocketAddress("192.168.8.94",9000);

		/* 
		 * Creates SCU with specified destination .
		 */
		CMoveSCU objCMoveSCU = new CMoveSCU(addr);

		/*
		 * Send the C-Move DataSet with Affected SOP class UID and Move Destination(C-Store Server) 
		 */
		String strAffectedSOPClassUID = DicomConstants.DCM_STUDY_ROOT_QUERY_OR_RETRIVE_INFORMATION_MODEL_MOVE;
		String strMoveDestination = "DCMRCV";
		objCMoveSCU.send("SDK","SDK",strAffectedSOPClassUID,strMoveDestination,getCMoveDatasets());

		System.out.println("C-Move response is received and Response Collection Size ==== " + objCMoveSCU.getDIMSEResponse().size());
		
		for(DicomMessage objDicomMessage : objCMoveSCU.getDIMSEResponse())
		{
			CMoveRSP objCMoveRSP = new CMoveRSP(objDicomMessage);
			System.out.println("C-Move Response Status is :" + objCMoveRSP.getStatus());
		}
	}

	/**
	 * This function retrieves the data set collection to be sent in C-Move request. 
	 * The data set contains the identifier.
	 * @return ISingleCollection<IDataSet> Data set collection which is to be sent in C-Move request.
	 * @throws IOException signals IOException if IO Operation fails.
	 */
	ISingleCollection<IDataSet> getCMoveDatasets() throws IOException
	{
		ISingleCollection<IDataSet> objDataSetCollection = new SingleCollection<IDataSet>();
		IDataSet objIDataSet = new DataSet();
		IDictionaryReader objIDictionaryReader = objIDataSet.getDictionaryReader();
		IDataElement objIDataElement = new ImplicitDE(0x0020,0x000d,"1.3.12.2.1107.5.4.3.4975316777216.19951114.94101.16",objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objDataSetCollection.add(objIDataSet);
		return objDataSetCollection;
	}
}
