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

/**
Servlet class for the addressbook

This class acts as a controller, forwarding HTML request onto the correct class/jsp page
depending on the value of request.action

*/
public class Task extends HttpServlet {

   public void doPost(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

      JspFactory jspFactory = null;
      PageContext pageContext = null;
      HttpSession session = null;
      Addressbook.Group group = null;

      String action = null;
      
      try {
      
         // If the Group bean hasn't been added to the session alerady, add it
         // this allows the user to go directly to ANY page
         jspFactory = JspFactory.getDefaultFactory();
         pageContext = jspFactory.getPageContext(this, request, response, "/error.jsp", true, 8192, true );
         session = pageContext.getSession();
         
         group = (Group)pageContext.getAttribute("group",PageContext.SESSION_SCOPE);
         if (group == null) {
            group=(Addressbook.Group)Beans.instantiate(this.getClass().getClassLoader(), "Addressbook.Group" );
            pageContext.setAttribute("group", group, PageContext.SESSION_SCOPE );
         }

      
         // Since case matters, we need to check for both....
         if ( request.getParameter("action") != null ) {
            action = request.getParameter("action");
         }
         else {
            action = request.getParameter("ACTION");
         }
         
         // Decide where to send the user depending on value of action
         if ( action != null ) {
            // Add a Contact
            if ( action.equalsIgnoreCase("addform") ) {
               gotoPage("/addContact.jsp",request,response);
            } else if ( action.equalsIgnoreCase("addcontact") ) {
               AddContact contact = new AddContact();
               contact.add(request,response);
               
               ListContacts lcontact = new ListContacts();
               lcontact.list(request,response);
               gotoPage("/listContacts.jsp",request,response);
               
            // List all contacts
            } else if ( action.equalsIgnoreCase("listcontacts") ) {
               ListContacts contact = new ListContacts();
               contact.list(request,response);
               gotoPage("/listContacts.jsp",request,response);
            
            // Show individual Contact detail
            } else if ( action.equalsIgnoreCase("showcontact") ) {
               gotoPage("/showContact.jsp",request,response);

            // Show individual XML file
            } else if ( action.equalsIgnoreCase("showxml") ) {
               gotoPage("/showXML.jsp",request,response);
                              
            // Edit a contact
            } else if ( action.equalsIgnoreCase("editcontactform") ) {
               gotoPage("/editContactForm.jsp",request,response);
            } else if ( action.equalsIgnoreCase("editcontact") ) {
               EditContact contact = new EditContact();
               contact.edit(request,response);
               
               ListContacts lcontact = new ListContacts();
               lcontact.list(request,response);
               gotoPage("/listContacts.jsp",request,response);
            
            // Search for a contact
            } else if ( action.equalsIgnoreCase("searchform") ) {
               gotoPage("/searchContacts.jsp",request,response);
            } else if ( action.equalsIgnoreCase("attributesearch") ) {
               SearchContacts contact = new SearchContacts();
               contact.attributeSearch(request,response);
               gotoPage("/listContacts.jsp",request,response);
            } else if ( action.equalsIgnoreCase("xpathsearch") ) {
               SearchContacts contact = new SearchContacts();
               contact.xpathSearch(request,response);
               gotoPage("/listContacts.jsp",request,response);
            
            // Delete a contact
            } else if ( action.equalsIgnoreCase("deletecontact") ) {
               DeleteContact contact = new DeleteContact();
               contact.delete(request,response);
               
               ListContacts lcontact = new ListContacts();
               lcontact.list(request,response);
               gotoPage("/listContacts.jsp",request,response);
               
            } else gotoPage("/error.jsp",request,response);
         }
         else {
            gotoPage("/error.jsp",request,response);
         }
         
      } 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();
      }
   }
   
   // Treat a form-get as a post, seding user to doPost
   public void doGet(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
   
      doPost(request,response);
   
   }

   // Forward the user on to the pages specified in address
   private void gotoPage( String address, HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
   
      RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
      dispatcher.forward(request, response);
   
   }

}

