package org.maskat.demo.petstore;
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.net.*; 
import java.sql.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.w3c.dom.NodeList;


public class ItemServlet extends HttpServlet{ 

	public void doPost (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{

	int i;
	Vector ret = null;
	String str = null;
	String resultText = null;
	String productId= null;
	String itemId= null;

	Document doc = null;
	Element root = null;
	Element Element = null;

	String[][] itemArray = {
            {"EST-1","FI-SW-01","Large Angelfish","16","fish1.jpg","Salt Water fish from Australia"},
            {"EST-2","FI-SW-01","Small Angelfish","16","fish1.jpg","Salt Water fish from Australia"},
            {"EST-3","FI-SW-02","Toothless Tiger Shark","18","fish4.jpg","Salt Water fish from Australia"},
            {"EST-4","FI-FW-01","Spotted Koi","18","fish3.jpg","Fresh Water fish from Japan"},
            {"EST-5","FI-FW-01","Spotless Koi","18","fish3.jpg","Fresh Water fish from Japan"},
            {"EST-20","FI-FW-02","Adult Male Goldfish","5","fish2.jpg","Fresh Water fish from China"},
            {"EST-21","FI-FW-02","Adult Female Goldfish","5","fish2.jpg","Fresh Water fish from China"},
            {"EST-6","K9-BD-01","Male Adult Bulldog","18","dog2.gif","Friendly dog from England"},
            {"EST-7","K9-BD-01","Female Puppy Bulldog","18","dog2.gif","Friendly dog from England"},
            {"EST-8","K9-PO-02","Male Puppy Poodle","18","dog6.gif","Cute dog from France"},
            {"EST-9","K9-DL-01","Spotless Male Puppy Poodle","18","dog5.gif","Great dog for a Fire Station"},
            {"EST-10","K9-DL-01","Spotted Adult Female Dalmation","18","dog5.gif","Great dog for a Fire Station"},
            {"EST-28","K9-RT-01","Adult Female Golden Retriever","155","dog1.gif","Great family dog"},
            {"EST-14","FL-DSH-01","Tailless Manx","58","cat3.gif","Great for reducing mouse populations"},
            {"EST-15","FL-DSH-01","With tail Manx","23","cat3.gif","Great for reducing mouse populations"},
            {"EST-16","FL-DSH-02","Adult Female Persian","93","cat1.gif","Friendly house cat, doubles as a princess"},
            {"EST-17","FL-DSH-02","Adult Male Persian","93","cat1.gif","Friendly house cat, doubles as a princess"},
            {"EST-11","RP-SN-01","Venomless Rattlesnake","18","lizard3.gif","Doubles as a watch dog"},
            {"EST-12","RP-SN-01","Rattleless Rattlesnake","18","lizard3.gif","Doubles as a watch dog"},
            {"EST-13","RP-LI-02","Green Adult Iguana","18","lizard2.gif","Friendly green friend"},            
            {"EST-18","AV-CB-01","Adult Male Amazon Parrot","193","bird4.gif","Great companion for up to 75 years"},
            {"EST-19","AV-SB-02","Adult Male Finch","15","bird1.gif","Great stress reliever"},
          };
	
	response.setContentType("text/xml; charset=UTF-8");	
	Writer writer = response.getWriter();

	try{
		DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
        // ドキュメントビルダーを生成
    	DocumentBuilder builder = dbfactory.newDocumentBuilder();
        // パースを実行してDocumentオブジェクトを取得
		//doc = builder.parse(new InputSource(new ByteArrayInputStream(xmlParam.getBytes())));
		doc = builder.parse(request.getInputStream());

		root = doc.getDocumentElement();
		
		NodeList list = root.getElementsByTagName("productId");
		Element = (Element)list.item(0);
		productId = Element.getFirstChild().getNodeValue();

	}catch(Exception e){
		e.printStackTrace();
	}
	
	resultText = "<?xml version='1.0' encoding='UTF-8' ?><info><items>";
	
	for (i=0;i<22;i++){
		if(productId.equals(itemArray[i][1])){
			 resultText = resultText + "<item><itemId>"+itemArray[i][0]+"</itemId><productId>"+itemArray[i][1]+"</productId><description>"+itemArray[i][2]+"</description><price>"+itemArray[i][3]+"</price><imgURL>"+itemArray[i][4]+"</imgURL><account>"+itemArray[i][5]+"</account></item>";
		}
    }

	resultText = resultText + "</items></info>";

	writer.write(resultText);
	
	writer.flush();

	}

}