1 package com.calipso.reportgenerator.common; 2 3 import javax.swing.table.TableModel ; 4 import java.util.*; 5 import java.text.NumberFormat ; 6 import java.text.DecimalFormat ; 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 20 21 public class CubeJasperReportDefinition implements IJasperDefinition { 22 23 private Map fields; 24 private Map variables; 25 private TableModel data; 26 private String 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 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 63 public CubeJasperReportDefinition(ReportResult reportResult, TableModel data, int dimensionsCount, int metricsCount, int nonGroupingDimensionsCount, int accuMetricsCount, String 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 format = NumberFormat.getInstance(LanguageTraslator.getLocale()); 86 numberFormatPattern = ((DecimalFormat )format).toPattern(); 87 } 88 89 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 return jasperDesign; 116 } 117 118 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 paramClass = getClassFor(param); 130 parameter.setValueClass(paramClass); 131 jasperDesign.addParameter(parameter); 132 } 133 } 134 135 140 private Class getClassFor(Map.Entry param) { 141 Class result = java.lang.String .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 156 private void addFields(JasperDesign jasperDesign) throws JRException { 157 String name; 159 if(dimensions.size()==0){ 160 dimensions.add("NONE"); 161 dimensionsSizes.add(new Integer (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())); }else{ 170 field.setValueClass(java.lang.String .class); 171 } 172 jasperDesign.addField(field); 173 } 174 for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) { 175 JRDesignField field = new JRDesignField(); 177 name = nonGroupingDimensions.elementAt(i).toString(); 178 field.setName(name); 179 field.setValueClass(ReportMap.getJavaLangType(reportResult.getDimensionFromName(name).getDataType())); jasperDesign.addField(field); 181 } 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 .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 .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 .class); 202 jasperDesign.addField(field); 203 } 204 205 } 206 207 212 private void addGroups(JasperDesign jasperDesign) throws JRException { 213 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())); }else{ 226 expression.setValueClass(java.lang.String .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 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 (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 (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 ) 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())); }else{ 269 expression.setValueClass(java.lang.String .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 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 s = (String ) vector.elementAt(i); 289 text = new JRDesignStaticText(); 290 text.setX(((Integer )columnIdentifiersPos.elementAt(i)).intValue()); 291 text.setY(20); 292 text.setWidth(getLenghtForCaption((new Integer (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 )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 for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) { 310 String s = (String ) nonGroupingDimensions.elementAt(i); 311 caption = reportResult.getReportSpec().getDimensionFromName(s).getCaption(); 312 text = new JRDesignStaticText(); 313 text.setX(((Integer )nonGroupingDimensionPosition.elementAt(i)).intValue()); 314 text.setY(40); 315 text.setWidth(getLenghtForCaption((new Integer (s.length())).intValue())); 316 text.setHeight(20); 317 text.setText(caption); 318 band.addElement(text); 319 } 320 } 321 322 ReportMetricSpec metricSpec = null; 324 for(int i = 0 ; i < metrics.size() ; i++) { 325 345 String s = getMetricName((String ) metrics.elementAt(i)); 346 metricSpec = reportResult.getReportSpec().getMetricFromName(s); 347 text = new JRDesignStaticText(); 348 text.setX(((Integer )metricsPosition.elementAt(i)).intValue()); 349 text.setY(40); 350 text.setWidth(getLenghtForCaption((new Integer (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 s = (String ) rowsTotals.elementAt(i); 359 StringTokenizer tokenizer = new StringTokenizer(s, "_"); 360 tokenizer.nextToken(); 361 String 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 )rowsTotalsPosition.elementAt(i)).intValue()); 371 text.setY(40); 372 text.setWidth(getLenghtForCaption((new Integer (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 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 )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 ) dimensionsSizes.elementAt(j)).intValue())); 459 } 460 461 textField.setHeight(20); 462 expression = new JRDesignExpression(); 463 expression.setValueClass(java.lang.Float .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 )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 ) dimensionsSizes.elementAt(j)).intValue())); 484 } 485 486 textField.setHeight(20); 487 expression = new JRDesignExpression(); 488 expression.setValueClass(java.lang.Float .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 521 522 groupingDimensionPosition.add(new Integer (printAt)); 523 printAt += 10; 525 jasperDesign.addGroup(group); 526 } 527 } 528 529 private String getMetricName(String s) { 530 if(nonGroupingDimensionsCount>0){ 531 StringTokenizer tokenizer = new StringTokenizer(s, "_"); 532 tokenizer.nextToken(); 533 String 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 549 private void addVariables(JasperDesign jasperDesign) throws JRException { 550 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 .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 .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 .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 .class); 576 expression.setText("$F{"+rowsTotals.elementAt(i).toString()+"}"); 577 jrVariable.setExpression(expression); 578 jasperDesign.addVariable(jrVariable); 579 } 580 } 581 } 582 583 587 private void addDetails(JasperDesign jasperDesign) throws JRException { 588 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 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 ) nonGroupingDimensionsSizes.elementAt(i)).intValue())); 631 textField.setHeight(20); 632 expression = new JRDesignExpression(); 633 expression.setValueClass(ReportMap.getJavaLangType(reportResult.getDimensionFromName(nonGroupingDimensionsSizes.elementAt(i).toString()).getDataType())); expression.setText("$F{"+nonGroupingDimensions.elementAt(i)+"}"); 635 textField.setExpression(expression); 636 band.addElement(textField); 637 638 nonGroupingDimensionPosition.add(new Integer (printAt)); 639 640 int first = getLenghtForCaption(((Integer ) 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 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 671 metricsWidth.add(new Integer (getLenghtForCaption((new Integer (metrics.elementAt(i).toString().length())).intValue())+25)); 672 textField.setWidth(getLenghtForCaption((new Integer (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 .class); 678 expression.setText("$F{"+metrics.elementAt(i)+"}"); 679 textField.setExpression(expression); 680 band.addElement(textField); 681 metricsPosition.add(new Integer (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 (printAt)); 687 } 688 } 689 if(data.getRowCount()==1){ 690 printAt += (metrics.elementAt(i).toString().length() + 40); 691 } else { 692 printAt += getLenghtForCaption(((Integer ) 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 703 accMetricsWidht.add(new Integer (getLenghtForCaption((new Integer (accuMetrics.elementAt(i).toString().length())).intValue())+25)); 704 textField.setWidth(getLenghtForCaption((new Integer (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 .class); 710 expression.setText("$F{"+accuMetrics.elementAt(i)+"}"); 711 textField.setExpression(expression); 712 band.addElement(textField); 713 metricsPosition.add(new Integer (printAt + 25)); 714 accMetricsPosition.add(new Integer (printAt + 25)); 715 printAt += getLenghtForCaption(((Integer ) 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 (getLenghtForCaption((new Integer (rowsTotals.elementAt(i).toString().length())).intValue())+25)); 724 textField.setWidth(getLenghtForCaption((new Integer (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 .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 (printAt )); 738 if (rowsTotalsSizes.size()>0){ 739 printAt += getLenghtForCaption(((Integer ) rowsTotalsSizes.elementAt(i)).intValue()) + 25 ; 740 } 741 } 742 if(rowsTotals.size()>0){ 743 if(((Integer )(rowsTotalsPosition.lastElement())).intValue() > jasperDesign.getPageWidth()){ 744 jasperDesign.setPageWidth(((Integer )(rowsTotalsPosition.lastElement())).intValue()+25); 745 } 746 } else if(((Integer )(metricsPosition.lastElement())).intValue() > jasperDesign.getPageWidth()){ 747 jasperDesign.setPageWidth(((Integer )(metricsPosition.lastElement())).intValue()+25); 748 } 749 jasperDesign.setDetail(band); 750 } 751 752 756 883 884 private void addPageHeader(JasperDesign jasperDesign) { 885 886 JRDesignBand band = new JRDesignBand(); 887 888 String caption = getCaption(LanguageTraslator.traslate("308"), dimensions, new StringBuffer ()); 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 ()); 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 sb = new StringBuffer (); 908 StringBuffer buffer = new StringBuffer (); 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 getCaption(String tittle, Vector vector, StringBuffer bufferString) { 923 StringBuffer buffer = new StringBuffer (); 924 buffer.append(tittle); 925 if(vector == dimensions || vector == nonGroupingDimensions) { 926 for(int i = 0 ; i < vector.size() ; i++){ 927 String 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 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 caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption(); 947 buffer.append(caption + ", "); 948 } 949 } 950 } else { 951 Object [] 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 969 private void addTitle(JasperDesign jasperDesign) throws JRException{ 970 JRDesignTextField textField = new JRDesignTextField(); 972 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 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 .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 1087 1088 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 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 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 (columnName.length())); 1169 Object value = data.getValueAt(0, getColumnIndexFromName(vector.elementAt(i).toString())); 1170 if (value != null){ 1171 String current = value.toString(); 1172 if (data.getRowCount() == 1) { 1173 ColumnSizes.add(new Integer (current.length())); 1174 break; 1175 } 1176 else { 1177 ColumnSizes.add(new Integer (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 (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 1197 private int getColumnIndexFromName(String 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 1214 private Integer getBestSize(Vector ColumnSizes) { 1215 int compareTo = 0; 1216 for (int i = 0; i < ColumnSizes.size(); i++) { 1217 if (((Integer ) ColumnSizes.elementAt(i)).intValue() > compareTo) { 1218 compareTo = ((Integer ) ColumnSizes.elementAt(i)).intValue(); 1219 } 1220 } 1221 return new Integer (compareTo); 1222 } 1223 1224 1230 private int getLenghtForCaptionHeader(int chars){ 1231 Integer integer = new Integer (chars); 1233 Double qsrt = new Double (Math.sqrt(integer.doubleValue())); 1234 return chars * 7 - ((chars / 2) - 3 * qsrt.intValue()); 1235 } 1236 1237 1242 private int calculateStartForItem() { 1243 int width = 0 ; 1244 for(int i = 0 ; i < dimensionsSizes.size() ; i++){ 1245 width += (((Integer )dimensionsSizes.elementAt(i)).intValue()); 1246 } 1247 1248 return 0; 1250 } 1251 1252 1258 private int getLenghtForCaption(int chars) { 1259 1265 1266 return chars * 8; 1267 } 1268 1269 1273 public Map getFields() { 1274 if (fields == null){ 1275 fields = new HashMap(); 1276 } 1277 return fields; 1278 } 1279 1280 1284 public Map getVariables() { 1285 if (variables == null ){ 1286 variables = new HashMap(); 1287 } 1288 return variables; 1289 } 1290} 1291 | Popular Tags |