package cdac.medinfo.sdk.hl7282.hl7net.auxiliaryprotocol.batch;

import java.io.IOException;
import java.util.Locale;

import cdac.medinfo.sdk.hl7282.baselibs.messagedictionary.MessageMapReader;
import cdac.medinfo.sdk.hl7282.baselibs.messages.Message;
import cdac.medinfo.sdk.hl7282.baselibs.messages.MessageFactory;
import cdac.medinfo.sdk.hl7282.baselibs.segments.BHS;
import cdac.medinfo.sdk.hl7282.baselibs.segments.BTS;
import cdac.medinfo.sdk.hl7282.baselibs.segments.FHS;
import cdac.medinfo.sdk.hl7282.baselibs.segments.FTS;
import cdac.medinfo.sdk.hl7282.baselibs.segments.MSH;
import cdac.medinfo.sdk.hl7282.baselibs.segments.QPD;
import cdac.medinfo.sdk.hl7282.baselibs.segments.RCP;
import cdac.medinfo.sdk.hl7282.baselibs.segments.Segment;
import cdac.medinfo.sdk.hl7282.baselibs.segments.SegmentSet;
import cdac.medinfo.sdk.hl7282.commoninterface.ICollectionIterator;
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.BatchException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidSegmentSetException;
import cdac.medinfo.sdk.hl7282.exceptions.ParseException;
import cdac.medinfo.sdk.hl7282.exceptions.ProcessFailedException;
import cdac.medinfo.sdk.hl7282.exceptions.SegmentMissingException;
import cdac.medinfo.sdk.hl7282.hl7net.commons.IBatchProcessor;
import cdac.medinfo.sdk.hl7282.hl7net.commons.IHL7Batch;
import cdac.medinfo.sdk.hl7282.hl7net.commons.IHL7BatchFile;
import cdac.medinfo.sdk.hl7282.utils.EnumMessageCode;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;
import cdac.medinfo.sdk.hl7282.utils.EnumTriggerEvent;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
import cdac.medinfo.sdk.hl7282.utils.HL7Constants;
import cdac.medinfo.sdk.hl7282.utils.HL7Logger;
import cdac.medinfo.sdk.hl7282.utils.IMap;
import cdac.medinfo.sdk.hl7282.utils.IMessageMapReader;
import cdac.medinfo.sdk.hl7282.utils.ResourceBundleWrapper;
import cdac.medinfo.sdk.hl7282.utils.SingleCollection;

/**
 * BatchAgent performs the handling of sending and receiving process for HL7Batch/HL7BatchFile during communication.
 * It provides additional utility methods for population and creation of Batch/BatchFile through SegmentSet and vice-versa. 
 */
public class BatchAgent implements IBatchProcessor 
{
	

	/**
	 * Retrieves populated HL7BatchFile through given SegmentSet.
	 * @param objMessageFactory instance of MessageFactory
	 * @param objSegmentSet instance of SegmentSet
	 * @return IHL7BatchFile - instance of HL7BatchFile 
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public IHL7BatchFile processReceive(MessageFactory objMessageFactory,ISegmentSet objISegmentSet) 
	{
		String strControlId = null;
		ISegment objSegment = objISegmentSet.getList().elementAt(0);
		if(objSegment.getName().equals(EnumSegments.HL7_SEG_BHS))
			strControlId = (String)(((BHS)objSegment).getBatchControlID().getValue());
		if(objSegment.getName().equals(EnumSegments.HL7_SEG_FHS))
			strControlId = (String)(((FHS)objSegment).getFileControlID().getValue());

		//Status Logging
		if(HL7Config.createInstance().isLoggingOn())
		{
			String strRecievedMessage = ResourceBundleWrapper.getValue("exceptions", "Batch_RecievedMessage",new Locale(HL7Config.createInstance().getLocale())) + strControlId;
			HL7Logger objHLogger = HL7Logger.getLogger();
			objHLogger.log(strRecievedMessage);
		}
		return populate(objMessageFactory, objISegmentSet, false);
	}

	/**
	 * Retrieves SegmentSet generated through provided HL7BatchFile.
	 * @param objHL7BatchFile instance of HL7BatchFile
	 * @return ISegmentSet - instance of ISegmentSet
	 */
	public ISegmentSet processSend(IHL7BatchFile objHL7BatchFile)
	{
		ISegmentSet objSegmentSet = null;
		objSegmentSet = new SegmentSet();
		if(false == fillSegmentSet(objHL7BatchFile,objSegmentSet))
		{
			return null;
		}
		else
		{
			String strControlId = null;
			ISegment objSegment = objSegmentSet.getList().elementAt(0);
			if(objSegment.getName().equals(EnumSegments.HL7_SEG_BHS))
				strControlId = (String)(((BHS)objSegment).getBatchControlID().getValue());
			if(objSegment.getName().equals(EnumSegments.HL7_SEG_FHS))
				strControlId = (String)(((FHS)objSegment).getFileControlID().getValue());

			//Status Logging
			if(HL7Config.createInstance().isLoggingOn())
			{
				String strRecievedMessage = ResourceBundleWrapper.getValue("exceptions", "Batch_SendingMessage",new Locale(HL7Config.createInstance().getLocale())) + strControlId;
				HL7Logger objHLogger = HL7Logger.getLogger();
				objHLogger.log(strRecievedMessage);
			}
			return objSegmentSet;
		}
	}

