001: package com.calipso.reportgenerator.common;
002:
003: import javax.swing.table.TableModel;
004:
005: import net.sf.jasperreports.engine.design.*;
006: import net.sf.jasperreports.engine.JRException;
007: import net.sf.jasperreports.engine.JRGroup;
008: import net.sf.jasperreports.engine.JRTextElement;
009: import net.sf.jasperreports.engine.JRQuery;
010:
011: import java.util.*;
012: import java.text.NumberFormat;
013: import java.text.DecimalFormat;
014:
015: /**
016: *
017: * User: jbassino
018: * Date: 01/06/2004
019: * Time: 14:29:40
020: *
021: */
022: public class StaticSQLJasperReportDefinition implements
023: IJasperDefinition {
024: private Map fields;
025: private Map variables;
026: private TableModel data;
027: private String tittle;
028: private Vector dimensions;
029: private Vector metrics;
030: private Vector accuMetrics;
031: private Vector nonGroupingDimensions;
032: private Vector dimensionsSizes;
033: private Vector metricsSizes;
034: private Vector accuMetricsSizes;
035: private Vector nonGroupingDimensionsSizes;
036: private int printAt;
037: private Vector metricsPosition = new Vector();
038: private Vector nonGroupingDimensionPosition = new Vector();
039: private Vector groupingDimensionPosition = new Vector();
040: //private ReportResult reportResult;
041: private ReportSpec reportSpec;
042: private String numberFormatPattern;
043: private Vector metricsWidth;
044: private Vector accMetricsPosition;
045: private Vector accMetricsWidht;
046: private JRQuery query = null;
047: private JasperDesign jasperDesign = null;
048: private Map reportParams;
049:
050: public StaticSQLJasperReportDefinition(ReportSpec reportSpec,
051: TableModel data, int dimensionsCount, int metricsCount,
052: int nonGroupingDimensionsCount, int accuMetricsCount,
053: String tittle, Map params) {
054: this .data = data;
055: this .tittle = tittle;
056: //this.reportResult = reportResult;
057: this .reportSpec = reportSpec;
058: metricsWidth = new Vector();
059: accMetricsPosition = new Vector();
060: accMetricsWidht = new Vector();
061: initializeVectors(dimensionsCount, metricsCount,
062: nonGroupingDimensionsCount, accuMetricsCount);
063: reportParams = params;
064: dimensionsSizes = calculateBestLenghts(dimensions);
065: metricsSizes = calculateBestLenghts(metrics);
066: accuMetricsSizes = calculateBestLenghts(this .accuMetrics);
067: nonGroupingDimensionsSizes = calculateBestLenghts(nonGroupingDimensions);
068: initializeNumberFormatPattern();
069: }
070:
071: public StaticSQLJasperReportDefinition(IJasperDefinition jasper,
072: boolean isLandscape) throws InfoException {
073: try {
074: jasperDesign = jasper.getJasperDefinition(isLandscape);
075: } catch (Exception e) {
076: throw new InfoException(LanguageTraslator.traslate("221"),
077: e);
078: }
079: }
080:
081: private void initializeNumberFormatPattern() {
082: NumberFormat format = NumberFormat
083: .getInstance(LanguageTraslator.getLocale());
084: numberFormatPattern = ((DecimalFormat) format).toPattern();
085: }
086:
087: public JasperDesign getJasperDefinition(boolean isLandscape)
088: throws JRException {
089: if (query == null) {
090: throw new JRException("La query es nula");
091: }
092: if (jasperDesign != null) {
093: jasperDesign.setQuery(query);
094: return jasperDesign;
095: }
096: JasperDesign jasperDesign = new JasperDesign();
097: if (isLandscape) {
098: jasperDesign
099: .setOrientation(JasperDesign.ORIENTATION_LANDSCAPE);
100: jasperDesign
101: .setPrintOrder(JasperDesign.PRINT_ORDER_VERTICAL);
102: jasperDesign.setPageWidth(842);
103: jasperDesign.setPageHeight(595);
104: } else {
105: jasperDesign
106: .setOrientation(JasperDesign.ORIENTATION_PORTRAIT);
107: jasperDesign
108: .setPrintOrder(JasperDesign.PRINT_ORDER_HORIZONTAL);
109: jasperDesign.setPageWidth(595);
110: jasperDesign.setPageHeight(842);
111: }
112: jasperDesign.setName("BasicReport");
113: addFields(jasperDesign);
114: addDetails(jasperDesign);
115: addGroups(jasperDesign);
116: addVariables(jasperDesign);
117: addTitle(jasperDesign);
118: addParams(jasperDesign);
119: //addPageHeader(jasperDesign);
120: if (query == null) {
121: throw new JRException("La query es nula");
122: }
123: jasperDesign.setQuery(query);
124: return jasperDesign;
125: }
126:
127: /**
128: * Agrega los parametros del reporte a la definicion del jasper para que puedan ser utilizados en la visualizacion.
129: * @param jasperDesign
130: * @throws JRException
131: */
132: private void addParams(JasperDesign jasperDesign)
133: throws JRException {
134: for (Iterator iterator = reportParams.entrySet().iterator(); iterator
135: .hasNext();) {
136: Map.Entry param = (Map.Entry) iterator.next();
137: JRDesignParameter parameter = new JRDesignParameter();
138: parameter.setName(param.getKey().toString());
139: parameter.setDescription(param.getKey().toString());
140: Class paramClass = getClassFor(param);
141: parameter.setValueClass(paramClass);
142: jasperDesign.addParameter(parameter);
143: }
144: }
145:
146: /**
147: * Obtiene la clase a la que pertenece el valor de un parametro dado.
148: * @param param
149: * @return
150: */
151: private Class getClassFor(Map.Entry param) {
152: Class result = java.lang.String.class;
153: Map map = new HashMap();
154: map.put(param.getKey(), param.getValue());
155: ReportMap.setParametersToSimpleType(map);
156: if (map.get(param.getKey()) != null) {
157: result = map.get(param.getKey()).getClass();
158: }
159: return result;
160: }
161:
162: public void setSQLText(String sqlText) {
163: String queryText = removeFilters(sqlText);
164: System.out.println(queryText);
165: query = new JRDesignQuery();
166: ((JRDesignQuery) query).setText(queryText);
167: }
168:
169: private String removeFilters(String sqlText) {
170: String result = "";
171: StringTokenizer tokenizer = new StringTokenizer(sqlText, "%");
172: String token;
173: int i = 0;
174: while (tokenizer.hasMoreElements()) {
175: token = tokenizer.nextToken();
176: result += token;
177: if (tokenizer.hasMoreElements()) {
178: tokenizer.nextToken();
179: }
180: }
181: return result;
182: }
183:
184: private void addFields(JasperDesign jasperDesign)
185: throws JRException {
186: //Fields
187: String name;
188: for (int i = 0; i < dimensions.size(); i++) {
189: JRDesignField field = new JRDesignField();
190: name = dimensions.elementAt(i).toString();
191: field.setName(name);
192: field.setValueClass(String.class);
193: jasperDesign.addField(field);
194: }
195: for (int i = 0; i < nonGroupingDimensions.size(); i++) {
196: JRDesignField field = new JRDesignField();
197: name = nonGroupingDimensions.elementAt(i).toString();
198: field.setName(name);
199: field.setValueClass(String.class);
200: jasperDesign.addField(field);
201: }
202: for (int i = 0; i < metrics.size(); i++) {
203: JRDesignField field = new JRDesignField();
204: name = metrics.elementAt(i).toString();
205: field.setName(name);
206: field.setValueClass(Float.class);
207: jasperDesign.addField(field);
208: }
209: for (int i = 0; i < accuMetrics.size(); i++) {
210: JRDesignField field = new JRDesignField();
211: name = accuMetrics.elementAt(i).toString();
212: field.setName(name);
213: field.setValueClass(Float.class);
214: jasperDesign.addField(field);
215: }
216: }
217:
218: /**
219: * Agrega los grupos a la definicion
220: * @param jasperDesign
221: * @throws JRException
222: */
223: private void addGroups(JasperDesign jasperDesign)
224: throws JRException {
225: //groups
226: JRDesignExpression expression;
227: JRDesignBand band;
228: JRDesignGroup group = null;
229: printAt = 0;
230: for (int j = 0; j < dimensions.size(); j++) {
231: group = new JRDesignGroup();
232: group
233: .setName(dimensions.elementAt(j).toString()
234: + "_GROUP");
235: group.setReprintHeaderOnEachPage(true);
236:
237: expression = new JRDesignExpression();
238: expression.setValueClass(String.class);
239: expression.setText("$F{" + dimensions.elementAt(j) + "}");
240: group.setExpression(expression);
241:
242: if (j + 1 == dimensions.size()) {
243: band = new JRDesignBand();
244: band.setHeight(40);
245: } else {
246: band = new JRDesignBand();
247: band.setHeight(20);
248: }
249:
250: JRDesignStaticText text = new JRDesignStaticText();
251: String caption = reportSpec.getDimensionFromName(
252: dimensions.elementAt(j).toString()).getCaption()
253: + ":";
254: text.setX(printAt);
255: text.setY(0);
256: text.setWidth(getLenghtForCaption((new Integer(caption
257: .length())).intValue()));
258: text.setHeight(20);
259: text.setText(caption);
260: band.addElement(text);
261:
262: JRDesignTextField textField = new JRDesignTextField();
263: textField
264: .setX(printAt
265: + getLenghtForCaption((new Integer(caption
266: .length())).intValue()) - 5);
267: textField.setY(0);
268: textField
269: .setWidth(getLenghtForCaption(((Integer) dimensionsSizes
270: .elementAt(j)).intValue()));
271: textField.setHeight(20);
272: expression = new JRDesignExpression();
273: expression.setValueClass(String.class);
274: expression.setText("$F{" + dimensions.elementAt(j) + "}");
275: textField.setExpression(expression);
276: band.addElement(textField);
277: group.setGroupHeader(band);
278:
279: if (j + 1 == dimensions.size()) {
280: for (int i = 0; i < nonGroupingDimensions.size(); i++) {
281: int x = ((Integer) nonGroupingDimensionPosition
282: .elementAt(i)).intValue();
283: text = new JRDesignStaticText();
284: text.setX(x);
285: text.setY(20);
286: //text.setWidth(((Integer) metricsWidth.elementAt(i)).intValue());
287: int first = getLenghtForCaption(((Integer) nonGroupingDimensionsSizes
288: .elementAt(i)).intValue());
289: int second = getLenghtForCaption(nonGroupingDimensions
290: .elementAt(i).toString().length());
291: if (first >= second) {
292: text.setWidth(getLenghtForCaption(first));
293: } else {
294: text.setWidth(getLenghtForCaption(second));
295: }
296: text.setHeight(20);
297: text.setText(reportSpec.getDimensionFromName(
298: nonGroupingDimensions.elementAt(i)
299: .toString()).getCaption());
300: band.addElement(text);
301: }
302:
303: for (int i = 0; i < metrics.size(); i++) {
304: int x = ((Integer) metricsPosition.elementAt(i))
305: .intValue();
306: text = new JRDesignStaticText();
307: text.setX(x);
308: text.setY(20);
309: text.setWidth(((Integer) metricsWidth.elementAt(i))
310: .intValue());
311: text.setHeight(20);
312: text.setText(reportSpec.getMetricFromName(
313: metrics.elementAt(i).toString())
314: .getCaption());
315: band.addElement(text);
316: }
317:
318: for (int i = 0; i < accMetricsPosition.size(); i++) {
319: int x = ((Integer) accMetricsPosition.elementAt(i))
320: .intValue();
321: text = new JRDesignStaticText();
322: text.setX(x);
323: text.setY(20);
324: text.setWidth(((Integer) accMetricsWidht
325: .elementAt(i)).intValue());
326: text.setHeight(20);
327: String metricName = getMetricName(accuMetrics
328: .elementAt(i).toString());
329: String metricCaption = reportSpec
330: .getMetricFromName(metricName).getCaption();
331: text.setText(metricCaption + " "
332: + LanguageTraslator.traslate("315"));
333: band.addElement(text);
334: }
335: group.setGroupHeader(band);
336: }
337:
338: band = new JRDesignBand();
339: band.setHeight(30);
340: text = new JRDesignStaticText();
341: text.setX(printAt);
342: text.setY(0);
343: text.setWidth(40);
344: text.setHeight(20);
345: text.setText(LanguageTraslator.traslate("358"));
346: band.addElement(text);
347: for (int i = 0; i < metrics.size(); i++) {
348: band.setHeight(20);
349: textField = new JRDesignTextField();
350: textField.setX(((Integer) metricsPosition.elementAt(i))
351: .intValue());
352: textField.setY(0);
353: textField
354: .setWidth(getLenghtForCaption(((Integer) dimensionsSizes
355: .elementAt(j)).intValue()) + 20);
356: textField.setHeight(20);
357: expression = new JRDesignExpression();
358: expression.setValueClass(Float.class);
359: expression.setText("$V{"
360: + dimensions.elementAt(j).toString()
361: + metrics.elementAt(i).toString() + "_SUM}");
362: textField.setPattern(numberFormatPattern);
363: textField.setExpression(expression);
364: band.addElement(textField);
365: group.setGroupFooter(band);
366: }
367: groupingDimensionPosition.add(new Integer(printAt));
368: printAt += 10;//getLenghtForCaption(((Integer)dimensionsSizes.elementAt(j)).intValue());
369:
370: jasperDesign.addGroup(group);
371: }
372: }
373:
374: private String getMetricName(String s) {
375: StringTokenizer tokenizer = new StringTokenizer(s, "_");
376: String returnVal = tokenizer.nextToken();
377: return returnVal;
378: }
379:
380: /**
381: * Agrega las variables a la definicion
382: * @param jasperDesign
383: * @throws JRException
384: */
385: private void addVariables(JasperDesign jasperDesign)
386: throws JRException {
387: //Variables
388: JRDesignExpression expression;
389: for (int j = 0; j < dimensions.size(); j++) {
390: for (int i = 0; i < metrics.size(); i++) {
391: JRDesignVariable jrVariable = new JRDesignVariable();
392: jrVariable.setName(dimensions.elementAt(j).toString()
393: + metrics.elementAt(i).toString() + "_SUM");
394: jrVariable.setValueClass(Float.class);
395: jrVariable
396: .setResetType(JRDesignVariable.RESET_TYPE_GROUP);
397: jrVariable
398: .setCalculation(JRDesignVariable.CALCULATION_SUM);
399: jrVariable.setResetGroup((JRGroup) jasperDesign
400: .getGroupsList().get(j));
401: expression = new JRDesignExpression();
402: expression.setValueClass(Float.class);
403: expression.setText("$F{"
404: + metrics.elementAt(i).toString() + "}");
405: jrVariable.setExpression(expression);
406: jasperDesign.addVariable(jrVariable);
407: }
408: }
409: }
410:
411: /**
412: * Agrega los detalles a la definicion
413: * @param jasperDesign
414: */
415: private void addDetails(JasperDesign jasperDesign) {
416: //Details
417: int printAt = calculateStartForItem();
418: JRDesignBand band = new JRDesignBand();
419: band.setHeight(20);
420: JRDesignExpression expression;
421: for (int i = 0; i < nonGroupingDimensions.size(); i++) {
422: JRDesignTextField textField = new JRDesignTextField();
423: textField.setX(printAt);
424: textField.setY(0);
425: textField
426: .setWidth(getLenghtForCaption(((Integer) nonGroupingDimensionsSizes
427: .elementAt(i)).intValue()));
428: textField.setHeight(20);
429: expression = new JRDesignExpression();
430: expression.setValueClass(String.class);
431: expression.setText("$F{"
432: + nonGroupingDimensions.elementAt(i) + "}");
433: textField.setExpression(expression);
434: band.addElement(textField);
435:
436: nonGroupingDimensionPosition.add(new Integer(printAt));
437:
438: int first = getLenghtForCaption(((Integer) nonGroupingDimensionsSizes
439: .elementAt(i)).intValue());
440: int second = getLenghtForCaption(nonGroupingDimensions
441: .elementAt(i).toString().length());
442: if (first >= second) {
443: printAt += first;
444: } else {
445: printAt += second;
446: }
447: //printAt += getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue());
448:
449: }
450: for (int i = 0; i < metrics.size(); i++) {
451: JRDesignTextField textField = new JRDesignTextField();
452: textField.setX(printAt + 25);
453: textField.setY(0);
454: //textField.setWidth(getLenghtForCaption(((Integer) metricsSizes.elementAt(i)).intValue())+15);
455:
456: metricsWidth.add(new Integer(
457: getLenghtForCaption((new Integer(metrics.elementAt(
458: i).toString().length())).intValue()) + 25));
459: textField
460: .setWidth(getLenghtForCaption((new Integer(metrics
461: .elementAt(i).toString().length()))
462: .intValue()) + 25);
463:
464: textField.setHeight(20);
465: textField.setPattern(numberFormatPattern);
466: expression = new JRDesignExpression();
467: expression.setValueClass(Float.class);
468: expression.setText("$F{" + metrics.elementAt(i) + "}");
469: textField.setExpression(expression);
470: band.addElement(textField);
471: metricsPosition.add(new Integer(printAt + 25));
472: printAt += getLenghtForCaption(((Integer) metricsSizes
473: .elementAt(i)).intValue()) + 25;
474: }
475:
476: for (int i = 0; i < accuMetrics.size(); i++) {
477: JRDesignTextField textField = new JRDesignTextField();
478: textField.setX(printAt + 25);
479: textField.setY(0);
480:
481: //textField.setWidth(getLenghtForCaption(((Integer) accuMetricsSizes.elementAt(i)).intValue())+15);
482:
483: accMetricsWidht.add(new Integer(
484: getLenghtForCaption((new Integer(accuMetrics
485: .elementAt(i).toString().length()))
486: .intValue()) + 25));
487: textField.setWidth(getLenghtForCaption((new Integer(
488: accuMetrics.elementAt(i).toString().length()))
489: .intValue()) + 25);
490:
491: textField.setHeight(20);
492: textField.setPattern(numberFormatPattern);
493: expression = new JRDesignExpression();
494: expression.setValueClass(Float.class);
495: expression.setText("$F{" + accuMetrics.elementAt(i) + "}");
496: textField.setExpression(expression);
497: band.addElement(textField);
498: metricsPosition.add(new Integer(printAt + 25));
499: accMetricsPosition.add(new Integer(printAt + 25));
500: printAt += getLenghtForCaption(((Integer) accuMetricsSizes
501: .elementAt(i)).intValue()) + 25;
502: }
503:
504: jasperDesign.setDetail(band);
505: }
506:
507: /**
508: * Agrega la cabecera de pagina a la definicion
509: * @param jasperDesign
510: */
511: /*private void addPageHeader(JasperDesign jasperDesign) {
512: int printAt = 45;
513: JRDesignBand band = new JRDesignBand();
514: for(int i = 0 ; i < dimensions.size() ; i++) {
515: String caption = reportResult.getReportSpec().getDimensionFromName(dimensions.elementAt(i).toString()).getCaption();
516: JRDesignStaticText text = new JRDesignStaticText();
517: text.setX(0);
518: text.setY(0);
519: text.setWidth(40);
520: text.setHeight(15);
521: text.setText(LanguageTraslator.traslate("308"));
522: band.addElement(text);
523: text = new JRDesignStaticText();
524: text.setX(printAt);
525: text.setY(0);
526: text.setWidth(getLenghtForCaptionHeader(caption.length()));
527: text.setHeight(15);
528: if(i == dimensions.size() - 1) {
529: text.setText(caption);
530: } else {
531: text.setText(caption + ",");
532: }
533: band.addElement(text);
534: printAt += getLenghtForCaptionHeader(caption.length());
535: JRDesignLine line = new JRDesignLine();
536: line = new JRDesignLine();
537: line.setX(0);
538: line.setY(16);
539: line.setWidth(515);
540: line.setHeight(0);
541: band.addElement(line);
542: }
543: printAt = 45;
544: for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
545: String caption = reportResult.getReportSpec().getDimensionFromName(nonGroupingDimensions.elementAt(i).toString()).getCaption();
546: JRDesignStaticText text = new JRDesignStaticText();
547: text.setX(0);
548: text.setY(15);
549: text.setWidth(40);
550: text.setHeight(15);
551: text.setText(LanguageTraslator.traslate("309"));
552: band.addElement(text);
553: text = new JRDesignStaticText();
554: text.setX(printAt);
555: text.setY(15);
556: text.setWidth(getLenghtForCaptionHeader(caption.length()));
557: text.setHeight(15);
558: if(i == nonGroupingDimensions.size() - 1) {
559: text.setText(caption);
560: } else {
561: text.setText(caption + ",");
562: }
563: band.addElement(text);
564: printAt += getLenghtForCaptionHeader(caption.length());
565: JRDesignLine line = new JRDesignLine();
566: line = new JRDesignLine();
567: line.setX(0);
568: line.setY(31);
569: line.setWidth(515);
570: line.setHeight(0);
571: band.addElement(line);
572: }
573: printAt = 45;
574: for(int i = 0 ; i < metrics.size() ; i++) {
575: String caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption();
576: JRDesignStaticText text = new JRDesignStaticText();
577: text.setX(0);
578: text.setY(30);
579: text.setWidth(40);
580: text.setHeight(15);
581: text.setText(LanguageTraslator.traslate("310"));
582: band.addElement(text);
583: text = new JRDesignStaticText();
584: text.setX(printAt);
585: text.setY(30);
586: text.setWidth(getLenghtForCaptionHeader(caption.length()));
587: text.setHeight(15);
588: if(accuMetrics.size() == 0) {
589: if(i == metrics.size() - 1) {
590: text.setText(caption);
591: } else {
592: text.setText(caption + ",");
593: }
594: } else {
595: text.setText(caption + ",");
596: }
597: band.addElement(text);
598: printAt += getLenghtForCaptionHeader(caption.length());
599: JRDesignLine line = new JRDesignLine();
600: line = new JRDesignLine();
601: line.setX(0);
602: line.setY(46);
603: line.setWidth(515);
604: line.setHeight(0);
605: band.addElement(line);
606: }
607: for(int i = 0 ; i < accuMetrics.size() ; i++) {
608: String caption = getAccMetricCaption(accuMetrics.elementAt(i).toString());
609: JRDesignStaticText text = new JRDesignStaticText();
610: text.setX(0);
611: text.setY(30);
612: text.setWidth(40);
613: text.setHeight(15);
614: text.setText(LanguageTraslator.traslate("310"));
615: band.addElement(text);
616: text = new JRDesignStaticText();
617: text.setX(printAt);
618: text.setY(30);
619: text.setWidth(getLenghtForCaptionHeader(caption.length()));
620: text.setHeight(15);
621: if(i == accuMetrics.size() - 1) {
622: text.setText(caption);
623: } else {
624: text.setText(caption + ",");
625: }
626: band.addElement(text);
627: printAt += getLenghtForCaptionHeader(caption.length());
628: JRDesignLine line = new JRDesignLine();
629: line = new JRDesignLine();
630: line.setX(0);
631: line.setY(46);
632: line.setWidth(515);
633: line.setHeight(0);
634: band.addElement(line);
635: }
636: jasperDesign.setPageHeader(band);
637: }*/
638:
639: private void addPageHeader(JasperDesign jasperDesign) {
640:
641: JRDesignBand band = new JRDesignBand();
642:
643: String caption = getCaption(LanguageTraslator.traslate("308"),
644: dimensions, new StringBuffer());
645: JRDesignStaticText text = new JRDesignStaticText();
646: text = new JRDesignStaticText();
647: text.setX(0);
648: text.setY(0);
649: text.setWidth(getLenghtForCaptionHeader(caption.length()));
650: text.setHeight(15);
651: text.setText(caption);
652: band.addElement(text);
653:
654: caption = getCaption(LanguageTraslator.traslate("309"),
655: nonGroupingDimensions, new StringBuffer());
656: text = new JRDesignStaticText();
657: text.setX(0);
658: text.setY(15);
659: text.setWidth(getLenghtForCaptionHeader(caption.length()));
660: text.setHeight(15);
661: text.setText(caption);
662: band.addElement(text);
663:
664: StringBuffer sb = new StringBuffer();
665: StringBuffer buffer = new StringBuffer();
666: sb.append(getCaption(LanguageTraslator.traslate("310"),
667: metrics, buffer));
668: sb.append(getCaption("", accuMetrics, buffer));
669: text = new JRDesignStaticText();
670: text.setX(0);
671: text.setY(30);
672: text
673: .setWidth(getLenghtForCaptionHeader(sb.toString()
674: .length()));
675: text.setHeight(15);
676: text.setText(sb.toString());
677: band.addElement(text);
678:
679: jasperDesign.setPageHeader(band);
680: }
681:
682: private String getCaption(String tittle, Vector vector,
683: StringBuffer bufferString) {
684: StringBuffer buffer = new StringBuffer();
685: buffer.append(tittle);
686: if (vector == dimensions || vector == nonGroupingDimensions) {
687: for (int i = 0; i < vector.size(); i++) {
688: String caption = reportSpec.getDimensionFromName(
689: vector.elementAt(i).toString()).getCaption();
690: if (i == vector.size() - 1) {
691: buffer.append(caption);
692: } else {
693: buffer.append(caption + ", ");
694: }
695: }
696: } else if (vector == metrics) {
697: if (accuMetrics.size() == 0) {
698: for (int i = 0; i < vector.size(); i++) {
699: String caption = reportSpec.getMetricFromName(
700: metrics.elementAt(i).toString())
701: .getCaption();
702: if (i == vector.size() - 1) {
703: buffer.append(caption);
704: } else {
705: buffer.append(caption + ", ");
706: }
707: }
708: } else {
709: for (int i = 0; i < vector.size(); i++) {
710: String caption = reportSpec.getMetricFromName(
711: metrics.elementAt(i).toString())
712: .getCaption();
713: buffer.append(caption + ", ");
714: }
715: }
716: } else {
717: Object[] accMetrics = reportSpec.getAccumulableMetrics();
718: for (int i = 0; i < accMetrics.length; i++) {
719: ReportMetricSpec metric = (ReportMetricSpec) accMetrics[i];
720: if (i == accMetrics.length - 1) {
721: buffer.append(metric.getCaption() + " "
722: + LanguageTraslator.traslate("315"));
723: } else {
724: buffer.append(metric.getCaption() + " "
725: + LanguageTraslator.traslate("315") + ", ");
726: }
727:
728: }
729: }
730: return buffer.toString();
731: }
732:
733: /**
734: * Agrega el titulo al reporte.
735: * @param jasperDesign
736: */
737: private void addTitle(JasperDesign jasperDesign) throws JRException {
738: //Title
739: JRDesignTextField textField = new JRDesignTextField();
740: //JRDesignLine line = new JRDesignLine();
741: JRDesignBand band = new JRDesignBand();
742: band.setHeight(50);
743: JRDesignStaticText staticText = new JRDesignStaticText();
744: staticText.setX(0);
745: staticText.setY(10);
746: staticText.setWidth(515);
747: staticText.setHeight(30);
748: staticText.setTextAlignment(JRTextElement.TEXT_ALIGN_CENTER);
749: JRDesignReportFont bigFont = new JRDesignReportFont();
750: bigFont.setName("Arial_Normal");
751: bigFont.setDefault(true);
752: bigFont.setFontName("Arial");
753: bigFont.setSize(16);
754: bigFont.setPdfFontName("Helvetica");
755: bigFont.setPdfEncoding("Cp1252");
756: bigFont.setPdfEmbedded(false);
757: jasperDesign.addFont(bigFont);
758: staticText.setFont(bigFont);
759: staticText.setText(tittle);
760: band.addElement(staticText);
761: /*line = new JRDesignLine();
762: line.setX(0);
763: line.setY(0);
764: line.setWidth(515);
765: line.setHeight(0);
766: band.addElement(line);*/
767: jasperDesign.setTitle(band);
768:
769: band = new JRDesignBand();
770: band.setHeight(15);
771: staticText = new JRDesignStaticText();
772: staticText.setX(0);
773: staticText.setY(0);
774: staticText.setWidth(40);
775: staticText.setHeight(15);
776: staticText.setText("Pagina: ");
777: band.addElement(staticText);
778: textField = new JRDesignTextField();
779: textField.setX(40);
780: textField.setY(0);
781: textField.setWidth(100);
782: textField.setHeight(15);
783: JRDesignExpression expression = new JRDesignExpression();
784: expression.setValueClass(Integer.class);
785: expression.setText("$V{PAGE_NUMBER}");
786: textField.setExpression(expression);
787: band.addElement(textField);
788: jasperDesign.setPageFooter(band);
789:
790: band = new JRDesignBand();
791: band.setHeight(20);
792: jasperDesign.setSummary(band);
793: }
794:
795: /**
796: * Inicializa los vectores con la descripcion de las dimensiones
797: * que agrupan, las que no agrupan y las metricas.
798: * @param dimensionsCount
799: * @param metricsCount
800: * @param nonGroupingDimensionsCount
801: * @param accMetricsCount
802: */
803: private void initializeVectors(int dimensionsCount,
804: int metricsCount, int nonGroupingDimensionsCount,
805: int accMetricsCount) {
806: dimensions = new Vector();
807: nonGroupingDimensions = new Vector();
808: metrics = new Vector();
809: accuMetrics = new Vector();
810: int i = 0;
811: for (; i < dimensionsCount; i++) {
812: dimensions.add(data.getColumnName(i));
813: }
814: for (int z = 0; z < nonGroupingDimensionsCount; i++, z++) {
815: nonGroupingDimensions.add(data.getColumnName(i));
816: }
817: for (int j = 0; j < metricsCount; i++, j++) {
818: metrics.add(data.getColumnName(i));
819: }
820: for (int j = 0; j < accMetricsCount; i++, j++) {
821: accuMetrics.add(data.getColumnName(i));
822: }
823: }
824:
825: /**
826: * Determina un Vector con los anchos mas precisos.
827: * @param vector
828: * @return
829: */
830: private Vector calculateBestLenghts(Vector vector) {
831: Vector bestSizes = new Vector();
832: for (int i = 0; i < vector.size(); i++) {
833: bestSizes.add(new Integer(vector.elementAt(i).toString()
834: .length()));
835: }
836: return bestSizes;
837: }
838:
839: /**
840: * Retorna un indice a partir de un nombre de columna.
841: * @param columnName
842: * @return
843: */
844: private int getColumnIndexFromName(String columnName) {
845: int index = 0;
846: for (int i = 0; i < data.getColumnCount(); i++) {
847: if (data.getColumnName(i).equals(columnName)) {
848: index = i;
849: break;
850: }
851: }
852: return index;
853: }
854:
855: /**
856: * Retorna el ancho mas eficiente comparando los elementos
857: * del Vector
858: * @param ColumnSizes
859: * @return
860: */
861: private Integer getBestSize(Vector ColumnSizes) {
862: int compareTo = 0;
863: for (int i = 0; i < ColumnSizes.size(); i++) {
864: if (((Integer) ColumnSizes.elementAt(i)).intValue() > compareTo) {
865: compareTo = ((Integer) ColumnSizes.elementAt(i))
866: .intValue();
867: }
868: }
869: return new Integer(compareTo);
870: }
871:
872: /**
873: * Determina el ancho para las descripciones
874: * de la cabecera
875: * @param chars
876: * @return
877: */
878: private int getLenghtForCaptionHeader(int chars) {
879: //return chars * 8 - chars;
880: Integer integer = new Integer(chars);
881: Double qsrt = new Double(Math.sqrt(integer.doubleValue()));
882: return chars * 7 - ((chars / 2) - 3 * qsrt.intValue());
883: }
884:
885: /**
886: * Determina el comienzo donde se mostraran las
887: * metricas o dimensiones que no agrupan.
888: * @return
889: */
890: private int calculateStartForItem() {
891: int width = 0;
892: for (int i = 0; i < dimensionsSizes.size(); i++) {
893: width += (((Integer) dimensionsSizes.elementAt(i))
894: .intValue());
895: }
896:
897: //return (width) + 100;
898: return 0;
899: }
900:
901: /**
902: * Retorna un ancho preciso a partir de una
903: * determinada cantidad de caracteres
904: * @param chars
905: * @return
906: */
907: private int getLenghtForCaption(int chars) {
908: if (chars == 2) {
909: return chars * 7;
910: }
911: Integer integer = new Integer(chars);
912: Double qsrt = new Double(Math.sqrt(integer.doubleValue()));
913: return chars * 8 - ((chars / 2) - 3 * qsrt.intValue());
914: }
915:
916: /**
917: * Retorna los fields.
918: * @return
919: */
920: public Map getFields() {
921: if (fields == null) {
922: fields = new HashMap();
923: }
924: return fields;
925: }
926:
927: /**
928: * Retorna las variables
929: * @return
930: */
931: public Map getVariables() {
932: if (variables == null) {
933: variables = new HashMap();
934: }
935: return variables;
936: }
937:
938: }
|