KickJava   Java API By Example, From Geeks To Geeks.

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


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.design.JRDesignReportFont;
13 import net.sf.jasperreports.engine.*;
14 import net.sf.jasperreports.engine.util.JRSaver;
15 import com.calipso.reportgenerator.common.*;
16
17 /**
18  * Construye una definición para los reportes dinamicos (Jasper)
19  */

20
21 public class CubeJasperReportDefinition implements IJasperDefinition {
22
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 int nonGroupingDimensionsCount;
32   private Vector dimensionsSizes;
33   private Vector metricsSizes;
34   private Vector accuMetricsSizes;
35   private Vector nonGroupingDimensionsSizes;
36   private Vector rowsTotalsSizes;
37   private int printAt;
38   private Vector metricsPosition = new Vector();
39   private Vector nonGroupingDimensionPosition = new Vector();
40   private Vector groupingDimensionPosition = new Vector();
41   private ReportResult reportResult;
42   private String JavaDoc numberFormatPattern;
43   private Vector metricsWidth;
44   private Vector accMetricsPosition;
45   private Vector accMetricsWidht;
46   private Vector columnIdentifiersPos;
47   private Vector rowsTotals;
48   private Vector rowsTotalsWidht;
49   private Vector rowsTotalsPosition;
50   private Map reportParams;
51
52
53   /**
54    * Inicializa una instancia de <coe>eJasperReportDefinition</code>
55    * @param reportResult
56    * @param data
57    * @param dimensionsCount
58    * @param metricsCount
59    * @param nonGroupingDimensionsCount
60    * @param accuMetricsCount
61    * @param tittle
62    */

63   public CubeJasperReportDefinition(ReportResult reportResult, TableModel JavaDoc data, int dimensionsCount, int metricsCount, int nonGroupingDimensionsCount, int accuMetricsCount, String JavaDoc tittle) throws InfoException {
64     this.data = data;
65     this.tittle = tittle;
66     this.reportResult = reportResult;
67     metricsWidth = new Vector();
68     accMetricsPosition = new Vector();
69     accMetricsWidht = new Vector();
70     rowsTotalsWidht = new Vector();
71     rowsTotalsPosition = new Vector();
72     this.nonGroupingDimensionsCount = nonGroupingDimensionsCount;
73     initializeVectors(dimensionsCount, metricsCount, nonGroupingDimensionsCount, accuMetricsCount);
74     reportParams = reportResult.getParamValues();
75     dimensionsSizes = calculateBestLenghts(dimensions);
76     metricsSizes = calculateBestLenghts(metrics);
77     accuMetricsSizes = calculateBestLenghts(this.accuMetrics);
78     nonGroupingDimensionsSizes = calculateBestLenghts(nonGroupingDimensions);
79     rowsTotalsSizes = calculateBestLenghts(rowsTotals);
80     initializeNumberFormatPattern();
81
82   }
83
84   private void initializeNumberFormatPattern() {
85     NumberFormat JavaDoc format = NumberFormat.getInstance(LanguageTraslator.getLocale());
86     numberFormatPattern = ((DecimalFormat JavaDoc)format).toPattern();
87   }
88
89   /**
90    * Retorna la definicion del reporte
91    * @return
92    * @throws net.sf.jasperreports.engine.JRException
93    */

94   public JasperDesign getJasperDefinition(boolean isLandscape) throws JRException {
95     JasperDesign jasperDesign = new JasperDesign();
96     if(isLandscape) {
97       jasperDesign.setOrientation(JasperDesign.ORIENTATION_LANDSCAPE);
98       jasperDesign.setPrintOrder(JasperDesign.PRINT_ORDER_VERTICAL);
99       jasperDesign.setPageWidth(842);
100       jasperDesign.setPageHeight(595);
101     } else {
102       jasperDesign.setOrientation(JasperDesign.ORIENTATION_PORTRAIT);
103       jasperDesign.setPrintOrder(JasperDesign.PRINT_ORDER_HORIZONTAL);
104       jasperDesign.setPageWidth(595);
105       jasperDesign.setPageHeight(842);
106     }
107     jasperDesign.setName("BasicReport");
108     addFields(jasperDesign);
109     addDetails(jasperDesign);
110     addGroups(jasperDesign);
111     addVariables(jasperDesign);
112     addTitle(jasperDesign);
113     addParams(jasperDesign);
114     //addPageHeader(jasperDesign);
115
return jasperDesign;
116   }
117
118   /**
119    * Agrega los parametros del reporte a la definicion del jasper para que puedan ser utilizados en la visualizacion.
120    * @param jasperDesign
121    * @throws JRException
122    */

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

140   private Class JavaDoc getClassFor(Map.Entry param) {
141     Class JavaDoc result = java.lang.String JavaDoc.class;
142     Map map = new HashMap();
143     map.put(param.getKey(), param.getValue());
144     ReportMap.setParametersToSimpleType(map);
145     if(map.get(param.getKey())!=null){
146       result = map.get(param.getKey()).getClass();
147     }
148     return result;
149   }
150
151   /**
152    * Agrega los campos a la definicion
153    * @param jasperDesign
154    * @throws net.sf.jasperreports.engine.JRException
155    */

156   private void addFields(JasperDesign jasperDesign) throws JRException {
157   //Fields
158
String JavaDoc name;
159     if(dimensions.size()==0){
160         dimensions.add("NONE");
161         dimensionsSizes.add(new Integer JavaDoc(20));
162     }
163     for(int i = 0 ; i < dimensions.size() ; i++) {
164         JRDesignField field = new JRDesignField();
165       name = dimensions.elementAt(i).toString();
166       field.setName(name);
167       if(reportResult.getDimensionFromName(name)!=null){
168         field.setValueClass(ReportMap.getJavaLangType(reportResult.getDimensionFromName(name).getDataType()));//java.lang.String.class);
169
}else{
170         field.setValueClass(java.lang.String JavaDoc.class);
171       }
172       jasperDesign.addField(field);
173     }
174     for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
175       //if(i + 1 == nonGroupingDimensions.size()) {
176
JRDesignField field = new JRDesignField();
177         name = nonGroupingDimensions.elementAt(i).toString();
178         field.setName(name);
179         field.setValueClass(ReportMap.getJavaLangType(reportResult.getDimensionFromName(name).getDataType()));//java.lang.String.class);
180
jasperDesign.addField(field);
181       //}
182
}
183     for(int i = 0 ; i < metrics.size() ; i++) {
184       JRDesignField field = new JRDesignField();
185       name = metrics.elementAt(i).toString();
186       field.setName(name);
187       field.setValueClass(java.lang.Float JavaDoc.class);
188       jasperDesign.addField(field);
189     }
190     for(int i = 0 ; i < accuMetrics.size() ; i++) {
191       JRDesignField field = new JRDesignField();
192       name = accuMetrics.elementAt(i).toString();
193       field.setName(name);
194       field.setValueClass(java.lang.Float JavaDoc.class);
195       jasperDesign.addField(field);
196     }
197     for(int i = 0 ; i < rowsTotals.size() ; i++) {
198       JRDesignField field = new JRDesignField();
199       name = rowsTotals.elementAt(i).toString();
200       field.setName(name);
201       field.setValueClass(java.lang.Float JavaDoc.class);
202       jasperDesign.addField(field);
203     }
204
205   }
206
207   /**
208    * Agrega los grupos a la definicion
209    * @param jasperDesign
210    * @throws net.sf.jasperreports.engine.JRException
211    */