	/**
	 * Creates HL7BatchFile and populate it through provided SegmentSet.
	 * It processes messages which are supported for sending through provided Message factory. 
	 * @param objMessageFactory instance of Message factory
	 * @param objSegmentSet instance of SegmentSet
	 * @return IHL7BatchFile - instance of HL7BatchFile
	 */
	public IHL7BatchFile createBatchFile(MessageFactory objMessageFactory,ISegmentSet objSegmentSet)
	{
		return populate(objMessageFactory, objSegmentSet, true);
	}

	/**
	 * Populates HL7BatchFile through provided SegmentSet.
	 * It processes messages which are supported for receiving through provided Message factory. 
	 * @param objMessageFactory instance of Message factory
	 * @param objSegmentSet instance of SegmentSet
	 * @return IHL7BatchFile - instance of HL7BatchFile
	 */
	public IHL7BatchFile populateBatchFile(MessageFactory objMessageFactory,ISegmentSet objSegmentSet)
	{
		return populate(objMessageFactory, objSegmentSet, false);
	}

	/**
	 * Creates HL7Batch through provided SegmentSet.
	 * It processes messages which are supported for receiving through provided Message factory. 
	 * @param objMessageFactory instance of Message factory
	 * @param objSegmentSet instance of SegmentSet
	 * @return IHL7Batch - instance of HL7BatchFile
	 */
	public IHL7Batch createBatch(MessageFactory objMessageFactory, ISegmentSet objSegmentSet)
	{
		return populateBatch(objMessageFactory, objSegmentSet, true);
	}
	
	/**
	 * Populates HL7Batch through provided SegmentSet.
	 * It processes messages which are supported for receiving through provided Message factory. 
	 * @param objMessageFactory instance of Message factory
	 * @param objSegmentSet instance of SegmentSet
	 * @return IHL7Batch - instance of HL7BatchFile
	 */
	public IHL7Batch populateBatch(MessageFactory objMessageFactory,ISegmentSet objSegmentSet)
	{
		return populateBatch(objMessageFactory, objSegmentSet, false);
	}
	
	/**
	 * Populates SegemntSet through provided HL7BatchFile.
	 * @param objHL7BatchFile instance of HL7BatchFile
	 * @return SegmentSet - instance of SegmentSet
	 */
	public ISegmentSet getSegmentSet(IHL7BatchFile objHL7BatchFile) 
	{
		if( objHL7BatchFile != null && objHL7BatchFile.validate())
		{
			ISegmentSet objSegmentSet = new SegmentSet();
			fillSegmentSet(objHL7BatchFile,objSegmentSet);
			return objSegmentSet;
		}
		return null;
	}

