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

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


import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.dicomparserserializers.DicomParser;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;

import cdac.medinfo.sdk.dcm30_04.services.dicomdir.DICOMDIR;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;

/**
 * This class is implemented to test data set storage in DICOMDIR.
 * This test code takes the path of DICOM File and the path where to store the file from the user. If the DICOMDIR is not created at the storage location 
 * then create DICOMDIR at that location, add the data set in the DICOMDIR and serialize DataSet with name of SOP Instance UID.
 * Otherwise add the DataSet to the existing DICOMDIR and serialize Data Set with name of SOP Instance UID. 
 */
public class DICOMDIR_ReadWrite 
{
	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'.
			objDicomConfig.enableLogging(EnumLoggingLevel.INFO, "C:\\DCM_Logs");

			
			
			

			

			DICOMDIR_ReadWrite objTestDICOMDIRWrite = new DICOMDIR_ReadWrite();
			objTestDICOMDIRWrite.addDataSetToDICOMDIR();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * This function parses the DICOM File and  get the data set out of it.
	 * Add this data set to DICOMDIR and serializes the data set.
	 * @throws IOException throws IOException if IO Operation fails.
	 */
	public void addDataSetToDICOMDIR() throws IOException
	{
		/*
		 * Enter the path of DICOM File which is to be added in DICOMDIR. 
		 */
		System.out.println("Enter File to be added in DICOMDIR ");
		BufferedReader b_reader =new BufferedReader( new InputStreamReader( System.in ) );
		String strPath = b_reader.readLine();

		/*
		 * Parse the Dicom File and get the DataSet.
		 */
		DicomParser objDicomParser = new DicomParser();
		IDataSet objIDataSet = objDicomParser.parseAsPart10File(strPath, true);

		/*
		 * Enter the path where to store the DICOM File.
		 */
		System.out.println("Enter Path where to Store the File ");
		b_reader =new BufferedReader( new InputStreamReader( System.in ) );
		strPath = b_reader.readLine();

		/*
		 * Store the DataSet in DICOM DIR as well as serialize it by using store , method of DICOMDIRImpl.
		 */
		DICOMDIR objDICOMDIR = new DICOMDIR(strPath);
		objDICOMDIR.write(objIDataSet);
		System.out.println("completed");

	}
}
