KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > common > StaticJasperReportDefinition


1 package com.calipso.reportgenerator.common;
2
3 import javax.swing.table.TableModel JavaDoc;
4 import java.util.*;
5 import java.text.NumberFormat JavaDoc;
6 import java.text.DecimalFormat JavaDoc;
7
8 import net.sf.jasperreports.engine.design.*;
9 import net.sf.jasperreports.engine.design.JRDesignGroup;
10 import net.sf.jasperreports.engine.design.JRDesignBand;
11 import net.sf.jasperreports.engine.design.JRDesignStaticText;
12 import net.sf.jasperreports.engine.*;
13 import com.calipso.reportgenerator.common.LanguageTraslator;
14 import com.calipso.reportgenerator.common.ReportResult;
15 import com.calipso.reportgenerator.common.ReportMetricSpec;
16 import com.calipso.reportgenerator.common.IJasperDefinition;
17
18 /**
19  * Construye una definición para los reportes estátícos (Jasper)
20  */

21
22 public class StaticJasperReportDefinition implements IJasperDefinition {
23
24   private Map fields;
25   private Map variables;
26   private TableModel JavaDoc data;
27   private String JavaDoc tittle;
28   private Vector dimensions;
29   private Vector metrics;
30   private Vector accuMetrics;
31   private Vector nonGroupingDimensions;
32   private Vector dimensionsSizes;
33   private Vector metricsSizes;
34   private Vector accuMetricsSizes;
35   private Vector nonGroupingDimensionsSizes;
36   private int printAt;
37   private Vector metricsPosition = new Vector();
38   private Vector nonGroupingDimensionPosition = new Vector();
39   private Vector groupingDimensionPosition = new Vector();
40   private ReportResult reportResult;
41   private String JavaDoc numberFormatPattern;
42   private Vector metricsWidth;
43   private Vector accMetricsPosition;
44   private Vector accMetricsWidht;
45   private Map reportParams;
46
47   /**
48    * Inicializa una instancia de <coe>eJasperReportDefinition</code>
49    * @param reportResult
50    * @param data
51    * @param dimensionsCount
52    * @param metricsCount
53    * @param nonGroupingDimensionsCount
54    * @param accuMetricsCount
55    * @param tittle
56    */

57   public StaticJasperReportDefinition(ReportResult reportResult, TableModel JavaDoc data, int dimensionsCount, int metricsCount, int nonGroupingDimensionsCount, int accuMetricsCount, String JavaDoc tittle) {
58     this.data = data;
59     this.tittle = tittle;
60     this.reportResult = reportResult;
61     metricsWidth = new Vector();
62     accMetricsPosition = new Vector();
63     accMetricsWidht = new Vector();
64     initializeVectors(dimensionsCount, metricsCount, nonGroupingDimensionsCount, accuMetricsCount);
65     reportParams = reportResult.getParamValues();
66     dimensionsSizes = calculateBestLenghts(dimensions);
67     metricsSizes = calculateBestLenghts(metrics);
68     accuMetricsSizes = calculateBestLenghts(this.accuMetrics);
69     nonGroupingDimensionsSizes = calculateBestLenghts(nonGroupingDimensions);
70     initializeNumberFormatPattern();
71   }
72
73   private void initializeNumberFormatPattern() {
74     NumberFormat JavaDoc format = NumberFormat.getInstance(LanguageTraslator.getLocale());
75     numberFormatPattern = ((DecimalFormat JavaDoc)format).toPattern();
76   }
77
78   /**
79    * Retorna la definicion del reporte
80    * @return
81    * @throws net.sf.jasperreports.engine.JRException
82    */

83   public JasperDesign getJasperDefinition(boolean isLandscape) throws JRException {
84     JasperDesign jasperDesign = new JasperDesign();
85     if(isLandscape) {
86       jasperDesign.setOrientation(JasperDesign.ORIENTATION_LANDSCAPE);
87       jasperDesign.setPrintOrder(JasperDesign.PRINT_ORDER_VERTICAL);
88       jasperDesign.setPageWidth(842);
89       jasperDesign.setPageHeight(595);
90     } else {
91       jasperDesign.setOrientation(JasperDesign.ORIENTATION_PORTRAIT);
92       jasperDesign.setPrintOrder(JasperDesign.PRINT_ORDER_HORIZONTAL);
93       jasperDesign.setPageWidth(595);
94       jasperDesign.setPageHeight(842);
95     }
96     jasperDesign.setName("BasicReport");
97     addFields(jasperDesign);
98     addDetails(jasperDesign);
99     addGroups(jasperDesign);
100     addVariables(jasperDesign);
101     addTitle(jasperDesign);
102     addParams(jasperDesign);
103     //addPageHeader(jasperDesign);
104
return jasperDesign;
105   }
106
107   /**
108    * Agrega los parametros del reporte a la definicion del jasper para que puedan ser utilizados en la visualizacion.
109    * @param jasperDesign
110    * @throws JRException
111    */

112   private void addParams(JasperDesign jasperDesign) throws JRException {
113     for (Iterator iterator = reportParams.entrySet().iterator(); iterator.hasNext();) {
114       Map.Entry param = (Map.Entry) iterator.next();
115       JRDesignParameter parameter = new JRDesignParameter();
116       parameter.setName(param.getKey().toString());
117       parameter.setDescription(param.getKey().toString());
118       Class JavaDoc paramClass = getClassFor(param);
119       parameter.setValueClass(paramClass);
120       jasperDesign.addParameter(parameter);
121     }
122   }
123
124   /**
125    * Obtiene la clase a la que pertenece el valor de un parametro dado.
126    * @param param
127    * @return
128    */

129   private Class JavaDoc getClassFor(Map.Entry param) {
130     Class JavaDoc result = java.lang.String JavaDoc.class;
131     Map map = new HashMap();
132     map.put(param.getKey(), param.getValue());
133     ReportMap.setParametersToSimpleType(map);
134     if(map.get(param.getKey())!=null){
135       result = map.get(param.getKey()).getClass();
136     }
137     return result;
138   }
139
140   /**
141    * Agrega los campos a la definicion
142    * @param jasperDesign
143    * @throws net.sf.jasperreports.engine.JRException
144    */

145   private void addFields(JasperDesign jasperDesign) throws JRException {
146   //Fields
147
String JavaDoc name;
148     for(int i = 0 ; i < dimensions.size() ; i++) {
149       JRDesignField field = new JRDesignField();
150       name = dimensions.elementAt(i).toString();
151       field.setName(name);
152       field.setValueClass(java.lang.String JavaDoc.class);
153       jasperDesign.addField(field);
154     }
155     for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
156       JRDesignField field = new JRDesignField();
157       name = nonGroupingDimensions.elementAt(i).toString();
158       field.setName(name);
159       field.setValueClass(java.lang.String JavaDoc.class);
160       jasperDesign.addField(field);
161     }
162     for(int i = 0 ; i < metrics.size() ; i++) {
163       JRDesignField field = new JRDesignField();
164       name = metrics.elementAt(i).toString();
165       field.setName(name);
166       field.setValueClass(java.lang.Float JavaDoc.class);
167       jasperDesign.addField(field);
168     }
169     for(int i = 0 ; i < accuMetrics.size() ; i++) {
170       JRDesignField field = new JRDesignField();
171       name = accuMetrics.elementAt(i).toString();
172       field.setName(name);
173       field.setValueClass(java.lang.Float JavaDoc.class);
174       jasperDesign.addField(field);
175     }
176   }
177
178   /**
179    * Agrega los grupos a la definicion
180    * @param jasperDesign
181    * @throws net.sf.jasperreports.engine.JRException
182    */

