使用java在现有报告模板中创建表

时间:2015-11-03 05:56:43

标签: java pentaho

我使用PRD 5.4.0.1创建了一个prpt文件,其中包含2个标签'Date'和'Duration',表示该表的2列。这2个标签位于内联子报告中。我正在尝试添加下面的文本字段将这2个标签和填充数据从数据库查询中获取到这些标签中,如下所示:

日期持续时间

29-10-2015 2小时
30-10-2015 1小时
01-11-2015 1小时30分钟

但数据不会出现在生成的pdf中。下面是代码:

public class DataAccessVisitor extends AbstractStructureVisitor {

private DataSource dataSource;

private Connection connection;

private DateFormat dateTimeFormat = new SimpleDateFormat(
        "dd-MM-yyyy");

public void inspect(MasterReport report, String id, Date startDate,
        Date endDate) {

    ReportParameterDefinition rpd = report.getParameterDefinition();
    ParameterDefinitionEntry[] paramEntries = rpd.getParameterDefinitions();

    for (int i = 0; i < paramEntries.length; i++) {
        inspectParameter(report, rpd, paramEntries[i]);
    }
    inspectElement(report, id, startDate, endDate);
    traverseSection(report, id, startDate, endDate);
}

public void traverseSection(Section section, String id, Date startDate,
        Date endDate) {
    int count = section.getElementCount();

    ReportElement element = null;
    for (int i = 0; i < count; i++) {
        element = section.getElement(i);
        inspectElement(element, id, startDate, endDate);

        if (element instanceof Section) {
            traverseSection((Section) element, id, startDate, endDate);
        }
    }
}

public void inspectElement(ReportElement element, String id,
        Date startDate, Date endDate) {
    int i = 4;

    ResultSet rs = null;
    Map<String, Object> attributeMap = new HashMap<>();
    Map<String, Object> styleMap = new HashMap<>();

    try {
        if ("subreport1".equals(element.getName())) {

            ReportElement[] dateColumn = element
                    .getChildElementsByName("date_label");
            ReportElement[] durationColumn = element
                    .getChildElementsByName("duration_label");
            ReportHeader header = (ReportHeader) dateColumn[0]
                    .getParentSection();

            rs = getDataFromDB(id, startDate, endDate);
            styleMap.put("font-size", 15);
            styleMap.put("bold", false);

            while (rs.next()) {
                attributeMap.put("name", "date_textfield_" + (i-4));
                styleMap
                        .put("x",
                                ((Float) dateColumn[0].getStyle().getStyleProperty(StyleKey.getStyleKey("x"))));
                styleMap
                        .put("y",
                                i+((Float) dateColumn[0].getStyle().getStyleProperty(StyleKey.getStyleKey("y"))));
                Element date = createTextField(attributeMap);
                date.setAttribute(date.getAttributeNamespaces()[0],
                        "value", rs.getDate("startdate"));

                styleMap
                        .put("x",
                                ((Float) durationColumn[0].getStyle().getStyleProperty(StyleKey.getStyleKey("x"))));
                styleMap
                        .put("y",
                                i+((Float) durationColumn[0].getStyle().getStyleProperty(StyleKey.getStyleKey("y"))));
                attributeMap.put("name", "duration_textfield_" + (i-4));
                Element duration = createTextField(attributeMap);
                duration.setAttribute(duration.getAttributeNamespaces()[0],
                        "value", rs.getLong("duration"));

                header.addElement(date);
                header.addElement(duration);
                i=i+2;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void inspectParameter(MasterReport report,
        ReportParameterDefinition rpd, ParameterDefinitionEntry entry) {
    System.out.println("Parameter name :" + entry.getName());
}

private Element createLabel(Map<String, Object> attributeMap) {
    LabelElementFactory labelFactory = new LabelElementFactory();
    labelFactory.setText(attributeMap.get("value") + "");
    labelFactory.setName(attributeMap.get("name") + "");
    labelFactory.setBold((Boolean) attributeMap.get("bold"));
    labelFactory.setFontSize((Integer) attributeMap.get("font-size"));
    Element label = labelFactory.createElement();
    return label;
}

private Element createTextField(Map<String, Object> attributeMap) {
    TextFieldElementFactory textFieldFactory = new TextFieldElementFactory();
    textFieldFactory.setName(attributeMap.get("name") + "");
    textFieldFactory.setFieldname(attributeMap.get("field") + "");
    textFieldFactory.setFontSize((Integer) attributeMap.get("font-size"));
    textFieldFactory.setBold((Boolean) attributeMap.get("bold"));
    textFieldFactory.setFieldname(attributeMap.get("field") + "");
    textFieldFactory.setX((Float) attributeMap.get("x"));
    textFieldFactory.setY((Float) attributeMap.get("y"));
    textFieldFactory.setVisible(true);
    Element textField = textFieldFactory.createElement();
    return textField;
}

public DataSource getDataSource() {
    return dataSource;
}

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}

public Connection getConnection() {
    return connection;
}

public void setConnection(Connection connection) {
    this.connection = connection;
}
}

0 个答案:

没有答案