001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: ReportFormulaContext.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    
032    package org.jfree.report.expressions;
033    
034    import org.jfree.formula.ContextEvaluationException;
035    import org.jfree.formula.FormulaContext;
036    import org.jfree.formula.LibFormulaErrorValue;
037    import org.jfree.formula.LocalizationContext;
038    import org.jfree.formula.function.FunctionRegistry;
039    import org.jfree.formula.operators.OperatorFactory;
040    import org.jfree.formula.typing.Type;
041    import org.jfree.formula.typing.TypeRegistry;
042    import org.jfree.formula.typing.coretypes.AnyType;
043    import org.jfree.report.DataFlags;
044    import org.jfree.report.DataRow;
045    import org.jfree.report.DataSourceException;
046    import org.jfree.util.Configuration;
047    import org.jfree.util.Log;
048    
049    /**
050     * Creation-Date: 29.11.2006, 17:54:33
051     *
052     * @author Thomas Morgner
053     */
054    public class ReportFormulaContext implements FormulaContext
055    {
056      private FormulaContext backend;
057      private DataRow dataRow;
058      private Object declaringElement;
059    
060      public ReportFormulaContext(final FormulaContext backend,
061                                  final DataRow dataRow)
062      {
063        this.backend = backend;
064        this.dataRow = dataRow;
065      }
066    
067      public LocalizationContext getLocalizationContext()
068      {
069        return backend.getLocalizationContext();
070      }
071    
072      public Configuration getConfiguration()
073      {
074        return backend.getConfiguration();
075      }
076    
077      public FunctionRegistry getFunctionRegistry()
078      {
079        return backend.getFunctionRegistry();
080      }
081    
082      public TypeRegistry getTypeRegistry()
083      {
084        return backend.getTypeRegistry();
085      }
086    
087      public OperatorFactory getOperatorFactory()
088      {
089        return backend.getOperatorFactory();
090      }
091    
092      public boolean isReferenceDirty(final Object name) throws ContextEvaluationException
093      {
094        try
095        {
096          final DataFlags flags = dataRow.getFlags(String.valueOf(name));
097          if (flags == null)
098          {
099            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    }