/*****************************************************************************
 *                                                                           *
 *         Licensed Materials - Property of IBM                              *
 *                                                                           *
 *         "Restricted Materials of IBM"                                     *
 *                                                                           *
 *         5799-QRY                                                          *
 *                                                                           *
 *         (C) Copyright IBM Corp. 1998                                      *
 *                                                                           *
 *****************************************************************************/
package com.ibm.devtools.SIPNoTE;

import java.util.*;

/**
 * This class represents the registry entry for a user within the
 * SIPNoTE system.  It keeps track of the user, their locations, as
 * well as their subscription information.
 *
 * @author Bill Nagy
 * @see SNServer
 */
public class UserRecord
{
  String userid;
  Vector locations = new Vector();
  Vector subscriptions = new Vector();

  /**
   * Create a new user record for the user with the new location.
   *
   * @param userid The userid to be added
   * @param location The new location for this userid.
   */
  public UserRecord(String userid, HostRecord location)
  {
    this.userid = userid;
    if (DebugDefine.DEBUG)
      System.out.println(userid+" has been registered at "+location.toHostRecordString());
    locations.addElement(location);
  }

  /**
   * Adds a new location record for this user if it is not already present.
   *
   * @param location The location record to be added.
   */
  public void addLocation(HostRecord location)
  {
    if (locations.contains(location))
      {
	if (DebugDefine.DEBUG)
	  System.out.println(userid+" already registered at "+location.toHostRecordString());
	return;
      }

    if (DebugDefine.DEBUG)
      System.out.println(location.toHostRecordString()+" has been added for "+userid);
    locations.addElement(location);
    return;
  }

  /**
   * Add instance subscriptions to this user's record.  A subscription
   * will not be added again if it already exists.
   *
   * @param subs The array of strings with subscription information.
   */
  public void addSubscriptions(String[] subs)
  {
    int cnt;

    for(cnt = 0; cnt < subs.length; cnt++)
      {
	//Do we really want to upcase this?
	if (!subscriptions.contains(subs[cnt].toUpperCase()))
	  {
	    if (DebugDefine.DEBUG)
	      System.out.println("Adding Subscription: "+subs[cnt].toUpperCase());
	    subscriptions.addElement(subs[cnt].toUpperCase());
	  }
      }
    return;
  }

  /**
   * Checks to see if the user is subscribed to this instance.
   *
   * @param sub The subscription to check for.
   */
  public boolean isSubscribedTo(String sub)
  {
    return (subscriptions.contains(sub.toUpperCase())
	    || (subscriptions.contains("*")));
  }

  /**
   * Get a list of locations for this particular user.
   *
   * @return An enumeration of the locations.
   */
  public Enumeration getLocation()
  {
    return locations.elements();
  }

  /**
   * Remove a particular location from this user's record.
   *
   * @param location The location to be removed.
   * @return Number of locations remaining for this user.
   */
  public int removeLocation(HostRecord location)
  {
    if (!locations.removeElement(location))
      System.err.println("Error, attempted to remove location from UserRecord that wasn't there.");
    else
      if (DebugDefine.DEBUG)
	System.out.println(location.toHostRecordString()+" has been removed as a location for "+userid);

    return locations.size();
  }
  
}
