KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.reportgenerator.common;
2
3 import javax.swing.table.TableModel JavaDoc;
4
5 import net.sf.jasperreports.engine.design.*;
6 import net.sf.jasperreports.engine.JRException;
7 import net.sf.jasperreports.engine.JRGroup;
8 import net.sf.jasperreports.engine.JRTextElement;
9 import net.sf.jasperreports.engine.JRQuery;
10
11 import java.util.*;
12 import java.text.NumberFormat JavaDoc;
13 import java.text.DecimalFormat JavaDoc;
14
15 /**
16  *
17  * User: jbassino
18  * Date: 01/06/2004
19  * Time: 14:29:40
20  *
21  */

22 public class StaticSQLJasperReportDefinition implements IJasperDefinition{
23   private Map fields;
24   private Map variables;
25   private TableModel JavaDoc data;
26   private String JavaDoc tittle;
27   private Vector dimensions;
28   private Vector metrics;
29   private Vector accuMetrics;
30   private Vector nonGroupingDimensions;
31   private Vector dimensionsSizes;
32   private Vector metricsSizes;
33   private Vector accuMetricsSizes;
34   private Vector nonGroupingDimensionsSizes;
35   private int printAt;
36   private Vector metricsPosition = new Vector();
37   private Vector nonGroupingDimensionPosition = new Vector();
38   private Vector groupingDimensionPosition = new Vector();
39   //private ReportResult reportResult;
40
private ReportSpec reportSpec;
41   private String JavaDoc numberFormatPattern;
42   private Vector metricsWidth;
43   private Vector accMetricsPosition;
44   private Vector accMetricsWidht;
45   private JRQuery query = null;
46   private JasperDesign jasperDesign = null;
47   private Map reportParams;
48
49   public StaticSQLJasperReportDefinition(ReportSpec reportSpec,TableModel JavaDoc data, int dimensionsCount, int metricsCount, int nonGroupingDimensionsCount, int accuMetricsCount, String JavaDoc tittle, Map params){
50     this.data = data;
51     this.tittle = tittle;
52     //this.reportResult = reportResult;
53
this.reportSpec = reportSpec;
54     metricsWidth = new Vector();
55     accMetricsPosition = new Vector();
56     accMetricsWidht = new Vector();
57     initializeVectors(dimensionsCount, metricsCount, nonGroupingDimensionsCount, accuMetricsCount);
58     reportParams = params;
59     dimensionsSizes = calculateBestLenghts(dimensions);
60     metricsSizes = calculateBestLenghts(metrics);
61     accuMetricsSizes = calculateBestLenghts(this.accuMetrics);
62     nonGroupingDimensionsSizes = calculateBestLenghts(nonGroupingDimensions);
63     initializeNumberFormatPattern();
64   }
65
66   public StaticSQLJasperReportDefinition(IJasperDefinition jasper, boolean isLandscape) throws InfoException{
67     try{
68       jasperDesign = jasper.getJasperDefinition(isLandscape);
69     }catch(Exception JavaDoc e){
70       throw new InfoException(LanguageTraslator.traslate("221"),e);
71     }
72   }
73
74   private void initializeNumberFormatPattern() {
75     NumberFormat JavaDoc format = NumberFormat.getInstance(LanguageTraslator.getLocale());
76     numberFormatPattern = ((DecimalFormat JavaDoc)format).toPattern();
77   }
78
79   public JasperDesign getJasperDefinition(boolean isLandscape) throws JRException {
80     if(query==null){
81           throw new JRException("La query es nula");
82         }
83     if(jasperDesign!=null){
84       jasperDesign.setQuery(query);
85       return jasperDesign;
86     }
87     JasperDesign jasperDesign = new JasperDesign();
88     if(isLandscape) {
89       jasperDesign.setOrientation(JasperDesign.ORIENTATION_LANDSCAPE);
90       jasperDesign.setPrintOrder(JasperDesign.PRINT_ORDER_VERTICAL);
91       jasperDesign.setPageWidth(842);
92       jasperDesign.setPageHeight(595);
93     } else {
94       jasperDesign.setOrientation(JasperDesign.ORIENTATION_PORTRAIT);
95       jasperDesign.setPrintOrder(JasperDesign.PRINT_ORDER_HORIZONTAL);
96       jasperDesign.setPageWidth(595);
97       jasperDesign.setPageHeight(842);
98     }
99     jasperDesign.setName("BasicReport");
100     addFields(jasperDesign);
101     addDetails(jasperDesign);
102     addGroups(jasperDesign);
103     addVariables(jasperDesign);
104     addTitle(jasperDesign);
105     addParams(jasperDesign);
106     //addPageHeader(jasperDesign);
107
if(query==null){
108       throw new JRException("La query es nula");
109     }
110     jasperDesign.setQuery(query);
111     return jasperDesign;
112   }
113
114   /**
115    * Agrega los parametros del reporte a la definicion del jasper para que puedan ser utilizados en la visualizacion.
116    * @param jasperDesign
117    * @throws JRException
118    */

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

136   private Class JavaDoc getClassFor(Map.Entry param) {
137     Class JavaDoc result = java.lang.String JavaDoc.class;
138     Map map = new HashMap();
139     map.put(param.getKey(), param.getValue());
140     ReportMap.setParametersToSimpleType(map);
141     if(map.get(param.getKey())!=null){
142       result = map.get(param.getKey()).getClass();
143     }
144     return result;
145   }
146
147
148   public void setSQLText(String JavaDoc sqlText){
149     String JavaDoc queryText = removeFilters(sqlText);
150     System.out.println(queryText);
151     query = new JRDesignQuery();
152     ((JRDesignQuery)query).setText(queryText);
153   }
154
155   private String JavaDoc removeFilters(String JavaDoc sqlText) {
156     String JavaDoc result = "";
157     StringTokenizer tokenizer = new StringTokenizer(sqlText, "%");
158     String JavaDoc token;
159     int i=0;
160     while(tokenizer.hasMoreElements()){
161       token = tokenizer.nextToken();
162       result += token;
163       if(tokenizer.hasMoreElements()){
164         tokenizer.nextToken();
165       }
166     }
167     return result;
168   }
169
170   private void addFields(JasperDesign jasperDesign) throws JRException {
171   //Fields
172
String JavaDoc name;
173     for(int i = 0 ; i < dimensions.size() ; i++) {
174       JRDesignField field = new JRDesignField();
175       name = dimensions.elementAt(i).toString();
176       field.setName(name);
177       field.setValueClass(String JavaDoc.class);
178       jasperDesign.addField(field);
179     }
180     for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
181       JRDesignField field = new JRDesignField();
182       name = nonGroupingDimensions.elementAt(i).toString();
183       field.setName(name);
184       field.setValueClass(String JavaDoc.class);
185       jasperDesign.addField(field);
186     }
187     for(int i = 0 ; i < metrics.size() ; i++) {
188       JRDesignField field = new JRDesignField();
189       name = metrics.elementAt(i).toString();
190       field.setName(name);
191       field.setValueClass(Float JavaDoc.class);
192       jasperDesign.addField(field);
193     }
194     for(int i = 0 ; i < accuMetrics.size() ; i++) {
195       JRDesignField field = new JRDesignField();
196       name = accuMetrics.elementAt(i).toString();
197       field.setName(name);
198       field.setValueClass(Float JavaDoc.class);
199       jasperDesign.addField(field);
200     }
201   }
202
203   /**
204    * Agrega los grupos a la definicion
205    * @param jasperDesign
206    * @throws JRException
207    */

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