212   private void addGroups(JasperDesign jasperDesign) throws JRException {
213     //groups
214
JRDesignExpression expression;
215     JRDesignBand band;
216     JRDesignGroup group = null;
217     printAt = 0;
218     for(int j = 0 ; j < dimensions.size() ; j++) {
219       group = new JRDesignGroup();
220       group.setName(dimensions.elementAt(j).toString()+"_GROUP");
221       group.setReprintHeaderOnEachPage(true);
222       expression = new JRDesignExpression();
223       if(reportResult.getDimensionFromName(dimensions.elementAt(j).toString()) != null){
224         expression.setValueClass(ReportMap.getJavaLangType(reportResult.getDimensionFromName(dimensions.elementAt(j).toString()).getDataType()));//java.lang.String.class);
225
}else{
226         expression.setValueClass(java.lang.String JavaDoc.class);
227       }
228       expression.setText("$F{"+dimensions.elementAt(j)+"}");
229       group.setExpression(expression);
230
231       if(j + 1 == dimensions.size()) {
232         band = new JRDesignBand();
233         band.setHeight(60);
234       } else {
235         band = new JRDesignBand();
236         band.setHeight(20);
237       }
238
239       JRDesignStaticText text = new JRDesignStaticText();
240       String JavaDoc caption = "";
241       if(!dimensions.elementAt(j).toString().equalsIgnoreCase("NONE")){
242          caption = reportResult.getReportSpec().getDimensionFromName(dimensions.elementAt(j).toString()).getCaption() + ":";
243       }
244       text.setX(0);
245       text.setY(0);
246       text.setWidth(getLenghtForCaption((new Integer JavaDoc(caption.length())).intValue()));
247       text.setHeight(20);
248       text.setText(caption);
249       JRDesignReportFont bigFont = new JRDesignReportFont();
250       bigFont.setFontSize(11);
251       bigFont.setBold(true);
252       bigFont.setItalic(true);
253       text.setFont(bigFont);
254       band.addElement(text);
255
256       JRDesignTextField textField = new JRDesignTextField();
257       textField.setX(getLenghtForCaption((new Integer JavaDoc(caption.length())).intValue()));
258       textField.setY(0);
259       if(data.getRowCount()==1){
260          textField.setWidth(dimensions.elementAt(j).toString().length() * 3);
261       } else {
262          textField.setWidth(getLenghtForCaption(((Integer JavaDoc) dimensionsSizes.elementAt(j)).intValue()));
263       }
264       textField.setHeight(20);
265       expression = new JRDesignExpression();
266       if(reportResult.getDimensionFromName(dimensions.elementAt(j).toString()) != null){
267         expression.setValueClass(ReportMap.getJavaLangType(reportResult.getDimensionFromName(dimensions.elementAt(j).toString()).getDataType()));//java.lang.String.class);
268
}else{
269         expression.setValueClass(java.lang.String JavaDoc.class);
270       }
271       expression.setText("$F{"+dimensions.elementAt(j)+"}");
272       textField.setExpression(expression);
273       band.addElement(textField);
274
275       if((nonGroupingDimensions.size() >= 0) && (dimensions.size() == j + 1)) {
276
277         //Empezamos estableciendo las descripciones de cada dimension que no agrupa
278
//para cada conjunto de metricas. Es decir, los valores de la dimension que
279
//estan ubicados en column.
280
Vector vector = null;
281         try {
282           vector = reportResult.getReportTableModel().getOccurrences();
283         } catch (InfoException e) {
284           throw new JRException(e);
285         }
286         if(vector.size() > 0) {
287           for(int i = 0 ; i < vector.size() ; i++) {
288             String JavaDoc s = (String JavaDoc) vector.elementAt(i);
289             text = new JRDesignStaticText();
290             text.setX(((Integer JavaDoc)columnIdentifiersPos.elementAt(i)).intValue());
291             text.setY(20);
292             text.setWidth(getLenghtForCaption((new Integer JavaDoc(s.length())).intValue()));
293             text.setHeight(20);
294             text.setText(s/* + ": "*/);
295             band.addElement(text);
296           }
297
298           if(reportResult.getReportQuery().isVisibleTotals()){
299             text = new JRDesignStaticText();
300             text.setX(((Integer JavaDoc)rowsTotalsPosition.elementAt(0)).intValue());
301             text.setY(20);
302             text.setWidth(40);
303             text.setHeight(20);
304             text.setText(LanguageTraslator.traslate("358") /*+ ":"*/);
305             band.addElement(text);
306           }
307
308           //Continuamos con los nombres de las dimensiones que no agrupan
309
for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
310             String JavaDoc s = (String JavaDoc) nonGroupingDimensions.elementAt(i);
311             caption = reportResult.getReportSpec().getDimensionFromName(s).getCaption();
312             text = new JRDesignStaticText();
313             text.setX(((Integer JavaDoc)nonGroupingDimensionPosition.elementAt(i)).intValue());
314             text.setY(40);
315             text.setWidth(getLenghtForCaption((new Integer JavaDoc(s.length())).intValue()));
316             text.setHeight(20);
317             text.setText(caption/* + ": "*/);
318             band.addElement(text);
319           }
320         }
321
322       //Continuamos con las descripciones de las metricas
323
ReportMetricSpec metricSpec = null;
324         for(int i = 0 ; i < metrics.size() ; i++) {
325           /*String s = (String) metrics.elementAt(i);
326           if (nonGroupingDimensions.size()>0) {
327             String metricName;
328             if(s.split("_").length > 1) {
329               StringTokenizer tokenizer = new StringTokenizer(s, "_");
330               tokenizer.nextToken();
331               metricName = "";
332               while (tokenizer.hasMoreTokens()){
333                 metricName += (tokenizer.nextToken());
334                 if(tokenizer.hasMoreTokens()){
335                     metricName += "_";
336                 }
337               }
338             }else{
339               metricName = s;
340             }
341             metricSpec = reportResult.getReportSpec().getMetricFromName(metricName);
342           } else {
343             metricSpec = reportResult.getReportSpec().getMetricFromName(s);
344           }*/

345           String JavaDoc s = getMetricName((String JavaDoc) metrics.elementAt(i));
346           metricSpec = reportResult.getReportSpec().getMetricFromName(s);
347           text = new JRDesignStaticText();
348           text.setX(((Integer JavaDoc)metricsPosition.elementAt(i)).intValue());
349           text.setY(40);
350           text.setWidth(getLenghtForCaption((new Integer JavaDoc(s.length())).intValue()));
351           text.setHeight(20);
352           text.setText(metricSpec.getCaption() /*+ ": "*/);
353           band.addElement(text);
354         }
355
356
357         for(int i = 0 ; i < rowsTotals.size() ; i++) {
358           String JavaDoc s = (String JavaDoc) rowsTotals.elementAt(i);
359           StringTokenizer tokenizer = new StringTokenizer(s, "_");
360           tokenizer.nextToken();
361           String JavaDoc metricName = "";
362           while (tokenizer.hasMoreTokens()){
363                 metricName += (tokenizer.nextToken());
364                 if(tokenizer.hasMoreTokens()){
365                     metricName += "_";
366                 }
367             }
368           metricSpec = reportResult.getMetricFromName(metricName);
369           text = new JRDesignStaticText();
370           text.setX(((Integer JavaDoc)rowsTotalsPosition.elementAt(i)).intValue());
371           text.setY(40);
372           text.setWidth(getLenghtForCaption((new Integer JavaDoc(s.length())).intValue()));
373           text.setHeight(20);
374           text.setText(metricSpec.getCaption() /*+ ": "*/);
375           band.addElement(text);
376
377
378         }
379
380       }
381       if(dimensions.elementAt(j).toString().equalsIgnoreCase("NONE")){
382         band = null;
383       }
384       group.setGroupHeader(band);
385
386       /*if(j + 1 == dimensions.size()) {
387         for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
388           int x = ((Integer) nonGroupingDimensionPosition.elementAt(i)).intValue();
389           text = new JRDesignStaticText();
390           text.setX(x);
391           text.setY(20);
392           //text.setWidth(((Integer) metricsWidth.elementAt(i)).intValue());
393           int first = getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue());
394           int second = getLenghtForCaption(nonGroupingDimensions.elementAt(i).toString().length());
395           if(first >= second) {
396             text.setWidth(getLenghtForCaption(first));
397           } else {
398             text.setWidth(getLenghtForCaption(second));
399           }
400           text.setHeight(20);
401           text.setText(reportResult.getReportSpec().getDimensionFromName(nonGroupingDimensions.elementAt(i).toString()).getCaption());
402           band.addElement(text);
403         }
404
405         for(int i = 0 ; i < metrics.size() ; i++) {
406           int x = ((Integer) metricsPosition.elementAt(i)).intValue();
407           text = new JRDesignStaticText();
408           text.setX(x);
409           text.setY(20);
410           text.setWidth(((Integer) metricsWidth.elementAt(i)).intValue());
411           text.setHeight(20);
412           text.setText(reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption());
413           band.addElement(text);
414         }
415
416         for(int i = 0 ; i < accMetricsPosition.size() ; i++) {
417           int x = ((Integer) accMetricsPosition.elementAt(i)).intValue();
418           text = new JRDesignStaticText();
419           text.setX(x);
420           text.setY(20);
421           text.setWidth(((Integer) accMetricsWidht.elementAt(i)).intValue());
422           text.setHeight(20);
423           String metricName = getMetricName(accuMetrics.elementAt(i).toString());
424           String metricCaption = reportResult.getReportSpec().getMetricFromName(metricName).getCaption();
425           text.setText(metricCaption + " " + LanguageTraslator.traslate("315"));
426           band.addElement(text);
427         }
428         group.setGroupHeader(band);
429       }*/

430
431       band = new JRDesignBand();
432       band.setHeight(30);
433       for(int i = 0 ; i < metrics.size() ; i++) {
434         band.setHeight(20);
435         JRDesignStaticText staticText = new JRDesignStaticText();
436         staticText.setX(0);
437         staticText.setY(0);
438         staticText.setWidth(60);
439         staticText.setHeight(20);
440         bigFont = new JRDesignReportFont();
441         bigFont.setItalic(true);
442         bigFont.setFontSize(11);
443         staticText.setFont(bigFont);
444         if(!dimensions.elementAt(j).toString().equalsIgnoreCase("NONE")){
445           ReportDimensionSpec dimensionSpec = reportResult.getReportSpec().getDimensionFromName(dimensions.elementAt(j).toString());
446           staticText.setText(dimensionSpec.getCaption());
447         } else {
448           staticText.setText("");
449         }
450         band.addElement(staticText);
451
452         textField = new JRDesignTextField();
453         textField.setX(((Integer JavaDoc)metricsPosition.elementAt(i)).intValue());
454         textField.setY(0);
455         if(data.getRowCount()==1){
456            textField.setWidth(dimensions.elementAt(j).toString().length() * 3);
457         } else {
458            textField.setWidth(getLenghtForCaption(((Integer JavaDoc) dimensionsSizes.elementAt(j)).intValue()));
459         }
460         /*textField.setWidth(getLenghtForCaption(((Integer) dimensionsSizes.elementAt(j)).intValue()) + 20);*/
461         textField.setHeight(20);
462         expression = new JRDesignExpression();
463         expression.setValueClass(java.lang.Float JavaDoc.class);
464         expression.setText("$V{"+dimensions.elementAt(j).toString() + metrics.elementAt(i).toString()+"_SUM}");
465         textField.setPattern(numberFormatPattern);
466         textField.setExpression(expression);
467         bigFont = new JRDesignReportFont();
468         bigFont.setBold(true);
469         bigFont.setItalic(true);
470         textField.setFont(bigFont);
471         band.addElement(textField);
472         group.setGroupFooter(band);
473       }
474
475       for(int i = 0 ; i < rowsTotals.size() ; i++) {
476         band.setHeight(20);
477         textField = new JRDesignTextField();
478         textField.setX(((Integer JavaDoc)rowsTotalsPosition.elementAt(i)).intValue());
479         textField.setY(0);
480         if(data.getRowCount()==1){
481            textField.setWidth(dimensions.elementAt(j).toString().length() * 3);
482         } else {
483            textField.setWidth(getLenghtForCaption(((Integer JavaDoc) dimensionsSizes.elementAt(j)).intValue()));
484         }
485         /*textField.setWidth(getLenghtForCaption(((Integer) dimensionsSizes.elementAt(j)).intValue()) + 20);*/
486         textField.setHeight(20);
487         expression = new JRDesignExpression();
488         expression.setValueClass(java.lang.Float JavaDoc.class);
489         expression.setText("$V{"+dimensions.elementAt(j).toString() + rowsTotals.elementAt(i).toString()+"_SUM}");
490         textField.setPattern(numberFormatPattern);
491         textField.setExpression(expression);
492         bigFont = new JRDesignReportFont();
493         bigFont.setBold(true);
494         bigFont.setItalic(true);
495         textField.setFont(bigFont);
496         band.addElement(textField);
497         group.setGroupFooter(band);
498       }
499
500
501
502       /*int atIndex = data.getColumnCount() - reportResult.getReportQuery().getMetrics().size();
503       int element = 0;
504       while(atIndex < data.getColumnCount()) {
505         float total = 0;
506         for(int i = 0 ; i < data.getRowCount() ; i++) {
507           Float aFloat = (Float) data.getValueAt(i, atIndex);
508           total += aFloat.floatValue();
509         }
510
511         text = new JRDesignStaticText();
512         text.setX(((Integer)rowsTotalsPosition.elementAt(element)).intValue());
513         text.setY(40);
514         text.setWidth(getLenghtForCaption(((Integer) rowsTotalsSizes.elementAt(element++)).intValue()) + 20);
515         text.setHeight(20);
516         text.setText(String.valueOf(total));
517         band.addElement(text);
518
519         atIndex++;
520       }*/

521
522       groupingDimensionPosition.add(new Integer JavaDoc(printAt));
523       printAt += 10;//getLenghtForCaption(((Integer)dimensionsSizes.elementAt(j)).intValue());
524

525       jasperDesign.addGroup(group);
526     }
527   }
528
529   private String JavaDoc getMetricName(String JavaDoc s) {
530     if(nonGroupingDimensionsCount>0){
531       StringTokenizer tokenizer = new StringTokenizer(s, "_");
532       tokenizer.nextToken();
533       String JavaDoc returnVal = "";
534       while(tokenizer.hasMoreTokens()){
535         returnVal += tokenizer.nextToken() + "_";
536       }
537       if(!returnVal.equalsIgnoreCase("")){
538         return returnVal.substring(0,returnVal.length()-1);
539       }
540     }
541     return s;
542   }
543
544   /**
545    * Agrega las variables a la definicion
546    * @param jasperDesign
547    * @throws net.sf.jasperreports.engine.JRException
548    */

