package cdac.medinfo.sdk.hl7282.HL7Editor;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import cdac.medinfo.sdk.hl7282.baselibs.messages.Message;
import cdac.medinfo.sdk.hl7282.baselibs.messages.MessageFactory;
import cdac.medinfo.sdk.hl7282.baselibs.parserserializer.HL7Parser;
import cdac.medinfo.sdk.hl7282.baselibs.segments.MSH;
import cdac.medinfo.sdk.hl7282.commoninterface.IDelimiter;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegment;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentKey;
import cdac.medinfo.sdk.hl7282.commoninterface.ISegmentSet;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.DelimiterMissingException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidDataTypeException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidDelimiterException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidMessageException;
import cdac.medinfo.sdk.hl7282.exceptions.ParseException;
import cdac.medinfo.sdk.hl7282.hl7model.messages.applicationmanagement.AppMgtRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.applicationmanagement.AppMgtSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.clinicallaboratoryautomation.ClinicalLabAutoRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.clinicallaboratoryautomation.ClinicalLabAutoSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.financialmanagement.FinancialRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.financialmanagement.FinancialSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.masterfiles.MasterFileRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.masterfiles.MasterFileSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.medicalrecords.MediRecRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.medicalrecords.MediRecSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.observationreporting.ObsvRepRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.observationreporting.ObsvRepSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.orderentry.OrderEntryRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.orderentry.OrderEntrySource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientadministration.ADTRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientadministration.ADTSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientcare.PatientCareRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientcare.PatientCareSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientreferral.PatientRefRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.patientreferral.PatientRefSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.personnelmanagement.PersonalMgmtRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.personnelmanagement.PersonalMgmtSource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.query.QryRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.query.QrySource;
import cdac.medinfo.sdk.hl7282.hl7model.messages.scheduling.SchedulingRecepient;
import cdac.medinfo.sdk.hl7282.hl7model.messages.scheduling.SchedulingSource;
import cdac.medinfo.sdk.hl7282.hl7streams.HL7BufferStream;
import cdac.medinfo.sdk.hl7282.hl7streams.HL7FileStream;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
import cdac.medinfo.sdk.hl7282.utils.HL7Constants;
import cdac.medinfo.sdk.hl7282.utils.HL7Exception;
import cdac.medinfo.sdk.hl7282.utils.HL7Logger;
import cdac.medinfo.sdk.hl7282.utils.MultiValuedPairedCollection;
import cdac.medinfo.sdk.hl7282.utils.ResourceBundleWrapper;
import cdac.medinfo.sdk.hl7282.utils.SingleCollection;

/**
 * This test code edits the HL7 file. This test code can remove the Segment or Group.
 * Also can update the Attribute of a segment in HL7 file if present or add if segment structure has that attribute.
 */
public class HL7Editor 
{
	private static HL7FileStream objIHL7Stream;

	//User define Log path
	String strLoggingPath = null;

	String isForecefullyRemove = "f";

	//User define HL7 file
	String strHL7File = null;

	//User can define its own destination folder
	//where user can dump HL7 file
	String strDestFolder = null;

	//If user wants to remove one or more segment/s then, user have to add those segment in the following collection.
	//But user have to make sure that particular segment/s is/are not the Compulsory segment of the message.
	//If segment/s is/are the Compulsory, then HL7Editor will not edited HL7 file.
	ISingleCollection<String> objRemoveSegInfoCollection = null;

	//If user wants to remove any attribute/s from a particular segment then user have to add those attribute's item id in the following collection.
	//But user have to make sure that the item id is belong to that particular segment.
	//If item id is not belonging to the particular segment, then HL7Editor will not edited HL7 file.
	ISingleCollection<String> objRemoveSegAttributeInfoCollection = null;

	//If user wants to update any attribute/s from a particular segment then user have to add those attribute's item id in the following collection.
	//But user have to make sure that the item id is belong to that particular segment.
	//If item id is not belonging to the particular segment, then HL7Editor will not edited HL7 file.
	ISingleCollection<String> objUpdateSegAttributeInfoCollection = null;

