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: RunningExpressionSlot.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.data;
033    
034    import org.jfree.report.DataRow;
035    import org.jfree.report.DataSourceException;
036    import org.jfree.report.ReportData;
037    import org.jfree.report.expressions.Expression;
038    import org.jfree.report.expressions.ExpressionRuntime;
039    import org.jfree.report.expressions.Function;
040    import org.jfree.report.flow.ReportContext;
041    import org.jfree.report.i18n.ResourceBundleFactory;
042    import org.jfree.util.Configuration;
043    
044    /**
045     * Creation-Date: 25.11.2006, 15:18:58
046     *
047     * @author Thomas Morgner
048     */
049    public class RunningExpressionSlot
050        implements ExpressionSlot, ExpressionRuntime
051    {
052      private StaticExpressionRuntimeData staticRuntimeData;
053      private Expression expression;
054      private Object value;
055      private String name;
056      private boolean queried;
057      private DataRow dataRow;
058    
059      public RunningExpressionSlot(final Expression expression,
060                                   final StaticExpressionRuntimeData runtimeData,
061                                   final PrecomputeNode precomputeNode)
062      {
063        this.staticRuntimeData = runtimeData;
064        this.expression = expression;
065        this.name = expression.getName();
066        this.expression.setRuntime(this);
067        this.expression.setRuntime(null);
068      }
069    
070      public Expression getExpression()
071      {
072        return expression;
073      }
074    
075      public Object getValue() throws DataSourceException
076      {
077        if (queried == false)
078        {
079          //noinspection SynchronizeOnNonFinalField
080          synchronized (expression)
081          {
082            expression.setRuntime(this);
083            value = expression.computeValue();
084            expression.setRuntime(null);
085          }
086          queried = true;
087        }
088        return value;
089      }
090    
091      public String getName()
092      {
093        return name;
094      }
095    
096      public DataRow getDataRow()
097      {
098        return dataRow;
099      }
100    
101      public Object clone() throws CloneNotSupportedException
102      {
103        return super.clone();
104      }
105    
106      public void updateDataRow(final DataRow dataRow)
107      {
108        this.dataRow = dataRow;
109      }
110    
111      /**
112       * Returns the report data used in this section. If subreports are used, this
113       * does not reflect the complete report data.
114       * <p/>
115       * All access to the report data must be properly synchronized. Failure to do
116       * so may result in funny results. Do not assume that the report data will be
117       * initialized on the current cursor positon.
118       *
119       * @return
120       */
121      public ReportData getData()
122      {
123        return staticRuntimeData.getData();
124      }
125    
126      public Object getDeclaringParent()
127      {
128        return staticRuntimeData.getDeclaringParent();
129      }
130    
131      public Configuration getConfiguration()
132      {
133        return staticRuntimeData.getConfiguration();
134      }
135    
136      public ResourceBundleFactory getResourceBundleFactory()
137      {
138        return staticRuntimeData.getResourceBundleFactory();
139      }
140    
141      public void advance() throws DataSourceException
142      {
143        if (expression instanceof Function)
144        {
145          final Function f = (Function) expression;
146          expression.setRuntime(this);
147          expression = f.advance();
148          f.setRuntime(null);
149          expression.setRuntime(null);
150        }
151        value = null;
152        queried = false;
153      }
154    
155      public boolean isDeepTraversing()
156      {
157        return expression.isDeepTraversing();
158      }
159    
160      public int getCurrentRow()
161      {
162        return staticRuntimeData.getCurrentRow();
163      }
164    
165      public ReportContext getReportContext()
166      {
167        return staticRuntimeData.getReportContext();
168      }
169    
170      public boolean isPreserve()
171      {
172        return expression.isPreserve();
173      }
174    }