183   private void addGroups(JasperDesign jasperDesign) throws JRException {
184     //groups
185
JRDesignExpression expression;
186     JRDesignBand band;
187     JRDesignGroup group = null;
188     printAt = 0;
189     if(dimensions.size()==0){
190       if (jasperDesign.getFields().length==0){
191         dimensions.add("");
192       } else{
193         dimensions.add(jasperDesign.getFields()[0].getName());
194       }
195       dimensionsSizes.add(new Integer JavaDoc(4));
196     }
197     for(int j = 0 ; j < dimensions.size() ; j++) {
198       group = new JRDesignGroup();
199       group.setName(dimensions.elementAt(j).toString()+"_GROUP");
200       group.setReprintHeaderOnEachPage(true);
201
202       expression = new JRDesignExpression();
203       expression.setValueClass(java.lang.String JavaDoc.class);
204       expression.setText("$F{"+dimensions.elementAt(j)+"}");
205       group.setExpression(expression);
206
207       if(j + 1 == dimensions.size()) {
208         band = new JRDesignBand();
209         band.setHeight(60);
210       } else {
211         band = new JRDesignBand();
212         band.setHeight(20);
213       }
214
215
216       JRDesignStaticText text = new JRDesignStaticText();
217       String JavaDoc caption = "";
218       if(dimensions.elementAt(j)!=""){
219         caption = reportResult.getReportSpec().getDimensionFromName(dimensions.elementAt(j).toString()).getCaption() + ":";
220       }
221       text.setX(printAt);
222       text.setY(0);
223       text.setWidth(getLenghtForCaption((new Integer JavaDoc(caption.length())).intValue()));
224       text.setHeight(20);
225       text.setText(caption);
226       band.addElement(text);
227
228       JRDesignTextField textField = new JRDesignTextField();
229       textField.setX(printAt + getLenghtForCaption((new Integer JavaDoc(caption.length())).intValue()) - 5);
230       textField.setY(0);
231       textField.setWidth(getLenghtForCaption(((Integer JavaDoc) dimensionsSizes.elementAt(j)).intValue()));
232       textField.setHeight(20);
233       expression = new JRDesignExpression();
234       expression.setValueClass(java.lang.String JavaDoc.class);
235       expression.setText("$F{"+dimensions.elementAt(j)+"}");
236       textField.setExpression(expression);
237       band.addElement(textField);
238       group.setGroupHeader(band);
239
240       if(j + 1 == dimensions.size()) {
241         for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
242           int x = ((Integer JavaDoc) nonGroupingDimensionPosition.elementAt(i)).intValue();
243           text = new JRDesignStaticText();
244           text.setX(x);
245           text.setY(20);
246           //text.setWidth(((Integer) metricsWidth.elementAt(i)).intValue());
247
int first = getLenghtForCaption(((Integer JavaDoc) nonGroupingDimensionsSizes.elementAt(i)).intValue());
248           int second = getLenghtForCaption(nonGroupingDimensions.elementAt(i).toString().length());
249           if(first >= second) {
250             text.setWidth(getLenghtForCaption(first));
251           } else {
252             text.setWidth(getLenghtForCaption(second));
253           }
254           text.setHeight(20);
255           text.setText(reportResult.getReportSpec().getDimensionFromName(nonGroupingDimensions.elementAt(i).toString()).getCaption());
256           band.addElement(text);
257         }
258
259         for(int i = 0 ; i < metrics.size() ; i++) {
260           int x = ((Integer JavaDoc) metricsPosition.elementAt(i)).intValue();
261           text = new JRDesignStaticText();
262           text.setX(x);
263           text.setY(20);
264           text.setWidth(((Integer JavaDoc) metricsWidth.elementAt(i)).intValue());
265           text.setHeight(20);
266           text.setText(reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption());
267           band.addElement(text);
268         }
269
270         for(int i = 0 ; i < accMetricsPosition.size() ; i++) {
271           int x = ((Integer JavaDoc) accMetricsPosition.elementAt(i)).intValue();
272           text = new JRDesignStaticText();
273           text.setX(x);
274           text.setY(20);
275           text.setWidth(((Integer JavaDoc) accMetricsWidht.elementAt(i)).intValue());
276           text.setHeight(20);
277           String JavaDoc metricName = getMetricName(accuMetrics.elementAt(i).toString());
278           String JavaDoc metricCaption = reportResult.getReportSpec().getMetricFromName(metricName).getCaption();
279           text.setText(metricCaption + " " + LanguageTraslator.traslate("315"));
280           band.addElement(text);
281         }
282         group.setGroupHeader(band);
283       }
284
285
286       band = new JRDesignBand();
287       band.setHeight(30);
288       text = new JRDesignStaticText();
289       text.setX(printAt);
290       text.setY(0);
291       text.setWidth(40);
292       text.setHeight(20);
293       text.setText(LanguageTraslator.traslate("358"));
294       band.addElement(text);
295       for(int i = 0 ; i < metrics.size() ; i++) {
296         band.setHeight(20);
297         textField = new JRDesignTextField();
298         textField.setX(((Integer JavaDoc)metricsPosition.elementAt(i)).intValue());
299         textField.setY(0);
300         textField.setWidth(getLenghtForCaption(((Integer JavaDoc) dimensionsSizes.elementAt(j)).intValue()) + 20);
301         textField.setHeight(20);
302         expression = new JRDesignExpression();
303         expression.setValueClass(java.lang.Float JavaDoc.class);
304         expression.setText("$V{"+dimensions.elementAt(j).toString() + metrics.elementAt(i).toString()+"_SUM}");
305         textField.setPattern(numberFormatPattern);
306         textField.setExpression(expression);
307         band.addElement(textField);
308         group.setGroupFooter(band);
309       }
310       groupingDimensionPosition.add(new Integer JavaDoc(printAt));
311       printAt += 10;//getLenghtForCaption(((Integer)dimensionsSizes.elementAt(j)).intValue());
312

313       jasperDesign.addGroup(group);
314     }
315   }
316
317   private String JavaDoc getMetricName(String JavaDoc s) {
318     StringTokenizer tokenizer = new StringTokenizer(s, "_");
319     String JavaDoc returnVal = tokenizer.nextToken();
320     return returnVal;
321   }
322
323   /**
324    * Agrega las variables a la definicion
325    * @param jasperDesign
326    * @throws net.sf.jasperreports.engine.JRException
327    */

