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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opengroupware.jope.eocontrol.EOQualifier;
import org.opengroupware.jope.foundation.NSObject;

/*
  NGRule

  This class represents a rule inside the rule-model. A rule conceptually has:
  - a qualifier - aka lhs (the condition which must be true for the rule)
  - an action   - aka rhs (the "thing" which is performed when the rule is
                           triggered)
  - a priority  - to select a rule if multiple ones match
  
  String Representation:
    qualifer => action [; priority]
    *true*   => action
  
  Example:
    "(request.isXmlRpcRequest = YES) => dispatcher = XmlRpc ;0"
*/
public class Rule extends NSObject
  implements RuleCandidate, RuleAction
{
  protected static Log log = LogFactory.getLog("JoRules");
  
  protected EOQualifier qualifier;
  protected Object      action;
  protected int         priority;

  public Rule(EOQualifier _qualifier, Object _action, int _priority) {
    this.qualifier = _qualifier;
    this.action    = _action;
    this.priority  = _priority;
  }
  
  /* accessors */

  public void setQualifier(EOQualifier _q) {
    this.qualifier = _q;
  }
  public EOQualifier qualifier() {
    return this.qualifier;
  }
  
  public void setAction(Object _action) {
    this.action = _action;
  }
  public Object action() {
    return this.action;
  }
  
  public void setPriority(int _value) {
    this.priority = _value;
  }
  public int priority() {
    return this.priority;
  }

  /* operations */
  
  public boolean isCandidateForKey(String _key) {
    if (_key == null)
      return true;
    
    Object o = this.action();
    if (o == null)
      return false;
    
    if (o instanceof RuleCandidate)
      return ((RuleCandidate)o).isCandidateForKey(_key);

    return false;
  }
  
  public Object fireInContext(RuleContext _ctx) {
    Object o = this.action();
    if (o == null)
      return null;
    
    if (o instanceof RuleAction)
      return ((RuleAction)o).fireInContext(_ctx);
    
    return null;
  }
  
  /* representations */
  
  public String stringRepresentation() {
    StringBuffer sb = new StringBuffer(256);
    sb.append(this.qualifier.stringRepresentation());
    
    sb.append(" => ");
    
    if (this.action instanceof RuleAssignment)
      sb.append(((RuleAssignment)this.action).stringRepresentation());
    else
      sb.append(this.action.toString());
    
    sb.append(" ; ");
    sb.append(this.priority);
    return sb.toString();
  }
  
  /* description */
  
  @Override
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    if (this.qualifier != null)
      _d.append(" q=" + this.qualifier);
    
    if (this.action != null)
      _d.append(" action=" + this.action);

    _d.append(" priority=" + this.priority);
  }
}
