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: AddContact.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 adding Contact records to the database.
public class AddContact extends Action {

   // Add document to the datbase using values passed in via form-post
   public boolean add(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
   
      Collection col = null;
      String ourdoc = null;
      
      try {
      
         // Get a copy of the collection instance
         col = getCollection(request,response);
         
         // Grab all parameters from the form, store as strings
         String fname = request.getParameter("FNAME");
         String lname = request.getParameter("LNAME");
         String workphone = request.getParameter("WORKPHONE");
         String homephone = request.getParameter("HOMEPHONE");
         String cellphone = request.getParameter("CELLPHONE");
         String homeemail = request.getParameter("HOMEEMAIL");
         String workemail = request.getParameter("WORKEMAIL");
         String homeaddress = request.getParameter("HOMEADDRESS");
         String workaddress = request.getParameter("WORKADDRESS");
         
         // Create xml string from from values
         ourdoc = toXml(fname,lname,workphone,homephone,cellphone,homeemail,workemail,
                           homeaddress,workaddress);
   
         // Create the XMLResource and store the document
         XMLResource resource = (XMLResource) col.createResource( "", "XMLResource" );
         resource.setContent(ourdoc);
         col.storeResource(resource);
         
      } 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;
   }
   
}