549   private void addVariables(JasperDesign jasperDesign) throws JRException {
550     //Variables
551
JRDesignExpression expression;
552     for(int j = 0 ; j < dimensions.size() ; j++) {
553       for(int i = 0 ; i < metrics.size() ; i++) {
554         JRDesignVariable jrVariable = new JRDesignVariable();
555         jrVariable.setName(dimensions.elementAt(j).toString()+ metrics.elementAt(i).toString()+"_SUM");
556         jrVariable.setValueClass(java.lang.Float JavaDoc.class);
557         jrVariable.setResetType(JRDesignVariable.RESET_TYPE_GROUP);
558         jrVariable.setCalculation(JRDesignVariable.CALCULATION_SUM);
559         jrVariable.setResetGroup((JRGroup)jasperDesign.getGroupsList().get(j));
560         expression = new JRDesignExpression();
561         expression.setValueClass(java.lang.Float JavaDoc.class);
562         expression.setText("$F{"+metrics.elementAt(i).toString()+"}");
563         jrVariable.setExpression(expression);
564         jasperDesign.addVariable(jrVariable);
565       }
566
567       for(int i = 0 ; i < rowsTotals.size() ; i++) {
568         JRDesignVariable jrVariable = new JRDesignVariable();
569         jrVariable.setName(dimensions.elementAt(j).toString()+ rowsTotals.elementAt(i).toString()+"_SUM");
570         jrVariable.setValueClass(java.lang.Float JavaDoc.class);
571         jrVariable.setResetType(JRDesignVariable.RESET_TYPE_GROUP);
572         jrVariable.setCalculation(JRDesignVariable.CALCULATION_SUM);
573         jrVariable.setResetGroup((JRGroup)jasperDesign.getGroupsList().get(j));
574         expression = new JRDesignExpression();
575         expression.setValueClass(java.lang.Float JavaDoc.class);
576         expression.setText("$F{"+rowsTotals.elementAt(i).toString()+"}");
577         jrVariable.setExpression(expression);
578         jasperDesign.addVariable(jrVariable);
579       }
580     }
581   }
582
583   /**
584    * Agrega los detalles a la definicion
585    * @param jasperDesign
586    */

