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: Group.java,v 1.1 2001/08/08 04:56:14 kstaken Exp $
 */
import java.util.Vector;
import java.util.Hashtable;

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

import org.w3c.dom.*;
import org.apache.xpath.*;
import org.apache.xpath.objects.*;

/**
 * Class for holding groups of the Person class
 */
public class Group {

   private Vector personList = new Vector();
   private int index = 0;

   /**
    * Returns current index   
    */
   public int getIndex() {
      return index - 1;
   }

   /**
    * Return the next person from the list 
    */
   public Person getNext() {
      Person person = (Person) personList.get(index);
      index = index + 1;
      return person;
   }

   /**
    * Return the person element at a certain index
    */
   public Person getPersonAt(int i) {
      return (Person) personList.get(i);
   }

   /**
    * Add person to Group
    */
   public void add(Person person) {
      personList.add(person);
   }

   /**
    * Add Persons from Result set
    */
   public void addResults(ResourceIterator results) {
      try {
         while (results.hasMoreResources()) {

            // Create a new person instance to add to Group
            Person person = new Person();

            // Retrieve the next node
            XMLResource resource = (XMLResource) results.nextResource();
            // Cast to Dom Node
            Node originalnode = resource.getContentAsDOM();

            // Get the document key for this node for later use            
            String dockey = resource.getDocumentId();
            person.setDocKey(dockey);

            // Use Xalan xpath parser to extract data, store into hastable
            XObject xo = new XObject();

            xo = XPathAPI.eval(originalnode, "/person/fname");
            person.setFname(xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/lname");
            person.setLname(xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/phone[@type='work']");
            person.setWorkPhone(xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/phone[@type='home']");
            person.setHomePhone(xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/phone[@type='cell']");
            person.setCellPhone(xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/email[@type='home']");
            person.setHomeEmail(xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/email[@type='work']");
            person.setWorkEmail(xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/address[@type='home']");
            person.setHomeAddress(xo.toString());

            xo = XPathAPI.eval(originalnode, "/person/address[@type='work']");
            person.setWorkAddress(xo.toString());

            // Add person to group
            this.add(person);
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }

   }

   /**
    * Remove Person from Group 
    */
   public void remove(int i) {
      personList.remove(i);
   }

   /**
    * Remove all Persons from Group 
    */
   public void removeAll() {
      personList.removeAllElements();
   }

   /**
    * Return the size of the Group     
    */
   public int sizeOf() {
      return personList.size();
   }

   /**
    * Reset counter for Person retrieval
    */
   public void reset() {
      index = 0;
   }

   /**
    * Return true if more persons exist
    */
   public boolean hasMorePersons() {
      return index < personList.size();
   }
}