	public HL7Editor()
	{
		objRemoveSegInfoCollection = new SingleCollection<String>();		
		objRemoveSegAttributeInfoCollection = new SingleCollection<String>();
		objUpdateSegAttributeInfoCollection = new SingleCollection<String>();
	}

	public static void main(String[] args)
	{
		HL7Editor objHL7Editor = new HL7Editor();

		HL7Config objHL7Config = HL7Config.createInstance();

		//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.
		objHL7Config.enableLogging(null);

		objHL7Editor.displayMenu();

		objHL7Editor.startEdit();
	}

	private void displayMenu()
	{
		try
		{
			
				System.out.println("Enter your choice to Edit HL7 Messages:   ");
				
				System.out.println("1. User want to remove Segment of HL7 File");
				System.out.println("2. User want to remove Segment's Attribute of HL7 File");
				System.out.println("3. User want to update/add Segment's Attribute of HL7 File");

				InputStreamReader input = new InputStreamReader(System.in);
				BufferedReader reader = new BufferedReader(input);
				int intChoice = 0;
				try
				{
					intChoice = Integer.parseInt(reader.readLine());
				
					check(intChoice);
			
				}
				catch(IOException ioe)
				{
					ioe.printStackTrace();
				}
				catch (NumberFormatException nfe) 
				{
					System.out.println(nfe.getMessage());
				}
			
		}
		catch(Exception e)
		{
			System.out.println("Error Ocured == > "+e.getMessage());
			e.printStackTrace();
		}
	}