328   private void addVariables(JasperDesign jasperDesign) throws JRException {
329     //Variables
330
JRDesignExpression expression;
331     for(int j = 0 ; j < dimensions.size() ; j++) {
332       for(int i = 0 ; i < metrics.size() ; i++) {
333         JRDesignVariable jrVariable = new JRDesignVariable();
334         jrVariable.setName(dimensions.elementAt(j).toString()+ metrics.elementAt(i).toString()+"_SUM");
335         jrVariable.setValueClass(java.lang.Float JavaDoc.class);
336         jrVariable.setResetType(JRDesignVariable.RESET_TYPE_GROUP);
337         jrVariable.setCalculation(JRDesignVariable.CALCULATION_SUM);
338         jrVariable.setResetGroup((JRGroup)jasperDesign.getGroupsList().get(j));
339         expression = new JRDesignExpression();
340         expression.setValueClass(java.lang.Float JavaDoc.class);
341         expression.setText("$F{"+metrics.elementAt(i).toString()+"}");
342         jrVariable.setExpression(expression);
343         jasperDesign.addVariable(jrVariable);
344       }
345     }
346   }
347
348   /**
349    * Agrega los detalles a la definicion
350    * @param jasperDesign
351    */

352   private void addDetails(JasperDesign jasperDesign) {
353     //Details
354
int printAt = calculateStartForItem();
355     JRDesignBand band = new JRDesignBand();
356     band.setHeight(20);
357     JRDesignExpression expression;
358     for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
359       JRDesignTextField textField = new JRDesignTextField();
360       textField.setX(printAt);
361       textField.setY(0);
362       textField.setWidth(getLenghtForCaption(((Integer JavaDoc) nonGroupingDimensionsSizes.elementAt(i)).intValue()));
363       textField.setHeight(20);
364       expression = new JRDesignExpression();
365       expression.setValueClass(java.lang.String JavaDoc.class);
366       expression.setText("$F{"+nonGroupingDimensions.elementAt(i)+"}");
367       textField.setExpression(expression);
368       band.addElement(textField);
369
370       nonGroupingDimensionPosition.add(new Integer JavaDoc(printAt));
371
372       int first = getLenghtForCaption(((Integer JavaDoc) nonGroupingDimensionsSizes.elementAt(i)).intValue());
373       int second = getLenghtForCaption(nonGroupingDimensions.elementAt(i).toString().length());
374       if(first >= second) {
375         printAt += first;
376       } else {
377         printAt += second;
378       }
379       //printAt += getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue());
380

381     }
382     for(int i = 0 ; i < metrics.size() ; i++) {
383       JRDesignTextField textField = new JRDesignTextField();
384       textField.setX(printAt + 25);
385       textField.setY(0);
386       //textField.setWidth(getLenghtForCaption(((Integer) metricsSizes.elementAt(i)).intValue())+15);
387

388       metricsWidth.add(new Integer JavaDoc(getLenghtForCaption((new Integer JavaDoc(metrics.elementAt(i).toString().length())).intValue())+25));
389       textField.setWidth(getLenghtForCaption((new Integer JavaDoc(metrics.elementAt(i).toString().length())).intValue())+25);
390
391       textField.setHeight(20);
392       textField.setPattern(numberFormatPattern);
393       expression = new JRDesignExpression();
394       expression.setValueClass(java.lang.Float JavaDoc.class);
395       expression.setText("$F{"+metrics.elementAt(i)+"}");
396       textField.setExpression(expression);
397       band.addElement(textField);
398       metricsPosition.add(new Integer JavaDoc(printAt + 25));
399       printAt += getLenghtForCaption(((Integer JavaDoc) metricsSizes.elementAt(i)).intValue()) + 25 ;
400     }
401
402     for(int i = 0 ; i < accuMetrics.size() ; i++) {
403       JRDesignTextField textField = new JRDesignTextField();
404       textField.setX(printAt + 25);
405       textField.setY(0);
406
407       //textField.setWidth(getLenghtForCaption(((Integer) accuMetricsSizes.elementAt(i)).intValue())+15);
408

409       accMetricsWidht.add(new Integer JavaDoc(getLenghtForCaption((new Integer JavaDoc(accuMetrics.elementAt(i).toString().length())).intValue())+25));
410       textField.setWidth(getLenghtForCaption((new Integer JavaDoc(accuMetrics.elementAt(i).toString().length())).intValue())+25);
411
412       textField.setHeight(20);
413       textField.setPattern(numberFormatPattern);
414       expression = new JRDesignExpression();
415       expression.setValueClass(java.lang.Float JavaDoc.class);
416       expression.setText("$F{"+accuMetrics.elementAt(i)+"}");
417       textField.setExpression(expression);
418       band.addElement(textField);
419       metricsPosition.add(new Integer JavaDoc(printAt + 25));
420       accMetricsPosition.add(new Integer JavaDoc(printAt + 25));
421       printAt += getLenghtForCaption(((Integer JavaDoc) accuMetricsSizes.elementAt(i)).intValue()) + 25 ;
422     }
423
424     jasperDesign.setDetail(band);
425   }
426
427   /**
428    * Agrega la cabecera de pagina a la definicion
429    * @param jasperDesign
430    */