327       jasperDesign.addGroup(group);
328     }
329   }
330
331   private String JavaDoc getMetricName(String JavaDoc s) {
332     StringTokenizer tokenizer = new StringTokenizer(s, "_");
333     String JavaDoc returnVal = tokenizer.nextToken();
334     return returnVal;
335   }
336
337   /**
338    * Agrega las variables a la definicion
339    * @param jasperDesign
340    * @throws JRException
341    */

342   private void addVariables(JasperDesign jasperDesign) throws JRException {
343     //Variables
344
JRDesignExpression expression;
345     for(int j = 0 ; j < dimensions.size() ; j++) {
346       for(int i = 0 ; i < metrics.size() ; i++) {
347         JRDesignVariable jrVariable = new JRDesignVariable();
348         jrVariable.setName(dimensions.elementAt(j).toString()+ metrics.elementAt(i).toString()+"_SUM");
349         jrVariable.setValueClass(Float JavaDoc.class);
350         jrVariable.setResetType(JRDesignVariable.RESET_TYPE_GROUP);
351         jrVariable.setCalculation(JRDesignVariable.CALCULATION_SUM);
352         jrVariable.setResetGroup((JRGroup)jasperDesign.getGroupsList().get(j));
353         expression = new JRDesignExpression();
354         expression.setValueClass(Float JavaDoc.class);
355         expression.setText("$F{"+metrics.elementAt(i).toString()+"}");
356         jrVariable.setExpression(expression);
357         jasperDesign.addVariable(jrVariable);
358       }
359     }
360   }
361
362   /**
363    * Agrega los detalles a la definicion
364    * @param jasperDesign
365    */

