package $package;

#foreach($import in $imports)
import $import ;
#end

import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.component.StateHolder;

/**
 * Validator-Id ${validator.id}
 * ${validator.xmlEncodedDescription}
 */
public class $validator.simpleClassName extends $validator.superclass #if(${validator.stateHolderRequired} && !${validator.superclassImplementsStateHolder})implements StateHolder#end
{

  public static final String VALIDATOR_ID = "${validator.id}";

  /**
   *  Default no-args contstructor
   */ 
  public $validator.simpleClassName ()
  {
  }

// Validator properties fields
#set( $fieldCounter = 0 )
#foreach( $prop in $validator.properties )
#if( !$prop.exist )
  /**
   * $prop.name
   * ${prop.xmlEncodedDescription}
   */
#if($prop.simpleType)
  private $prop.simpleClassName  _$prop.name = ${prop.defaultvalue};		
  /**
   * Flag indicated that $prop.name is set.
   */
  private boolean _${prop.name}Set = false;	
#set( $fieldCounter = $fieldCounter+2)
#else
  private $prop.simpleClassName  _$prop.name = null; /* Default is ${prop.defaultvalue}*/
#set( $fieldCounter = $fieldCounter+1)
#end
#end	 
#end

// Getters and setters
#foreach( $prop in $validator.properties )
#if( !$prop.exist )
  /**
   * ${prop.xmlEncodedDescription}
   * Setter for $prop.name
   * @param $prop.name - new value
   */
  public void ${prop.setterName}( $prop.simpleClassName  __$prop.name )
  {
    this._${prop.name} = __$prop.name;
#if($prop.simpleType)
	this._${prop.name}Set = true;
#end
  }

  /**
   * ${prop.xmlEncodedDescription}
   * Getter for $prop.name
   * @return $prop.name value from local variable or value binding
   */
  public $prop.simpleClassName ${prop.getterName}()
  {
    ValueBinding vb = getValueBinding(_${prop.name});
#if($prop.simpleType)
    if(this._${prop.name}Set)
    {
	  return this._${prop.name};
    }
    if (vb != null) 
    {
      ${prop.boxingClass} value = (${prop.boxingClass}) vb.getValue(getFacesContext());
      if (null == value) 
      {
        return this._${prop.name};
      }
      //return (value.${prop.classname}Value());
      } else 
      {
        return (this._${prop.name});
      }
    }
#else
    if (null != this._${prop.name}) 
    {
      return this._${prop.name};
    }
#if( !$prop.isInstanceof("javax.faces.el.MethodBinding") && !$prop.isInstanceof("javax.faces.el.ValueBinding"))
    else if (null != vb)
    {
      return (${prop.simpleClassName})vb.getValue(getFacesContext());
    }
#end
    else
    {
      return ${prop.defaultvalue};
    }
  }
#end
#if($prop.raw)
  /**
   * ${prop.xmlEncodedDescription}
   * Raw Setter for $prop.name
   * @param $prop.name - new value
   */
  public void ${prop.setterName}Raw( $prop.simpleClassName  __$prop.name )
  {
    this._${prop.name} = __$prop.name;
  }
  /*
   * ${prop.xmlEncodedDescription}
   * Raw ( bypadd bindings ) Getter for $prop.name
   * @return $prop.name value from local variable 
   */
  public $prop.simpleClassName ${prop.getterName}Raw()
  {
    return this._${prop.name};
  }
#end
#end	 
#end


#if($validator.stateHolderRequired)
	
  // Save state
  // ----------------------------------------------------- StateHolder Methods

  private boolean _transient;
#if(${validator.superclassImplementsStateHolder})
#set($currentField=1)
#set( $fieldCounter = $fieldCounter+1)
#else
#set($currentField=0)
#end
  public Object saveState(FacesContext context) {
#if(${validator.superSaveStateMethodExists})
    Object values[] = new Object[${fieldCounter}];
    values[0] = super.saveState(context);
#else
    Object values[] = new Object[${fieldCounter}];
#end
#foreach( $prop in $validator.properties )
#if( !$prop.exist )
#if($prop.simpleType)		
    values[${currentField}] = new ${prop.boxingClass}(_$prop.name);
#set( $currentField = $currentField+1)
    values[${currentField}] = Boolean.valueOf(_${prop.name}Set);	
#set( $currentField = $currentField+1)
#else
#if(!${prop.attachedstate} )
    values[${currentField}] = _$prop.name;
#else
    values[${currentField}] = saveAttachedState(context, _$prop.name );		
#end
#set( $currentField = $currentField+1)
#end	
#end	 
#end
    return values;
  }
   
#if(${validator.superclassImplementsStateHolder})
#set($currentField=1)
#else
#set($currentField=0)
#end
  public void restoreState(FacesContext context, Object state) {
#if(${validator.superRestoreStateMethodExists})
    Object values[] = (Object[]) state;
    super.restoreState(context, values[0]);
#else
    Object values[] = (Object[]) state;
#end
#foreach( $prop in $validator.properties )
#if( !$prop.exist )
#if($prop.simpleType)		
    _$prop.name = ((${prop.boxingClass})values[${currentField}]).${prop.classname}Value();
#set( $currentField = $currentField+1)
    _${prop.name}Set = ((Boolean)values[${currentField}]).booleanValue();	
#set( $currentField = $currentField+1)
#else
#if( !${prop.attachedstate} )
    _$prop.name = (${prop.simpleClassName})values[${currentField}] ;
#else
    _$prop.name = (${prop.simpleClassName})restoreAttachedState(context,values[${currentField}] );		
#end
#set( $currentField = $currentField+1)
#end
	
#end	 
#end
  }
  
#if(!$validator.superIsTransientMethodExists)
  public boolean isTransient()
  {
    return _transient;
  }
#end

#if(!$validator.superSetTransientMethodExists)
  public void setTransient(boolean newTransientValue)
  {
    this._transient = newTransientValue;
  }
#end

#end
	

// Utilites

  private FacesContext getFacesContext()
  {
    return FacesContext.getCurrentInstance();
  }
  
  private ValueBinding getValueBinding(String name)
  {
    return getFacesContext().getApplication().createValueBinding(name);
  }

}