/*
  Copyright (C) 2006 Helge Hess

  This file is part of JOPE.

  JOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  JOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with JOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

package org.opengroupware.jope.appserver.publisher;

import org.opengroupware.jope.foundation.NSKeyValueCoding;
import org.opengroupware.jope.foundation.kvc.MissingPropertyException;

/*
 * JoObject
 * 
 * A JoObject is an object which exposes named child objects to the web. Per
 * default all KVC are exposed, so be careful and properly setup permissions.
 */
public interface JoObject {

  public Object lookupName(String _name, JoContext _ctx, boolean _acquire);

  /* utility class (use those methods in code to work on arbitary objects) */
  public static class Utility {
    
    public static Object lookupName
      (Object _self, String _name, JoContext _ctx, boolean _acquire)
    {
      if (_self == null)
        return null;
      
      if (_self instanceof JoObject)
        return ((JoObject)_self).lookupName(_name, _ctx, _acquire);
      
      /* no specific handlers, use default implementation */
      
      return DefaultImplementation.lookupName(_self, _name, _ctx, _acquire);
    }

    public static JoClass joClass(Object _self, JoContext _ctx) {
      if (_self == null)
        return null;
      
      /* try to discover joClass using KVC */
      
      try {
        return (JoClass)NSKeyValueCoding.DefaultImplementation
          .valueForKey(_self, "joClass");
      }
      catch (MissingPropertyException e) {
      }
      
      /* no specific handlers, use default implementation */
      
      return DefaultImplementation.joClass(_self, _ctx);
    }
  }

  /* This can be used by Object subclasses which want to implement
   * JoObject and need a fallback.
   */
  public static class DefaultImplementation {
    
    public static JoClass joClass(Object _self, JoContext _ctx) {
      if (_self == null)
        return null;
      
      if (_ctx == null)
        return null;
      
      /* default behaviour is to reflect on the Java class */
      JoClassRegistry classRegistry = _ctx.joClassRegistry();
      if (classRegistry != null)
        return classRegistry.joClassForJavaObject(_self.getClass(), _ctx);

      /* no context found */
      return null;
    }
    
    public static Object lookupName
      (Object _self, String _name, JoContext _ctx, boolean _acquire)
    {
      if (_self == null)
        return null;
      
      /* try to find name using KVC */
      // TODO: be sure to properly apply security checks!
      // TODO: maybe we do not want to map KVC to Jo 'names'
      
      try {
        return NSKeyValueCoding.DefaultImplementation.valueForKey(_self, _name);
      }
      catch (MissingPropertyException e) {
      }
      
      /* try to find name in JoClass */
      
      JoClass joClass = Utility.joClass(_self, _ctx);
      if (joClass != null)
        return joClass.lookupName(_self, _name, _ctx);
      
      /* not found */
      return null;
    }
  }
}