366   private void addDetails(JasperDesign jasperDesign) {
367     //Details
368
int printAt = calculateStartForItem();
369     JRDesignBand band = new JRDesignBand();
370     band.setHeight(20);
371     JRDesignExpression expression;
372     for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
373       JRDesignTextField textField = new JRDesignTextField();
374       textField.setX(printAt);
375       textField.setY(0);
376       textField.setWidth(getLenghtForCaption(((Integer JavaDoc) nonGroupingDimensionsSizes.elementAt(i)).intValue()));
377       textField.setHeight(20);
378       expression = new JRDesignExpression();
379       expression.setValueClass(String JavaDoc.class);
380       expression.setText("$F{"+nonGroupingDimensions.elementAt(i)+"}");
381       textField.setExpression(expression);
382       band.addElement(textField);
383
384       nonGroupingDimensionPosition.add(new Integer JavaDoc(printAt));
385
386       int first = getLenghtForCaption(((Integer JavaDoc) nonGroupingDimensionsSizes.elementAt(i)).intValue());
387       int second = getLenghtForCaption(nonGroupingDimensions.elementAt(i).toString().length());
388       if(first >= second) {
389         printAt += first;
390       } else {
391         printAt += second;
392       }
393       //printAt += getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue());
394

395     }
396     for(int i = 0 ; i < metrics.size() ; i++) {
397       JRDesignTextField textField = new JRDesignTextField();
398       textField.setX(printAt + 25);
399       textField.setY(0);
400       //textField.setWidth(getLenghtForCaption(((Integer) metricsSizes.elementAt(i)).intValue())+15);
401

402       metricsWidth.add(new Integer JavaDoc(getLenghtForCaption((new Integer JavaDoc(metrics.elementAt(i).toString().length())).intValue())+25));
403       textField.setWidth(getLenghtForCaption((new Integer JavaDoc(metrics.elementAt(i).toString().length())).intValue())+25);
404
405       textField.setHeight(20);
406       textField.setPattern(numberFormatPattern);
407       expression = new JRDesignExpression();
408       expression.setValueClass(Float JavaDoc.class);
409       expression.setText("$F{"+metrics.elementAt(i)+"}");
410       textField.setExpression(expression);
411       band.addElement(textField);
412       metricsPosition.add(new Integer JavaDoc(printAt + 25));
413       printAt += getLenghtForCaption(((Integer JavaDoc) metricsSizes.elementAt(i)).intValue()) + 25 ;
414     }
415
416     for(int i = 0 ; i < accuMetrics.size() ; i++) {
417       JRDesignTextField textField = new JRDesignTextField();
418       textField.setX(printAt + 25);
419       textField.setY(0);
420
421       //textField.setWidth(getLenghtForCaption(((Integer) accuMetricsSizes.elementAt(i)).intValue())+15);
422

423       accMetricsWidht.add(new Integer JavaDoc(getLenghtForCaption((new Integer JavaDoc(accuMetrics.elementAt(i).toString().length())).intValue())+25));
424       textField.setWidth(getLenghtForCaption((new Integer JavaDoc(accuMetrics.elementAt(i).toString().length())).intValue())+25);
425
426       textField.setHeight(20);
427       textField.setPattern(numberFormatPattern);
428       expression = new JRDesignExpression();
429       expression.setValueClass(Float JavaDoc.class);
430       expression.setText("$F{"+accuMetrics.elementAt(i)+"}");
431       textField.setExpression(expression);
432       band.addElement(textField);
433       metricsPosition.add(new Integer JavaDoc(printAt + 25));
434       accMetricsPosition.add(new Integer JavaDoc(printAt + 25));
435       printAt += getLenghtForCaption(((Integer JavaDoc) accuMetricsSizes.elementAt(i)).intValue()) + 25 ;
436     }
437
438     jasperDesign.setDetail(band);
439   }
440
441   /**
442    * Agrega la cabecera de pagina a la definicion
443    * @param jasperDesign
444    */

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

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

