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

import java.util.Set;

/*
 * EONotQualifier
 * 
 * Negates the value of the contained qualifier.
 * 
 * Syntax:
 *   NOT qualifier
 *   NOT lastname = 'Duck'
 */
public class EONotQualifier extends EOQualifier
  implements EOQualifierEvaluation
{

  protected EOQualifier qualifier = null;
  
  public EONotQualifier(EOQualifier _q) {
    this.qualifier = _q;
  }
  
  /* accessors */
  
  public EOQualifier qualifier() {
    return this.qualifier;
  }
  
  /* evaluation */
  
  public boolean evaluateWithObject(Object _object) {
    return !((EOQualifierEvaluation)this.qualifier).evaluateWithObject(_object);
  }
  
  /* keys */
  
  public void addQualifierKeysToSet(Set<String> _keys) {
    if (this.qualifier != null)
      this.qualifier.addQualifierKeysToSet(_keys);
  }
  
  /* bindings */
  
  public void addBindingKeysToSet(Set<String> _keys) {
    if (this.qualifier != null)
      this.qualifier.addBindingKeysToSet(_keys);
  }
  
  public String keyPathForBindingKey(String _variable) {
    return this.qualifier.keyPathForBindingKey(_variable);
  }
  
  public EOQualifier qualifierWithBindings(Object _vals, boolean _requiresAll) {
    if (this.qualifier == null)
      return null;
    
    EOQualifier boundQualifier =
      this.qualifier.qualifierWithBindings(_vals, _requiresAll);
    
    if (boundQualifier == this.qualifier)
      return this; /* did not change */
    
    return new EONotQualifier(boundQualifier);
  }
  
  /* string representation */
  
  public boolean appendStringRepresentation(StringBuffer _sb) {
    if (this.qualifier == null) return false; 
    _sb.append("NOT ");
    return this.qualifier.appendStringRepresentation(_sb);
  }
  
  /* description */

  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    _d.append(" qualifier=" + this.qualifier);
  }
}
