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

import java.util.*;
import org.apache.log4j.Logger;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

public class PropertyUtils
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(PropertyUtils.class);

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

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

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

  public static boolean copyProperty(ObjectStore dest, ObjectProvider src, String namespace, String name)
  {
    try
    { // copy object from provider to store
      dest.setObject(namespace,name,src.getObject(namespace,name));
      return true;

    } // end try
    catch (NoSuchObjectException e)
    { // property not found in source
      logger.warn("caught NoSuchObjectException",e);
      return false;

    } // end catch
    catch (ObjectStoreException e)
    { // error setting property into destination
      logger.warn("caught ObjectStoreException",e);
      return false;

    } // end catch

  } // end copyProperty

  public static boolean copyProperty(SecureObjectStore dest, ObjectProvider src, DynamoUser caller,
				     String namespace, String name) throws DynamoSecurityException
  {
    try
    { // copy object from provider to secure store
      dest.setObject(caller,namespace,name,src.getObject(namespace,name));
      return true;

    } // end try
    catch (NoSuchObjectException e)
    { // property not found in source
      logger.warn("caught NoSuchObjectException",e);
      return false;

    } // end catch
    catch (DatabaseException e)
    { // unable to store property in destination
      logger.warn("caught DatabaseException",e);
      return false;

    } // end catch

  } // end copyProperty

  public static boolean copyPropertySet(ObjectStore dest, ObjectProvider src, String namespace,
					Collection names)
  {
    boolean rc = true;
    Iterator it = names.iterator();
    while (it.hasNext())
    { // copy all the named properties
      String n = it.next().toString();
      rc = copyProperty(dest,src,namespace,n) && rc;

    } // end while

    return rc;

  } // end copyPropertySet

  public static boolean copyPropertySet(SecureObjectStore dest, ObjectProvider src, DynamoUser caller,
					String namespace, Collection names) throws DynamoSecurityException
  {
    boolean rc = true;
    Iterator it = names.iterator();
    while (it.hasNext())
    { // copy all the named properties
      String n = it.next().toString();
      rc = copyProperty(dest,src,caller,namespace,n) && rc;

    } // end while

    return rc;

  } // end copyPropertySet

  public static boolean copyPropertyNamespace(ObjectStore dest, ObjectStore src, String namespace)
  {
    Collection coll = null;
    try
    { // get all the names to copy from one namespace to another
      coll = src.getNamesForNamespace(namespace);

    } // end try
    catch (ObjectStoreException e)
    { // failed to get list of names
      return false;

    } // end catch

    return copyPropertySet(dest,src,namespace,coll);

  } // end copyPropertyNamespace

  public static boolean copyPropertyNamespace(ObjectStore dest, SecureObjectStore src, String namespace)
  {
    Collection coll = null;
    try
    { // get all the names to copy from one namespace to another
      coll = src.getNamesForNamespace(namespace);

    } // end try
    catch (DatabaseException e)
    { // failed to get list of names
      return false;

    } // end catch

    return copyPropertySet(dest,src,namespace,coll);

  } // end copyPropertyNamespace

  public static boolean copyPropertyNamespace(SecureObjectStore dest, ObjectStore src, DynamoUser caller,
					      String namespace) throws DynamoSecurityException
  {
    Collection coll = null;
    try
    { // get all the names to copy from one namespace to another
      coll = src.getNamesForNamespace(namespace);

    } // end try
    catch (ObjectStoreException e)
    { // failed to get list of names
      return false;

    } // end catch

    return copyPropertySet(dest,src,caller,namespace,coll);

  } // end copyPropertyNamespace

  public static boolean copyPropertyNamespace(SecureObjectStore dest, SecureObjectStore src, DynamoUser caller,
					      String namespace) throws DynamoSecurityException
  {
    Collection coll = null;
    try
    { // get all the names to copy from one namespace to another
      coll = src.getNamesForNamespace(namespace);

    } // end try
    catch (DatabaseException e)
    { // failed to get list of names
      return false;

    } // end catch

    return copyPropertySet(dest,src,caller,namespace,coll);

  } // end copyPropertyNamespace

  public static boolean copyPropertyNamespaces(ObjectStore dest, ObjectStore src, Collection namespaces)
  {
    boolean rc = true;
    Iterator it = namespaces.iterator();
    while (it.hasNext())
    { // copy each namespace in turn
      String ns = it.next().toString();
      rc = copyPropertyNamespace(dest,src,ns) && rc;

    } // end while

    return rc;

  } // end copyPropertyNamespaces

  public static boolean copyPropertyNamespaces(ObjectStore dest, SecureObjectStore src, Collection namespaces)
  {
    boolean rc = true;
    Iterator it = namespaces.iterator();
    while (it.hasNext())
    { // copy each namespace in turn
      String ns = it.next().toString();
      rc = copyPropertyNamespace(dest,src,ns) && rc;

    } // end while

    return rc;

  } // end copyPropertyNamespaces

  public static boolean copyPropertyNamespaces(SecureObjectStore dest, ObjectStore src, DynamoUser caller,
					       Collection namespaces) throws DynamoSecurityException
  {
    boolean rc = true;
    Iterator it = namespaces.iterator();
    while (it.hasNext())
    { // copy each namespace in turn
      String ns = it.next().toString();
      rc = copyPropertyNamespace(dest,src,caller,ns) && rc;

    } // end while

    return rc;

  } // end copyPropertyNamespaces

  public static boolean copyPropertyNamespaces(SecureObjectStore dest, SecureObjectStore src,
					       DynamoUser caller, Collection namespaces)
      throws DynamoSecurityException
  {
    boolean rc = true;
    Iterator it = namespaces.iterator();
    while (it.hasNext())
    { // copy each namespace in turn
      String ns = it.next().toString();
      rc = copyPropertyNamespace(dest,src,caller,ns) && rc;

    } // end while

    return rc;

  } // end copyPropertyNamespaces

  public static void setIfNonNull(ObjectStore store, String namespace, String name, Object value)
  {
    if (value!=null)
      store.setObject(namespace,name,value);

  } // end setIfNonNull

  public static void setIfNonNull(SecureObjectStore store, DynamoUser caller, String namespace, String name,
				  Object value) throws DatabaseException, DynamoSecurityException
  {
    if (value!=null)
      store.setObject(caller,namespace,name,value);

  } // end setIfNonNull

  public static void setOrRemove(ObjectStore store, String namespace, String name, Object value)
  {
    if (value!=null)
      store.setObject(namespace,name,value);
    else
      store.removeObject(namespace,name);

  } // end setOrRemove

  public static void setOrRemove(SecureObjectStore store, DynamoUser caller, String namespace, String name,
				 Object value) throws DatabaseException, DynamoSecurityException
  {
    if (value!=null)
      store.setObject(caller,namespace,name,value);
    else
      store.removeObject(caller,namespace,name);

  } // end setOrRemove

  public static boolean hasProperty(ObjectProvider prov, String namespace, String name)
  {
    try
    { // try to get it
      prov.getObject(namespace,name);

    } // end try
    catch (NoSuchObjectException e)
    { // we don't have the property - bail out
      return false;

    } // end catch

    return true;

  } // end hasProperty

  public static Object getPropertyNoErr(ObjectProvider prov, String namespace, String name)
  {
    try
    { // try to get it
      return prov.getObject(namespace,name);

    } // end try
    catch (NoSuchObjectException e)
    { // we don't have the property - bail out
      return null;

    } // end catch

  } // end getPropertyNoErr

} // end class PropertyUtils