587   private void addDetails(JasperDesign jasperDesign) throws JRException {
588     //Details
589
//int printAt = calculateStartForItem();
590
int printAt;
591
592     if(nonGroupingDimensions.size() > 0) {
593       printAt = 0;
594     } else {
595       printAt = 75;
596     }
597     JRDesignBand band = new JRDesignBand();
598     band.setHeight(20);
599     JRDesignExpression expression;
600     /*for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
601       JRDesignTextField textField = new JRDesignTextField();
602       textField.setX(printAt);
603       textField.setY(0);
604       textField.setWidth(getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue()));
605       textField.setHeight(20);
606       expression = new JRDesignExpression();
607       expression.setValueClass(java.lang.String.class);
608       expression.setText("$F{"+nonGroupingDimensions.elementAt(i)+"}");
609       textField.setExpression(expression);
610       band.addElement(textField);
611
612       nonGroupingDimensionPosition.add(new Integer(printAt));
613
614       int first = getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue());
615       int second = getLenghtForCaption(nonGroupingDimensions.elementAt(i).toString().length());
616       if(first >= second) {
617         printAt += first;
618       } else {
619         printAt += second;
620       }
621       //printAt += getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue());
622
623     }*/

624
625     if(nonGroupingDimensions.size() > 0) {
626       for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
627         JRDesignTextField textField = new JRDesignTextField();
628         textField.setX(printAt);
629         textField.setY(0);
630         textField.setWidth(getLenghtForCaption(((Integer JavaDoc) nonGroupingDimensionsSizes.elementAt(i)).intValue()));
631         textField.setHeight(20);
632         expression = new JRDesignExpression();
633         expression.setValueClass(ReportMap.getJavaLangType(reportResult.getDimensionFromName(nonGroupingDimensionsSizes.elementAt(i).toString()).getDataType()));//java.lang.String.class);
634
expression.setText("$F{"+nonGroupingDimensions.elementAt(i)+"}");
635         textField.setExpression(expression);
636         band.addElement(textField);
637
638         nonGroupingDimensionPosition.add(new Integer JavaDoc(printAt));
639
640         int first = getLenghtForCaption(((Integer JavaDoc) nonGroupingDimensionsSizes.elementAt(i)).intValue());
641         int second = getLenghtForCaption(nonGroupingDimensions.elementAt(i).toString().length());
642         if(first >= second) {
643           printAt += first;
644         } else {
645           printAt += second;
646         }
647         //printAt += getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue());
648

649       }
650     }
651
652     Vector vector = null;
653     if(nonGroupingDimensions.size() >= 0) {
654       try {
655         vector = reportResult.getReportTableModel().getOccurrences();
656       } catch (InfoException e) {
657         throw new JRException(e);
658       }
659       if(vector.size() > 0 ) {
660         columnIdentifiersPos = new Vector();
661       }
662     }
663
664
665     for(int i = 0, j = 0 ; i < metrics.size() ; i++, j++) {
666       JRDesignTextField textField = new JRDesignTextField();
667       textField.setX(printAt);
668       textField.setY(0);
669       //textField.setWidth(getLenghtForCaption(((Integer) metricsSizes.elementAt(i)).intValue())+15);
670

671       metricsWidth.add(new Integer JavaDoc(getLenghtForCaption((new Integer JavaDoc(metrics.elementAt(i).toString().length())).intValue())+25));
672       textField.setWidth(getLenghtForCaption((new Integer JavaDoc(metrics.elementAt(i).toString().length())).intValue())+25);
673
674       textField.setHeight(20);
675       textField.setPattern(numberFormatPattern);
676       expression = new JRDesignExpression();
677       expression.setValueClass(java.lang.Float JavaDoc.class);
678       expression.setText("$F{"+metrics.elementAt(i)+"}");
679       textField.setExpression(expression);
680       band.addElement(textField);
681       metricsPosition.add(new Integer JavaDoc(printAt));
682
683       if(nonGroupingDimensions.size() >= 0 && vector.size() > 0) {
684         int cut = metrics.size() / vector.size();
685         if(j == 0 || (j % cut == 0)) {
686           columnIdentifiersPos.add(new Integer JavaDoc(printAt));
687         }
688       }
689       if(data.getRowCount()==1){
690           printAt += (metrics.elementAt(i).toString().length() + 40);
691       } else {
692           printAt += getLenghtForCaption(((Integer JavaDoc) metricsSizes.elementAt(i)).intValue()) + 25 ;
693       }
694     }
695
696     for(int i = 0 ; i < accuMetrics.size() ; i++) {
697       JRDesignTextField textField = new JRDesignTextField();
698       textField.setX(printAt + 25);
699       textField.setY(0);
700
701       //textField.setWidth(getLenghtForCaption(((Integer) accuMetricsSizes.elementAt(i)).intValue())+15);
702

703       accMetricsWidht.add(new Integer JavaDoc(getLenghtForCaption((new Integer JavaDoc(accuMetrics.elementAt(i).toString().length())).intValue())+25));
704       textField.setWidth(getLenghtForCaption((new Integer JavaDoc(accuMetrics.elementAt(i).toString().length())).intValue())+25);
705
706       textField.setHeight(20);
707       textField.setPattern(numberFormatPattern);
708       expression = new JRDesignExpression();
709       expression.setValueClass(java.lang.Float JavaDoc.class);
710       expression.setText("$F{"+accuMetrics.elementAt(i)+"}");
711       textField.setExpression(expression);
712       band.addElement(textField);
713       metricsPosition.add(new Integer JavaDoc(printAt + 25));
714       accMetricsPosition.add(new Integer JavaDoc(printAt + 25));
715       printAt += getLenghtForCaption(((Integer JavaDoc) accuMetricsSizes.elementAt(i)).intValue()) + 25 ;
716     }
717
718     for(int i = 0 ; i < rowsTotals.size() ; i++) {
719       JRDesignTextField textField = new JRDesignTextField();
720       textField.setX(printAt );
721       textField.setY(0);
722
723       rowsTotalsWidht.add(new Integer JavaDoc(getLenghtForCaption((new Integer JavaDoc(rowsTotals.elementAt(i).toString().length())).intValue())+25));
724       textField.setWidth(getLenghtForCaption((new Integer JavaDoc(rowsTotals.elementAt(i).toString().length())).intValue())+25);
725
726       textField.setHeight(20);
727       textField.setPattern(numberFormatPattern);
728       expression = new JRDesignExpression();
729       expression.setValueClass(java.lang.Float JavaDoc.class);
730       expression.setText("$F{"+rowsTotals.elementAt(i)+"}");
731       textField.setExpression(expression);
732       JRDesignReportFont bigFont = new JRDesignReportFont();
733       bigFont.setBold(true);
734       bigFont.setItalic(true);
735       textField.setFont(bigFont);
736       band.addElement(textField);
737       rowsTotalsPosition.add(new Integer JavaDoc(printAt ));
738       if (rowsTotalsSizes.size()>0){
739         printAt += getLenghtForCaption(((Integer JavaDoc) rowsTotalsSizes.elementAt(i)).intValue()) + 25 ;
740       }
741     }
742     if(rowsTotals.size()>0){
743       if(((Integer JavaDoc)(rowsTotalsPosition.lastElement())).intValue() > jasperDesign.getPageWidth()){
744         jasperDesign.setPageWidth(((Integer JavaDoc)(rowsTotalsPosition.lastElement())).intValue()+25);
745       }
746     } else if(((Integer JavaDoc)(metricsPosition.lastElement())).intValue() > jasperDesign.getPageWidth()){
747        jasperDesign.setPageWidth(((Integer JavaDoc)(metricsPosition.lastElement())).intValue()+25);
748     }
749     jasperDesign.setDetail(band);
750   }
751
752   /**
753    * Agrega la cabecera de pagina a la definicion
754    * @param jasperDesign
755    */

