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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opengroupware.jope.appserver.associations.WOKeyPathAssociation;
import org.opengroupware.jope.appserver.associations.WOValueAssociation;
import org.opengroupware.jope.foundation.NSJavaRuntime;
import org.opengroupware.jope.foundation.NSObject;

public abstract class WOAssociation extends NSObject {
  protected static final Log log = LogFactory.getLog("WOAssociations");

  public static WOAssociation associationWithKeyPath(String _keyPath) {
    return new WOKeyPathAssociation(_keyPath);
  }
  
  public static WOAssociation associationWithValue(Object _value) {
    return new WOValueAssociation(_value);
  }
    
  /* accessors */
  
  public abstract String keyPath();
  
  /* reflection */
  
  public boolean isValueConstant() {
    return false;
  }
  
  public boolean isValueSettable() {
    return true;
  }
  
  public boolean isValueConstantInComponent(Object _cursor) {
    return this.isValueConstant();
  }
  
  public boolean isValueSettableInComponent(Object _cursor) {
    return this.isValueSettable();
  }
  
  /* values */
  
  public void setValue(Object _value, Object _cursor) {
    /* Note: crappy name due to WO compatibility */
  }
  
  public Object valueInComponent(Object _cursor) {
    return null;
  }
  
  /* specific values */
  
  public void setBooleanValue(boolean _value, Object _cursor) {
    this.setValue(new Boolean(_value), _cursor);
  }
  public boolean booleanValueInComponent(Object _cursor) {
    return NSJavaRuntime.boolValueForObject(this.valueInComponent(_cursor));
  }
  
  public void setIntValue(int _value, Object _cursor) {
    this.setValue(new Integer(_value), _cursor);
  }
  public int intValueInComponent(Object _cursor) {
    return NSJavaRuntime.intValueForObject(this.valueInComponent(_cursor));
  }
  
  public void setStringValue(String _value, Object _cursor) {
    this.setValue(_value, _cursor);
  }
  public String stringValueInComponent(Object _cursor) {
    Object v = this.valueInComponent(_cursor);
    
    if (v == null)
      return null;
    
    if (v instanceof String)
      return (String)v;
    
    return v.toString();    
  }
}