431   /*private void addPageHeader(JasperDesign jasperDesign) {
432     int printAt = 45;
433     JRDesignBand band = new JRDesignBand();
434     for(int i = 0 ; i < dimensions.size() ; i++) {
435       String caption = reportResult.getReportSpec().getDimensionFromName(dimensions.elementAt(i).toString()).getCaption();
436       JRDesignStaticText text = new JRDesignStaticText();
437       text.setX(0);
438       text.setY(0);
439       text.setWidth(40);
440       text.setHeight(15);
441       text.setText(LanguageTraslator.traslate("308"));
442       band.addElement(text);
443       text = new JRDesignStaticText();
444       text.setX(printAt);
445       text.setY(0);
446       text.setWidth(getLenghtForCaptionHeader(caption.length()));
447       text.setHeight(15);
448       if(i == dimensions.size() - 1) {
449         text.setText(caption);
450       } else {
451         text.setText(caption + ",");
452       }
453       band.addElement(text);
454       printAt += getLenghtForCaptionHeader(caption.length());
455       JRDesignLine line = new JRDesignLine();
456       line = new JRDesignLine();
457       line.setX(0);
458       line.setY(16);
459       line.setWidth(515);
460       line.setHeight(0);
461       band.addElement(line);
462     }
463     printAt = 45;
464     for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
465       String caption = reportResult.getReportSpec().getDimensionFromName(nonGroupingDimensions.elementAt(i).toString()).getCaption();
466       JRDesignStaticText text = new JRDesignStaticText();
467       text.setX(0);
468       text.setY(15);
469       text.setWidth(40);
470       text.setHeight(15);
471       text.setText(LanguageTraslator.traslate("309"));
472       band.addElement(text);
473       text = new JRDesignStaticText();
474       text.setX(printAt);
475       text.setY(15);
476       text.setWidth(getLenghtForCaptionHeader(caption.length()));
477       text.setHeight(15);
478       if(i == nonGroupingDimensions.size() - 1) {
479         text.setText(caption);
480       } else {
481         text.setText(caption + ",");
482       }
483       band.addElement(text);
484       printAt += getLenghtForCaptionHeader(caption.length());
485       JRDesignLine line = new JRDesignLine();
486       line = new JRDesignLine();
487       line.setX(0);
488       line.setY(31);
489       line.setWidth(515);
490       line.setHeight(0);
491       band.addElement(line);
492     }
493     printAt = 45;
494     for(int i = 0 ; i < metrics.size() ; i++) {
495       String caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption();
496       JRDesignStaticText text = new JRDesignStaticText();
497       text.setX(0);
498       text.setY(30);
499       text.setWidth(40);
500       text.setHeight(15);
501       text.setText(LanguageTraslator.traslate("310"));
502       band.addElement(text);
503       text = new JRDesignStaticText();
504       text.setX(printAt);
505       text.setY(30);
506       text.setWidth(getLenghtForCaptionHeader(caption.length()));
507       text.setHeight(15);
508       if(accuMetrics.size() == 0) {
509         if(i == metrics.size() - 1) {
510           text.setText(caption);
511         } else {
512           text.setText(caption + ",");
513         }
514       } else {
515         text.setText(caption + ",");
516       }
517       band.addElement(text);
518       printAt += getLenghtForCaptionHeader(caption.length());
519       JRDesignLine line = new JRDesignLine();
520       line = new JRDesignLine();
521       line.setX(0);
522       line.setY(46);
523       line.setWidth(515);
524       line.setHeight(0);
525       band.addElement(line);
526     }
527     for(int i = 0 ; i < accuMetrics.size() ; i++) {
528       String caption = getAccMetricCaption(accuMetrics.elementAt(i).toString());
529       JRDesignStaticText text = new JRDesignStaticText();
530       text.setX(0);
531       text.setY(30);
532       text.setWidth(40);
533       text.setHeight(15);
534       text.setText(LanguageTraslator.traslate("310"));
535       band.addElement(text);
536       text = new JRDesignStaticText();
537       text.setX(printAt);
538       text.setY(30);
539       text.setWidth(getLenghtForCaptionHeader(caption.length()));
540       text.setHeight(15);
541       if(i == accuMetrics.size() - 1) {
542         text.setText(caption);
543       } else {
544         text.setText(caption + ",");
545       }
546       band.addElement(text);
547       printAt += getLenghtForCaptionHeader(caption.length());
548       JRDesignLine line = new JRDesignLine();
549       line = new JRDesignLine();
550       line.setX(0);
551       line.setY(46);
552       line.setWidth(515);
553       line.setHeight(0);
554       band.addElement(line);
555     }
556     jasperDesign.setPageHeader(band);
557   }*/

