/*******************************************************************************
 *  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.enums.EnumSOPClassUIDs;

import cdac.medinfo.sdk.dcm30_04.services.association.DefaultAssociation;
import cdac.medinfo.sdk.dcm30_04.services.association.RQPresContext;
import cdac.medinfo.sdk.dcm30_04.services.cget.commons.CGetRSP;
import cdac.medinfo.sdk.dcm30_04.services.cget.scu.CGetSCU;
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 CGet SCU.
 * "Stand Alone" means that the User is required to write a minimal set of code to actually send a 
 * CGet Request. Once the required parameters are given to the CGetSCU object it is 
 * capable of making a connection to the SCP, establishing association, sending the CGet 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 "CGet_SCU.java". 
 */
public class CGet_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 CGet_SCU_Standalone and start CGet operation.
			 */
			CGet_SCU_Standalone objCGetSCUTest = new CGet_SCU_Standalone();
			objCGetSCUTest.doCGet();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * This function initiates C-Get SCU and sends C-Get Request.
	 * @throws IOException signals IOException if IO Operation fails.
	 */
	public void doCGet() throws IOException
	{
		/* 
		 * Creates SCU with specified destination .
		 */
		InetSocketAddress addr = new InetSocketAddress("192.168.8.94",9000);
		CGetSCU objCGetSCU = new CGetSCU(addr);

		RQPresContext objRQPresContextCGET = new RQPresContext();
		objRQPresContextCGET.setAbstractSyntaxName(EnumSOPClassUIDs.STUDYROOTQUERYGET.getValue());
		objRQPresContextCGET.addTransferSyntax(DicomConstants.TS_DEFAULT_IMPLICIT_VR_LITTLE_ENDIAN);

		//RQ Pres Context for X-Ray
		RQPresContext objRQPresContextXRay = new RQPresContext();
		objRQPresContextXRay.setAbstractSyntaxName(EnumSOPClassUIDs.XRAYANGIOGRAPHICIOD.getValue());
		objRQPresContextXRay.addTransferSyntax(DicomConstants.TS_LOSSY_JPEG_8_BIT);

		//RQ Pres Context for CT
		RQPresContext objRQPresContextCT = new RQPresContext();
		objRQPresContextCT.setAbstractSyntaxName(EnumSOPClassUIDs.CTIOD.getValue());
		objRQPresContextCT.addTransferSyntax(DicomConstants.TS_DEFAULT_IMPLICIT_VR_LITTLE_ENDIAN);

		//RQ Pres Context for NM
		RQPresContext objRQPresContextNM = new RQPresContext();
		objRQPresContextNM.setAbstractSyntaxName(EnumSOPClassUIDs.NMIOD.getValue());
		objRQPresContextNM.addTransferSyntax(DicomConstants.TS_EXPLICIT_VR_LITTLE_ENDIAN);

		//RQ Pres Context for SC
		RQPresContext objRQPresContextSC = new RQPresContext();
		objRQPresContextSC.setAbstractSyntaxName(EnumSOPClassUIDs.SCIOD.getValue());
		objRQPresContextSC.addTransferSyntax(DicomConstants.TS_DEFAULT_IMPLICIT_VR_LITTLE_ENDIAN);

		ISingleCollection<RQPresContext> objPressConColl = new SingleCollection<RQPresContext>();
		objPressConColl.add(objRQPresContextXRay);
		objPressConColl.add(objRQPresContextCT);
		objPressConColl.add(objRQPresContextNM);
		objPressConColl.add(objRQPresContextSC);
		objPressConColl.add(objRQPresContextCGET);

		/*
		 * Load association using RQ Presentation Context Collection.
		 */
		DefaultAssociation objDefaultAssociation = new DefaultAssociation("SDK");
		objDefaultAssociation.loadAssociation(objPressConColl);

		/*
		 * Send the C-Get DataSet with Affected SOP class UID 
		 */
		String strAffectedSOPClassUID = DicomConstants.DCM_STUDY_ROOT_QUERY_OR_RETRIVE_INFORMATION_MODEL_GET;
		objCGetSCU.setDestinationPath("C:\\GetFiles");
		objCGetSCU.send(strAffectedSOPClassUID,objDefaultAssociation.getAssociationRQ("SDK"),getCGetDatasets());

		/*
		 * After the request is send user can get the response collection.
		 */
		System.out.println("C-Get operation is completed. The response size Collection is ==== " + objCGetSCU.getDIMSEResponse().size());
		for(DicomMessage objDicomMessage : objCGetSCU.getDIMSEResponse())
		{
			CGetRSP objCGetRSP = new CGetRSP(objDicomMessage);
			System.out.println("C-Get Response Status :" + objCGetRSP.getStatus());
		}
	}

	/**
	 * This function retrieves the data set collection to be sent in C-Get request. 
	 * The data set contains the identifier.
	 * @return ISingleCollection<IDataSet> Data set collection which is to be sent in C-Get request.
	 * @throws IOException signals IOException if IO Operation fails.
	 */
	ISingleCollection<IDataSet> getCGetDatasets() throws IOException
	{
		ISingleCollection<IDataSet> objDataSetCollection = new SingleCollection<IDataSet>();
		IDataSet objIDataSet = new DataSet();
		IDictionaryReader objIDictionaryReader = objIDataSet.getDictionaryReader();

		//Querying at patient level for a specific Patient-ID 
		IDataElement objIDataElement = new ImplicitDE(0x0010,0x0020,"322-292-73-6",objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objDataSetCollection.add(objIDataSet);
		return objDataSetCollection;
	}
}