/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.content;

import java.util.*;
import com.silverwrist.dynamo.event.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.venice.VeniceNamespaces;

public class UserDateFormatterBottomHalf extends DateFormatterBottomHalf
    implements SessionValueBindListener, DynamicUpdateListener
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String NAMESPACE = VeniceNamespaces.USER_SETTINGS_NAMESPACE;
  private static final String PROP_LOCALE = "locale";
  private static final String PROP_TIMEZONE = "timezone";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int m_uid;                          // UID we're linked to
  private ObjectProvider m_props;             // source for user properties
  private Locale m_locale;                    // the locale we're guarding
  private TimeZone m_timezone;                // the timezone we're guarding
  private ComponentShutdown m_shut_listener;  // shutdown event listener

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public UserDateFormatterBottomHalf(Request r, DynamoUser user)
  {
    super();
    m_uid = user.getUID();
    m_props = user;

    // Get the initial values we're guarding.
    m_locale = retrieveLocale();
    m_timezone = retrieveTimeZone();

    // Set up a listener to monitor for changes in those values.
    EventListenerRegistration reg =
        (EventListenerRegistration)(r.queryService(EventListenerRegistration.class));
    m_shut_listener = reg.registerDynamicUpdateListener(UserPropertyUpdateEvent.class,this);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private final void destroy()
  {
    m_shut_listener.shutdown();
    m_shut_listener = null;
    m_props = null;

  } // end destroy

  private final Locale retrieveLocale()
  {
    return (Locale)(m_props.getObject(NAMESPACE,PROP_LOCALE));

  } // end retrieveLocale

  private final TimeZone retrieveTimeZone()
  {
    return (TimeZone)(m_props.getObject(NAMESPACE,PROP_TIMEZONE));

  } // end retrieveTimeZone

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class DateFormatterBottomHalf
   *--------------------------------------------------------------------------------
   */

  Locale getLocale()
  {
    return m_locale;

  } // end getLocale

  TimeZone getTimeZone()
  {
    return m_timezone;

  } // end getTimeZone

  /*--------------------------------------------------------------------------------
   * Implementations from interface SessionValueBindListener
   *--------------------------------------------------------------------------------
   */

  public void valueAdded(SessionValueBindEvent event)
  { // do nothing
  } // end valueAdded

  public void valueRemoved(SessionValueBindEvent event)
  {
    destroy();

  } // end valueRemoved

  public void valueReplaced(SessionValueBindEvent event)
  {
    destroy();

  } // end valueReplaced

  /*--------------------------------------------------------------------------------
   * Implementations from interface DynamicUpdateListener
   *--------------------------------------------------------------------------------
   */

  public void updateReceived(DynamicUpdateEvent evt)
  {
    UserPropertyUpdateEvent event = (UserPropertyUpdateEvent)evt;
    if ((event.getUID()==m_uid) && NAMESPACE.equals(event.getPropertyNamespace()))
    { // try to update our properties
      try
      { // match one of the two property names
	if (PROP_LOCALE.equals(event.getPropertyName()))
	{ // re-get the locale
	  Locale loc = retrieveLocale();
	  m_locale = loc;

	} // end if
	else if (PROP_TIMEZONE.equals(event.getPropertyName()))
	{ // re-get the timezone
	  TimeZone tz = retrieveTimeZone();
	  m_timezone = tz;

	} // end else if

      } // end try
      catch (NoSuchObjectException e)
      { // do nothing here
      } // end catch

    } // end if

  } // end updateReceived

} // end class UserDateFormatterBottomHalf
