/*******************************************************************************
 *  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.ORMS.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.keratometry.KeratometryMeasurementsIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.ophthalmic.AutorefractionMeasurementsIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.ophthalmic.LensometryMeasurementsIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.subjectiverefraction.SubjectiveRefractionMeasurementsIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.visualacuity.VisualAcuityMeasurementsIOD;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;
import cdac.medinfo.sdk.dcm30_04.services.orms.scu.ORMS_SCU;
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 ORMS 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 ORMSSCU object it is 
 * capable of making a connection to the SCP, establishing association, sending the ORMS 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 "ORMS_SCU.java". 
 */
public class ORMS_SCU_Standalone
{

	/**
	 * Creates ORMS SCU by specifying address and port of the destination ORMS SCP.
	 * Sends the available dataset to SCP.
	 * @throws IOException Signals IOException if IO operation fails.
	 */
	public void doORMS() throws IOException
	{
		try
		{
			//	Enter the  DICOM File with full path
			String strFilePath = null;
			System.out.println("Enter the ORMS 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.
	
			KeratometryMeasurementsIOD objKeratometryMeasurementsIOD= new KeratometryMeasurementsIOD(strFilePath);
			IDataSet objIDataSet = objKeratometryMeasurementsIOD.getDataSet();
			
			
			//LensometryMeasurementsIOD objLensometryMeasurementsIOD=new LensometryMeasurementsIOD(strFilePath);
			//IDataSet objIDataSet = objLensometryMeasurementsIOD.getDataSet();
			
			//SubjectiveRefractionMeasurementsIOD objSubjectiveRefractionMeasurementsIOD=new SubjectiveRefractionMeasurementsIOD(strFilePath);
			//IDataSet objIDataSet = objSubjectiveRefractionMeasurementsIOD.getDataSet();
			
			//VisualAcuityMeasurementsIOD objVisualAcuityMeasurementsIOD=new VisualAcuityMeasurementsIOD(strFilePath);
			//IDataSet objIDataSet = objVisualAcuityMeasurementsIOD.getDataSet();
			
			//AutorefractionMeasurementsIOD objAutorefractionMeasurementsIOD=new AutorefractionMeasurementsIOD(strFilePath);
			//IDataSet objIDataSet = objAutorefractionMeasurementsIOD.getDataSet();
			

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

			/* 
			 * Creates SCU with specified destination .
			 */
			ORMS_SCU objORMSSCU = new ORMS_SCU(addr);

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

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

			String strSOPClassUID=objIDataSet.getSOPClassUID();
			/*
			 * Send the C-Find DataSet with Affected SOP class UID of ORMS
			 */
			objORMSSCU.send("SDK","SDK",strSOPClassUID, objIDataSetCollection);

			/*
			 * After the request is send user can get the response collection.
			 */

			ISingleCollection<DicomMessage> objSingleCollection = objORMSSCU.getDIMSEResponse();
			System.out.println("ORMS operation Response Collection Size :: " + objSingleCollection.size());
		}
		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.
			 */
			ORMS_SCU_Standalone objORMSSCUTest = new ORMS_SCU_Standalone();
			objORMSSCUTest.doORMS();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
