/*************************************************************
 * AddressBookManagerServlet parses the AddressBook.xml file
 * and manages all DOM-based updates, deletes, and so on.
 *
 * Kelli Wiseth [kelli_wiseth@yahoo.com]
 * NDNU | XML and Web Services
 * Fall 2004
 *
 ****************************************************************
 */
 
// Java core packages
import java.io.*;
import java.util.*;

// Java extension packages
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

// third-party packages
import org.w3c.dom.*;
import org.xml.sax.*;

public class AddressBookManagerServlet extends HttpServlet 
  {
   
   DocumentBuilder addressBookParser;
   Document addressBook;
   Element contact;
   
   
/*public void init(ServletConfig config) throws ServletException
{
//	super.init(config);
//	ServletContext context = getServletContext();
	System.out.println("inside the init method ");

}
*/
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
    {
   System.out.println("inside the HttpServletRequest but before try-block ");
  
   try
   {
   // obtain default parser and parse the XML addressbook
   System.out.println("inside the try block, but before parse ");
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   dbf.setValidating( false );

   DocumentBuilder addressBookParser = dbf.newDocumentBuilder();
   Document addressBook = addressBookParser.parse("http://localhost:80/addressbook/AddressBook.xml");
   addressBook.getDocumentElement().normalize();
   NodeList contactsAll = addressBook.getElementsByTagName("Contact");
   NodeList namesAll = addressBook.getElementsByTagName("Name");



   // Setup PrintWriter for output and start writing HTML to client
   response.setContentType( "text/html" );
   PrintWriter out = response.getWriter();
   out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");
   out.println("<head><html><title>Contact Management System [Servlet reply]</title>");
   out.println("<LINK href=\"techlab.css\" TYPE=\"text/css\" REL=\"stylesheet\"></LINK>");
   out.println("</head><body><h3>Contact Management System [Servlet Reply]</h3>");
   out.println("<table width=\"650\" border=\"0\" cellpadding=\"5\" cellspacing=\"5\">");
   out.println("<tr><td colspan=\"5\" class=\"BodyCopy\">");
   out.println("</TR><tr><td colspan=\"6\">&nbsp;</td></tr>");
   out.println("<tr><td colspan=\"6\" class=\"whiteCopy\">");
   out.println("<strong>AddressBook Listing</strong></td></tr>");
   out.println("<tr><td bgcolor=\"#eeeeee\" width=\"150\">Last name</td>");
   out.println("<td bgcolor=\"#eeeeee\" width=\"150\">First name</td>");
   out.println("<td bgcolor=\"#eeeeee\" width=\"70\">Contact id</td>");
   out.println("<td width=\"100\">&nbsp;</td><td colspan=\"2\" bgcolor=\"#cccccc\">Manage this");
   out.println(" contact:</td></tr>");
   

   for (int ctr = 0; ctr < contactsAll.getLength(); ctr++) 
    {
     int nodeIndex = ctr+1;
     Node contactName = namesAll.item(ctr);
     Element names = (Element)namesAll.item(ctr);
     NodeList lastNameList = names.getElementsByTagName("Last");
     Node lastNameElement =  lastNameList.item(0);
     Node lastNameTextNode = lastNameElement.getFirstChild();
     String lastNameText = lastNameTextNode.getNodeValue().trim();
     NodeList firstNameList = names.getElementsByTagName("First");
     Element firstNameElement = (Element)firstNameList.item(0);
     Node firstNameTextNode = firstNameElement.getFirstChild();
     String firstNameText = firstNameTextNode.getNodeValue().trim();

     out.println("<tr><td>" + lastNameText + "</td><td>" +  firstNameText+ "</td><td>");
     out.println("</td><td>" + nodeIndex + "</td><td>&nbsp</td></tr>"); 
     } //for-loop ctr1

    // close PrintWriter
     out.println("</body></html>");
  
 
     }//try block
   
     // handle exception thrown by DocumentBuilder
     catch ( ParserConfigurationException parserException ) 
     {
     parserException.printStackTrace();
     }
      
     // handle exception thrown by Parser
     catch ( SAXException saxException ) 
     {
     saxException.printStackTrace();         
     }
      
     // handle exception thrown when reading data from file
     catch ( IOException ioException ) 
     {
      System.out.print("some kind of IO problem... oops..\n");
      System.exit( 1 );
     }
      
  } //doGet   
                     
} // class AddressBookManagerServlet

