| 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: ExpressionDataRow.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 |
package org.jfree.report.data; |
| 32 |
|
| 33 |
import java.util.HashMap; |
| 34 |
|
| 35 |
import org.jfree.report.DataFlags; |
| 36 |
import org.jfree.report.DataRow; |
| 37 |
import org.jfree.report.DataSourceException; |
| 38 |
import org.jfree.report.flow.ReportContext; |
| 39 |
|
| 40 |
/** |
| 41 |
* A datarow for all expressions encountered in the report. This datarow is a |
| 42 |
* stack-like structure, which allows easy adding and removing of expressions, |
| 43 |
* even if these expressions have been cloned and or otherwisely modified. |
| 44 |
* |
| 45 |
* @author Thomas Morgner |
| 46 |
*/ |
| 47 |
public final class ExpressionDataRow implements DataRow |
| 48 |
{ |
| 49 |
private ExpressionSlot[] expressions; |
| 50 |
private int length; |
| 51 |
private HashMap nameCache; |
| 52 |
private GlobalMasterRow masterRow; |
| 53 |
private ReportContext reportContext; |
| 54 |
|
| 55 |
public ExpressionDataRow(final GlobalMasterRow masterRow, |
| 56 |
final ReportContext reportContext, |
| 57 |
int capacity) |
| 58 |
{ |
| 59 |
this.masterRow = masterRow; |
| 60 |
this.nameCache = new HashMap(capacity); |
| 61 |
this.expressions = new ExpressionSlot[capacity]; |
| 62 |
this.reportContext = reportContext; |
| 63 |
} |
| 64 |
|
| 65 |
private ExpressionDataRow(final GlobalMasterRow masterRow, |
| 66 |
final ExpressionDataRow previousRow) |
| 67 |
throws CloneNotSupportedException |
| 68 |
{ |
| 69 |
this.reportContext = previousRow.reportContext; |
| 70 |
this.masterRow = masterRow; |
| 71 |
this.nameCache = (HashMap) previousRow.nameCache.clone(); |
| 72 |
this.expressions = new ExpressionSlot[previousRow.expressions.length]; |
| 73 |
this.length = previousRow.length; |
| 74 |
for (int i = 0; i < length; i++) |
| 75 |
{ |
| 76 |
final ExpressionSlot expression = previousRow.expressions[i]; |
| 77 |
if (expression == null) |
| 78 |
{ |
| 79 |
// Log.debug("Error: Expression is null..."); |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
expressions[i] = (ExpressionSlot) expression.clone(); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
private void ensureCapacity(int requestedSize) |
| 89 |
{ |
| 90 |
final int capacity = this.expressions.length; |
| 91 |
if (capacity > requestedSize) |
| 92 |
{ |
| 93 |
return; |
| 94 |
} |
| 95 |
final int newSize = Math.max(capacity * 2, requestedSize + 10); |
| 96 |
|
| 97 |
final ExpressionSlot[] newExpressions = new ExpressionSlot[newSize]; |
| 98 |
|
| 99 |
System.arraycopy(expressions, 0, newExpressions, 0, length); |
| 100 |
|
| 101 |
this.expressions = newExpressions; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* This adds the expression to the data-row and queries the expression for the |
| 106 |
* first time. |
| 107 |
* |
| 108 |
* @param ex |
| 109 |
* @param rd |
| 110 |
* @throws DataSourceException |
| 111 |
*/ |
| 112 |
public synchronized void pushExpression(final ExpressionSlot expressionSlot) |
| 113 |
throws DataSourceException |
| 114 |
{ |
| 115 |
if (expressionSlot == null) |
| 116 |
{ |
| 117 |
throw new NullPointerException(); |
| 118 |
} |
| 119 |
|
| 120 |
ensureCapacity(length + 1); |
| 121 |
|
| 122 |
this.expressions[length] = expressionSlot; |
| 123 |
final String name = expressionSlot.getName(); |
| 124 |
if (name != null) |
| 125 |
{ |
| 126 |
nameCache.put(name, expressionSlot); |
| 127 |
} |
| 128 |
length += 1; |
| 129 |
|
| 130 |
expressionSlot.updateDataRow(masterRow.getGlobalView()); |
| 131 |
// A manual advance to initialize the function. |
| 132 |
expressionSlot.advance(); |
| 133 |
if (name != null) |
| 134 |
{ |
| 135 |
final Object value = expressionSlot.getValue(); |
| 136 |
final MasterDataRowChangeEvent chEvent = new MasterDataRowChangeEvent |
| 137 |
(MasterDataRowChangeEvent.COLUMN_ADDED, name, value); |
| 138 |
masterRow.dataRowChanged(chEvent); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public synchronized void pushExpressions(final ExpressionSlot[] expressionSlots) |
| 143 |
throws DataSourceException |
| 144 |
{ |
| 145 |
if (expressionSlots == null) |
| 146 |
{ |
| 147 |
throw new NullPointerException(); |
| 148 |
} |
| 149 |
|
| 150 |
ensureCapacity(length + expressionSlots.length); |
| 151 |
for (int i = 0; i < expressionSlots.length; i++) |
| 152 |
{ |
| 153 |
ExpressionSlot expression = expressionSlots[i]; |
| 154 |
if (expression == null) |
| 155 |
{ |
| 156 |
continue; |
| 157 |
} |
| 158 |
pushExpression(expression); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public synchronized void popExpressions(final int counter) throws |
| 163 |
DataSourceException |
| 164 |
{ |
| 165 |
for (int i = 0; i < counter; i++) |
| 166 |
{ |
| 167 |
popExpression(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
public synchronized void popExpression() throws DataSourceException |
| 172 |
{ |
| 173 |
if (length == 0) |
| 174 |
{ |
| 175 |
return; |
| 176 |
} |
| 177 |
String originalName = expressions[length - 1].getName(); |
| 178 |
boolean preserve = expressions[length - 1].isPreserve(); |
| 179 |
this.expressions[length - 1] = null; |
| 180 |
this.length -= 1; |
| 181 |
if (originalName != null) |
| 182 |
{ |
| 183 |
int otherIndex = -1; |
| 184 |
for (int i = length - 1; i >= 0; i -= 1) |
| 185 |
{ |
| 186 |
ExpressionSlot expression = expressions[i]; |
| 187 |
if (originalName.equals(expression.getName())) |
| 188 |
{ |
| 189 |
otherIndex = i; |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
if (otherIndex == -1) |
| 194 |
{ |
| 195 |
nameCache.remove(originalName); |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
nameCache.put(originalName, expressions[otherIndex]); |
| 200 |
} |
| 201 |
|
| 202 |
if (preserve == false) |
| 203 |
{ |
| 204 |
final MasterDataRowChangeEvent chEvent = new MasterDataRowChangeEvent |
| 205 |
(MasterDataRowChangeEvent.COLUMN_REMOVED, originalName, null); |
| 206 |
masterRow.dataRowChanged(chEvent); |
| 207 |
} |
| 208 |
// for preserved elements we do not send an remove-event. |
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Returns the value of the expressions or column in the tablemodel using the |
| 215 |
* given column number as index. For functions and expressions, the |
| 216 |
* <code>getValue()</code> method is called and for columns from the |
| 217 |
* tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets |
| 218 |
* called. |
| 219 |
* |
| 220 |
* @param col the item index. |
| 221 |
* @return the value. |
| 222 |
* @throws IllegalStateException if the datarow detected a deadlock. |
| 223 |
*/ |
| 224 |
public Object get(int col) throws DataSourceException |
| 225 |
{ |
| 226 |
return expressions[col].getValue(); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Returns the value of the function, expressions or column using its specific |
| 231 |
* name. The given name is translated into a valid column number and the the |
| 232 |
* column is queried. For functions and expressions, the |
| 233 |
* <code>getValue()</code> method is called and for columns from the |
| 234 |
* tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets |
| 235 |
* called. |
| 236 |
* |
| 237 |
* @param col the item index. |
| 238 |
* @return the value. |
| 239 |
* @throws IllegalStateException if the datarow detected a deadlock. |
| 240 |
*/ |
| 241 |
public Object get(String col) throws DataSourceException |
| 242 |
{ |
| 243 |
final ExpressionSlot es = (ExpressionSlot) nameCache.get(col); |
| 244 |
if (es == null) |
| 245 |
{ |
| 246 |
return null; |
| 247 |
} |
| 248 |
|
| 249 |
return es.getValue(); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Returns the name of the column, expressions or function. For columns from |
| 254 |
* the tablemodel, the tablemodels <code>getColumnName</code> method is |
| 255 |
* called. For functions, expressions and report properties the assigned name |
| 256 |
* is returned. |
| 257 |
* |
| 258 |
* @param col the item index. |
| 259 |
* @return the name. |
| 260 |
*/ |
| 261 |
public String getColumnName(int col) |
| 262 |
{ |
| 263 |
return expressions[col].getName(); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Returns the number of columns, expressions and functions and marked |
| 268 |
* ReportProperties in the report. |
| 269 |
* |
| 270 |
* @return the item count. |
| 271 |
*/ |
| 272 |
public int getColumnCount() |
| 273 |
{ |
| 274 |
return length; |
| 275 |
} |
| 276 |
|
| 277 |
public DataFlags getFlags(String col) |
| 278 |
{ |
| 279 |
throw new UnsupportedOperationException(); |
| 280 |
} |
| 281 |
|
| 282 |
public DataFlags getFlags(int col) |
| 283 |
{ |
| 284 |
throw new UnsupportedOperationException(); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Advances to the next row and attaches the given master row to the objects |
| 289 |
* contained in that client data row. |
| 290 |
* |
| 291 |
* @param master |
| 292 |
* @param deepTraversing only advance expressions that have been marked as |
| 293 |
* deeply traversing |
| 294 |
* @return |
| 295 |
*/ |
| 296 |
public ExpressionDataRow advance(final GlobalMasterRow master, |
| 297 |
final boolean deepTraversing) |
| 298 |
throws DataSourceException |
| 299 |
{ |
| 300 |
try |
| 301 |
{ |
| 302 |
final ExpressionDataRow edr = new ExpressionDataRow(master, this); |
| 303 |
|
| 304 |
// It is defined, that new expressions get evaluated before any older |
| 305 |
// expression. |
| 306 |
for (int i = edr.length - 1; i >= 0; i--) |
| 307 |
{ |
| 308 |
ExpressionSlot expressionSlot = edr.expressions[i]; |
| 309 |
expressionSlot.updateDataRow(master.getGlobalView()); |
| 310 |
if (deepTraversing == false || |
| 311 |
(deepTraversing && expressionSlot.isDeepTraversing())) |
| 312 |
{ |
| 313 |
expressionSlot.advance(); |
| 314 |
} |
| 315 |
// Query the value (once per advance) .. |
| 316 |
final Object value = expressionSlot.getValue(); |
| 317 |
final String name = expressionSlot.getName(); |
| 318 |
if (name != null) |
| 319 |
{ |
| 320 |
final MasterDataRowChangeEvent chEvent = new MasterDataRowChangeEvent |
| 321 |
(MasterDataRowChangeEvent.COLUMN_UPDATED, name, value); |
| 322 |
master.dataRowChanged(chEvent); |
| 323 |
} |
| 324 |
} |
| 325 |
return edr; |
| 326 |
} |
| 327 |
catch (CloneNotSupportedException e) |
| 328 |
{ |
| 329 |
throw new DataSourceException("Cloning failed", e); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
public ExpressionDataRow derive(final GlobalMasterRow master) |
| 334 |
throws DataSourceException |
| 335 |
{ |
| 336 |
try |
| 337 |
{ |
| 338 |
return new ExpressionDataRow(master, this); |
| 339 |
} |
| 340 |
catch (CloneNotSupportedException e) |
| 341 |
{ |
| 342 |
throw new DataSourceException("Cloning failed", e); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
public ExpressionSlot[] getSlots() |
| 347 |
{ |
| 348 |
// to be totally safe from any tampering, we would have to do some sort of |
| 349 |
// deep-copy here. |
| 350 |
ExpressionSlot[] slots = new ExpressionSlot[length]; |
| 351 |
System.arraycopy(expressions, 0, slots, 0, length); |
| 352 |
return slots; |
| 353 |
} |
| 354 |
} |