756   /*private void addPageHeader(JasperDesign jasperDesign) {
757     int printAt = 45;
758     JRDesignBand band = new JRDesignBand();
759     for(int i = 0 ; i < dimensions.size() ; i++) {
760       String caption = reportResult.getReportSpec().getDimensionFromName(dimensions.elementAt(i).toString()).getCaption();
761       JRDesignStaticText text = new JRDesignStaticText();
762       text.setX(0);
763       text.setY(0);
764       text.setWidth(40);
765       text.setHeight(15);
766       text.setText(LanguageTraslator.traslate("308"));
767       band.addElement(text);
768       text = new JRDesignStaticText();
769       text.setX(printAt);
770       text.setY(0);
771       text.setWidth(getLenghtForCaptionHeader(caption.length()));
772       text.setHeight(15);
773       if(i == dimensions.size() - 1) {
774         text.setText(caption);
775       } else {
776         text.setText(caption + ",");
777       }
778       band.addElement(text);
779       printAt += getLenghtForCaptionHeader(caption.length());
780       JRDesignLine line = new JRDesignLine();
781       line = new JRDesignLine();
782       line.setX(0);
783       line.setY(16);
784       line.setWidth(515);
785       line.setHeight(0);
786       band.addElement(line);
787     }
788     printAt = 45;
789     for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
790       String caption = reportResult.getReportSpec().getDimensionFromName(nonGroupingDimensions.elementAt(i).toString()).getCaption();
791       JRDesignStaticText text = new JRDesignStaticText();
792       text.setX(0);
793       text.setY(15);
794       text.setWidth(40);
795       text.setHeight(15);
796       text.setText(LanguageTraslator.traslate("309"));
797       band.addElement(text);
798       text = new JRDesignStaticText();
799       text.setX(printAt);
800       text.setY(15);
801       text.setWidth(getLenghtForCaptionHeader(caption.length()));
802       text.setHeight(15);
803       if(i == nonGroupingDimensions.size() - 1) {
804         text.setText(caption);
805       } else {
806         text.setText(caption + ",");
807       }
808       band.addElement(text);
809       printAt += getLenghtForCaptionHeader(caption.length());
810       JRDesignLine line = new JRDesignLine();
811       line = new JRDesignLine();
812       line.setX(0);
813       line.setY(31);
814       line.setWidth(515);
815       line.setHeight(0);
816       band.addElement(line);
817     }
818     printAt = 45;
819     for(int i = 0 ; i < metrics.size() ; i++) {
820       String caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption();
821       JRDesignStaticText text = new JRDesignStaticText();
822       text.setX(0);
823       text.setY(30);
824       text.setWidth(40);
825       text.setHeight(15);
826       text.setText(LanguageTraslator.traslate("310"));
827       band.addElement(text);
828       text = new JRDesignStaticText();
829       text.setX(printAt);
830       text.setY(30);
831       text.setWidth(getLenghtForCaptionHeader(caption.length()));
832       text.setHeight(15);
833       if(accuMetrics.size() == 0) {
834         if(i == metrics.size() - 1) {
835           text.setText(caption);
836         } else {
837           text.setText(caption + ",");
838         }
839       } else {
840         text.setText(caption + ",");
841       }
842       band.addElement(text);
843       printAt += getLenghtForCaptionHeader(caption.length());
844       JRDesignLine line = new JRDesignLine();
845       line = new JRDesignLine();
846       line.setX(0);
847       line.setY(46);
848       line.setWidth(515);
849       line.setHeight(0);
850       band.addElement(line);
851     }
852     for(int i = 0 ; i < accuMetrics.size() ; i++) {
853       String caption = getAccMetricCaption(accuMetrics.elementAt(i).toString());
854       JRDesignStaticText text = new JRDesignStaticText();
855       text.setX(0);
856       text.setY(30);
857       text.setWidth(40);
858       text.setHeight(15);
859       text.setText(LanguageTraslator.traslate("310"));
860       band.addElement(text);
861       text = new JRDesignStaticText();
862       text.setX(printAt);
863       text.setY(30);
864       text.setWidth(getLenghtForCaptionHeader(caption.length()));
865       text.setHeight(15);
866       if(i == accuMetrics.size() - 1) {
867         text.setText(caption);
868       } else {
869         text.setText(caption + ",");
870       }
871       band.addElement(text);
872       printAt += getLenghtForCaptionHeader(caption.length());
873       JRDesignLine line = new JRDesignLine();
874       line = new JRDesignLine();
875       line.setX(0);
876       line.setY(46);
877       line.setWidth(515);
878       line.setHeight(0);
879       band.addElement(line);
880     }
881     jasperDesign.setPageHeader(band);
882   }*/

