Source for org.jfree.report.expressions.ReportFormulaContext

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: ReportFormulaContext.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.expressions;
  33: 
  34: import org.jfree.formula.ContextEvaluationException;
  35: import org.jfree.formula.FormulaContext;
  36: import org.jfree.formula.LibFormulaErrorValue;
  37: import org.jfree.formula.LocalizationContext;
  38: import org.jfree.formula.function.FunctionRegistry;
  39: import org.jfree.formula.operators.OperatorFactory;
  40: import org.jfree.formula.typing.Type;
  41: import org.jfree.formula.typing.TypeRegistry;
  42: import org.jfree.formula.typing.coretypes.AnyType;
  43: import org.jfree.report.DataFlags;
  44: import org.jfree.report.DataRow;
  45: import org.jfree.report.DataSourceException;
  46: import org.jfree.util.Configuration;
  47: import org.jfree.util.Log;
  48: 
  49: /**
  50:  * Creation-Date: 29.11.2006, 17:54:33
  51:  *
  52:  * @author Thomas Morgner
  53:  */
  54: public class ReportFormulaContext implements FormulaContext
  55: {
  56:   private FormulaContext backend;
  57:   private DataRow dataRow;
  58:   private Object declaringElement;
  59: 
  60:   public ReportFormulaContext(final FormulaContext backend,
  61:                               final DataRow dataRow)
  62:   {
  63:     this.backend = backend;
  64:     this.dataRow = dataRow;
  65:   }
  66: 
  67:   public LocalizationContext getLocalizationContext()
  68:   {
  69:     return backend.getLocalizationContext();
  70:   }
  71: 
  72:   public Configuration getConfiguration()
  73:   {
  74:     return backend.getConfiguration();
  75:   }
  76: 
  77:   public FunctionRegistry getFunctionRegistry()
  78:   {
  79:     return backend.getFunctionRegistry();
  80:   }
  81: 
  82:   public TypeRegistry getTypeRegistry()
  83:   {
  84:     return backend.getTypeRegistry();
  85:   }
  86: 
  87:   public OperatorFactory getOperatorFactory()
  88:   {
  89:     return backend.getOperatorFactory();
  90:   }
  91: 
  92:   public boolean isReferenceDirty(final Object name) throws ContextEvaluationException
  93:   {
  94:     try
  95:     {
  96:       final DataFlags flags = dataRow.getFlags(String.valueOf(name));
  97:       if (flags == null)
  98:       {
  99:         throw new ContextEvaluationException
 100:             (new LibFormulaErrorValue(LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
 101:       }
 102:       return flags.isChanged();
 103:     }
 104:     catch(Exception e)
 105:     {
 106:       throw new ContextEvaluationException
 107:           (new LibFormulaErrorValue(LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
 108:     }
 109:   }
 110: 
 111:   public Type resolveReferenceType(final Object name)
 112:   {
 113:     return AnyType.TYPE;
 114:   }
 115: 
 116:   public Object resolveReference(final Object name) throws ContextEvaluationException
 117:   {
 118:     if (name == null)
 119:     {
 120:       throw new NullPointerException();
 121:     }
 122:     try
 123:     {
 124:       return dataRow.get(String.valueOf(name));
 125:     }
 126:     catch (DataSourceException e)
 127:     {
 128:       Log.debug ("Error while resolving formula reference: ", e);
 129:       throw new ContextEvaluationException(new LibFormulaErrorValue
 130:           (LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
 131:     }
 132:   }
 133: 
 134:   public DataRow getDataRow()
 135:   {
 136:     return dataRow;
 137:   }
 138: 
 139:   public void setDataRow(final DataRow dataRow)
 140:   {
 141:     this.dataRow = dataRow;
 142:   }
 143: 
 144:   public Object getDeclaringElement()
 145:   {
 146:     return declaringElement;
 147:   }
 148: 
 149:   public void setDeclaringElement(final Object declaringElement)
 150:   {
 151:     this.declaringElement = declaringElement;
 152:   }
 153: }