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,v 1.5 2007/04/01 18:49:25 taqua Exp $
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(FormulaContext backend,
061                                  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(Object name) throws ContextEvaluationException
093      {
094        try
095        {
096          final DataFlags flags = dataRow.getFlags(String.valueOf(name));
097          return flags.isChanged();
098        }
099        catch(Exception e)
100        {
101          throw new ContextEvaluationException
102              (new LibFormulaErrorValue(LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
103        }
104      }
105    
106      public Type resolveReferenceType(Object name)
107      {
108        return AnyType.TYPE;
109      }
110    
111      public Object resolveReference(Object name) throws ContextEvaluationException
112      {
113        if (name == null)
114        {
115          throw new NullPointerException();
116        }
117        try
118        {
119          return dataRow.get(String.valueOf(name));
120        }
121        catch (DataSourceException e)
122        {
123          Log.debug ("Error while resolving formula reference: ", e);
124          throw new ContextEvaluationException(new LibFormulaErrorValue
125              (LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
126        }
127      }
128    
129      public DataRow getDataRow()
130      {
131        return dataRow;
132      }
133    
134      public void setDataRow(final DataRow dataRow)
135      {
136        this.dataRow = dataRow;
137      }
138    
139      public Object getDeclaringElement()
140      {
141        return declaringElement;
142      }
143    
144      public void setDeclaringElement(final Object declaringElement)
145      {
146        this.declaringElement = declaringElement;
147      }
148    }