0001: package com.calipso.reportgenerator.common;
0002:
0003: import javax.swing.table.TableModel;
0004: import java.util.*;
0005: import java.text.NumberFormat;
0006: import java.text.DecimalFormat;
0007:
0008: import net.sf.jasperreports.engine.design.*;
0009: import net.sf.jasperreports.engine.design.JRDesignGroup;
0010: import net.sf.jasperreports.engine.design.JRDesignBand;
0011: import net.sf.jasperreports.engine.design.JRDesignStaticText;
0012: import net.sf.jasperreports.engine.*;
0013: import com.calipso.reportgenerator.common.LanguageTraslator;
0014: import com.calipso.reportgenerator.common.ReportResult;
0015: import com.calipso.reportgenerator.common.ReportMetricSpec;
0016: import com.calipso.reportgenerator.common.IJasperDefinition;
0017:
0018: /**
0019: * Construye una definición para los reportes estátícos (Jasper)
0020: */
0021:
0022: public class StaticJasperReportDefinition implements IJasperDefinition {
0023:
0024: private Map fields;
0025: private Map variables;
0026: private TableModel data;
0027: private String tittle;
0028: private Vector dimensions;
0029: private Vector metrics;
0030: private Vector accuMetrics;
0031: private Vector nonGroupingDimensions;
0032: private Vector dimensionsSizes;
0033: private Vector metricsSizes;
0034: private Vector accuMetricsSizes;
0035: private Vector nonGroupingDimensionsSizes;
0036: private int printAt;
0037: private Vector metricsPosition = new Vector();
0038: private Vector nonGroupingDimensionPosition = new Vector();
0039: private Vector groupingDimensionPosition = new Vector();
0040: private ReportResult reportResult;
0041: private String numberFormatPattern;
0042: private Vector metricsWidth;
0043: private Vector accMetricsPosition;
0044: private Vector accMetricsWidht;
0045: private Map reportParams;
0046:
0047: /**
0048: * Inicializa una instancia de <coe>eJasperReportDefinition</code>
0049: * @param reportResult
0050: * @param data
0051: * @param dimensionsCount
0052: * @param metricsCount
0053: * @param nonGroupingDimensionsCount
0054: * @param accuMetricsCount
0055: * @param tittle
0056: */
0057: public StaticJasperReportDefinition(ReportResult reportResult,
0058: TableModel data, int dimensionsCount, int metricsCount,
0059: int nonGroupingDimensionsCount, int accuMetricsCount,
0060: String tittle) {
0061: this .data = data;
0062: this .tittle = tittle;
0063: this .reportResult = reportResult;
0064: metricsWidth = new Vector();
0065: accMetricsPosition = new Vector();
0066: accMetricsWidht = new Vector();
0067: initializeVectors(dimensionsCount, metricsCount,
0068: nonGroupingDimensionsCount, accuMetricsCount);
0069: reportParams = reportResult.getParamValues();
0070: dimensionsSizes = calculateBestLenghts(dimensions);
0071: metricsSizes = calculateBestLenghts(metrics);
0072: accuMetricsSizes = calculateBestLenghts(this .accuMetrics);
0073: nonGroupingDimensionsSizes = calculateBestLenghts(nonGroupingDimensions);
0074: initializeNumberFormatPattern();
0075: }
0076:
0077: private void initializeNumberFormatPattern() {
0078: NumberFormat format = NumberFormat
0079: .getInstance(LanguageTraslator.getLocale());
0080: numberFormatPattern = ((DecimalFormat) format).toPattern();
0081: }
0082:
0083: /**
0084: * Retorna la definicion del reporte
0085: * @return
0086: * @throws net.sf.jasperreports.engine.JRException
0087: */
0088: public JasperDesign getJasperDefinition(boolean isLandscape)
0089: throws JRException {
0090: JasperDesign jasperDesign = new JasperDesign();
0091: if (isLandscape) {
0092: jasperDesign
0093: .setOrientation(JasperDesign.ORIENTATION_LANDSCAPE);
0094: jasperDesign
0095: .setPrintOrder(JasperDesign.PRINT_ORDER_VERTICAL);
0096: jasperDesign.setPageWidth(842);
0097: jasperDesign.setPageHeight(595);
0098: } else {
0099: jasperDesign
0100: .setOrientation(JasperDesign.ORIENTATION_PORTRAIT);
0101: jasperDesign
0102: .setPrintOrder(JasperDesign.PRINT_ORDER_HORIZONTAL);
0103: jasperDesign.setPageWidth(595);
0104: jasperDesign.setPageHeight(842);
0105: }
0106: jasperDesign.setName("BasicReport");
0107: addFields(jasperDesign);
0108: addDetails(jasperDesign);
0109: addGroups(jasperDesign);
0110: addVariables(jasperDesign);
0111: addTitle(jasperDesign);
0112: addParams(jasperDesign);
0113: //addPageHeader(jasperDesign);
0114: return jasperDesign;
0115: }
0116:
0117: /**
0118: * Agrega los parametros del reporte a la definicion del jasper para que puedan ser utilizados en la visualizacion.
0119: * @param jasperDesign
0120: * @throws JRException
0121: */
0122: private void addParams(JasperDesign jasperDesign)
0123: throws JRException {
0124: for (Iterator iterator = reportParams.entrySet().iterator(); iterator
0125: .hasNext();) {
0126: Map.Entry param = (Map.Entry) iterator.next();
0127: JRDesignParameter parameter = new JRDesignParameter();
0128: parameter.setName(param.getKey().toString());
0129: parameter.setDescription(param.getKey().toString());
0130: Class paramClass = getClassFor(param);
0131: parameter.setValueClass(paramClass);
0132: jasperDesign.addParameter(parameter);
0133: }
0134: }
0135:
0136: /**
0137: * Obtiene la clase a la que pertenece el valor de un parametro dado.
0138: * @param param
0139: * @return
0140: */
0141: private Class getClassFor(Map.Entry param) {
0142: Class result = java.lang.String.class;
0143: Map map = new HashMap();
0144: map.put(param.getKey(), param.getValue());
0145: ReportMap.setParametersToSimpleType(map);
0146: if (map.get(param.getKey()) != null) {
0147: result = map.get(param.getKey()).getClass();
0148: }
0149: return result;
0150: }
0151:
0152: /**
0153: * Agrega los campos a la definicion
0154: * @param jasperDesign
0155: * @throws net.sf.jasperreports.engine.JRException
0156: */
0157: private void addFields(JasperDesign jasperDesign)
0158: throws JRException {
0159: //Fields
0160: String name;
0161: for (int i = 0; i < dimensions.size(); i++) {
0162: JRDesignField field = new JRDesignField();
0163: name = dimensions.elementAt(i).toString();
0164: field.setName(name);
0165: field.setValueClass(java.lang.String.class);
0166: jasperDesign.addField(field);
0167: }
0168: for (int i = 0; i < nonGroupingDimensions.size(); i++) {
0169: JRDesignField field = new JRDesignField();
0170: name = nonGroupingDimensions.elementAt(i).toString();
0171: field.setName(name);
0172: field.setValueClass(java.lang.String.class);
0173: jasperDesign.addField(field);
0174: }
0175: for (int i = 0; i < metrics.size(); i++) {
0176: JRDesignField field = new JRDesignField();
0177: name = metrics.elementAt(i).toString();
0178: field.setName(name);
0179: field.setValueClass(java.lang.Float.class);
0180: jasperDesign.addField(field);
0181: }
0182: for (int i = 0; i < accuMetrics.size(); i++) {
0183: JRDesignField field = new JRDesignField();
0184: name = accuMetrics.elementAt(i).toString();
0185: field.setName(name);
0186: field.setValueClass(java.lang.Float.class);
0187: jasperDesign.addField(field);
0188: }
0189: }
0190:
0191: /**
0192: * Agrega los grupos a la definicion
0193: * @param jasperDesign
0194: * @throws net.sf.jasperreports.engine.JRException
0195: */
0196: private void addGroups(JasperDesign jasperDesign)
0197: throws JRException {
0198: //groups
0199: JRDesignExpression expression;
0200: JRDesignBand band;
0201: JRDesignGroup group = null;
0202: printAt = 0;
0203: if (dimensions.size() == 0) {
0204: if (jasperDesign.getFields().length == 0) {
0205: dimensions.add("");
0206: } else {
0207: dimensions.add(jasperDesign.getFields()[0].getName());
0208: }
0209: dimensionsSizes.add(new Integer(4));
0210: }
0211: for (int j = 0; j < dimensions.size(); j++) {
0212: group = new JRDesignGroup();
0213: group
0214: .setName(dimensions.elementAt(j).toString()
0215: + "_GROUP");
0216: group.setReprintHeaderOnEachPage(true);
0217:
0218: expression = new JRDesignExpression();
0219: expression.setValueClass(java.lang.String.class);
0220: expression.setText("$F{" + dimensions.elementAt(j) + "}");
0221: group.setExpression(expression);
0222:
0223: if (j + 1 == dimensions.size()) {
0224: band = new JRDesignBand();
0225: band.setHeight(60);
0226: } else {
0227: band = new JRDesignBand();
0228: band.setHeight(20);
0229: }
0230:
0231: JRDesignStaticText text = new JRDesignStaticText();
0232: String caption = "";
0233: if (dimensions.elementAt(j) != "") {
0234: caption = reportResult.getReportSpec()
0235: .getDimensionFromName(
0236: dimensions.elementAt(j).toString())
0237: .getCaption()
0238: + ":";
0239: }
0240: text.setX(printAt);
0241: text.setY(0);
0242: text.setWidth(getLenghtForCaption((new Integer(caption
0243: .length())).intValue()));
0244: text.setHeight(20);
0245: text.setText(caption);
0246: band.addElement(text);
0247:
0248: JRDesignTextField textField = new JRDesignTextField();
0249: textField
0250: .setX(printAt
0251: + getLenghtForCaption((new Integer(caption
0252: .length())).intValue()) - 5);
0253: textField.setY(0);
0254: textField
0255: .setWidth(getLenghtForCaption(((Integer) dimensionsSizes
0256: .elementAt(j)).intValue()));
0257: textField.setHeight(20);
0258: expression = new JRDesignExpression();
0259: expression.setValueClass(java.lang.String.class);
0260: expression.setText("$F{" + dimensions.elementAt(j) + "}");
0261: textField.setExpression(expression);
0262: band.addElement(textField);
0263: group.setGroupHeader(band);
0264:
0265: if (j + 1 == dimensions.size()) {
0266: for (int i = 0; i < nonGroupingDimensions.size(); i++) {
0267: int x = ((Integer) nonGroupingDimensionPosition
0268: .elementAt(i)).intValue();
0269: text = new JRDesignStaticText();
0270: text.setX(x);
0271: text.setY(20);
0272: //text.setWidth(((Integer) metricsWidth.elementAt(i)).intValue());
0273: int first = getLenghtForCaption(((Integer) nonGroupingDimensionsSizes
0274: .elementAt(i)).intValue());
0275: int second = getLenghtForCaption(nonGroupingDimensions
0276: .elementAt(i).toString().length());
0277: if (first >= second) {
0278: text.setWidth(getLenghtForCaption(first));
0279: } else {
0280: text.setWidth(getLenghtForCaption(second));
0281: }
0282: text.setHeight(20);
0283: text.setText(reportResult.getReportSpec()
0284: .getDimensionFromName(
0285: nonGroupingDimensions.elementAt(i)
0286: .toString()).getCaption());
0287: band.addElement(text);
0288: }
0289:
0290: for (int i = 0; i < metrics.size(); i++) {
0291: int x = ((Integer) metricsPosition.elementAt(i))
0292: .intValue();
0293: text = new JRDesignStaticText();
0294: text.setX(x);
0295: text.setY(20);
0296: text.setWidth(((Integer) metricsWidth.elementAt(i))
0297: .intValue());
0298: text.setHeight(20);
0299: text.setText(reportResult.getReportSpec()
0300: .getMetricFromName(
0301: metrics.elementAt(i).toString())
0302: .getCaption());
0303: band.addElement(text);
0304: }
0305:
0306: for (int i = 0; i < accMetricsPosition.size(); i++) {
0307: int x = ((Integer) accMetricsPosition.elementAt(i))
0308: .intValue();
0309: text = new JRDesignStaticText();
0310: text.setX(x);
0311: text.setY(20);
0312: text.setWidth(((Integer) accMetricsWidht
0313: .elementAt(i)).intValue());
0314: text.setHeight(20);
0315: String metricName = getMetricName(accuMetrics
0316: .elementAt(i).toString());
0317: String metricCaption = reportResult.getReportSpec()
0318: .getMetricFromName(metricName).getCaption();
0319: text.setText(metricCaption + " "
0320: + LanguageTraslator.traslate("315"));
0321: band.addElement(text);
0322: }
0323: group.setGroupHeader(band);
0324: }
0325:
0326: band = new JRDesignBand();
0327: band.setHeight(30);
0328: text = new JRDesignStaticText();
0329: text.setX(printAt);
0330: text.setY(0);
0331: text.setWidth(40);
0332: text.setHeight(20);
0333: text.setText(LanguageTraslator.traslate("358"));
0334: band.addElement(text);
0335: for (int i = 0; i < metrics.size(); i++) {
0336: band.setHeight(20);
0337: textField = new JRDesignTextField();
0338: textField.setX(((Integer) metricsPosition.elementAt(i))
0339: .intValue());
0340: textField.setY(0);
0341: textField
0342: .setWidth(getLenghtForCaption(((Integer) dimensionsSizes
0343: .elementAt(j)).intValue()) + 20);
0344: textField.setHeight(20);
0345: expression = new JRDesignExpression();
0346: expression.setValueClass(java.lang.Float.class);
0347: expression.setText("$V{"
0348: + dimensions.elementAt(j).toString()
0349: + metrics.elementAt(i).toString() + "_SUM}");
0350: textField.setPattern(numberFormatPattern);
0351: textField.setExpression(expression);
0352: band.addElement(textField);
0353: group.setGroupFooter(band);
0354: }
0355: groupingDimensionPosition.add(new Integer(printAt));
0356: printAt += 10;//getLenghtForCaption(((Integer)dimensionsSizes.elementAt(j)).intValue());
0357:
0358: jasperDesign.addGroup(group);
0359: }
0360: }
0361:
0362: private String getMetricName(String s) {
0363: StringTokenizer tokenizer = new StringTokenizer(s, "_");
0364: String returnVal = tokenizer.nextToken();
0365: return returnVal;
0366: }
0367:
0368: /**
0369: * Agrega las variables a la definicion
0370: * @param jasperDesign
0371: * @throws net.sf.jasperreports.engine.JRException
0372: */
0373: private void addVariables(JasperDesign jasperDesign)
0374: throws JRException {
0375: //Variables
0376: JRDesignExpression expression;
0377: for (int j = 0; j < dimensions.size(); j++) {
0378: for (int i = 0; i < metrics.size(); i++) {
0379: JRDesignVariable jrVariable = new JRDesignVariable();
0380: jrVariable.setName(dimensions.elementAt(j).toString()
0381: + metrics.elementAt(i).toString() + "_SUM");
0382: jrVariable.setValueClass(java.lang.Float.class);
0383: jrVariable
0384: .setResetType(JRDesignVariable.RESET_TYPE_GROUP);
0385: jrVariable
0386: .setCalculation(JRDesignVariable.CALCULATION_SUM);
0387: jrVariable.setResetGroup((JRGroup) jasperDesign
0388: .getGroupsList().get(j));
0389: expression = new JRDesignExpression();
0390: expression.setValueClass(java.lang.Float.class);
0391: expression.setText("$F{"
0392: + metrics.elementAt(i).toString() + "}");
0393: jrVariable.setExpression(expression);
0394: jasperDesign.addVariable(jrVariable);
0395: }
0396: }
0397: }
0398:
0399: /**
0400: * Agrega los detalles a la definicion
0401: * @param jasperDesign
0402: */
0403: private void addDetails(JasperDesign jasperDesign) {
0404: //Details
0405: int printAt = calculateStartForItem();
0406: JRDesignBand band = new JRDesignBand();
0407: band.setHeight(20);
0408: JRDesignExpression expression;
0409: for (int i = 0; i < nonGroupingDimensions.size(); i++) {
0410: JRDesignTextField textField = new JRDesignTextField();
0411: textField.setX(printAt);
0412: textField.setY(0);
0413: textField
0414: .setWidth(getLenghtForCaption(((Integer) nonGroupingDimensionsSizes
0415: .elementAt(i)).intValue()));
0416: textField.setHeight(20);
0417: expression = new JRDesignExpression();
0418: expression.setValueClass(java.lang.String.class);
0419: expression.setText("$F{"
0420: + nonGroupingDimensions.elementAt(i) + "}");
0421: textField.setExpression(expression);
0422: band.addElement(textField);
0423:
0424: nonGroupingDimensionPosition.add(new Integer(printAt));
0425:
0426: int first = getLenghtForCaption(((Integer) nonGroupingDimensionsSizes
0427: .elementAt(i)).intValue());
0428: int second = getLenghtForCaption(nonGroupingDimensions
0429: .elementAt(i).toString().length());
0430: if (first >= second) {
0431: printAt += first;
0432: } else {
0433: printAt += second;
0434: }
0435: //printAt += getLenghtForCaption(((Integer) nonGroupingDimensionsSizes.elementAt(i)).intValue());
0436:
0437: }
0438: for (int i = 0; i < metrics.size(); i++) {
0439: JRDesignTextField textField = new JRDesignTextField();
0440: textField.setX(printAt + 25);
0441: textField.setY(0);
0442: //textField.setWidth(getLenghtForCaption(((Integer) metricsSizes.elementAt(i)).intValue())+15);
0443:
0444: metricsWidth.add(new Integer(
0445: getLenghtForCaption((new Integer(metrics.elementAt(
0446: i).toString().length())).intValue()) + 25));
0447: textField
0448: .setWidth(getLenghtForCaption((new Integer(metrics
0449: .elementAt(i).toString().length()))
0450: .intValue()) + 25);
0451:
0452: textField.setHeight(20);
0453: textField.setPattern(numberFormatPattern);
0454: expression = new JRDesignExpression();
0455: expression.setValueClass(java.lang.Float.class);
0456: expression.setText("$F{" + metrics.elementAt(i) + "}");
0457: textField.setExpression(expression);
0458: band.addElement(textField);
0459: metricsPosition.add(new Integer(printAt + 25));
0460: printAt += getLenghtForCaption(((Integer) metricsSizes
0461: .elementAt(i)).intValue()) + 25;
0462: }
0463:
0464: for (int i = 0; i < accuMetrics.size(); i++) {
0465: JRDesignTextField textField = new JRDesignTextField();
0466: textField.setX(printAt + 25);
0467: textField.setY(0);
0468:
0469: //textField.setWidth(getLenghtForCaption(((Integer) accuMetricsSizes.elementAt(i)).intValue())+15);
0470:
0471: accMetricsWidht.add(new Integer(
0472: getLenghtForCaption((new Integer(accuMetrics
0473: .elementAt(i).toString().length()))
0474: .intValue()) + 25));
0475: textField.setWidth(getLenghtForCaption((new Integer(
0476: accuMetrics.elementAt(i).toString().length()))
0477: .intValue()) + 25);
0478:
0479: textField.setHeight(20);
0480: textField.setPattern(numberFormatPattern);
0481: expression = new JRDesignExpression();
0482: expression.setValueClass(java.lang.Float.class);
0483: expression.setText("$F{" + accuMetrics.elementAt(i) + "}");
0484: textField.setExpression(expression);
0485: band.addElement(textField);
0486: metricsPosition.add(new Integer(printAt + 25));
0487: accMetricsPosition.add(new Integer(printAt + 25));
0488: printAt += getLenghtForCaption(((Integer) accuMetricsSizes
0489: .elementAt(i)).intValue()) + 25;
0490: }
0491:
0492: jasperDesign.setDetail(band);
0493: }
0494:
0495: /**
0496: * Agrega la cabecera de pagina a la definicion
0497: * @param jasperDesign
0498: */
0499: /*private void addPageHeader(JasperDesign jasperDesign) {
0500: int printAt = 45;
0501: JRDesignBand band = new JRDesignBand();
0502: for(int i = 0 ; i < dimensions.size() ; i++) {
0503: String caption = reportResult.getReportSpec().getDimensionFromName(dimensions.elementAt(i).toString()).getCaption();
0504: JRDesignStaticText text = new JRDesignStaticText();
0505: text.setX(0);
0506: text.setY(0);
0507: text.setWidth(40);
0508: text.setHeight(15);
0509: text.setText(LanguageTraslator.traslate("308"));
0510: band.addElement(text);
0511: text = new JRDesignStaticText();
0512: text.setX(printAt);
0513: text.setY(0);
0514: text.setWidth(getLenghtForCaptionHeader(caption.length()));
0515: text.setHeight(15);
0516: if(i == dimensions.size() - 1) {
0517: text.setText(caption);
0518: } else {
0519: text.setText(caption + ",");
0520: }
0521: band.addElement(text);
0522: printAt += getLenghtForCaptionHeader(caption.length());
0523: JRDesignLine line = new JRDesignLine();
0524: line = new JRDesignLine();
0525: line.setX(0);
0526: line.setY(16);
0527: line.setWidth(515);
0528: line.setHeight(0);
0529: band.addElement(line);
0530: }
0531: printAt = 45;
0532: for(int i = 0 ; i < nonGroupingDimensions.size() ; i++) {
0533: String caption = reportResult.getReportSpec().getDimensionFromName(nonGroupingDimensions.elementAt(i).toString()).getCaption();
0534: JRDesignStaticText text = new JRDesignStaticText();
0535: text.setX(0);
0536: text.setY(15);
0537: text.setWidth(40);
0538: text.setHeight(15);
0539: text.setText(LanguageTraslator.traslate("309"));
0540: band.addElement(text);
0541: text = new JRDesignStaticText();
0542: text.setX(printAt);
0543: text.setY(15);
0544: text.setWidth(getLenghtForCaptionHeader(caption.length()));
0545: text.setHeight(15);
0546: if(i == nonGroupingDimensions.size() - 1) {
0547: text.setText(caption);
0548: } else {
0549: text.setText(caption + ",");
0550: }
0551: band.addElement(text);
0552: printAt += getLenghtForCaptionHeader(caption.length());
0553: JRDesignLine line = new JRDesignLine();
0554: line = new JRDesignLine();
0555: line.setX(0);
0556: line.setY(31);
0557: line.setWidth(515);
0558: line.setHeight(0);
0559: band.addElement(line);
0560: }
0561: printAt = 45;
0562: for(int i = 0 ; i < metrics.size() ; i++) {
0563: String caption = reportResult.getReportSpec().getMetricFromName(metrics.elementAt(i).toString()).getCaption();
0564: JRDesignStaticText text = new JRDesignStaticText();
0565: text.setX(0);
0566: text.setY(30);
0567: text.setWidth(40);
0568: text.setHeight(15);
0569: text.setText(LanguageTraslator.traslate("310"));
0570: band.addElement(text);
0571: text = new JRDesignStaticText();
0572: text.setX(printAt);
0573: text.setY(30);
0574: text.setWidth(getLenghtForCaptionHeader(caption.length()));
0575: text.setHeight(15);
0576: if(accuMetrics.size() == 0) {
0577: if(i == metrics.size() - 1) {
0578: text.setText(caption);
0579: } else {
0580: text.setText(caption + ",");
0581: }
0582: } else {
0583: text.setText(caption + ",");
0584: }
0585: band.addElement(text);
0586: printAt += getLenghtForCaptionHeader(caption.length());
0587: JRDesignLine line = new JRDesignLine();
0588: line = new JRDesignLine();
0589: line.setX(0);
0590: line.setY(46);
0591: line.setWidth(515);
0592: line.setHeight(0);
0593: band.addElement(line);
0594: }
0595: for(int i = 0 ; i < accuMetrics.size() ; i++) {
0596: String caption = getAccMetricCaption(accuMetrics.elementAt(i).toString());
0597: JRDesignStaticText text = new JRDesignStaticText();
0598: text.setX(0);
0599: text.setY(30);
0600: text.setWidth(40);
0601: text.setHeight(15);
0602: text.setText(LanguageTraslator.traslate("310"));
0603: band.addElement(text);
0604: text = new JRDesignStaticText();
0605: text.setX(printAt);
0606: text.setY(30);
0607: text.setWidth(getLenghtForCaptionHeader(caption.length()));
0608: text.setHeight(15);
0609: if(i == accuMetrics.size() - 1) {
0610: text.setText(caption);
0611: } else {
0612: text.setText(caption + ",");
0613: }
0614: band.addElement(text);
0615: printAt += getLenghtForCaptionHeader(caption.length());
0616: JRDesignLine line = new JRDesignLine();
0617: line = new JRDesignLine();
0618: line.setX(0);
0619: line.setY(46);
0620: line.setWidth(515);
0621: line.setHeight(0);
0622: band.addElement(line);
0623: }
0624: jasperDesign.setPageHeader(band);
0625: }*/
0626:
0627: private void addPageHeader(JasperDesign jasperDesign) {
0628:
0629: JRDesignBand band = new JRDesignBand();
0630:
0631: String caption = getCaption(LanguageTraslator.traslate("308"),
0632: dimensions, new StringBuffer());
0633: JRDesignStaticText text = new JRDesignStaticText();
0634: text = new JRDesignStaticText();
0635: text.setX(0);
0636: text.setY(0);
0637: text.setWidth(getLenghtForCaptionHeader(caption.length()));
0638: text.setHeight(15);
0639: text.setText(caption);
0640: band.addElement(text);
0641:
0642: caption = getCaption(LanguageTraslator.traslate("309"),
0643: nonGroupingDimensions, new StringBuffer());
0644: text = new JRDesignStaticText();
0645: text.setX(0);
0646: text.setY(15);
0647: text.setWidth(getLenghtForCaptionHeader(caption.length()));
0648: text.setHeight(15);
0649: text.setText(caption);
0650: band.addElement(text);
0651:
0652: StringBuffer sb = new StringBuffer();
0653: StringBuffer buffer = new StringBuffer();
0654: sb.append(getCaption(LanguageTraslator.traslate("310"),
0655: metrics, buffer));
0656: sb.append(getCaption("", accuMetrics, buffer));
0657: text = new JRDesignStaticText();
0658: text.setX(0);
0659: text.setY(30);
0660: text
0661: .setWidth(getLenghtForCaptionHeader(sb.toString()
0662: .length()));
0663: text.setHeight(15);
0664: text.setText(sb.toString());
0665: band.addElement(text);
0666:
0667: jasperDesign.setPageHeader(band);
0668: }
0669:
0670: private String getCaption(String tittle, Vector vector,
0671: StringBuffer bufferString) {
0672: StringBuffer buffer = new StringBuffer();
0673: buffer.append(tittle);
0674: if (vector == dimensions || vector == nonGroupingDimensions) {
0675: for (int i = 0; i < vector.size(); i++) {
0676: String caption = reportResult.getReportSpec()
0677: .getDimensionFromName(
0678: vector.elementAt(i).toString())
0679: .getCaption();
0680: if (i == vector.size() - 1) {
0681: buffer.append(caption);
0682: } else {
0683: buffer.append(caption + ", ");
0684: }
0685: }
0686: } else if (vector == metrics) {
0687: if (accuMetrics.size() == 0) {
0688: for (int i = 0; i < vector.size(); i++) {
0689: String caption = reportResult.getReportSpec()
0690: .getMetricFromName(
0691: metrics.elementAt(i).toString())
0692: .getCaption();
0693: if (i == vector.size() - 1) {
0694: buffer.append(caption);
0695: } else {
0696: buffer.append(caption + ", ");
0697: }
0698: }
0699: } else {
0700: for (int i = 0; i < vector.size(); i++) {
0701: String caption = reportResult.getReportSpec()
0702: .getMetricFromName(
0703: metrics.elementAt(i).toString())
0704: .getCaption();
0705: buffer.append(caption + ", ");
0706: }
0707: }
0708: } else {
0709: Object[] accMetrics = reportResult.getReportSpec()
0710: .getAccumulableMetrics();
0711: for (int i = 0; i < accMetrics.length; i++) {
0712: ReportMetricSpec metric = (ReportMetricSpec) accMetrics[i];
0713: if (i == accMetrics.length - 1) {
0714: buffer.append(metric.getCaption() + " "
0715: + LanguageTraslator.traslate("315"));
0716: } else {
0717: buffer.append(metric.getCaption() + " "
0718: + LanguageTraslator.traslate("315") + ", ");
0719: }
0720:
0721: }
0722: }
0723: return buffer.toString();
0724: }
0725:
0726: /**
0727: * Agrega el titulo al reporte.
0728: * @param jasperDesign
0729: */
0730: private void addTitle(JasperDesign jasperDesign) throws JRException {
0731: //Title
0732: JRDesignTextField textField = new JRDesignTextField();
0733: //JRDesignLine line = new JRDesignLine();
0734: JRDesignBand band = new JRDesignBand();
0735: band.setHeight(50);
0736: JRDesignStaticText staticText = new JRDesignStaticText();
0737: staticText.setX(0);
0738: staticText.setY(10);
0739: staticText.setWidth(515);
0740: staticText.setHeight(30);
0741: staticText.setTextAlignment(JRTextElement.TEXT_ALIGN_CENTER);
0742: JRDesignReportFont bigFont = new JRDesignReportFont();
0743: bigFont.setName("Arial_Normal");
0744: bigFont.setDefault(true);
0745: bigFont.setFontName("Arial");
0746: bigFont.setSize(16);
0747: bigFont.setPdfFontName("Helvetica");
0748: bigFont.setPdfEncoding("Cp1252");
0749: bigFont.setPdfEmbedded(false);
0750: jasperDesign.addFont(bigFont);
0751: staticText.setFont(bigFont);
0752: staticText.setText(tittle);
0753: band.addElement(staticText);
0754: /*line = new JRDesignLine();
0755: line.setX(0);
0756: line.setY(0);
0757: line.setWidth(515);
0758: line.setHeight(0);
0759: band.addElement(line);*/
0760: jasperDesign.setTitle(band);
0761:
0762: JRDesignBand emptyBand = new JRDesignBand();
0763: emptyBand.setHeight(1);
0764: jasperDesign.setPageHeader(emptyBand);
0765: emptyBand = new JRDesignBand();
0766: emptyBand.setHeight(1);
0767: jasperDesign.setColumnHeader(emptyBand);
0768: emptyBand = new JRDesignBand();
0769: emptyBand.setHeight(1);
0770: jasperDesign.setColumnFooter(emptyBand);
0771:
0772: band = new JRDesignBand();
0773: band.setHeight(15);
0774: staticText = new JRDesignStaticText();
0775: staticText.setX(0);
0776: staticText.setY(0);
0777: staticText.setWidth(40);
0778: staticText.setHeight(15);
0779: staticText.setText("Pagina: ");
0780: band.addElement(staticText);
0781: textField = new JRDesignTextField();
0782: textField.setX(40);
0783: textField.setY(0);
0784: textField.setWidth(100);
0785: textField.setHeight(15);
0786: JRDesignExpression expression = new JRDesignExpression();
0787: expression.setValueClass(java.lang.Integer.class);
0788: expression.setText("$V{PAGE_NUMBER}");
0789: textField.setExpression(expression);
0790: band.addElement(textField);
0791: jasperDesign.setPageFooter(band);
0792:
0793: band = new JRDesignBand();
0794: band.setHeight(20);
0795: jasperDesign.setSummary(band);
0796: }
0797:
0798: /* private void addColumnsHeader(JasperDesign jasperDesign) {
0799: //columnHeader
0800: JRDesignBand band = new JRDesignBand();
0801: for(int i = 0 ; i < metrics.size() ; i++) {
0802: band.setHeight(20);
0803: JRDesignStaticText text = new JRDesignStaticText();
0804: text.setX(((Integer)metricsPosition.elementAt(i)).intValue());
0805: text.setY(0);
0806: text.setWidth(40);
0807: text.setHeight(20);
0808: text.setText(reportResult.getReportSpec().getDimensionFromName(metrics.elementAt(i).toString()).getCaption());
0809: band.addElement(text);
0810: }
0811: jasperDesign.setColumnHeader(band);
0812: }
0813:
0814: private void addColumnsFooter(JasperDesign jasperDesign) {
0815: JRDesignBand band = new JRDesignBand();
0816: JRDesignTextField textField;
0817: String name;
0818: band.setHeight(20);
0819: JRDesignStaticText text = new JRDesignStaticText();
0820: text.setX(40);
0821: text.setY(0);
0822: text.setWidth(40);
0823: text.setHeight(20);
0824: text.setText("Total");
0825: band.addElement(text);
0826:
0827: for(int i = 0 ; i < metrics.size() ; i++) {
0828: textField = new JRDesignTextField();
0829: textField.setX(((Integer)metricsPosition.elementAt(i)).intValue());
0830: textField.setY(0);
0831: textField.setWidth(100);
0832: textField.setHeight(15);
0833:
0834: JRDesignExpression expression = new JRDesignExpression();
0835: expression.setValueClass(java.lang.Float.class);
0836: name = metrics.elementAt(i).toString()+"_SUM";
0837: expression.setText("$V{"+name+"}");
0838: textField.setExpression(expression);
0839: band.addElement(textField);
0840:
0841: band.addElement(text);
0842: }
0843:
0844: jasperDesign.setColumnFooter(band);
0845: }
0846: */
0847:
0848: /**
0849: * Inicializa los vectores con la descripcion de las dimensiones
0850: * que agrupan, las que no agrupan y las metricas.
0851: * @param dimensionsCount
0852: * @param metricsCount
0853: * @param nonGroupingDimensionsCount
0854: * @param accMetricsCount
0855: */
0856: private void initializeVectors(int dimensionsCount,
0857: int metricsCount, int nonGroupingDimensionsCount,
0858: int accMetricsCount) {
0859: dimensions = new Vector();
0860: nonGroupingDimensions = new Vector();
0861: metrics = new Vector();
0862: accuMetrics = new Vector();
0863: int i = 0;
0864: for (; i < dimensionsCount; i++) {
0865: dimensions.add(data.getColumnName(i));
0866: }
0867: for (int z = 0; z < nonGroupingDimensionsCount; i++, z++) {
0868: nonGroupingDimensions.add(data.getColumnName(i));
0869: }
0870: for (int j = 0; j < metricsCount; i++, j++) {
0871: metrics.add(data.getColumnName(i));
0872: }
0873: for (int j = 0; j < accMetricsCount; i++, j++) {
0874: accuMetrics.add(data.getColumnName(i));
0875: }
0876: }
0877:
0878: /**
0879: * Determina un Vector con los anchos mas precisos.
0880: * @param vector
0881: * @return
0882: */
0883: private Vector calculateBestLenghts(Vector vector) {
0884: Vector bestSizes = null;
0885: Vector ColumnSizes;
0886: if (data.getRowCount() != 0) {
0887: bestSizes = new Vector();
0888: for (int i = 0; i < vector.size(); i++) {
0889: ColumnSizes = new Vector();
0890: String current = data.getValueAt(
0891: 0,
0892: getColumnIndexFromName(vector.elementAt(i)
0893: .toString())).toString();
0894: if (data.getRowCount() == 1) {
0895: ColumnSizes.add(new Integer(current.length()));
0896: break;
0897: } else {
0898: ColumnSizes.add(new Integer(current.length()));
0899: for (int j = 0; j < data.getRowCount(); j++) {
0900: if (!current.equals(data.getValueAt(j, i)
0901: .toString())) {
0902: ColumnSizes
0903: .add(new Integer(
0904: data
0905: .getValueAt(
0906: j,
0907: getColumnIndexFromName(vector
0908: .elementAt(
0909: i)
0910: .toString()))
0911: .toString()
0912: .length()));
0913: current = data.getValueAt(j, i).toString();
0914: }
0915: }
0916: bestSizes.add(getBestSize(ColumnSizes));
0917: }
0918: }
0919: }
0920: return bestSizes;
0921: }
0922:
0923: /**
0924: * Retorna un indice a partir de un nombre de columna.
0925: * @param columnName
0926: * @return
0927: */
0928: private int getColumnIndexFromName(String columnName) {
0929: int index = 0;
0930: for (int i = 0; i < data.getColumnCount(); i++) {
0931: if (data.getColumnName(i).equals(columnName)) {
0932: index = i;
0933: break;
0934: }
0935: }
0936: return index;
0937: }
0938:
0939: /**
0940: * Retorna el ancho mas eficiente comparando los elementos
0941: * del Vector
0942: * @param ColumnSizes
0943: * @return
0944: */
0945: private Integer getBestSize(Vector ColumnSizes) {
0946: int compareTo = 0;
0947: for (int i = 0; i < ColumnSizes.size(); i++) {
0948: if (((Integer) ColumnSizes.elementAt(i)).intValue() > compareTo) {
0949: compareTo = ((Integer) ColumnSizes.elementAt(i))
0950: .intValue();
0951: }
0952: }
0953: return new Integer(compareTo);
0954: }
0955:
0956: /**
0957: * Determina el ancho para las descripciones
0958: * de la cabecera
0959: * @param chars
0960: * @return
0961: */
0962: private int getLenghtForCaptionHeader(int chars) {
0963: //return chars * 8 - chars;
0964: Integer integer = new Integer(chars);
0965: Double qsrt = new Double(Math.sqrt(integer.doubleValue()));
0966: return chars * 7 - ((chars / 2) - 3 * qsrt.intValue());
0967: }
0968:
0969: /**
0970: * Determina el comienzo donde se mostraran las
0971: * metricas o dimensiones que no agrupan.
0972: * @return
0973: */
0974: private int calculateStartForItem() {
0975: int width = 0;
0976: for (int i = 0; i < dimensionsSizes.size(); i++) {
0977: width += (((Integer) dimensionsSizes.elementAt(i))
0978: .intValue());
0979: }
0980:
0981: //return (width) + 100;
0982: return 0;
0983: }
0984:
0985: /**
0986: * Retorna un ancho preciso a partir de una
0987: * determinada cantidad de caracteres
0988: * @param chars
0989: * @return
0990: */
0991: private int getLenghtForCaption(int chars) {
0992: if (chars == 2) {
0993: return chars * 7;
0994: }
0995: Integer integer = new Integer(chars);
0996: Double qsrt = new Double(Math.sqrt(integer.doubleValue()));
0997: return chars * 8 - ((chars / 2) - 3 * qsrt.intValue());
0998: }
0999:
1000: /**
1001: * Retorna los fields.
1002: * @return
1003: */
1004: public Map getFields() {
1005: if (fields == null) {
1006: fields = new HashMap();
1007: }
1008: return fields;
1009: }
1010:
1011: /**
1012: * Retorna las variables
1013: * @return
1014: */
1015: public Map getVariables() {
1016: if (variables == null) {
1017: variables = new HashMap();
1018: }
1019: return variables;
1020: }
1021: }
|