558
559   private void addPageHeader(JasperDesign jasperDesign) {
560
561       JRDesignBand band = new JRDesignBand();
562
563       String JavaDoc caption = getCaption(LanguageTraslator.traslate("308"), dimensions, new StringBuffer JavaDoc());
564       JRDesignStaticText text = new JRDesignStaticText();
565       text = new JRDesignStaticText();
566       text.setX(0);
567       text.setY(0);
568       text.setWidth(getLenghtForCaptionHeader(caption.length()));
569       text.setHeight(15);
570       text.setText(caption);
571       band.addElement(text);
572
573       caption = getCaption(LanguageTraslator.traslate("309"), nonGroupingDimensions, new StringBuffer JavaDoc());
574       text = new JRDesignStaticText();
575       text.setX(0);
576       text.setY(15);
577       text.setWidth(getLenghtForCaptionHeader(caption.length()));
578       text.setHeight(15);
579       text.setText(caption);
580       band.addElement(text);
581
582       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
583       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
584       sb.append(getCaption(LanguageTraslator.traslate("310"), metrics, buffer));
585       sb.append(getCaption("", accuMetrics, buffer));
586       text = new JRDesignStaticText();
587       text.setX(0);
588       text.setY(30);
589       text.setWidth(getLenghtForCaptionHeader(sb.toString().length()));
590       text.setHeight(15);
591       text.setText(sb.toString());
592       band.addElement(text);
593
594       jasperDesign.setPageHeader(band);
595   }
596
597   private String JavaDoc getCaption(String JavaDoc tittle, Vector vector, StringBuffer JavaDoc bufferString) {
598     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
599     buffer.append(tittle);
600     if(vector == dimensions || vector == nonGroupingDimensions) {
601       for(int i = 0 ; i < vector.size() ; i++){
602         String JavaDoc caption = reportResult.getReportSpec().getDimensionFromName(vector.elementAt(i).toString()).getCaption();
603         if(i == vector.size() - 1) {
604           buffer.append(caption);
605         } else {
606           buffer.append(caption + ", ");
607         }
608       }
609     } else if(vector == metrics) {
610       if(accuMetrics.size() == 0) {
611         for(int i = 0 ; i < vector.size() ; i++) {
612           String JavaDoc caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption();
613           if(i == vector.size() - 1) {
614             buffer.append(caption);
615           } else {
616             buffer.append(caption + ", ");
617           }
618         }
619       } else {
620         for(int i = 0 ; i < vector.size() ; i++) {
621           String JavaDoc caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption();
622           buffer.append(caption + ", ");
623         }
624       }
625     } else {
626       Object JavaDoc [] accMetrics = reportResult.getReportSpec().getAccumulableMetrics();
627       for(int i = 0 ; i < accMetrics.length ; i++) {
628         ReportMetricSpec metric = (ReportMetricSpec) accMetrics[i];
629         if(i == accMetrics.length - 1) {
630           buffer.append(metric.getCaption() + " " + LanguageTraslator.traslate("315"));
631         } else {
632           buffer.append(metric.getCaption() + " " + LanguageTraslator.traslate("315") + ", ") ;
633         }
634
635       }
636     }
637     return buffer.toString();
638   }
639
640   /**
641    * Agrega el titulo al reporte.
642    * @param jasperDesign
643    */

