1 28 29 33 package net.sf.jasperreports.engine.fill; 34 35 import java.util.Map ; 36 37 import net.sf.jasperreports.engine.JRException; 38 import net.sf.jasperreports.engine.JRExpression; 39 import net.sf.jasperreports.engine.JRVariable; 40 41 42 46 public class JRCalculator implements JRFillExpressionEvaluator 47 { 48 49 50 53 protected Map parsm = null; 54 protected Map fldsm = null; 55 protected Map varsm = null; 56 protected JRFillVariable[] variables = null; 57 protected JRFillGroup[] groups = null; 58 protected JRFillElementDataset[] datasets = null; 59 60 private JRFillVariable pageNumber = null; 61 private JRFillVariable columnNumber = null; 62 63 66 private final JREvaluator evaluator; 67 68 69 74 protected JRCalculator(JREvaluator evaluator) 75 { 76 this.evaluator = evaluator; 77 } 78 79 80 86 protected void init(JRFillDataset dataset) throws JRException 87 { 88 parsm = dataset.parametersMap; 89 fldsm = dataset.fieldsMap; 90 varsm = dataset.variablesMap; 91 variables = dataset.variables; 92 groups = dataset.groups; 93 datasets = dataset.elementDatasets; 94 95 pageNumber = (JRFillVariable)varsm.get(JRVariable.PAGE_NUMBER); 96 columnNumber = (JRFillVariable)varsm.get(JRVariable.COLUMN_NUMBER); 97 98 byte whenResourceMissingType = dataset.getWhenResourceMissingType(); 99 evaluator.init(parsm, fldsm,varsm, whenResourceMissingType); 100 } 101 102 103 106 public JRFillVariable getPageNumber() 107 { 108 return pageNumber; 109 } 110 111 112 115 public JRFillVariable getColumnNumber() 116 { 117 return columnNumber; 118 } 119 120 121 124 public void calculateVariables() throws JRException 125 { 126 if (variables != null && variables.length > 0) 127 { 128 for(int i = 0; i < variables.length; i++) 129 { 130 JRFillVariable variable = variables[i]; 131 Object expressionValue = evaluate(variable.getExpression()); 132 Object newValue = variable.getIncrementer().increment(variable, expressionValue, AbstractValueProvider.getCurrentValueProvider()); 133 variable.setValue(newValue); 134 variable.setInitialized(false); 135 136 if (variable.getIncrementType() == JRVariable.RESET_TYPE_NONE) 137 { 138 variable.setIncrementedValue(variable.getValue()); 139 } 140 } 141 } 142 143 if (datasets != null && datasets.length > 0) 144 { 145 for(int i = 0; i < datasets.length; i++) 146 { 147 JRFillElementDataset elementDataset = datasets[i]; 148 elementDataset.evaluate(this); 149 150 if (elementDataset.getIncrementType() == JRVariable.RESET_TYPE_NONE) 151 { 152 elementDataset.increment(); 153 } 154 } 155 } 156 } 157 158 159 162 public void estimateVariables() throws JRException 163 { 164 if (variables != null && variables.length > 0) 165 { 166 JRFillVariable variable = null; 167 Object expressionValue = null; 168 Object newValue = null; 169 170 for(int i = 0; i < variables.length; i++) 171 { 172 variable = variables[i]; 173 expressionValue = evaluateEstimated(variable.getExpression()); 174 newValue = variable.getIncrementer().increment(variable, expressionValue, AbstractValueProvider.getEstimatedValueProvider()); 175 variable.setEstimatedValue(newValue); 176 } 178 } 179 } 180 181 182 190 public void estimateGroupRuptures() throws JRException 191 { 192 JRFillGroup group = null; 193 Object oldValue = null; 194 Object estimatedValue = null; 195 boolean groupHasChanged = false; 196 boolean isTopLevelChange = false; 197 if (groups != null && groups.length > 0) 198 { 199 for(int i = 0; i < groups.length; i++) 200 { 201 group = groups[i]; 202 203 isTopLevelChange = false; 204 205 if (!groupHasChanged) 206 { 207 oldValue = evaluateOld(group.getExpression()); 208 estimatedValue = evaluateEstimated(group.getExpression()); 209 210 if ( 211 (oldValue == null && estimatedValue != null) || 212 (oldValue != null && !oldValue.equals(estimatedValue)) 213 ) 214 { 215 groupHasChanged = true; 216 isTopLevelChange = true; 217 } 218 } 219 220 group.setHasChanged(groupHasChanged); 221 group.setTopLevelChange(isTopLevelChange); 222 } 223 } 224 } 225 226 227 230 public void initializeVariables(byte resetType) throws JRException 231 { 232 if (variables != null && variables.length > 0) 233 { 234 for(int i = 0; i < variables.length; i++) 235 { 236 incrementVariable(variables[i], resetType); 237 initializeVariable(variables[i], resetType); 238 } 239 } 240 241 if (datasets != null && datasets.length > 0) 242 { 243 for(int i = 0; i < datasets.length; i++) 244 { 245 incrementDataset(datasets[i], resetType); 246 initializeDataset(datasets[i], resetType); 247 } 248 } 249 } 250 251 252 255 private void incrementVariable(JRFillVariable variable, byte incrementType) 256 { 257 if (variable.getIncrementType() != JRVariable.RESET_TYPE_NONE) 258 { 259 boolean toIncrement = false; 260 switch (incrementType) 261 { 262 case JRVariable.RESET_TYPE_REPORT : 263 { 264 toIncrement = true; 265 break; 266 } 267 case JRVariable.RESET_TYPE_PAGE : 268 { 269 toIncrement = 270 ( 271 variable.getIncrementType() == JRVariable.RESET_TYPE_PAGE || 272 variable.getIncrementType() == JRVariable.RESET_TYPE_COLUMN 273 ); 274 break; 275 } 276 case JRVariable.RESET_TYPE_COLUMN : 277 { 278 toIncrement = (variable.getIncrementType() == JRVariable.RESET_TYPE_COLUMN); 279 break; 280 } 281 case JRVariable.RESET_TYPE_GROUP : 282 { 283 if (variable.getIncrementType() == JRVariable.RESET_TYPE_GROUP) 284 { 285 JRFillGroup group = (JRFillGroup)variable.getIncrementGroup(); 286 toIncrement = group.hasChanged(); 287 } 288 break; 289 } 290 case JRVariable.RESET_TYPE_NONE : 291 default : 292 { 293 } 294 } 295 296 if (toIncrement) 297 { 298 variable.setIncrementedValue(variable.getValue()); 299 } 304 } 305 else 306 { 307 variable.setIncrementedValue(variable.getValue()); 308 } 312 } 313 314 315 318 private void incrementDataset(JRFillElementDataset elementDataset, byte incrementType) 319 { 320 if (elementDataset.getIncrementType() != JRVariable.RESET_TYPE_NONE) 321 { 322 boolean toIncrement = false; 323 switch (incrementType) 324 { 325 case JRVariable.RESET_TYPE_REPORT : 326 { 327 toIncrement = true; 328 break; 329 } 330 case JRVariable.RESET_TYPE_PAGE : 331 { 332 toIncrement = 333 ( 334 elementDataset.getIncrementType() == JRVariable.RESET_TYPE_PAGE || 335 elementDataset.getIncrementType() == JRVariable.RESET_TYPE_COLUMN 336 ); 337 break; 338 } 339 case JRVariable.RESET_TYPE_COLUMN : 340 { 341 toIncrement = (elementDataset.getIncrementType() == JRVariable.RESET_TYPE_COLUMN); 342 break; 343 } 344 case JRVariable.RESET_TYPE_GROUP : 345 { 346 if (elementDataset.getIncrementType() == JRVariable.RESET_TYPE_GROUP) 347 { 348 JRFillGroup group = (JRFillGroup)elementDataset.getIncrementGroup(); 349 toIncrement = group.hasChanged(); 350 } 351 break; 352 } 353 case JRVariable.RESET_TYPE_NONE : 354 default : 355 { 356 } 357 } 358 359 if (toIncrement) 360 { 361 elementDataset.increment(); 362 } 363 } 364 } 365 366 367 370 private void initializeVariable(JRFillVariable variable, byte resetType) throws JRException 371 { 372 if (variable.getResetType() != JRVariable.RESET_TYPE_NONE) 374 { 375 boolean toInitialize = false; 376 switch (resetType) 377 { 378 case JRVariable.RESET_TYPE_REPORT : 379 { 380 toInitialize = true; 381 break; 382 } 383 case JRVariable.RESET_TYPE_PAGE : 384 { 385 toInitialize = 386 ( 387 variable.getResetType() == JRVariable.RESET_TYPE_PAGE || 388 variable.getResetType() == JRVariable.RESET_TYPE_COLUMN 389 ); 390 break; 391 } 392 case JRVariable.RESET_TYPE_COLUMN : 393 { 394 toInitialize = (variable.getResetType() == JRVariable.RESET_TYPE_COLUMN); 395 break; 396 } 397 case JRVariable.RESET_TYPE_GROUP : 398 { 399 if (variable.getResetType() == JRVariable.RESET_TYPE_GROUP) 400 { 401 JRFillGroup group = (JRFillGroup)variable.getResetGroup(); 402 toInitialize = group.hasChanged(); 403 } 404 break; 405 } 406 case JRVariable.RESET_TYPE_NONE : 407 default : 408 { 409 } 410 } 411 412 if (toInitialize) 413 { 414 variable.setValue( 415 evaluate(variable.getInitialValueExpression()) 416 ); 417 variable.setInitialized(true); 418 variable.setIncrementedValue(null); 419 } 420 } 421 else 422 { 423 variable.setValue( 424 evaluate(variable.getExpression()) 425 ); 426 variable.setIncrementedValue(variable.getValue()); 427 } 428 } 429 430 431 434 private void initializeDataset(JRFillElementDataset elementDataset, byte resetType) 435 { 436 boolean toInitialize = false; 437 switch (resetType) 438 { 439 case JRVariable.RESET_TYPE_REPORT : 440 { 441 toInitialize = true; 442 break; 443 } 444 case JRVariable.RESET_TYPE_PAGE : 445 { 446 toInitialize = 447 ( 448 elementDataset.getResetType() == JRVariable.RESET_TYPE_PAGE || 449 elementDataset.getResetType() == JRVariable.RESET_TYPE_COLUMN 450 ); 451 break; 452 } 453 case JRVariable.RESET_TYPE_COLUMN : 454 { 455 toInitialize = (elementDataset.getResetType() == JRVariable.RESET_TYPE_COLUMN); 456 break; 457 } 458 case JRVariable.RESET_TYPE_GROUP : 459 { 460 if (elementDataset.getResetType() == JRVariable.RESET_TYPE_GROUP) 461 { 462 JRFillGroup group = (JRFillGroup)elementDataset.getResetGroup(); 463 toInitialize = group.hasChanged(); 464 } 465 break; 466 } 467 case JRVariable.RESET_TYPE_NONE : 468 default : 469 { 470 } 471 } 472 473 if (toInitialize) 474 { 475 elementDataset.initialize(); 476 } 477 } 478 479 480 483 public Object evaluate( 484 JRExpression expression, 485 byte evaluationType 486 ) throws JRException 487 { 488 Object value = null; 489 490 switch (evaluationType) 491 { 492 case JRExpression.EVALUATION_OLD : 493 { 494 value = evaluateOld(expression); 495 break; 496 } 497 case JRExpression.EVALUATION_ESTIMATED : 498 { 499 value = evaluateEstimated(expression); 500 break; 501 } 502 case JRExpression.EVALUATION_DEFAULT : 503 default : 504 { 505 value = evaluate(expression); 506 break; 507 } 508 } 509 510 return value; 511 } 512 513 514 517 public Object evaluateOld(JRExpression expression) throws JRExpressionEvalException 518 { 519 return evaluator.evaluateOld(expression); 520 } 521 522 523 526 public Object evaluateEstimated(JRExpression expression) throws JRExpressionEvalException 527 { 528 return evaluator.evaluateEstimated(expression); 529 } 530 531 532 535 public Object evaluate(JRExpression expression) throws JRExpressionEvalException 536 { 537 return evaluator.evaluate(expression); 538 } 539 } 540 | Popular Tags |