Package addressbook

Source Code of addressbook.ListContacts

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: ListContacts.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 to list all contacts in the database
*/
public class ListContacts extends Action {

   public boolean list(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
  
      Collection col;
      try {
         // Get a reference to the session
         HttpSession session = request.getSession(true);
  
         // Retrieve the Group instance from the session
         Group group = (Group)session.getAttribute("group");
  
         // Get a collection instance
         col = getCollection(request,response);
  
         XPathQueryService service = (XPathQueryService)col.getService("XPathQueryService",XMLDBAPIVERSION);
  
         // Get all Person elements from the database
         ResourceSet resultSet = service.query("/person");
         ResourceIterator results = resultSet.getIterator();
        
         // Clear out group object...
         group.removeAll();
        
         // Add results of the Xpath query to the 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.ListContacts

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.