644   private void addTitle(JasperDesign jasperDesign) throws JRException{
645     //Title
646
JRDesignTextField textField = new JRDesignTextField();
647     //JRDesignLine line = new JRDesignLine();
648
JRDesignBand band = new JRDesignBand();
649         band.setHeight(50);
650         JRDesignStaticText staticText = new JRDesignStaticText();
651         staticText.setX(0);
652         staticText.setY(10);
653         staticText.setWidth(515);
654         staticText.setHeight(30);
655         staticText.setTextAlignment(JRTextElement.TEXT_ALIGN_CENTER);
656         JRDesignReportFont bigFont = new JRDesignReportFont();
657         bigFont.setName("Arial_Normal");
658         bigFont.setDefault(true);
659         bigFont.setFontName("Arial");
660         bigFont.setSize(16);
661         bigFont.setPdfFontName("Helvetica");
662         bigFont.setPdfEncoding("Cp1252");
663         bigFont.setPdfEmbedded(false);
664     jasperDesign.addFont(bigFont);
665     staticText.setFont(bigFont);
666     staticText.setText(tittle);
667         band.addElement(staticText);
668     /*line = new JRDesignLine();
669         line.setX(0);
670         line.setY(0);
671         line.setWidth(515);
672         line.setHeight(0);
673         band.addElement(line);*/

674         jasperDesign.setTitle(band);
675
676     JRDesignBand emptyBand = new JRDesignBand();
677     emptyBand.setHeight(1);
678     jasperDesign.setPageHeader(emptyBand);
679     emptyBand = new JRDesignBand();
680     emptyBand.setHeight(1);
681     jasperDesign.setColumnHeader(emptyBand);
682     emptyBand = new JRDesignBand();
683     emptyBand.setHeight(1);
684     jasperDesign.setColumnFooter(emptyBand);
685
686     band = new JRDesignBand();
687     band.setHeight(15);
688     staticText = new JRDesignStaticText();
689     staticText.setX(0);
690     staticText.setY(0);
691     staticText.setWidth(40);
692     staticText.setHeight(15);
693     staticText.setText("Pagina: ");
694     band.addElement(staticText);
695     textField = new JRDesignTextField();
696     textField.setX(40);
697     textField.setY(0);
698     textField.setWidth(100);
699     textField.setHeight(15);
700     JRDesignExpression expression = new JRDesignExpression();
701     expression.setValueClass(java.lang.Integer JavaDoc.class);
702     expression.setText("$V{PAGE_NUMBER}");
703     textField.setExpression(expression);
704     band.addElement(textField);
705     jasperDesign.setPageFooter(band);
706
707     band = new JRDesignBand();
708     band.setHeight(20);
709     jasperDesign.setSummary(band);
710   }
711
712 /* private void addColumnsHeader(JasperDesign jasperDesign) {
713     //columnHeader
714     JRDesignBand band = new JRDesignBand();
715     for(int i = 0 ; i < metrics.size() ; i++) {
716       band.setHeight(20);
717       JRDesignStaticText text = new JRDesignStaticText();
718       text.setX(((Integer)metricsPosition.elementAt(i)).intValue());
719       text.setY(0);
720       text.setWidth(40);
721       text.setHeight(20);
722       text.setText(reportResult.getReportSpec().getDimensionFromName(metrics.elementAt(i).toString()).getCaption());
723       band.addElement(text);
724     }
725     jasperDesign.setColumnHeader(band);
726   }
727
728   private void addColumnsFooter(JasperDesign jasperDesign) {
729     JRDesignBand band = new JRDesignBand();
730     JRDesignTextField textField;
731     String name;
732     band.setHeight(20);
733     JRDesignStaticText text = new JRDesignStaticText();
734     text.setX(40);
735     text.setY(0);
736     text.setWidth(40);
737     text.setHeight(20);
738     text.setText("Total");
739     band.addElement(text);
740
741     for(int i = 0 ; i < metrics.size() ; i++) {
742       textField = new JRDesignTextField();
743       textField.setX(((Integer)metricsPosition.elementAt(i)).intValue());
744       textField.setY(0);
745       textField.setWidth(100);
746       textField.setHeight(15);
747
748       JRDesignExpression expression = new JRDesignExpression();
749       expression.setValueClass(java.lang.Float.class);
750       name = metrics.elementAt(i).toString()+"_SUM";
751       expression.setText("$V{"+name+"}");
752       textField.setExpression(expression);
753       band.addElement(textField);
754
755       band.addElement(text);
756     }
757
758     jasperDesign.setColumnFooter(band);
759   }
760   */