658   private void addTitle(JasperDesign jasperDesign) throws JRException{
659     //Title
660
JRDesignTextField textField = new JRDesignTextField();
661     //JRDesignLine line = new JRDesignLine();
662
JRDesignBand band = new JRDesignBand();
663     band.setHeight(50);
664     JRDesignStaticText staticText = new JRDesignStaticText();
665     staticText.setX(0);
666     staticText.setY(10);
667     staticText.setWidth(515);
668     staticText.setHeight(30);
669     staticText.setTextAlignment(JRTextElement.TEXT_ALIGN_CENTER);
670     JRDesignReportFont bigFont = new JRDesignReportFont();
671     bigFont.setName("Arial_Normal");
672     bigFont.setDefault(true);
673     bigFont.setFontName("Arial");
674     bigFont.setSize(16);
675     bigFont.setPdfFontName("Helvetica");
676     bigFont.setPdfEncoding("Cp1252");
677     bigFont.setPdfEmbedded(false);
678     jasperDesign.addFont(bigFont);
679     staticText.setFont(bigFont);
680     staticText.setText(tittle);
681     band.addElement(staticText);
682     /*line = new JRDesignLine();
683         line.setX(0);
684         line.setY(0);
685         line.setWidth(515);
686         line.setHeight(0);
687         band.addElement(line);*/

688     jasperDesign.setTitle(band);
689
690     band = new JRDesignBand();
691     band.setHeight(15);
692     staticText = new JRDesignStaticText();
693     staticText.setX(0);
694     staticText.setY(0);
695     staticText.setWidth(40);
696     staticText.setHeight(15);
697     staticText.setText("Pagina: ");
698     band.addElement(staticText);
699     textField = new JRDesignTextField();
700     textField.setX(40);
701     textField.setY(0);
702     textField.setWidth(100);
703     textField.setHeight(15);
704     JRDesignExpression expression = new JRDesignExpression();
705     expression.setValueClass(Integer JavaDoc.class);
706     expression.setText("$V{PAGE_NUMBER}");
707     textField.setExpression(expression);
708     band.addElement(textField);
709     jasperDesign.setPageFooter(band);
710
711     band = new JRDesignBand();
712     band.setHeight(20);
713     jasperDesign.setSummary(band);
714   }
715
716   /**
717    * Inicializa los vectores con la descripcion de las dimensiones
718    * que agrupan, las que no agrupan y las metricas.
719    * @param dimensionsCount
720    * @param metricsCount
721    * @param nonGroupingDimensionsCount
722    * @param accMetricsCount
723    */

