package org.opengroupware.jope.woextensions;

import java.util.Map;

import org.opengroupware.jope.appserver.WOActionResults;
import org.opengroupware.jope.appserver.WOAssociation;
import org.opengroupware.jope.appserver.WOContext;
import org.opengroupware.jope.appserver.WODynamicElement;
import org.opengroupware.jope.appserver.WOElement;
import org.opengroupware.jope.appserver.WORequest;
import org.opengroupware.jope.appserver.WOResponse;

public class WOMethodInvocation extends WODynamicElement {
  
  protected WOAssociation invoke;
  protected WOElement     template;

  public WOMethodInvocation
    (String _name, Map<String, WOAssociation> _assocs, WOElement _template)
  {
    super(_name, _assocs, _template);
    this.invoke   = grabAssociation(_assocs, "invoke");
    this.template = _template;
  }
  
  /* responder */

  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    if (this.invoke != null)
      this.invoke.valueInComponent(_ctx.cursor());
    if (this.template != null)
      this.template.takeValuesFromRequest(_rq, _ctx);
  }

  public WOActionResults invokeAction(WORequest _rq, WOContext _ctx) {
    if (this.invoke != null)
      this.invoke.valueInComponent(_ctx.cursor());
    if (this.template != null)
      return this.template.invokeAction(_rq, _ctx);
    return null;
  }
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    if (this.invoke != null)
      this.invoke.valueInComponent(_ctx.cursor());
    if (this.template != null)
      this.template.appendToResponse(_r, _ctx);
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    this.appendAssocToDescription(_d, "invoke", this.invoke);
  }  
}