761
762   /**
763    * Inicializa los vectores con la descripcion de las dimensiones
764    * que agrupan, las que no agrupan y las metricas.
765    * @param dimensionsCount
766    * @param metricsCount
767    * @param nonGroupingDimensionsCount
768    * @param accMetricsCount
769    */

770   private void initializeVectors(int dimensionsCount, int metricsCount, int nonGroupingDimensionsCount, int accMetricsCount) {
771     dimensions = new Vector();
772     nonGroupingDimensions = new Vector();
773     metrics = new Vector();
774     accuMetrics = new Vector();
775     int i = 0;
776     for (; i < dimensionsCount; i++) {
777       dimensions.add(data.getColumnName(i));
778     }
779     for (int z = 0; z < nonGroupingDimensionsCount; i++, z++) {
780       nonGroupingDimensions.add(data.getColumnName(i));
781     }
782     for (int j = 0; j < metricsCount; i++, j++) {
783       metrics.add(data.getColumnName(i));
784     }
785     for (int j = 0; j < accMetricsCount; i++, j++) {
786       accuMetrics.add(data.getColumnName(i));
787     }
788   }
789
790   /**
791    * Determina un Vector con los anchos mas precisos.
792    * @param vector
793    * @return
794    */

795   private Vector calculateBestLenghts(Vector vector) {
796     Vector bestSizes = null;
797     Vector ColumnSizes;
798     if (data.getRowCount() != 0) {
799       bestSizes = new Vector();
800       for (int i = 0; i < vector.size(); i++) {
801         ColumnSizes = new Vector();
802         String JavaDoc current = data.getValueAt(0, getColumnIndexFromName(vector.elementAt(i).toString())).toString();
803         if (data.getRowCount() == 1) {
804           ColumnSizes.add(new Integer JavaDoc(current.length()));
805           break;
806         }
807         else {
808           ColumnSizes.add(new Integer JavaDoc(current.length()));
809           for (int j = 0; j < data.getRowCount(); j++) {
810             if (!current.equals(data.getValueAt(j, i).toString())) {
811               ColumnSizes.add(new Integer JavaDoc(data.getValueAt(j, getColumnIndexFromName(vector.elementAt(i).toString())).toString().length()));
812               current = data.getValueAt(j, i).toString();
813             }
814           }
815           bestSizes.add(getBestSize(ColumnSizes));
816         }
817       }
818     }
819     return bestSizes;
820   }
821
822   /**
823    * Retorna un indice a partir de un nombre de columna.
824    * @param columnName
825    * @return
826    */

