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

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

import java.net.*;

// Class for deleting records from the database
public class DeleteContact extends Action {

   // Delete the document with a key value of request.index
   public boolean delete(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
   
      Collection col = null;
   
      try {
         
         // Get a reference to the session
         HttpSession session = request.getSession(true);
   
         // Get a collection instance
         col = getCollection(request,response);
                  
         // Get the index we are going to delete
         int index = Integer.parseInt((String)request.getParameter("index"));
         
         // Pull the Group out of the session, and get the Person instance at the Index
         Group group = (Group)session.getAttribute("group");
         Person person = (Person)group.getPersonAt(index);
         
         // Get the documentKey for this particular Person
         String dockey = person.getDocKey();
         
         // Delete the document
         col.removeResource(col.getResource(dockey) );
         
      } 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;
   }
   
}