	/**
	 * Populates SegemntSet through provided HL7Batch.
	 * @param objHL7Batch instance of HL7Batch
	 * @return SegmentSet - instance of SegmentSet
	 */
	public ISegmentSet getSegmentSet(IHL7Batch objBatch) 
	{
		if( objBatch != null && objBatch.validate())
		{
			ISegmentSet objSegmentSet = new SegmentSet();
			fillSegmentSet(objBatch,objSegmentSet);
			return objSegmentSet;
		}
		return null;
	}

	/*
	 * Fills SegmentSet for provided HL7BatchFile.
	 */
	private boolean fillSegmentSet(IHL7BatchFile objHL7BatchFile,ISegmentSet objSegmentSet)
	{

		FHS objBatchFileHeader = objHL7BatchFile.getBatchFileHeader();
		if(objBatchFileHeader!=null)
			objSegmentSet.addSegment(objBatchFileHeader);
		ISingleCollection<IHL7Batch> objBatchList = objHL7BatchFile.getHL7BatchCollection();
		if(objBatchList != null)
		{
			for(IHL7Batch objBatch : objBatchList)
			{
				fillSegmentSet(objBatch,objSegmentSet);
			}
		}
		FTS objBatchFileTrailer = objHL7BatchFile.getBatchFileTrailer();
		if(objBatchFileTrailer!=null)
			objSegmentSet.addSegment(objBatchFileTrailer);
		return true;


	}