883
884   private void addPageHeader(JasperDesign jasperDesign) {
885
886       JRDesignBand band = new JRDesignBand();
887
888       String JavaDoc caption = getCaption(LanguageTraslator.traslate("308"), dimensions, new StringBuffer JavaDoc());
889       JRDesignStaticText text = new JRDesignStaticText();
890       text = new JRDesignStaticText();
891       text.setX(0);
892       text.setY(0);
893       text.setWidth(getLenghtForCaptionHeader(caption.length()));
894       text.setHeight(15);
895       text.setText(caption);
896       band.addElement(text);
897
898       caption = getCaption(LanguageTraslator.traslate("309"), nonGroupingDimensions, new StringBuffer JavaDoc());
899       text = new JRDesignStaticText();
900       text.setX(0);
901       text.setY(15);
902       text.setWidth(getLenghtForCaptionHeader(caption.length()));
903       text.setHeight(15);
904       text.setText(caption);
905       band.addElement(text);
906
907       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
908       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
909       sb.append(getCaption(LanguageTraslator.traslate("310"), metrics, buffer));
910       sb.append(getCaption("", accuMetrics, buffer));
911       text = new JRDesignStaticText();
912       text.setX(0);
913       text.setY(30);
914       text.setWidth(getLenghtForCaptionHeader(sb.toString().length()));
915       text.setHeight(15);
916       text.setText(sb.toString());
917       band.addElement(text);
918
919       jasperDesign.setPageHeader(band);
920   }
921
922   private String JavaDoc getCaption(String JavaDoc tittle, Vector vector, StringBuffer JavaDoc bufferString) {
923     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
924     buffer.append(tittle);
925     if(vector == dimensions || vector == nonGroupingDimensions) {
926       for(int i = 0 ; i < vector.size() ; i++){
927         String JavaDoc caption = reportResult.getReportSpec().getDimensionFromName(vector.elementAt(i).toString()).getCaption();
928         if(i == vector.size() - 1) {
929           buffer.append(caption);
930         } else {
931           buffer.append(caption + ", ");
932         }
933       }
934     } else if(vector == metrics) {
935       if(accuMetrics.size() == 0) {
936         for(int i = 0 ; i < vector.size() ; i++) {
937           String JavaDoc caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption();
938           if(i == vector.size() - 1) {
939             buffer.append(caption);
940           } else {
941             buffer.append(caption + ", ");
942           }
943         }
944       } else {
945         for(int i = 0 ; i < vector.size() ; i++) {
946           String JavaDoc caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption();
947           buffer.append(caption + ", ");
948         }
949       }
950     } else {
951       Object JavaDoc [] accMetrics = reportResult.getReportSpec().getAccumulableMetrics();
952       for(int i = 0 ; i < accMetrics.length ; i++) {
953         ReportMetricSpec metric = (ReportMetricSpec) accMetrics[i];
954         if(i == accMetrics.length - 1) {
955           buffer.append(metric.getCaption() + " " + LanguageTraslator.traslate("315"));
956         } else {
957           buffer.append(metric.getCaption() + " " + LanguageTraslator.traslate("315") + ", ") ;
958         }
959
960       }
961     }
962     return buffer.toString();
963   }
964
965   /**
966    * Agrega el titulo al reporte.
967    * @param jasperDesign
968    */

