package com.extrememessaging.ResponseMaster.example;

import java.io.FileWriter;
import java.io.IOException;
import org.w3c.dom.*;

import com.extrememessaging.ResponseMaster.dispositions.IDisposition;
import com.extrememessaging.ResponseMaster.ResponseMasterException;
import com.extrememessaging.ResponseMaster.CMessage;
import com.extrememessaging.ResponseMaster.CMonitorManager;
import com.extrememessaging.ResponseMaster.CXMLHelper;
import com.extrememessaging.ResponseMaster.eMessageFields;
import com.extrememessaging.ResponseMaster.InvalidMessageFieldName;
//You may want these imports too, but this example doesn't use them.
//import com.extrememessaging.ResponseMaster.COutputField;
//import com.extrememessaging.ResponseMaster.CHelper;

/**
 * An example disposition object that writes the guid and category to a file
 * @see	com.extrememessaging.ResponseMaster.dispositions.IDisposition
 * The corresponding entry for this in dispositionTypes.xml is something like this:
	<DispositionType>
		<Name>ExampleFileWriter</Name>
		<DetailedDispositionFile>1</DetailedDispositionFile>
		<ClassName>com.extrememessaging.ResponseMaster.example.CExampleDisposition</ClassName>
	</DispositionType>
 * */
public class CExampleDisposition implements IDisposition
{	
	private FileWriter		fwOutput;
	private String			sOutputFileName;
							   
	public synchronized void Close()
	{
		if (fwOutput!=null)
		{
			try
			{
				fwOutput.close();
			} catch (Exception e)
			{
			}
		}
	}

	public void ReadFromFile(String sFileName)
		throws ResponseMasterException
	{
		org.w3c.dom.Document	oDoc;
		org.w3c.dom.Element		oElement;
		org.w3c.dom.NodeList	oNodeList;
		
		//Verify that the fields we need are available
		try
		{
			eMessageFields.GetMessageFieldIdFromName("MessageGUID");
			eMessageFields.GetMessageFieldIdFromName("Category");
		} catch (InvalidMessageFieldName imfn)
		{
			throw new ResponseMasterException("The example disposition requires the MessageGUID and Category fields to be available");
		}
		
		CMonitorManager.LogSystem("Reading detailed disposition config file=" + sFileName);
		//load the doc
		oDoc=CXMLHelper.LoadFileIntoDOM(sFileName); //throws an exception if there is a problem

		//get the Disposition node.
		oNodeList=oDoc.getElementsByTagName("Disposition");
		if (oNodeList.getLength()!=1)
		{
			throw new ResponseMasterException("Expected one Disposition element at the root of the detailed disposition config file(" + sFileName + "); there were " + oNodeList.getLength());
		}
		oElement=(Element)oNodeList.item(0);		
		
		//get file name and open it
		sOutputFileName=CXMLHelper.getChildElementValue(oElement,"FileName","output.txt");		
		CMonitorManager.LogSystem("Output file=" + sOutputFileName);
		try 
		{
			fwOutput=new FileWriter(sOutputFileName,true);  //true->append
		}
		catch (IOException e)
		{
			CMonitorManager.LogError("Error opening file in example disposition object",e);
			throw new ResponseMasterException("Error opening file in example disposition object");
		}
		
		CMonitorManager.LogSystem("Finished reading detailed disposition config file=" + sFileName);
		//success
	}
	
	public synchronized void Dispose(CMessage oMessage)
		throws ResponseMasterException
	{
		StringBuffer	sBuffer=new StringBuffer(200);

		sBuffer.append(oMessage.getFieldNonNull("MessageGUID"));
		sBuffer.append("\t");
		sBuffer.append(oMessage.getFieldNonNull("Category"));
		sBuffer.append("\r\n");
		try 
		{
			fwOutput.write(sBuffer.toString());
			fwOutput.flush();
			return; //success
		}
		catch (IOException e)
		{
			CMonitorManager.LogError("Error writing to output file",e);
			throw new ResponseMasterException("Error writing to output file");
		}
	}
}