	/*
	 * Fills SegmentSet for provided HL7Batch.
	 */
	private boolean fillSegmentSet(IHL7Batch objBatch,ISegmentSet objSegmentSet)
	{
		BHS objBatchHeader = objBatch.getBatchHeader();
		if(objBatchHeader != null)
			objSegmentSet.addSegment(objBatchHeader);

		// handle Query Segments here
		QPD objQPD = objBatch.getQueryParameterDefinitionSegment();
		RCP objRCP = objBatch.getResponseControlParameterSegment();

		if(objQPD!=null) // Enhanced Mode
		{
			objSegmentSet.addSegment(objQPD);
			if(objRCP!=null)
				objSegmentSet.addSegment(objRCP);
		}

		ISingleCollection<Message> objMessageCollection = objBatch.getMessageCollection();
		if(objMessageCollection != null)
		{
			for(Message objMessage : objMessageCollection)
			{
				ISegmentSet objTempSegmentSet = objMessage.getSegmentSet();
				ICollectionIterator<ISegment> objIterator = objTempSegmentSet.iterator();
				while(objIterator.hasNext())
				{
					objSegmentSet.addSegment(objIterator.next());
				}
			}
		}
		BTS objBatchTrailer = objBatch.getBatchTrailer();
		if(objBatchTrailer != null)
			objSegmentSet.addSegment(objBatchTrailer);
		return true;
	}
	
	
	private IHL7Batch populateBatch(MessageFactory objMessageFactory,ISegmentSet objSegmentSet,boolean isSender)
	{
		IHL7Batch objHL7Batch = null;
		try 
		{
			
			ISingleCollection<ISegment> objSegmentCollection = objSegmentSet.getList();
			if(objSegmentCollection!= null && objSegmentCollection.size()>0)
			{
				int iSegSetSize = objSegmentCollection.size();
				ISingleCollection<Message> objMessageCollection = null;
				ISegmentSet objTempSegmentSet = null;
				for(int iRead=0;iRead<iSegSetSize;iRead++)
				{
					ISegment objSegment = null;
					objSegment = objSegmentCollection.elementAt(iRead);
					if(objSegment == null)
						throw new BatchException(this.getClass().getSimpleName());
					ISegmentKey objISegmentKey = objSegment.getName();

					if(EnumSegments.HL7_SEG_BHS.equals(objISegmentKey))
					{
						if(objHL7Batch == null)
						{
							objHL7Batch = new HL7Batch();
							objHL7Batch.setBatchHeader((BHS)objSegment);
							iRead++;
							objSegment = objSegmentCollection.elementAt(iRead);
							if(objSegment == null)
								throw new BatchException(this.getClass().getSimpleName());

							if(objSegment.getName() == EnumSegments.HL7_SEG_QPD || objSegment.getName() == EnumSegments.HL7_SEG_RCP)
							{
								if(objSegment.getName() != EnumSegments.HL7_SEG_QPD)
									throw new SegmentMissingException(EnumSegments.HL7_SEG_QPD.getValue());

								QPD objQPD = (QPD)objSegment;
								RCP objRCP = null;
								iRead++;
								objSegment = objSegmentCollection.elementAt(iRead);
								if(objSegment != null && objSegment.getName() == EnumSegments.HL7_SEG_RCP)
								{
									objRCP = (RCP)objSegment;
								}
								else
									iRead--;
								objHL7Batch.setQuerySegments(objQPD, objRCP);
							}
							else
								iRead--;
						}
						else
						{
							throw new BatchException(this.getClass().getSimpleName());
						}
						continue;
					}
					else if(EnumSegments.HL7_SEG_MSH.equals(objISegmentKey))
					{
						if(objTempSegmentSet!=null)
						{
							String strTriggerEvent = null;
							String strMessageCode = null;
							Segment objTempSegment = (Segment)objTempSegmentSet.getSegment(0);
							if(objTempSegment.getName().getValue().equals(EnumSegments.HL7_SEG_MSH.getValue()))
							{
								MSH objMSH = (MSH)objTempSegment;
								strTriggerEvent = (String)objMSH.getMessageType().getTriggerEvent().getValue();
								strMessageCode = (String)objMSH.getMessageType().getMessageCode().getValue();	
							}
							Message objMessage = null;
							if(isSender)
							{
								objMessage = objMessageFactory.createSendSupportedMessage(strMessageCode, strTriggerEvent);
								objMessage.populate(objTempSegmentSet);
							}
							else
							{
								objMessage = objMessageFactory.populateReceiveSupportedMessage(strMessageCode, strTriggerEvent, objTempSegmentSet);
							}
							objTempSegmentSet = null;
							if(objMessageCollection == null)
								objMessageCollection = new SingleCollection<Message>();
							objMessageCollection.add(objMessage);
						}
						objTempSegmentSet = new SegmentSet();
						objTempSegmentSet.addSegment(objSegment);
						continue;
					}
					else if(EnumSegments.HL7_SEG_BTS.equals(objISegmentKey))
					{
						if(objHL7Batch == null)
						{
							throw new BatchException(this.getClass().getSimpleName());
						}
						else
						{
							if(objTempSegmentSet!=null)
							{
								String strTriggerEvent = null;
								String strMessageCode = null;
								Segment objTempSegment = (Segment)objTempSegmentSet.getSegment(0);
								if(objTempSegment.getName().getValue().equals(EnumSegments.HL7_SEG_MSH.getValue()))
								{
									MSH objMSH = (MSH)objTempSegment;
									strTriggerEvent = (String)objMSH.getMessageType().getTriggerEvent().getValue();
									strMessageCode = (String)objMSH.getMessageType().getMessageCode().getValue();	
								}
								Message objMessage = null;
								if(isSender)
								{
									objMessage = objMessageFactory.createSendSupportedMessage(strMessageCode, strTriggerEvent);
									objMessage.populate(objTempSegmentSet);
								}
								else
								{
									objMessage = objMessageFactory.populateReceiveSupportedMessage(strMessageCode, strTriggerEvent, objTempSegmentSet);
								}
								objTempSegmentSet = null;
								if(objMessageCollection == null)
									objMessageCollection = new SingleCollection<Message>();
								objMessageCollection.add(objMessage);
							}

							objHL7Batch.setBatchTrailer((BTS)objSegment);
							if(objMessageCollection != null)
							{
								objHL7Batch.setMessageCollection(objMessageCollection);
								objMessageCollection = null;
							}
							
						}
						continue;
					}
					else
					{
						if(objTempSegmentSet!=null)
						{
							objTempSegmentSet.addSegment(objSegment);
						}
						else
						{
							throw new BatchException(this.getClass().getSimpleName());
						}
					}
				}
			}
		}
		catch (ParseException e) 
		{
			throw new BatchException(ResourceBundleWrapper.getValue("exceptions","ParseFailed",new Locale(HL7Config.createInstance().getLocale())));
		} catch (ProcessFailedException e) {
			e.printStackTrace();
		} 
		return objHL7Batch;
	}