969   private void addTitle(JasperDesign jasperDesign) throws JRException{
970     //Title
971
JRDesignTextField textField = new JRDesignTextField();
972     //JRDesignLine line = new JRDesignLine();
973
JRDesignBand band = new JRDesignBand();
974         band.setHeight(50);
975         JRDesignStaticText staticText = new JRDesignStaticText();
976         staticText.setX(0);
977         staticText.setY(10);
978         staticText.setWidth(515);
979         staticText.setHeight(30);
980     staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
981     JRDesignReportFont bigFont = new JRDesignReportFont();
982         bigFont.setName("Arial_Normal");
983         bigFont.setDefault(true);
984         bigFont.setFontName("Arial");
985         bigFont.setFontSize(16);
986         bigFont.setPdfFontName("Helvetica");
987         bigFont.setPdfEncoding("Cp1252");
988         bigFont.setPdfEmbedded(false);
989     jasperDesign.addFont(bigFont);
990     staticText.setFont(bigFont);
991     staticText.setText(tittle);
992         band.addElement(staticText);
993     /*line = new JRDesignLine();
994         line.setX(0);
995         line.setY(0);
996         line.setWidth(515);
997         line.setHeight(0);
998         band.addElement(line);*/

999         jasperDesign.setTitle(band);
1000
1001    JRDesignBand emptyBand = new JRDesignBand();
1002    emptyBand.setHeight(1);
1003    jasperDesign.setPageHeader(emptyBand);
1004    emptyBand = new JRDesignBand();
1005    emptyBand.setHeight(1);
1006    jasperDesign.setColumnHeader(emptyBand);
1007    emptyBand = new JRDesignBand();
1008    emptyBand.setHeight(1);
1009    jasperDesign.setColumnFooter(emptyBand);
1010
1011
1012    band = new JRDesignBand();
1013    band.setHeight(15);
1014    staticText = new JRDesignStaticText();
1015    staticText.setX(0);
1016    staticText.setY(0);
1017    staticText.setWidth(40);
1018    staticText.setHeight(15);
1019    staticText.setText("Pagina: ");
1020    band.addElement(staticText);
1021    textField = new JRDesignTextField();
1022    textField.setX(40);
1023    textField.setY(0);
1024    textField.setWidth(100);
1025    textField.setHeight(15);
1026    JRDesignExpression expression = new JRDesignExpression();
1027    expression.setValueClass(java.lang.Integer JavaDoc.class);
1028    expression.setText("$V{PAGE_NUMBER}");
1029    textField.setExpression(expression);
1030    band.addElement(textField);
1031    jasperDesign.setPageFooter(band);
1032
1033    band = new JRDesignBand();
1034    band.setHeight(20);
1035    jasperDesign.setSummary(band);
1036  }
1037
1038/* private void addColumnsHeader(JasperDesign jasperDesign) {
1039    //columnHeader
1040    JRDesignBand band = new JRDesignBand();
1041    for(int i = 0 ; i < metrics.size() ; i++) {
1042      band.setHeight(20);
1043      JRDesignStaticText text = new JRDesignStaticText();
1044      text.setX(((Integer)metricsPosition.elementAt(i)).intValue());
1045      text.setY(0);
1046      text.setWidth(40);
1047      text.setHeight(20);
1048      text.setText(reportResult.getReportSpec().getDimensionFromName(metrics.elementAt(i).toString()).getCaption());
1049      band.addElement(text);
1050    }
1051    jasperDesign.setColumnHeader(band);
1052  }
1053
1054  private void addColumnsFooter(JasperDesign jasperDesign) {
1055    JRDesignBand band = new JRDesignBand();
1056    JRDesignTextField textField;
1057    String name;
1058    band.setHeight(20);
1059    JRDesignStaticText text = new JRDesignStaticText();
1060    text.setX(40);
1061    text.setY(0);
1062    text.setWidth(40);
1063    text.setHeight(20);
1064    text.setText("Total");
1065    band.addElement(text);
1066
1067    for(int i = 0 ; i < metrics.size() ; i++) {
1068      textField = new JRDesignTextField();
1069      textField.setX(((Integer)metricsPosition.elementAt(i)).intValue());
1070      textField.setY(0);
1071      textField.setWidth(100);
1072      textField.setHeight(15);
1073
1074      JRDesignExpression expression = new JRDesignExpression();
1075      expression.setValueClass(java.lang.Float.class);
1076      name = metrics.elementAt(i).toString()+"_SUM";
1077      expression.setText("$V{"+name+"}");
1078      textField.setExpression(expression);
1079      band.addElement(textField);
1080
1081      band.addElement(text);
1082    }
1083
1084    jasperDesign.setColumnFooter(band);
1085  }
1086  */

1087
1088  /**
1089   * Inicializa los vectores con la descripcion de las dimensiones
1090   * que agrupan, las que no agrupan y las metricas.
1091   * @param dimensionsCount
1092   * @param metricsCount
1093   * @param nonGroupingDimensionsCount
1094   * @param accMetricsCount
1095   */

