KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRCalculator


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * 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,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28
29 /*
30  * Contributors:
31  * Peter Severin - peter_p_s@users.sourceforge.net
32  */

33 package net.sf.jasperreports.engine.fill;
34
35 import java.util.Map JavaDoc;
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 /**
43  * @author Teodor Danciu (teodord@users.sourceforge.net)
44  * @version $Id: JRCalculator.java 1470 2006-11-08 18:07:35 +0200 (Wed, 08 Nov 2006) teodord $
45  */

46 public class JRCalculator implements JRFillExpressionEvaluator
47 {
48
49
50     /**
51      *
52      */

53     protected Map JavaDoc parsm = null;
54     protected Map JavaDoc fldsm = null;
55     protected Map JavaDoc 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     /**
64      * The expression evaluator
65      */

66     private final JREvaluator evaluator;
67
68
69     /**
70      * Creates a calculator using an expression evaluator.
71      *
72      * @param evaluator the expression evaluator
73      */

74     protected JRCalculator(JREvaluator evaluator)
75     {
76         this.evaluator = evaluator;
77     }
78
79
80     /**
81      * Initializes the calculator.
82      *
83      * @param dataset the dataset this calculator is used for
84      * @throws JRException
85      */

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     /**
104      *
105      */

106     public JRFillVariable getPageNumber()
107     {
108         return pageNumber;
109     }
110     
111
112     /**
113      *
114      */

115     public JRFillVariable getColumnNumber()
116     {
117         return columnNumber;
118     }
119     
120
121     /**
122      *
123      */

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 JavaDoc expressionValue = evaluate(variable.getExpression());
132                 Object JavaDoc 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     /**
160      *
161      */

162     public void estimateVariables() throws JRException
163     {
164         if (variables != null && variables.length > 0)
165         {
166             JRFillVariable variable = null;
167             Object JavaDoc expressionValue = null;
168             Object JavaDoc 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                 //variable.setInitialized(false);
177
}
178         }
179     }
180
181
182     /**
183      * Determines group breaks based on estimated report values.
184      * <p>
185      * {@link #estimateVariables() estimateVariables()} needs to be called prior to this method.
186      * </p>
187      *
188      * @throws JRException
189      */

190     public void estimateGroupRuptures() throws JRException
191     {
192         JRFillGroup group = null;
193         Object JavaDoc oldValue = null;
194         Object JavaDoc 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     /**
228      *
229      */

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     /**
253      *
254      */

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 // variable.setValue(
300
// evaluate(variable.getInitialValueExpression())
301
// );
302
// variable.setInitialized(true);
303
}
304         }
305         else
306         {
307             variable.setIncrementedValue(variable.getValue());
308 // variable.setValue(
309
// evaluate(variable.getExpression())
310
// );
311
}
312     }
313
314
315     /**
316      *
317      */

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     /**
368      *
369      */

370     private void initializeVariable(JRFillVariable variable, byte resetType) throws JRException
371     {
372         //if (jrVariable.getCalculation() != JRVariable.CALCULATION_NOTHING)
373
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     /**
432      *
433      */

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     /**
481      *
482      */

483     public Object JavaDoc evaluate(
484         JRExpression expression,
485         byte evaluationType
486         ) throws JRException
487     {
488         Object JavaDoc 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     /**
515      *
516      */

517     public Object JavaDoc evaluateOld(JRExpression expression) throws JRExpressionEvalException
518     {
519         return evaluator.evaluateOld(expression);
520     }
521
522
523     /**
524      *
525      */

526     public Object JavaDoc evaluateEstimated(JRExpression expression) throws JRExpressionEvalException
527     {
528         return evaluator.evaluateEstimated(expression);
529     }
530
531
532     /**
533      *
534      */

535     public Object JavaDoc evaluate(JRExpression expression) throws JRExpressionEvalException
536     {
537         return evaluator.evaluate(expression);
538     }
539 }
540
Popular Tags