	/*
	 * Populates HL7BatchFile through provided SegmentSet.
	 * Sending and receiving capability will be checked according to the provided boolean value. 
	 */
	private IHL7BatchFile populate(MessageFactory objMessageFactory,ISegmentSet objSegmentSet,boolean isSender)
	{
		IHL7BatchFile objBatchFile = null;
		try 
		{
			objBatchFile = new HL7BatchFile();
			ISingleCollection<ISegment> objSegmentCollection = objSegmentSet.getList();
			if(objSegmentCollection!= null && objSegmentCollection.size()>0)
			{
				int iSegSetSize = objSegmentCollection.size();
				boolean isFileHeaderSet = false;
				IHL7Batch objHL7Batch = null;
				ISingleCollection<Message> objMessageCollection = null;
				ISingleCollection<IHL7Batch> objIHL7BatchCollection = null;
				ISegmentSet objTempSegmentSet = null;
				for(int iRead=0;iRead<iSegSetSize;iRead++)
				{
					ISegment objSegment = null;
					objSegment = objSegmentCollection.elementAt(iRead);
					if(objSegment == null)
						throw new BatchException(this.getClass().getSimpleName());
					ISegmentKey objISegmentKey = objSegment.getName();

					if(EnumSegments.HL7_SEG_FHS.equals(objISegmentKey))
					{
						if(false == isFileHeaderSet)
						{
							objBatchFile.setBatchFileHeader((FHS)objSegment);
							isFileHeaderSet = true;
							
							//Status Logging
							if(HL7Config.createInstance().isLoggingOn())
							{
								String strRecievedMessage = ResourceBundleWrapper.getValue("exceptions", "Batch_SendingMessage",new Locale(HL7Config.createInstance().getLocale()));
								HL7Logger objHLogger = HL7Logger.getLogger();
								objHLogger.log(strRecievedMessage);
							}
						}
						else
							throw new BatchException(this.getClass().getSimpleName());
						continue;
					}
					else if(EnumSegments.HL7_SEG_BHS.equals(objISegmentKey))
					{
						if(objHL7Batch == null)
						{
							objHL7Batch = new HL7Batch();
							objHL7Batch.setBatchHeader((BHS)objSegment);
							if(objIHL7BatchCollection == null)
								objIHL7BatchCollection = new SingleCollection<IHL7Batch>();
							iRead++;
							objSegment = objSegmentCollection.elementAt(iRead);
							if(objSegment == null)
								throw new BatchException(this.getClass().getSimpleName());

							if(objSegment.getName() == EnumSegments.HL7_SEG_QPD || objSegment.getName() == EnumSegments.HL7_SEG_RCP)
							{
								if(objSegment.getName() != EnumSegments.HL7_SEG_QPD)
									throw new SegmentMissingException(EnumSegments.HL7_SEG_QPD.getValue());

								QPD objQPD = (QPD)objSegment;
								RCP objRCP = null;
								iRead++;
								objSegment = objSegmentCollection.elementAt(iRead);
								if(objSegment != null && objSegment.getName() == EnumSegments.HL7_SEG_RCP)
								{
									objRCP = (RCP)objSegment;
								}
								else
									iRead--;
								objHL7Batch.setQuerySegments(objQPD, objRCP);
							}
							else
								iRead--;
						}
						else
						{
							throw new BatchException(this.getClass().getSimpleName());
						}
						continue;
					}
					else if(EnumSegments.HL7_SEG_MSH.equals(objISegmentKey))
					{
						if(objTempSegmentSet!=null)
						{
							String strTriggerEvent = null;
							String strMessageCode = null;
							Segment objTempSegment = (Segment)objTempSegmentSet.getSegment(0);
							if(objTempSegment.getName().getValue().equals(EnumSegments.HL7_SEG_MSH.getValue()))
							{
								MSH objMSH = (MSH)objTempSegment;
								strTriggerEvent = (String)objMSH.getMessageType().getTriggerEvent().getValue();
								strMessageCode = (String)objMSH.getMessageType().getMessageCode().getValue();	
							}
							Message objMessage = null;
							if(isSender)
							{
								objMessage = objMessageFactory.createSendSupportedMessage(strMessageCode, strTriggerEvent);
								objMessage.populate(objTempSegmentSet);
							}
							else
							{
								objMessage = objMessageFactory.populateReceiveSupportedMessage(strMessageCode, strTriggerEvent, objTempSegmentSet);
							}
							objTempSegmentSet = null;
							if(objMessageCollection == null)
								objMessageCollection = new SingleCollection<Message>();
							objMessageCollection.add(objMessage);
						}
						objTempSegmentSet = new SegmentSet();
						objTempSegmentSet.addSegment(objSegment);
						continue;
					}
					else if(EnumSegments.HL7_SEG_BTS.equals(objISegmentKey))
					{
						if(objHL7Batch == null)
						{
							throw new BatchException(this.getClass().getSimpleName());
						}
						else
						{
							if(objTempSegmentSet!=null)
							{
								String strTriggerEvent = null;
								String strMessageCode = null;
								Segment objTempSegment = (Segment)objTempSegmentSet.getSegment(0);
								if(objTempSegment.getName().getValue().equals(EnumSegments.HL7_SEG_MSH.getValue()))
								{
									MSH objMSH = (MSH)objTempSegment;
									strTriggerEvent = (String)objMSH.getMessageType().getTriggerEvent().getValue();
									strMessageCode = (String)objMSH.getMessageType().getMessageCode().getValue();	
								}
								Message objMessage = null;
								if(isSender)
								{
									objMessage = objMessageFactory.createSendSupportedMessage(strMessageCode, strTriggerEvent);
									objMessage.populate(objTempSegmentSet);
								}
								else
								{
									objMessage = objMessageFactory.populateReceiveSupportedMessage(strMessageCode, strTriggerEvent, objTempSegmentSet);
								}
								objTempSegmentSet = null;
								if(objMessageCollection == null)
									objMessageCollection = new SingleCollection<Message>();
								objMessageCollection.add(objMessage);
							}

							objHL7Batch.setBatchTrailer((BTS)objSegment);
							if(objMessageCollection != null)
							{
								objHL7Batch.setMessageCollection(objMessageCollection);
								objMessageCollection = null;
							}
							objIHL7BatchCollection.add(objHL7Batch);
							objHL7Batch = null;
							if((iRead+1)== iSegSetSize)
							{
								if(isFileHeaderSet)
									throw new BatchException(this.getClass().getSimpleName());
								else
									objBatchFile.setHL7BatchCollection(objIHL7BatchCollection);
							}
						}
						continue;
					}
					else if(EnumSegments.HL7_SEG_FTS.equals(objISegmentKey))
					{
						if(false == isFileHeaderSet || objHL7Batch != null)
							throw new BatchException(this.getClass().getSimpleName());
						else
						{
							if(objTempSegmentSet!=null)
							{
								if(objIHL7BatchCollection != null)
									throw new BatchException(this.getClass().getSimpleName());

								String strTriggerEvent = null;
								String strMessageCode = null;
								Segment objTempSegment = (Segment)objTempSegmentSet.getSegment(0);
								if(objTempSegment.getName().getValue().equals(EnumSegments.HL7_SEG_MSH.getValue()))
								{
									MSH objMSH = (MSH)objTempSegment;
									strTriggerEvent = (String)objMSH.getMessageType().getTriggerEvent().getValue();
									strMessageCode = (String)objMSH.getMessageType().getMessageCode().getValue();	
								}
								Message objMessage = null;
								if(isSender)
								{
									objMessage = objMessageFactory.createSendSupportedMessage(strMessageCode, strTriggerEvent);
									objMessage.populate(objTempSegmentSet);
								}
								else
								{
									objMessage = objMessageFactory.populateReceiveSupportedMessage(strMessageCode, strTriggerEvent, objTempSegmentSet);
								}
								objTempSegmentSet = null;
								if(objMessageCollection == null)
									objMessageCollection = new SingleCollection<Message>();
								objMessageCollection.add(objMessage);
								objHL7Batch = new HL7Batch(objMessageCollection);
								objIHL7BatchCollection = new SingleCollection<IHL7Batch>();
								objIHL7BatchCollection.add(objHL7Batch);
							}
							objBatchFile.setBatchFileTrailer((FTS)objSegment);
							objBatchFile.setHL7BatchCollection(objIHL7BatchCollection);
							if((iRead+1)!= iSegSetSize)
							{
								throw new BatchException(this.getClass().getSimpleName());
							}
						}
						continue;
					}
					else
					{
						if(objTempSegmentSet!=null)
						{
							objTempSegmentSet.addSegment(objSegment);
						}
						else
						{
							throw new BatchException(this.getClass().getSimpleName());
						}
					}
				}
			}
		}
		catch (ParseException e) 
		{
			throw new BatchException(ResourceBundleWrapper.getValue("exceptions","ParseFailed",new Locale(HL7Config.createInstance().getLocale())));
		} catch (ProcessFailedException e) {
			e.printStackTrace();
		} 
		return objBatchFile;
	}
	
