package net.sf.jasperreports.engine;

import java.util.Map;
import net.sf.jasperreports.engine.fill.JRFillField;
import net.sf.jasperreports.engine.fill.JRFillGroup;
import net.sf.jasperreports.engine.fill.JRFillParameter;
import net.sf.jasperreports.engine.fill.JRFillVariable;

public abstract class JRAbstractScriptlet {
  protected Map parametersMap = null;
  
  protected Map fieldsMap = null;
  
  protected Map variablesMap = null;
  
  protected JRFillGroup[] groups = null;
  
  public void setData(Map parsm, Map fldsm, Map varsm, JRFillGroup[] grps) {
    this.parametersMap = parsm;
    this.fieldsMap = fldsm;
    this.variablesMap = varsm;
    this.groups = grps;
  }
  
  public Object getParameterValue(String parameterName) throws JRScriptletException {
    JRFillParameter parameter = (JRFillParameter)this.parametersMap.get(parameterName);
    if (parameter == null)
      throw new JRScriptletException("Parameter not found : " + parameterName); 
    return parameter.getValue();
  }
  
  public Object getFieldValue(String fieldName) throws JRScriptletException {
    JRFillField field = (JRFillField)this.fieldsMap.get(fieldName);
    if (field == null)
      throw new JRScriptletException("Field not found : " + fieldName); 
    return field.getValue();
  }
  
  public Object getVariableValue(String variableName) throws JRScriptletException {
    JRFillVariable variable = (JRFillVariable)this.variablesMap.get(variableName);
    if (variable == null)
      throw new JRScriptletException("Variable not found : " + variableName); 
    return variable.getValue();
  }
  
  public void setVariableValue(String variableName, Object value) throws JRScriptletException {
    JRFillVariable variable = (JRFillVariable)this.variablesMap.get(variableName);
    if (variable == null)
      throw new JRScriptletException("Variable not found : " + variableName); 
    if (value != null && !variable.getValueClass().isInstance(value))
      throw new JRScriptletException("Incompatible value assigned to variable " + variableName + ". Expected " + variable.getValueClassName() + "."); 
    variable.setValue(value);
  }
  
  public void callBeforeReportInit() throws JRScriptletException {
    beforeReportInit();
    beforePageInit();
    beforeColumnInit();
    if (this.groups != null && this.groups.length > 0)
      for (int i = 0; i < this.groups.length; i++)
        beforeGroupInit(this.groups[i].getName());  
  }
  
  public void callAfterReportInit() throws JRScriptletException {
    if (this.groups != null && this.groups.length > 0)
      for (int i = this.groups.length - 1; i >= 0; i--)
        afterGroupInit(this.groups[i].getName());  
    afterColumnInit();
    afterPageInit();
    afterReportInit();
  }
  
  public void callBeforePageInit() throws JRScriptletException {
    beforePageInit();
    beforeColumnInit();
  }
  
  public void callAfterPageInit() throws JRScriptletException {
    afterColumnInit();
    afterPageInit();
  }
  
  public void callBeforeColumnInit() throws JRScriptletException {
    beforeColumnInit();
  }
  
  public void callAfterColumnInit() throws JRScriptletException {
    afterColumnInit();
  }
  
  public void callBeforeGroupInit() throws JRScriptletException {
    if (this.groups != null && this.groups.length > 0) {
      JRFillGroup group = null;
      for (int i = 0; i < this.groups.length; i++) {
        group = this.groups[i];
        if (group.hasChanged())
          beforeGroupInit(group.getName()); 
      } 
    } 
  }
  
  public void callAfterGroupInit() throws JRScriptletException {
    if (this.groups != null && this.groups.length > 0) {
      JRFillGroup group = null;
      for (int i = this.groups.length - 1; i >= 0; i--) {
        group = this.groups[i];
        if (group.hasChanged())
          afterGroupInit(group.getName()); 
      } 
    } 
  }
  
  public void callBeforeDetailEval() throws JRScriptletException {
    beforeDetailEval();
  }
  
  public void callAfterDetailEval() throws JRScriptletException {
    afterDetailEval();
  }
  
  public abstract void beforeReportInit() throws JRScriptletException;
  
  public abstract void afterReportInit() throws JRScriptletException;
  
  public abstract void beforePageInit() throws JRScriptletException;
  
  public abstract void afterPageInit() throws JRScriptletException;
  
  public abstract void beforeColumnInit() throws JRScriptletException;
  
  public abstract void afterColumnInit() throws JRScriptletException;
  
  public abstract void beforeGroupInit(String paramString) throws JRScriptletException;
  
  public abstract void afterGroupInit(String paramString) throws JRScriptletException;
  
  public abstract void beforeDetailEval() throws JRScriptletException;
  
  public abstract void afterDetailEval() throws JRScriptletException;
}
