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

import java.text.ParseException;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opengroupware.jope.appserver.core.WOAssociation;
import org.opengroupware.jope.appserver.core.WOContext;
import org.opengroupware.jope.appserver.core.WOElement;
import org.opengroupware.jope.appserver.core.WORequest;

/*
 * WOInput
 * 
 * Abstract superclass for elements which participate in FORM processing.
 * 
 * Bindings:
 *   name     [in] - string
 *   value    [io] - object
 *   disabled [in] - boolean
 */
public abstract class WOInput extends WOHTMLDynamicElement {
  protected static final Log log = LogFactory.getLog("WOForms");
  
  protected WOAssociation name;
  protected WOAssociation value;
  protected WOAssociation disabled;

  public WOInput(String _name, Map<String, WOAssociation> _assocs,
                 WOElement _template)
  {
    super(_name, _assocs, _template);
    
    this.name     = grabAssociation(_assocs, "name");
    this.value    = grabAssociation(_assocs, "value");
    this.disabled = grabAssociation(_assocs, "disabled");
    
    /* type is defined by the element itself ... */
    grabAssociation(_assocs, "type");
    
    // TODO: warn against NAME association?
  }
  
  /* methods */
  
  protected String elementNameInContext(WOContext _ctx) {
    if (this.name == null) {
      if (log.isDebugEnabled()) log.debug("input has no name binding, use eid");
      return _ctx.elementID();
    }
    
    String s = this.name.stringValueInComponent(_ctx.cursor());
    if (s != null) return s;
    
    if (log.isDebugEnabled())
      log.debug("name binding of input is null, use eid");
    return _ctx.elementID();
  }

  /* taking form values */
  
  @SuppressWarnings("unused")
  protected Object parseFormValue(Object _value, WOContext _ctx)
    throws ParseException
  {
    /* redefined in subclasses */
    return _value;
  }
  
  @Override
  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    Object cursor = _ctx.cursor();
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(cursor))
        return;
    }
    
    if (this.value == null) { /* nothing to push to */
      log.info("missing value binding for element: " + this);
      return;
    }
    if (!this.value.isValueSettableInComponent(cursor)) {
      log.info("value binding cannot be set for element: " + this);
      return;
    }
    
    String formName  = this.elementNameInContext(_ctx);
    Object formValue = _rq.formValueForKey(formName);
    
    if (formValue == null) {
      /* This one is tricky
       * 
       * This only pushes values if the request actually specified a value
       * for this field. For example if you have a WOTextField with name
       * 'q', this will push a value to the field:
       *   /Page/default?q=abc
       * but this won't:
       *   /Page/default
       * To push an empty value, you need to use
       *   /Page/default?q=
       * 
       * TODO: does just q work as well?
       * 
       * Note: checkboxes do NOT submit values when they are not checked,
       *       so they need special handling! (otherwise you won't be able
       *       to "uncheck" a checkbox. Usually you do this at the top of
       *       your component.
       */
      if (log.isDebugEnabled())
        log.debug("got not form value for form: " + formName);
      return;
    }
    
    try {
      formValue = this.parseFormValue(formValue, _ctx);
    }
    catch (ParseException e) {
      // TODO: add to some 'error report' object?
      log.warn("failed to parse form value with Format: " + formValue, e);
    }
    
    if (log.isDebugEnabled()) {
      log.debug("push field " + formName + " value: " + formValue + 
                     " => " + this.value);
    }
    this.value.setValue(formValue, cursor);
  }
  
  /* description */
  
  @Override
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    this.appendAssocToDescription(_d, "name",  this.name);
    this.appendAssocToDescription(_d, "value", this.value);
  }  
}