	/*public int writeBatchToXML_v2(String strFilePath, ISegmentSet objISegmentSet) 
	{
		ICollectionIterator<ISegment> objSegmentIterator= objISegmentSet.iterator();
		IDelimiter objIDelimiter = null;
		
		if(objISegmentSet != null && objISegmentSet.size() != 0)
		{
			ISegment objSegment = objISegmentSet.getSegment(0);

			String strSegmentName = objSegment.getName().getValue();
			String strMsgHeader = EnumSegments.HL7_SEG_MSH.getValue();
			String strBatchHeader = EnumSegments.HL7_SEG_BHS.getValue();
			String strBatchFileHeader = EnumSegments.HL7_SEG_FHS.getValue();


			if(strMsgHeader.equals(strSegmentName) || strBatchHeader.equals(strSegmentName) || strBatchFileHeader.equals(strSegmentName))
			{
				while(objSegmentIterator.hasNext())
				{
					objSegment = objSegmentIterator.next();
					SegmentSet objMessageSegmentSet = new SegmentSet();
					if(objSegment.getName() == EnumSegments.HL7_SEG_FHS)
					{
						FHS objBatchFileHeader=(FHS)objSegment;
					}
					else if(objSegment.getName() == EnumSegments.HL7_SEG_FTS)
					{
						FTS objBatchFileTrailer=(FTS)objSegment;
					}
					else if(objSegment.getName() == EnumSegments.HL7_SEG_BHS)
					{
						BHS objBatchHeader= (BHS)objSegment;
					}
					else if(objSegment.getName() == EnumSegments.HL7_SEG_BTS)
					{
						BTS objBatchTrailer = (BTS)objSegment;
					}
					else if(objSegment.getName() == EnumSegments.HL7_SEG_MSH)
					{
						MSH objMessageHearder=(MSH)objSegment;
						String strMessageCode=(String) objMessageHearder.getMessageType().getMessageCode().getValue();
						String strTriggerEvent=(String)objMessageHearder.getMessageType().getTriggerEvent().getValue();
						String strMessageStructure=(String) objMessageHearder.getMessageType().getMessageStructure().getValue();
						EnumTriggerEvent enumTriggerEvent=EnumTriggerEvent.valueOf(strTriggerEvent);
						EnumMessageCode enumMessageCode= EnumMessageCode.valueOf(strMessageCode);
						
						IMap objMessageMap = null;
						IMessageMapReader objMessageMapReader = MessageMapReader.createInstance();
						
						objMessageMap=objMessageMapReader.getMessageMap(enumMessageCode, enumTriggerEvent);
						
						//create new message
						Message objmessage = new Message(objMessageMap,strMessageCode, strTriggerEvent, strMessageStructure);
						objMessageSegmentSet.addSegment(objSegment);
					}

					//objSegment.serialize(objHL7Stream, objIDelimiter);
				}
			}
			else
				throw new InvalidSegmentSetException(ResourceBundleWrapper.getValue("exceptions", "MissingHeader", new Locale(HL7Config.createInstance().getLocale())));
		}
		else
			throw new InvalidSegmentSetException(ResourceBundleWrapper.getValue("exceptions", "MissingHeader", new Locale(HL7Config.createInstance().getLocale())));
		return HL7Constants.HL7_FUNCTION_SUCCESS;
	}
*/	
}