	private void check(int intChoice)
	{
		try
		{
			InputStreamReader input = new InputStreamReader(System.in);
			BufferedReader reader = new BufferedReader(input);

			
			if(intChoice == 1)
			{
				System.out.println("Enter HL7 file to Edit:   ");
				strHL7File = reader.readLine();
				
				System.out.println("Enter folder to dump HL7 file:   ");
				strDestFolder = reader.readLine();
				
				boolean bChoice = true;
				while(bChoice)
				{
					System.out.println("Enter the Segment to remove (E.g. NK1):  ");

					objRemoveSegInfoCollection.add(reader.readLine());

					System.out.println("Would you like to remove more segment(y/n):  ");
					String srtChoice = reader.readLine();
					if(srtChoice.equalsIgnoreCase("n"))
						bChoice = false;
				}
				System.out.println("Would you like to remove segment ForceFully(y/n):  ");
				String strForceFully = reader.readLine();
				if(strForceFully.equalsIgnoreCase("n"))
					isForecefullyRemove = "f";
				else
					isForecefullyRemove = "t";
			}
			else if(intChoice == 2)
			{
				System.out.println("Enter HL7 file to Edit:   ");
				strHL7File = reader.readLine();
				
				System.out.println("Enter folder to dump HL7 file:   ");
				strDestFolder = reader.readLine();
				
				boolean bChoice = true;
				while(bChoice)
				{
					System.out.println("Enter the Segment's Attribute to remove (E.g. PV1:133):  ");

					objRemoveSegAttributeInfoCollection.add(reader.readLine());

					System.out.println("Would you like to remove more attribute(y/n):  ");
					String srtChoice = reader.readLine();
					if(srtChoice.equalsIgnoreCase("n"))
						bChoice = false;
				}
				System.out.println("Would you like to remove attribute ForceFully(y/n):  ");
				String strForceFully = reader.readLine();
				if(strForceFully.equalsIgnoreCase("n"))
					isForecefullyRemove = "f";
				else
					isForecefullyRemove = "t";
			}
			else if(intChoice == 3)
			{
				System.out.println("Enter HL7 file to Edit:   ");
				strHL7File = reader.readLine();
				
				System.out.println("Enter folder to dump HL7 file:   ");
				strDestFolder = reader.readLine();
				
				boolean bChoice = true;
				while(bChoice)
				{
					System.out.println("Enter the Segment's Attribute to update (E.g. PID:106:newValue):  ");

					objUpdateSegAttributeInfoCollection.add(reader.readLine());

					System.out.println("Would you like to update more attribute(y/n):  ");
					String srtChoice = reader.readLine();
					if(srtChoice.equalsIgnoreCase("n"))
						bChoice = false;
				}
			}
			else
			{
				System.out.println("Invalid Choice....");
				displayMenu();
			}
			
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}

	private void startEdit()
	{
		if(strDestFolder == null || strDestFolder.equalsIgnoreCase(""))
		{
			strDestFolder = System.getProperty("java.io.tmpdir")+"/CDAC-SDK/HL7-J/HL7Editor/";
		}

		if(strLoggingPath == null)
		{
			strLoggingPath = System.getProperty("java.io.tmpdir")+"/CDAC-SDK/HL7-J/Logs/";
		}

		// Create Message
		Message objMessage = createMessageAndDump(strHL7File, strDestFolder);

		int iResult = HL7Constants.HL7_FUNCTION_FAIL;
		boolean bForce = false;
		if(isForecefullyRemove.equalsIgnoreCase("t"))
			bForce = true;

		MSH objMsh = objMessage.getHeader();
		IDelimiter objDelimiter = objMsh.getDelimiter();

		try
		{
			// REMOVE SEGMENT OR GROUP
			if(objRemoveSegInfoCollection.size() > 0)
			{
				iResult = removeSegments(objMessage,objRemoveSegInfoCollection, bForce);

			}
			// REMOVE SEGMENT ATTRIBUTE
			if(objRemoveSegAttributeInfoCollection.size() > 0)
			{
				iResult = removeSegmentAttribute(objMessage,objRemoveSegAttributeInfoCollection, bForce);
			}
			// UPDATE ATTRIBUTE IF PRESENT OTHERWISE ADD.
			if(objUpdateSegAttributeInfoCollection.size() > 0)
			{
				iResult = updateSegmentAttribute(objMessage, objUpdateSegAttributeInfoCollection, objDelimiter);
			}

			if(iResult == HL7Constants.HL7_FUNCTION_SUCCESS)
			{
				
				objMessage.dumpMessage(objIHL7Stream);
				System.out.println("\nHL7 message edited successfully. \n");
				System.out.println("\nDump of HL7 message is stored at " + strDestFolder + "\n");
			}
			else
			{
				objMessage.dumpMessage(objIHL7Stream);
				System.out.println("\nHL7 message not edited successfully. Please refer log at the path " + strLoggingPath + "\n");
				System.out.println("\nDump of HL7 message is stored at " + strDestFolder + "\n");
			}
		}
		catch(Exception e)
		{
			System.out.println("HL7 file edition failed........");
			e.printStackTrace();
		}
	}

	/**
	 * Removes Segment Attributes.
	 * @param objMessage Object of Message
	 * @param objRemoveSegAttributeInfoCollection Collection of segment attributes to be removed.
	 * @param force Boolean value True - for removing attribute forcefully.
	 * @return integer value of HL7Constants foe success or failure.
	 */
	private int removeSegmentAttribute(Message objMessage, ISingleCollection<String> objRemoveSegAttributeInfoCollection, boolean force)
	{
		MultiValuedPairedCollection<ISegmentKey, Integer> objSegmentKeyCollection = new MultiValuedPairedCollection<ISegmentKey, Integer>();
		int iResult = HL7Constants.HL7_FUNCTION_SUCCESS;
		try
		{
			for(String strRemoveSegAttributeInfo : objRemoveSegAttributeInfoCollection)
			{
				if(strRemoveSegAttributeInfo.contains(":"))
				{
					String[] arraySeg_ItemID = strRemoveSegAttributeInfo.split(":");
					if(arraySeg_ItemID.length!=2)
						throw new HL7Exception(ResourceBundleWrapper.getValue("exceptions", "InvalidInputsForAttributeRemoval", new Locale(HL7Config.createInstance().getLocale())));
					String strSegment = arraySeg_ItemID[0];
					String strItemID = arraySeg_ItemID[1];
					int iItemID = Integer.parseInt(strItemID);

					ISegmentKey objSegmentKey  = null;
					if(strSegment != null || strSegment != "")
					{
						String strPrefix = "HL7_SEG_";
						objSegmentKey  = EnumSegments.valueOf(strPrefix + strSegment);
						objSegmentKeyCollection.put(objSegmentKey, iItemID);
					}
				}
			}
			if(objSegmentKeyCollection != null)
			{
				iResult = objMessage.removeSegmentAttribute(objSegmentKeyCollection , force);
			}
			return iResult;
		}
		catch(IllegalArgumentException iae)
		{
			System.out.println("Invalid Segment Name........");
			System.out.println("HL7 file edition failed........");
			iae.printStackTrace();
			return HL7Constants.HL7_FUNCTION_FAIL;
		}
	}


	/**
	 * Removes segments from HL7 message
	 * @param objMessage Object of Message
	 * @param objRemoveSegInfoCollection Collection of segments to be removed.
	 * @param bForce Boolean value True - for removing attribute forcefully.
	 * @return integer value of HL7Constants foe success or failure.
	 */
	private int removeSegments(Message objMessage,ISingleCollection<String> objRemoveSegInfoCollection,boolean bForce)
	{
		ISingleCollection<ISegmentKey> objRemoveSegKeyCollection = new SingleCollection<ISegmentKey>();
		try
		{	
			for(String strRemoveSegmentName : objRemoveSegInfoCollection)
			{
				if(strRemoveSegmentName != null && (strRemoveSegmentName.equalsIgnoreCase("MSH") || strRemoveSegmentName.equalsIgnoreCase("FHS") || strRemoveSegmentName.equalsIgnoreCase("BHS")))
				{
					System.out.println("HL7 Header can not be remove from the HL7 file");
				}
				else if(strRemoveSegmentName != null || strRemoveSegmentName != "")
					{
					String strPrefix = null;
					if(strRemoveSegmentName.length() == 3) // if Segment
						strPrefix = "HL7_SEG_";
					else								// if Group
						strPrefix = "HL7_GROUP_";

					ISegmentKey objSegmentKey  = EnumSegments.valueOf(strPrefix + strRemoveSegmentName);
					objRemoveSegKeyCollection.add(objSegmentKey);
				}
			}
			int iResult = removeSegmentOrGroup(objMessage,objRemoveSegKeyCollection,bForce);
			return iResult;
		}
		catch(IllegalArgumentException iae)
		{
			System.out.println("Invalid Segment Name........");
			System.out.println("HL7 file edition failed........");
			iae.printStackTrace();
			return HL7Constants.HL7_FUNCTION_FAIL;
		}
	}

	/**
	 * Populate message from given HL7 file.
	 * @param strHL7File String value for absolute path of HL7 file.
	 * @param strDestinationPath String value for Destination folder to which HL7 file dump in the form of MS doc will be stored.
	 * @return Object of Message.
	 */
	private Message createMessageAndDump(String strHL7File, String strDestinationPath)
	{
		Message objMessage = null;
		try
		{
			HL7Parser objParser = new HL7Parser();
			ISegmentSet objSegmentSet = objParser.parse(strHL7File);

			ISegment objSegment = objSegmentSet.getSegment(0);
			String strSegment = objSegment.getName().getValue();
			strSegment = strSegment.trim();

			if(strSegment.equals(EnumSegments.HL7_SEG_MSH.getValue()))
			{
				
				//Retrieve TriggerEvent and MessageCode
				MSH objMSH = (MSH)objSegment; 
				String strTriggerEvent = (String)objMSH.getMessageType().getTriggerEvent().getValue();
				String strMessageCode = (String)objMSH.getMessageType().getMessageCode().getValue();

				objMessage = getMessage(strMessageCode, strTriggerEvent, objSegmentSet);

				getFileForDump(strTriggerEvent, strMessageCode, strDestinationPath, "Original");

				//dump original Message
				objMessage.dumpMessage(objIHL7Stream);

				getFileForDump(strTriggerEvent, strMessageCode, strDestinationPath, null);
			}
			else
			{
				System.out.println("\nOperation supported for HL7 messages only. ");
			}
		}
		catch(ParseException pe)
		{

			if(HL7Config.createInstance().isLoggingOn())
			{
				HL7Logger.getLogger().log(pe);
			}
			pe.printStackTrace();
		}
		catch (InvalidMessageException e) 
		{
			if(HL7Config.createInstance().isLoggingOn())
			{
				HL7Logger.getLogger().log(e);
			}
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			if(HL7Config.createInstance().isLoggingOn())
			{
				HL7Logger.getLogger().log(e);
			}
			e.printStackTrace();
		}
		return objMessage;
	}

	/**
	 * Creates the MS document fo HL7 file.
	 * @param strHL7File HL7 File path.
	 * @param strDestinationPath  Destination folder to store created document.
	 */
	private int removeSegmentOrGroup(Message objMessage, ISingleCollection<ISegmentKey> objRemoveSegKeyCollection, boolean isForecefullyRemove)
	{
		int iResult = HL7Constants.HL7_FUNCTION_FAIL;
		int cnt = 0;
		//Call Remove
		if(objMessage != null)
		{
			if(objRemoveSegKeyCollection != null && objRemoveSegKeyCollection.size() >0)
			{
				for(ISegmentKey key : objRemoveSegKeyCollection)
				{
					iResult = objMessage.removeSegment(key, isForecefullyRemove);
					if(iResult == HL7Constants.HL7_FUNCTION_SUCCESS)
					{
						System.out.println("HL7 message edited for : " + key.getValue());
					}
					else
					{
						cnt = 1;
						System.out.println("HL7 message can not be edited for : " + key.getValue());
					}
				}
			}
		}
		if(cnt == 1) 
			return HL7Constants.HL7_FUNCTION_FAIL;
		else
			return iResult;
	}

	/*
	 * Populates message through provided SegmentSet,MessageCode and TriggerEvent
	 */
	private Message getMessage(String strMessageCode,String strTriggerEvent,ISegmentSet objSegmentSet)
	{
		Message objMessage = null;

		MessageFactory objMessageFactory = getMessageFactory();

		objMessage = objMessageFactory.populateReceiveSupportedMessage(strMessageCode, strTriggerEvent, objSegmentSet);

		if(objMessage != null)
		{
			objMessage = objMessageFactory.createSendSupportedMessage(strMessageCode, strTriggerEvent);
			if(objMessage != null)
				objMessage.populate(objSegmentSet);
		}

		return objMessage;
	}

	/*
	 * Populates Message Factory
	 */
	private MessageFactory getMessageFactory()
	{
		MessageFactory objMessageFactory = MessageFactory.createInstance();

		objMessageFactory.addMessageSource(new ObsvRepSource());
		objMessageFactory.addMessageRecepient(new ObsvRepRecepient());
		objMessageFactory.addMessageSource(new AppMgtSource());
		objMessageFactory.addMessageRecepient(new AppMgtRecepient());
		objMessageFactory.addMessageSource(new ClinicalLabAutoSource());
		objMessageFactory.addMessageRecepient(new ClinicalLabAutoRecepient());
		objMessageFactory.addMessageSource(new FinancialSource());
		objMessageFactory.addMessageRecepient(new FinancialRecepient());
		objMessageFactory.addMessageSource(new MasterFileSource());
		objMessageFactory.addMessageRecepient(new MasterFileRecepient());
		objMessageFactory.addMessageSource(new MediRecSource());
		objMessageFactory.addMessageRecepient(new MediRecRecepient());
		objMessageFactory.addMessageSource(new OrderEntrySource());
		objMessageFactory.addMessageRecepient(new OrderEntryRecepient());
		objMessageFactory.addMessageSource(new ADTSource());
		objMessageFactory.addMessageRecepient(new ADTRecepient());
		objMessageFactory.addMessageSource(new PatientCareSource());
		objMessageFactory.addMessageRecepient(new PatientCareRecepient());
		objMessageFactory.addMessageSource(new PatientRefSource());
		objMessageFactory.addMessageRecepient(new PatientRefRecepient());
		objMessageFactory.addMessageSource(new PersonalMgmtSource());
		objMessageFactory.addMessageRecepient(new PersonalMgmtRecepient());
		objMessageFactory.addMessageSource(new QrySource());
		objMessageFactory.addMessageRecepient(new QryRecepient());
		objMessageFactory.addMessageSource(new SchedulingSource());
		objMessageFactory.addMessageRecepient(new SchedulingRecepient());

		return objMessageFactory;
	}

	/**
	 * Updates the attribute of segment if present, otherwise add it if segment structure has that attribute.
	 * @param objMessage Object of Message
	 * @param objUpdateSegAttributeInfoCollection Collection of Attribute information.
	 * @param objDelimiter Object of IDelimiter
	 * @return 
	 */
	private int updateSegmentAttribute(Message objMessage,ISingleCollection<String> objUpdateSegAttributeInfoCollection, IDelimiter objDelimiter)
	{
		int iResult = HL7Constants.HL7_FUNCTION_FAIL;
		try
		{
			for(String strUpdateSegAttributeInfo : objUpdateSegAttributeInfoCollection)
			{
				if(strUpdateSegAttributeInfo.contains(":"))
				{
					String[] arraySeg_ItemID = strUpdateSegAttributeInfo.split(":");
					if(arraySeg_ItemID.length!=3)
						throw new HL7Exception(ResourceBundleWrapper.getValue("exceptions", "InvalidInputsForAttributeUpdate", new Locale(HL7Config.createInstance().getLocale())));
					String strSegment = arraySeg_ItemID[0];
					String strItemID = arraySeg_ItemID[1];
					String strAttributeValue = arraySeg_ItemID[2].toString();

					int iItemID = Integer.parseInt(strItemID);

					ISegmentKey objSegmentKey  = null;
					if(strSegment != null || strSegment != "")
					{
						String strPrefix = "HL7_SEG_";
						objSegmentKey  = EnumSegments.valueOf(strPrefix + strSegment);
						iResult = objMessage.updateSegmentAttribute(objSegmentKey, iItemID, strAttributeValue, objDelimiter);
						if(iResult == HL7Constants.HL7_FUNCTION_FAIL)
							break; 
					}
				}
			}
			return iResult;
		}
		catch(IllegalArgumentException iae)
		{
			System.out.println("Invalid Segment Name........");
			System.out.println("HL7 file edition failed........");
			iae.printStackTrace();
			return HL7Constants.HL7_FUNCTION_FAIL;
		} 
		catch (DelimiterMissingException e)
		{
			e.printStackTrace();
			return HL7Constants.HL7_FUNCTION_FAIL;
		}
		catch (InvalidDelimiterException e)
		{
			e.printStackTrace();
			return HL7Constants.HL7_FUNCTION_FAIL;
		}
		catch (InvalidDataTypeException e)
		{
			e.printStackTrace();
			return HL7Constants.HL7_FUNCTION_FAIL;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return HL7Constants.HL7_FUNCTION_FAIL;
		}   
	}

	/**
	 * Generates file for dumping.
	 * @param strTriggerEvent String value for TriggerEvent
	 * @param strMessageCode String value for MessageCode
	 * @param strDestinationPath String value Destination Dolder Path
	 */
	private void getFileForDump(String strTriggerEvent,String strMessageCode, String strDestinationPath, String strPrefix)
	{
		File objFileWrite;
		FileOutputStream objFileOutputStream;
		objIHL7Stream = new HL7FileStream();
		try
		{
			File testDir =  new File(strDestinationPath);
			testDir.mkdir();
			String strPathForDump = null;

			if(strPrefix != null)
				strPathForDump = strDestinationPath + "\\" + strPrefix + "_" + strMessageCode+"_"+strTriggerEvent+".doc";
			else
				strPathForDump = strDestinationPath + "\\" +strMessageCode+"_"+strTriggerEvent+".doc";

			objFileWrite = new File(strPathForDump);
			objFileWrite.createNewFile();
			objFileOutputStream = new FileOutputStream(objFileWrite);	
			objIHL7Stream.setOutputStream(objFileOutputStream);
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
}
