package Addressbook;

/*
 * dbXML Core - Example Code
 *
 * Copyright (c) 1999-2001 The dbXML Group
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 *    The above copyright notice and this permission notice shall be included
 *       in all copies of substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * $Id: Action.java,v 1.1 2001/08/08 04:56:13 kstaken Exp $
 */
import org.xmldb.api.DatabaseManager;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;

import org.w3c.dom.*;
import org.apache.xpath.*;
import org.apache.xpath.objects.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Common class for the Addressbook
 */
public abstract class Action extends HttpServlet {

   // Version of the XMLDB Api that we are using
   public final static String XMLDBAPIVERSION = "1.0";

   // Singleton class used to hold connnection to database
   protected static DBConnection connection = new DBConnection();

   /**
    * Return a collection object for the addressbook
    */
   public Collection getCollection(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
      return connection.getCollectionInstance(request, response);
   }

   /**
    * Return a vector of hashtables populated with all elements in 
    * ResourceIterator
    */
   public Vector getContactVectorFromIterator(ResourceIterator results) {

      // new Vector instance to be returned
      Vector vector = new Vector();
      try {
         while (results.hasMoreResources()) {

            // Create an XObject to be used in Xpath query
            XObject xo = new XObject();
            // Create a new hastable to store into vector
            Hashtable table = new Hashtable();

            // Retrieve the next node
            XMLResource resource = (XMLResource) results.nextResource();            
            Node originalnode = resource.getContentAsDOM();

            // Get the document key for this node for later use
            String dockey = resource.getDocumentId();
            table.put("dockey", dockey);

            // Use Xalan xpath parser to extract data, store into hastable
            xo = XPathAPI.eval(originalnode, "/person/fname");
            table.put("fname", xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/lname");
            table.put("lname", xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/phone[@type='work']");
            table.put("workphone", xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/phone[@type='home']");
            table.put("homephone", xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/phone[@type='cell']");
            table.put("cellphone", xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/email[@type='home']");
            table.put("homeemail", xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/email[@type='work']");
            table.put("workemail", xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/address[@type='home']");
            table.put("homeaddress", xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/address[@type='work']");
            table.put("workaddress", xo.toString());

            // Add hashtable to vector to pass on to JSP page
            vector.add(table);
         }

      }
      catch (Exception e) {
         e.printStackTrace();
      }

      return vector;
   }

   /**
    * Create a String XML document from passed in parameters
    */
   public String toXml(String fname, String lname, String workphone,
         String homephone, String cellphone, String homeemail, String workemail,
         String homeaddress, String workaddress) {

      String ourdoc;

      // Build up the string to represent our document
      ourdoc = "<person>";
      ourdoc = ourdoc + "<fname>" + fname + "</fname>";
      ourdoc = ourdoc + "<lname>" + lname + "</lname>";
      ourdoc = ourdoc + "<phone type='work'>" + workphone + "</phone>";
      ourdoc = ourdoc + "<phone type='home'>" + homephone + "</phone>";
      ourdoc = ourdoc + "<phone type='cell'>" + cellphone + "</phone>";
      ourdoc = ourdoc + "<email type='home'>" + homeemail + "</email>";
      ourdoc = ourdoc + "<email type='work'>" + workemail + "</email>";
      ourdoc = ourdoc + "<address type='home'>" + homeaddress + "</address>";
      ourdoc = ourdoc + "<address type='work'>" + workaddress + "</address>";
      ourdoc = ourdoc + "</person>";

      // Return the created XML string
      return ourdoc;
   }

}