724   private void initializeVectors(int dimensionsCount, int metricsCount, int nonGroupingDimensionsCount, int accMetricsCount) {
725     dimensions = new Vector();
726     nonGroupingDimensions = new Vector();
727     metrics = new Vector();
728     accuMetrics = new Vector();
729     int i = 0;
730     for (; i < dimensionsCount; i++) {
731       dimensions.add(data.getColumnName(i));
732     }
733     for (int z = 0; z < nonGroupingDimensionsCount; i++, z++) {
734       nonGroupingDimensions.add(data.getColumnName(i));
735     }
736     for (int j = 0; j < metricsCount; i++, j++) {
737       metrics.add(data.getColumnName(i));
738     }
739     for (int j = 0; j < accMetricsCount; i++, j++) {
740       accuMetrics.add(data.getColumnName(i));
741     }
742   }
743
744   /**
745    * Determina un Vector con los anchos mas precisos.
746    * @param vector
747    * @return
748    */

749   private Vector calculateBestLenghts(Vector vector) {
750     Vector bestSizes = new Vector();
751     for(int i=0;i < vector.size();i++){
752       bestSizes.add(new Integer JavaDoc(vector.elementAt(i).toString().length()));
753     }
754     return bestSizes;
755   }
756
757   /**
758    * Retorna un indice a partir de un nombre de columna.
759    * @param columnName
760    * @return
761    */

762   private int getColumnIndexFromName(String JavaDoc columnName) {
763     int index = 0;
764     for (int i = 0; i < data.getColumnCount(); i++) {
765       if (data.getColumnName(i).equals(columnName)) {
766         index = i;
767         break;
768       }
769     }
770     return index;
771   }
772
773   /**
774    * Retorna el ancho mas eficiente comparando los elementos
775    * del Vector
776    * @param ColumnSizes
777    * @return
778    */

779   private Integer JavaDoc getBestSize(Vector ColumnSizes) {
780     int compareTo = 0;
781     for (int i = 0; i < ColumnSizes.size(); i++) {
782       if (((Integer JavaDoc) ColumnSizes.elementAt(i)).intValue() > compareTo) {
783         compareTo = ((Integer JavaDoc) ColumnSizes.elementAt(i)).intValue();
784       }
785     }
786     return new Integer JavaDoc(compareTo);
787   }
788
789   /**
790    * Determina el ancho para las descripciones
791    * de la cabecera
792    * @param chars
793    * @return
794    */

795   private int getLenghtForCaptionHeader(int chars){
796     //return chars * 8 - chars;
797
Integer JavaDoc integer = new Integer JavaDoc(chars);
798     Double JavaDoc qsrt = new Double JavaDoc(Math.sqrt(integer.doubleValue()));
799     return chars * 7 - ((chars / 2) - 3 * qsrt.intValue());
800   }
801
802   /**
803    * Determina el comienzo donde se mostraran las
804    * metricas o dimensiones que no agrupan.
805    * @return
806    */

807   private int calculateStartForItem() {
808     int width = 0 ;
809     for(int i = 0 ; i < dimensionsSizes.size() ; i++){
810       width += (((Integer JavaDoc)dimensionsSizes.elementAt(i)).intValue());
811     }
812
813     //return (width) + 100;
814
return 0;
815   }
816
817   /**
818    * Retorna un ancho preciso a partir de una
819    * determinada cantidad de caracteres
820    * @param chars
821    * @return
822    */

823   private int getLenghtForCaption(int chars) {
824     if(chars == 2 ) {
825       return chars * 7;
826     }
827     Integer JavaDoc integer = new Integer JavaDoc(chars);
828     Double JavaDoc qsrt = new Double JavaDoc(Math.sqrt(integer.doubleValue()));
829     return chars * 8 - ((chars / 2) - 3 * qsrt.intValue());
830   }
831
832   /**
833    * Retorna los fields.
834    * @return
835    */

836   public Map getFields() {
837     if (fields == null){
838       fields = new HashMap();
839     }
840     return fields;
841   }
842
843   /**
844    * Retorna las variables
845    * @return
846    */

847   public Map getVariables() {
848     if (variables == null ){
849       variables = new HashMap();
850     }
851     return variables;
852   }
853
854 }
855
Popular Tags