package cdac.medinfo.sdk.hl7282.hl7model;


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

import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Parser;
import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Serializer;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentSet;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientadministration.ADTRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientadministration.ADT_A01;
import cdac.medinfo.sdk.hl7282.utils.EnumMessageCode;
import cdac.medinfo.sdk.hl7282.utils.EnumTriggerEvent;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
import cdac.medinfo.sdk.hl7282.utils.HL7Constants;
/**
 * This class tests  parsing & Serialization of ADT_A01 message file.
 */
public class ADT_A01_FileTest 
{
	public static void main(String[] args) 
	{
		//Specifying logging directory.
		//User can specify any directory where log files will be created.
		//If no path is specified then logs will be generated in user's temp directory.
		HL7Config.createInstance().enableLogging("E:\\HL7Model_Logs");
		
		ADT_A01_FileTest objTestADTA01 = new ADT_A01_FileTest();
		objTestADTA01.readHL7File();
	}

	private void readHL7File()
	{
		ADT_A01 objADT_A01 = (ADT_A01)new ADTRecepient().createMessage(EnumMessageCode.ADT, EnumTriggerEvent.A01);

		try 
		{
			System.out.println("Enter ADT_A01 HL7 file to parse:   ");
			
			InputStreamReader input = new InputStreamReader(System.in);
			BufferedReader reader = new BufferedReader(input);
			
			String strHL7File = reader.readLine();
			
			/* 
			 * The HL7Parser parses the HL7Message and provides a SegmentSet.
			 * SegmentSet contains all segments of a HL7 Message in the same order as they appeared.
			 */
			HL7Parser objHL7Parser = new HL7Parser();
			ISegmentSet objSegmentSet = objHL7Parser.parse(strHL7File);
			if(objADT_A01.populate(objSegmentSet) == HL7Constants.HL7_FUNCTION_SUCCESS)
			{
				System.out.print("Message parsed successfully\n");
			}

			System.out.println("Enter file name to serialize ADT_A01:   ");
			
			strHL7File = reader.readLine();
			
			objSegmentSet = objADT_A01.getSegmentSet();
			HL7Serializer objHL7Serializer = new HL7Serializer();
			objHL7Serializer.serialize(objSegmentSet,strHL7File);
			System.out.print("Message serialized successfully\n");		
		}		 
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
}
