package boundary;

import org.apache.xmlrpc.client.*;
import java.util.*;

public class PerlCommunicator {
	/* constants that determine where the perl script is on the server, 
	 * where the config files are (stock_list.xml and stock_attributes.xml)
	 * and what function we are calling
	 */
	public static String SERVER_URL = "http://localhost/cgi-bin/server.pl";
	public static String FETCH_STOCKS = "PerlFunctions.fetch_stocks";
	
	public PerlCommunicator() {
		
	}
	
	/* fetchStocks() takes no parameters
	 * fetchStocks() returns an XML document as a String
	 * the XML document contains the information received from the perl script
	 */
	public static String fetchStocks() {
		String retval = null;
		try {
			// create an xmlrpcclient, and use it to call the perl function
			XmlRpcClient xmlrpc = new XmlRpcClient();
			XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
			config.setServerURL(new java.net.URL(SERVER_URL));
			// our function does not take any parameters, but the Collection type is required as
			// part of xmlrpc.execute()
			Vector<Integer> noParams = new Vector<Integer>(0);
			System.out.println("Fetching stocks...");
			retval = (String) xmlrpc.execute(config, FETCH_STOCKS, noParams);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return retval;
	}
	
	public static void main(String[] args) {
		System.out.println(PerlCommunicator.fetchStocks());
	}
}
