package com.agiledeveloper.ws;

import com.agiledeveloper.xmlprocessing.InfoReader;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.soap.*;
import org.apache.axis.*;

public class InformationProvider
{
	public String getFullName(String lastname)
	{
		// Typically we would access this information from a database. To keep things simple,
		// we will fetch information from a xml document.
		// The document is assumed to be located in c:\temp\data directory.

		String result = "No record found";

		try
		{
			InfoReader reader = new InfoReader(lastname);
			result = reader.getFullName();
		}
		catch(Exception ex)
		{
			result = ex.toString();
		}

		return result;
	}

	public String getRecord(String lastname)
	{
		// Typically we would access this information from a database. To keep things simple,
		// we will fetch information from a xml document.
		// The document is assumed to be located in c:\temp\data directory.

		String result = "No record found";

		try
		{
			InfoReader reader = new InfoReader(lastname);
			result = reader.getRecord();
		}
		catch(Exception ex)
		{
			result = ex.toString();
		}

		return result;
	}

	public String getPicture(String lastname)
	{
		// Typically we would access this information from a database. To keep things simple,
		// we will fetch information from a xml document.
		// The document is assumed to be located in c:\temp\data directory.

		String result = "No record-found";

		try
		{
			InfoReader reader = new InfoReader(lastname);
			String fileName = reader.getPictureFileName();
			DataHandler handler = new DataHandler(new FileDataSource(fileName));

			org.apache.axis.MessageContext msgContext = org.apache.axis.MessageContext.getCurrentContext(); 			
			org.apache.axis.Message msg = msgContext.getResponseMessage();
			AttachmentPart attachment = msg.createAttachmentPart();
			attachment.setDataHandler(handler);
			msg.addAttachmentPart(attachment);

			result = "Picture found for " + lastname;
		}
		catch(Exception ex)
		{
			result = result + " " + ex.toString();
			ex.printStackTrace();
		}

		return result;
	}
}