/*
 * 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.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;
import com.silverwrist.venice.VeniceNamespaces;

public class DefaultDateFormatterBottomHalf extends DateFormatterBottomHalf
{
  /*--------------------------------------------------------------------------------
   * 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 Locale m_locale;                    // the locale we're guarding
  private TimeZone m_timezone;                // the timezone we're guarding

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

  public DefaultDateFormatterBottomHalf(ServiceProvider services)
  {
    // Get the global object provider.
    ObjectProvider props = null;
    try
    { // get the "properties" service from that object
      ServiceProvider gdm_sp =
          (ServiceProvider)(GetObjectUtils.getDynamoComponent(services,ServiceProvider.class,"globals"));
      props = (ObjectProvider)(gdm_sp.queryService(ObjectProvider.class,"properties"));

    } // end try
    catch (ConfigException e)
    { // this shouldn't happen, but...
      m_locale = Locale.getDefault();
      m_timezone = new SimpleTimeZone(0,"UTC");
      return;

    } // end catch
    catch (NoSuchServiceException e)
    { // this shouldn't happen, but...
      m_locale = Locale.getDefault();
      m_timezone = new SimpleTimeZone(0,"UTC");
      return;

    } // end catch

    try
    { // get the default user locale
      m_locale = (Locale)(props.getObject(NAMESPACE,PROP_LOCALE));

    } // end try
    catch (NoSuchObjectException e)
    { // fall back on error
      m_locale = Locale.getDefault();

    } // end catch

    try
    { // get the default user timezone
      m_timezone = (TimeZone)(props.getObject(NAMESPACE,PROP_TIMEZONE));

    } // end try
    catch (NoSuchObjectException e)
    { // fall back on error
      m_timezone = new SimpleTimeZone(0,"UTC");

    } // end catch

  } // end constructor

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

  Locale getLocale()
  {
    return m_locale;

  } // end getLocale

  TimeZone getTimeZone()
  {
    return m_timezone;

  } // end getTimeZone

} // end class DefaultDateFormatterBottomHalf