1096  private void initializeVectors(int dimensionsCount, int metricsCount, int nonGroupingDimensionsCount, int accMetricsCount) throws InfoException {
1097    rowsTotals = new Vector();
1098    dimensions = new Vector();
1099    nonGroupingDimensions = new Vector();
1100    metrics = new Vector();
1101    accuMetrics = new Vector();
1102    int i = 0;
1103    if(nonGroupingDimensionsCount > 0) {
1104      for (; i < dimensionsCount; i++) {
1105        dimensions.add(data.getColumnName(i));
1106      }
1107
1108      if(nonGroupingDimensionsCount > 1) {
1109        for (int z = 0; z < nonGroupingDimensionsCount - 1; i++, z++) {
1110          nonGroupingDimensions.add(data.getColumnName(i));
1111        }
1112      }
1113
1114      Vector vector = reportResult.getReportTableModel().getOccurrences();
1115
1116      for (int j = 0; j < vector.size() * metricsCount ; i++, j++) {
1117        metrics.add(data.getColumnName(i));
1118      }
1119
1120      for (int j = 0; j < accMetricsCount; i++, j++) {
1121        accuMetrics.add(data.getColumnName(i));
1122      }
1123
1124      if(reportResult.getReportQuery().isVisibleTotals()){
1125        int startingPoint = data.getColumnCount() - metricsCount;
1126        for(int x = startingPoint ; x < data.getColumnCount() ; x++) {
1127          rowsTotals.add(data.getColumnName(x));
1128        }
1129      }
1130
1131    } else {
1132      for (; i < dimensionsCount; i++) {
1133        dimensions.add(data.getColumnName(i));
1134      }
1135
1136      for (int j = 0 ; j < metricsCount; j++, i++) {
1137        metrics.add(data.getColumnName(i));
1138      }
1139
1140      for (int z = 0 ; z < accMetricsCount; z++, i++) {
1141        accuMetrics.add(data.getColumnName(i));
1142      }
1143
1144    }
1145  }
1146
1147  /**
1148   * Determina un Vector con los anchos mas precisos.
1149   * @param vector
1150   * @return
1151   */

1152  private Vector calculateBestLenghts(Vector vector) {
1153    Vector bestSizes = null;
1154    Vector ColumnSizes;
1155    if (data.getRowCount() != 0) {
1156      bestSizes = new Vector();
1157      for (int i = 0; i < vector.size(); i++) {
1158        ColumnSizes = new Vector();
1159        String JavaDoc columnName = "";
1160        ReportFieldSpec spec;
1161        if((spec = reportResult.getDimensionFromName(vector.elementAt(i).toString()))!=null){
1162          columnName = spec.getCaption();
1163        }else if((spec = reportResult.getMetricFromName(getMetricName(vector.elementAt(i).toString())))!=null){
1164          columnName = spec.getCaption();
1165        }else{
1166          columnName = "";
1167        }
1168        ColumnSizes.add(new Integer JavaDoc(columnName.length()));
1169        Object JavaDoc value = data.getValueAt(0, getColumnIndexFromName(vector.elementAt(i).toString()));
1170        if (value != null){
1171          String JavaDoc current = value.toString();
1172          if (data.getRowCount() == 1) {
1173            ColumnSizes.add(new Integer JavaDoc(current.length()));
1174            break;
1175          }
1176          else {
1177            ColumnSizes.add(new Integer JavaDoc(current.length()));
1178            for (int j = 0; j < data.getRowCount(); j++) {
1179              if (!current.equals(data.getValueAt(j, i).toString())) {
1180                ColumnSizes.add(new Integer JavaDoc(data.getValueAt(j, getColumnIndexFromName(vector.elementAt(i).toString())).toString().length()));
1181                current = data.getValueAt(j, i).toString();
1182              }
1183            }
1184            bestSizes.add(getBestSize(ColumnSizes));
1185          }
1186        }
1187      }
1188    }
1189    return bestSizes;
1190  }
1191
1192  /**
1193   * Retorna un indice a partir de un nombre de columna.
1194   * @param columnName
1195   * @return
1196   */

1197  private int getColumnIndexFromName(String JavaDoc columnName) {
1198    int index = 0;
1199    for (int i = 0; i < data.getColumnCount(); i++) {
1200      if (data.getColumnName(i).equals(columnName)) {
1201        index = i;
1202        break;
1203      }
1204    }
1205    return index;
1206  }
1207
1208  /**
1209   * Retorna el ancho mas eficiente comparando los elementos
1210   * del Vector
1211   * @param ColumnSizes
1212   * @return
1213   */

1214  private Integer JavaDoc getBestSize(Vector ColumnSizes) {
1215    int compareTo = 0;
1216    for (int i = 0; i < ColumnSizes.size(); i++) {
1217      if (((Integer JavaDoc) ColumnSizes.elementAt(i)).intValue() > compareTo) {
1218        compareTo = ((Integer JavaDoc) ColumnSizes.elementAt(i)).intValue();
1219      }
1220    }
1221    return new Integer JavaDoc(compareTo);
1222  }
1223
1224  /**
1225   * Determina el ancho para las descripciones
1226   * de la cabecera
1227   * @param chars
1228   * @return
1229   */

1230  private int getLenghtForCaptionHeader(int chars){
1231    //return chars * 8 - chars;
1232
Integer JavaDoc integer = new Integer JavaDoc(chars);
1233    Double JavaDoc qsrt = new Double JavaDoc(Math.sqrt(integer.doubleValue()));
1234    return chars * 7 - ((chars / 2) - 3 * qsrt.intValue());
1235  }
1236
1237  /**
1238   * Determina el comienzo donde se mostraran las
1239   * metricas o dimensiones que no agrupan.
1240   * @return
1241   */

1242  private int calculateStartForItem() {
1243    int width = 0 ;
1244    for(int i = 0 ; i < dimensionsSizes.size() ; i++){
1245      width += (((Integer JavaDoc)dimensionsSizes.elementAt(i)).intValue());
1246    }
1247
1248    //return (width) + 100;
1249
return 0;
1250  }
1251
1252  /**
1253   * Retorna un ancho preciso a partir de una
1254   * determinada cantidad de caracteres
1255   * @param chars
1256   * @return
1257   */

1258  private int getLenghtForCaption(int chars) {
1259    /*if(chars == 2 ) {
1260      return chars * 7;
1261    }
1262    Integer integer = new Integer(chars);
1263    Double qsrt = new Double(Math.sqrt(integer.doubleValue()));
1264    return chars * 8 - ((chars / 2) - 3 * qsrt.intValue());*/

1265
1266    return chars * 8;
1267  }
1268
1269  /**
1270   * Retorna los fields.
1271   * @return
1272   */

1273  public Map getFields() {
1274    if (fields == null){
1275      fields = new HashMap();
1276    }
1277    return fields;
1278  }
1279
1280  /**
1281   * Retorna las variables
1282   * @return
1283   */

1284  public Map getVariables() {
1285    if (variables == null ){
1286      variables = new HashMap();
1287    }
1288    return variables;
1289  }
1290}
1291
Popular Tags