Package addressbook

Source Code of addressbook.SearchContacts

package addressbook;

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: SearchContacts.java 581305 2007-10-02 16:55:49Z vgritsenko $
*/
import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.modules.XPathQueryService;

// 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;
      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) {
          e.printStackTrace();

          // there's not much else we can do if the response is committed
          if (response.isCommitted()) {
              return true;
          }

          // 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(), "UTF-8"));
          } else {
              response.sendRedirect("/addressbook/error.jsp");
          }
      }
     
      return true;
   }

   public boolean xpathSearch(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
  
       Collection col;
       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) {
           e.printStackTrace();

           // there's not much else we can do if the response is committed
           if (response.isCommitted()) {
               return true;
           }

           // 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(), "UTF-8"));
           } else {
               response.sendRedirect("/addressbook/error.jsp");
           }
       }
     
       return true;
   }
}
TOP

Related Classes of addressbook.SearchContacts

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.