package cdac.medinfo.sdk.hl7282.hl7net.localextension;



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

import cdac.medinfo.sdk.hl7282.baselibs.messages.LocalExtensionAgent;
import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Parser;
import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Serializer;
import cdac.medinfo.sdk.hl7282.baselibs.segments.EVN;
import cdac.medinfo.sdk.hl7282.baselibs.segments.MSH;
import cdac.medinfo.sdk.hl7282.baselibs.segments.PID;
import cdac.medinfo.sdk.hl7282.baselibs.segments.PV1;
import cdac.medinfo.sdk.hl7282.baselibs.segments.SegmentSet;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentSet;
import cdac.medinfo.sdk.hl7282.hl7net.testnetwork.localmessages.ZL7;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;

/**
 * This class gives demonstration for the parsing and Serialization of Locally extended Message.
 *
 */
public class Parse_Serialize_LocalExtension 
{
	public static void main(String[] args) 
	{
		try
		{
			
			/*
			 * 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("C:\\LocalExtensionLogs");

			//Instantiating messageTest
			MessageTest objMessageTest = new MessageTest();
			
			//Instantiating populateTest for populating segments.
			Populate_Test objPopulateTest = new Populate_Test();

			/*Instantiating Segment Set*/
			ISegmentSet objSegmentSet = new SegmentSet();

			/*Get MSH Segment and set Message Type for Message.*/
			MSH objHeader = objPopulateTest.populateMSH();
			objHeader.setMessageType("ADT","A99","ADT_A99");
			
			//Add Segment in Segment set
			objSegmentSet.addSegment(objHeader);

			/*Populate EVN Segment and add in Segment Set*/
			EVN objEVN = objPopulateTest.populateEVN();
			objSegmentSet.addSegment(objEVN);
			
			/*Populate PID Segment and add in Segment Set*/
			PID objPID = objPopulateTest.populatePID();
			objSegmentSet.addSegment(objPID);
			
			/*Populate ZL7 Custom Segment and add in Segment Set*/
			ZL7 objZL7 = objPopulateTest.populateZL7();
			objSegmentSet.addSegment(objZL7);
			
			/*Populate PID Segment and add in Segment Set*/
			PV1 objPV1 = objPopulateTest.populatePV1();
			objSegmentSet.addSegment(objPV1);

			System.out.println("Enter ADT_A99 HL7 file to serialize:   ");
			
			InputStreamReader input = new InputStreamReader(System.in);
			BufferedReader reader = new BufferedReader(input);
			
			String strHL7File = reader.readLine();
			
			/*Initialize HL7Serealizer which Serializes the Segment Set at given path*/
			HL7Serializer objHL7Serializer = new HL7Serializer();
			objHL7Serializer.serialize(objSegmentSet,strHL7File);
			
			System.out.println("Segment Set is Serialized");
			
			/*Instantiating Local Extension Agent*/
			LocalExtensionAgent objLocalExtensionAgent = new LocalExtensionAgent();
			
			/*
			 * Registering ZL7 Custom Segment with local extension.
			 * By giving Segment name and Qualified name(Package name)    
			 */
			objLocalExtensionAgent.registerSegment("ZL7","cdac.medinfo.sdk.hl7282.hl7net.testnetwork.localmessages.ZL7");
			
			/*Initialize HL7Parser which Parses File*/
			HL7Parser objHL7Parser = new HL7Parser();
			
			/* 
			 * Sets the local extension Agent on Parser to
			 * understand locally extended  message and segments 
			 */
			objHL7Parser.setLocalExtensionAgent(objLocalExtensionAgent);
			
			//providing file path where Message is to be parsed.
			ISegmentSet objTempSegmentSet = objHL7Parser.parse(strHL7File);
			System.out.println("Message parsed Successfully");
			
			System.out.println("Enter file name to serialize ADT_A99:   ");
			
			strHL7File = reader.readLine();
			
			/*Serializes the Segment Set at specified location*/
			objHL7Serializer.serialize(objTempSegmentSet,strHL7File);
			System.out.println("serialized SegmentSet Successfully");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
