//Helper class to help you
//place Helper.java in jsdk2.1/examples/WEB-INF/servlets/
//use "javac Helper.java" to compile
//the usage refer to Use.java
// Helper.queryPrice(ID,URL) - give a product ID and a site's URL, it will return the
// price quote from that site (if that site does not have this product, null will return)
// Helper.queryOnhand(ID,URL) - give a product ID and a site's URL, it will return the
// onhand quote from that site (if that site does not have this product, null will return)

import java.io.*;
import java.net.*;
import java.util.*;
import nanoxml.*;

public class Helper {
	public static String queryPrice (String ID, String urlstring) {
		URL url;
		URLConnection urlconnection;
		PrintWriter out;
		BufferedReader in;
		StringBuffer buffer=new StringBuffer(100);
		String tempLine;
		
		XMLElement root;
		try {
			url=new URL(urlstring);
			urlconnection=url.openConnection();
			urlconnection.setDoOutput(true);
			out=new PrintWriter(urlconnection.getOutputStream());
			out.print("field=ID&query="+ID);
			out.close();
			in=new BufferedReader(new InputStreamReader(urlconnection.getInputStream()));
			do {
				tempLine=in.readLine();
				if (tempLine!=null) {
					buffer.append(tempLine);
				} else {
					break;
				}
			} while (true);
			in.close();
			
			// begin to parse the XML read from CompareXML
			// construct a XMLElement object
			root=new XMLElement();
			
			// call its method to read data from a string containing XML
			root.parseString(buffer.toString());
			
			// start to anaylse the XML DOM tree
			XMLElement product=findTag(root,"product");
			if (product!=null) {
				XMLElement company=findTag(product,"company");
				if (company!=null) {
					XMLElement price=findTag(company,"price");
					if (price!=null) {
						 return price.getContents();
					} else {
						return null;
					}
				} else {
					return null;
				}
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String queryOnhand (String ID, String urlstring) {
		URL url;
		URLConnection urlconnection;
		PrintWriter out;
		BufferedReader in;
		StringBuffer buffer=new StringBuffer(100);
		String tempLine;
		
		XMLElement root;
		try {
			url=new URL(urlstring);
			urlconnection=url.openConnection();
			urlconnection.setDoOutput(true);
			out=new PrintWriter(urlconnection.getOutputStream());
			out.print("field=ID&query="+ID);
			out.close();
			in=new BufferedReader(new InputStreamReader(urlconnection.getInputStream()));
			do {
				tempLine=in.readLine();
				if (tempLine!=null) {
					buffer.append(tempLine);
				} else {
					break;
				}
			} while (true);
			in.close();
			
			// begin to parse the XML read from CompareXML
			// construct a XMLElement object
			root=new XMLElement();
			
			// call its method to read data from a string containing XML
			root.parseString(buffer.toString());
			
			// start to anaylse the XML DOM tree
			XMLElement product=findTag(root,"product");
			if (product!=null) {
				XMLElement company=findTag(product,"company");
				if (company!=null) {
					XMLElement onhand=findTag(company,"onhand");
					if (onhand!=null) {
						 return onhand.getContents();
					} else {
						return null;
					}
				} else {
					return null;
				}
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private static XMLElement findTag(XMLElement src,String str) {
		Vector v = src.getChildren();
		for (int i = 0; i < v.size(); i++) {
			XMLElement e = (XMLElement)(v.elementAt(i));
			if (e.getTagName().compareTo(str) == 0) {
				return e;
			} 
		}
		return null;
	}
}
