Debian Codegraph @

source/org/jfree/report/expressions/ReportFormulaContext.java

raw
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 10756 2009-12-02 15:58:24Z 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.report.DataFlags;
35 import org.jfree.report.DataRow;
36 import org.jfree.report.DataSourceException;
37 import org.pentaho.reporting.libraries.base.config.Configuration;
38 import org.pentaho.reporting.libraries.base.util.DebugLog;
39 import org.pentaho.reporting.libraries.formula.ContextEvaluationException;
40 import org.pentaho.reporting.libraries.formula.FormulaContext;
41 import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
42 import org.pentaho.reporting.libraries.formula.LocalizationContext;
43 import org.pentaho.reporting.libraries.formula.function.FunctionRegistry;
44 import org.pentaho.reporting.libraries.formula.operators.OperatorFactory;
45 import org.pentaho.reporting.libraries.formula.typing.Type;
46 import org.pentaho.reporting.libraries.formula.typing.TypeRegistry;
47 import org.pentaho.reporting.libraries.formula.typing.coretypes.AnyType;
48 import org.pentaho.reporting.libraries.formula.typing.coretypes.DateTimeType;
49 import org.pentaho.reporting.libraries.formula.typing.coretypes.NumberType;
50 import org.pentaho.reporting.libraries.formula.typing.coretypes.TextType;
51
52 /**
53 * Creation-Date: 29.11.2006, 17:54:33
54 *
55 * @author Thomas Morgner
56 */
57 public class ReportFormulaContext implements FormulaContext
58 {
59 private FormulaContext backend;
60 private DataRow dataRow;
61 private Object declaringElement;
62
63 public ReportFormulaContext(FormulaContext backend,
64 DataRow dataRow)
65 {
66 this.backend = backend;
67 this.dataRow = dataRow;
68 }
69
70 public LocalizationContext getLocalizationContext()
71 {
72 return backend.getLocalizationContext();
73 }
74
75 public Configuration getConfiguration()
76 {
77 return backend.getConfiguration();
78 }
79
80 public FunctionRegistry getFunctionRegistry()
81 {
82 return backend.getFunctionRegistry();
83 }
84
85 public TypeRegistry getTypeRegistry()
86 {
87 return backend.getTypeRegistry();
88 }
89
90 public OperatorFactory getOperatorFactory()
91 {
92 return backend.getOperatorFactory();
93 }
94
95 public boolean isReferenceDirty(Object name) throws ContextEvaluationException
96 {
97 try
98 {
99 final DataFlags flags = dataRow.getFlags(String.valueOf(name));
100 if (flags == null)
101 {
102 throw new ContextEvaluationException
103 (new LibFormulaErrorValue(LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
104 }
105 return flags.isChanged();
106 }
107 catch (Exception e)
108 {
109 throw new ContextEvaluationException
110 (new LibFormulaErrorValue(LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
111 }
112 }
113
114 public Type resolveReferenceType(Object name)
115 {
116 try
117 {
118 final DataFlags flags = dataRow.getFlags(String.valueOf(name));
119 if (flags != null)
120 {
121 if (flags.isDate())
122 {
123 return DateTimeType.DATE_TYPE;
124 }
125 if (flags.isNumeric())
126 {
127 return NumberType.GENERIC_NUMBER;
128 }
129 return TextType.TYPE;
130 }
131 }
132 catch (DataSourceException ex)
133 {
134 }
135 return AnyType.TYPE;
136 }
137
138 public Object resolveReference(Object name) throws ContextEvaluationException
139 {
140 if (name == null)
141 {
142 throw new NullPointerException();
143 }
144 try
145 {
146 return dataRow.get(String.valueOf(name));
147 }
148 catch (DataSourceException e)
149 {
150 DebugLog.log("Error while resolving formula reference: ", e);
151 throw new ContextEvaluationException(new LibFormulaErrorValue
152 (LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
153 }
154 }
155
156 public DataRow getDataRow()
157 {
158 return dataRow;
159 }
160
161 public void setDataRow(final DataRow dataRow)
162 {
163 this.dataRow = dataRow;
164 }
165
166 public Object getDeclaringElement()
167 {
168 return declaringElement;
169 }
170
171 public void setDeclaringElement(final Object declaringElement)
172 {
173 this.declaringElement = declaringElement;
174 }
175 }