827   private int getColumnIndexFromName(String JavaDoc columnName) {
828     int index = 0;
829     for (int i = 0; i < data.getColumnCount(); i++) {
830       if (data.getColumnName(i).equals(columnName)) {
831         index = i;
832         break;
833       }
834     }
835     return index;
836   }
837
838   /**
839    * Retorna el ancho mas eficiente comparando los elementos
840    * del Vector
841    * @param ColumnSizes
842    * @return
843    */

844   private Integer JavaDoc getBestSize(Vector ColumnSizes) {
845     int compareTo = 0;
846     for (int i = 0; i < ColumnSizes.size(); i++) {
847       if (((Integer JavaDoc) ColumnSizes.elementAt(i)).intValue() > compareTo) {
848         compareTo = ((Integer JavaDoc) ColumnSizes.elementAt(i)).intValue();
849       }
850     }
851     return new Integer JavaDoc(compareTo);
852   }
853
854   /**
855    * Determina el ancho para las descripciones
856    * de la cabecera
857    * @param chars
858    * @return
859    */

860   private int getLenghtForCaptionHeader(int chars){
861     //return chars * 8 - chars;
862
Integer JavaDoc integer = new Integer JavaDoc(chars);
863     Double JavaDoc qsrt = new Double JavaDoc(Math.sqrt(integer.doubleValue()));
864     return chars * 7 - ((chars / 2) - 3 * qsrt.intValue());
865   }
866
867   /**
868    * Determina el comienzo donde se mostraran las
869    * metricas o dimensiones que no agrupan.
870    * @return
871    */

872   private int calculateStartForItem() {
873     int width = 0 ;
874     for(int i = 0 ; i < dimensionsSizes.size() ; i++){
875       width += (((Integer JavaDoc)dimensionsSizes.elementAt(i)).intValue());
876     }
877
878     //return (width) + 100;
879
return 0;
880   }
881
882   /**
883    * Retorna un ancho preciso a partir de una
884    * determinada cantidad de caracteres
885    * @param chars
886    * @return
887    */

888   private int getLenghtForCaption(int chars) {
889     if(chars == 2 ) {
890       return chars * 7;
891     }
892     Integer JavaDoc integer = new Integer JavaDoc(chars);
893     Double JavaDoc qsrt = new Double JavaDoc(Math.sqrt(integer.doubleValue()));
894     return chars * 8 - ((chars / 2) - 3 * qsrt.intValue());
895   }
896
897   /**
898    * Retorna los fields.
899    * @return
900    */

901   public Map getFields() {
902     if (fields == null){
903       fields = new HashMap();
904     }
905     return fields;
906   }
907
908   /**
909    * Retorna las variables
910    * @return
911    */

912   public Map getVariables() {
913     if (variables == null ){
914       variables = new HashMap();
915     }
916     return variables;
917   }
918 }
Popular Tags