
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: SearchContacts.java,v 1.1 2001/08/08 04:56:14 kstaken Exp $
 */
 
import java.io.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;

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

import java.net.*;

// Class used to search the database using an Xpath query
public class SearchContacts extends Action {

   public boolean attributeSearch(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
   
      Collection col = null;
      Vector vector = null;
      String xpath = null;
      
      try {
      
         HttpSession session = request.getSession(true);
   
         // Get a collection instance
         col = getCollection(request,response);
   
         XPathQueryService service = (XPathQueryService)col.getService("XPathQueryService",XMLDBAPIVERSION);
         
         // Get the seach parameters from the form
         String searchtype = request.getParameter("SEARCHTYPE");
         String searchstring = request.getParameter("SEARCHSTRING");
         
         // Setup xpath string depending on search type
         if ( searchtype.equals("fname") || searchtype.equals("lname") ) {
            xpath = "/person[" + searchtype + "='" + searchstring + "']";
         } else if ( searchtype.equals("workphone")) {
            xpath = "/person[phone/@type='work' and phone/text() = '" + searchstring + "' ]" ;
         } else if ( searchtype.equals("homephone")) {
            xpath = "/person[phone/@type='home' and phone/text() = '" + searchstring + "' ]" ;
         } else if ( searchtype.equals("cellphone")) {
            xpath = "/person[phone/@type='cell' and phone/text() = '" + searchstring + "' ]" ;
         } else if ( searchtype.equals("homeemail")) {
            xpath = "/person[email/@type='home' and email/text() = '" + searchstring + "' ]" ;
         } else if ( searchtype.equals("workemail")) {
            xpath = "/person[email/@type='work' and email/text() = '" + searchstring + "' ]" ;
         } else if ( searchtype.equals("homeaddress")) {
            xpath = "/person[address/@type='home' and address/text() = '" + searchstring + "' ]" ;
         } else if ( searchtype.equals("workaddress")) {
            xpath = "/person[address/@type='work' and address/text() = '" + searchstring + "' ]" ;
         }
         
         ResourceSet resultSet = service.query(xpath);
         ResourceIterator results = resultSet.getIterator();
         
         Group group = (Group)session.getAttribute("group");
         
         // Clear out group object...
         group.removeAll();
         
         // Add results to Group instance
         group.addResults(results);
         
      } catch(Exception e) {
         // Catch the exception and send the user to the error page
         if (e.getMessage() != null ) {
            response.sendRedirect("/Addressbook/error.jsp?error=" + URLEncoder.encode(e.getMessage()) );
         }
         else {
            response.sendRedirect("/Addressbook/error.jsp" );
         }
         e.printStackTrace();
      }
      
      return true;
   }

   public boolean xpathSearch(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
   
      Collection col = null;
      Vector vector = null;
      String xpath = null;
      
      try {
      
         HttpSession session = request.getSession(true);
   
         // Get a collection instance
         col = getCollection(request,response);
   
         XPathQueryService service = (XPathQueryService)col.getService("XPathQueryService",XMLDBAPIVERSION);
         
         // Get the seach parameters from the form
         String searchstring = request.getParameter("SEARCHSTRING");
         
         ResourceSet resultSet = service.query(searchstring);
         ResourceIterator results = resultSet.getIterator();
         
         Group group = (Group)session.getAttribute("group");
         
         // Clear out group object...
         group.removeAll();
         
         // Add results to Group instance
         group.addResults(results);
         
      } catch(Exception e) {
         // Catch the exception and send the user to the error page
         if (e.getMessage() != null ) {
            response.sendRedirect("/Addressbook/error.jsp?error=" + URLEncoder.encode(e.getMessage()) );
         }
         else {
            response.sendRedirect("/Addressbook/error.jsp" );
         }
         e.printStackTrace();
      }
      
      return true;
   }


}

