/*
 * 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.script;

import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.venice.iface.*;

public class LibraryVeniceCast
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public LibraryVeniceCast()
  { // do nothing
  } // end constructor

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

  private final Object query(Object obj, Class klass)
  {
    if (klass.isInstance(obj))
      return obj;
    if (obj instanceof ServiceProvider)
    { // try using ServiceProvider
      try
      { // query for this object as a service
	return ((ServiceProvider)obj).queryService(klass);

      } // end try
      catch (NoSuchServiceException e)
      { // do nothing with this exception
      } // end catch

    } // end if

    throw new ClassCastException("object is not an instance of " + klass.getName());

  } // end query

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final SecureObjectStore getGlobalBlocksStore(Request r)
  {
    ObjectProvider op = (ObjectProvider)(r.queryService(ObjectProvider.class));
    ServiceProvider sp = (ServiceProvider)(op.getObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"globals"));
    return (SecureObjectStore)(sp.queryService(SecureObjectStore.class,"blocks"));

  } // end getGlobalPropertiesStore

  public final SecureObjectStore getGlobalPropertiesStore(Request r)
  {
    ObjectProvider op = (ObjectProvider)(r.queryService(ObjectProvider.class));
    ServiceProvider sp = (ServiceProvider)(op.getObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"globals"));
    return (SecureObjectStore)(sp.queryService(SecureObjectStore.class,"properties"));

  } // end getGlobalPropertiesStore

  public final ButtonProvider queryButtonProvider(Object obj)
  {
    return (ButtonProvider)query(obj,ButtonProvider.class);

  } // end queryButtonProvider

  public final ContentBlockProvider queryContentBlockProvider(Object obj)
  {
    return (ContentBlockProvider)query(obj,ContentBlockProvider.class);

  } // end queryContentBlockProvider

  public final RenderImage queryRenderImage(Object obj)
  {
    return (RenderImage)query(obj,RenderImage.class);

  } // end queryRenderImage

  public final UserDefaultPropertyNamespace queryUserDefaultPropertyNamespace(Object obj)
  {
    return (UserDefaultPropertyNamespace)query(obj,UserDefaultPropertyNamespace.class);

  } // end queryUserDefaultPropertyNamespace

} // end class LibraryVeniceCast
