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 511428 2007-02-25 03:30:52Z 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 = 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) {
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()) );
}
else {
response.sendRedirect("/addressbook/error.jsp" );
}
}
return true;
}
public boolean xpathSearch(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
Collection col = 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) {
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()) );
}
else {
response.sendRedirect("/addressbook/error.jsp" );
}
}
return true;
}
}