General

Components

Community

Development

PPMC

ASF

Demos > Navigation API in Hotel Receipt

Overview

This demo is a simple template application about hotel receipt. By loading the configuration file "consume-data.properties" and navigating the hotel receipt template, this demo could generate the ODF documents (ODT, ODP, and ODS format).

In the generated ODF documents, you can see the concrete information about hotel receipt, such as hotel name, customer name, consume time, consume data, total expense, headcount, and consume item details including the price, quantity, total expense of each item. Taking the ODT document as an example, the ODT document template is shown in the following picture. Please notice the area surrounding by eight small green square, it is a chart. Because this chart is generated by the column ConsumeItem and the column Total Expense($) in the above table and the values of the column Total Expense($) are not numeric type, the template chart might not display correctly in your ODF editor, but it will display correctly after the template is expanded.


And the generated ODT document is shown in the following picture.

Use Simple API to complete this simple template application. This demo mainly uses the API of package navigation.

In the root directory of this demo project, there are three defined ODF template documents, "Navigation-ODT-Templating.odt", "Navigation-ODP-Templating.odp" and "Navigation-ODS-Templating.ods", and one configuration file, "consume-data.properties". Run NavigationForODF.java, and then in the root directory of this demo project you can find three generated ODF documents, "Navigation-ODT-Generated.odt", "Navigation-ODP-Generated.odp" and "Navigation-ODS-Generated.ods", which contain the information of "consume-data.properties". In these ODF documents, the consume item details are shown as table and chart.

Please notice the differences of ODT, ODP, ODS template document. In ODT and ODS template documents, the chart could be automatically generated by the ODF editor according to the data of table, and in this demo the data of chart comes from the column of ConsumeItem and Total Expense($). But in ODP template documents, the chart can not be automatically generated by the table, in fact, the chart in ODP template document is created manually through the function "insert chart" of openoffice, and the type of second column must be numeric. In addition, please notice the cell value of ODS document, the cell value has character type and numeric type, for example, ‘5,000 represents character type and 5,000 represents numeric type, and we could see that the different point is the single quotation marks before 5,000.

Code Introduction

First, load the key-value in the file "consume-data.properties" into the instance of Properties, and put ConsumeItem as key and TotalExpenseOfItem as value to the instance of HashMap for navigating ODP document template.

public void loadConsumeData(String filepath) throws Exception {
    InputStream is = new FileInputStream(filepath);
    properties.load(is);
    is.close();
    // put ConsumeItem as key and TotalExpenseOfItem as value to map
    Enumeration<String> enu = (Enumeration<String>) properties.propertyNames();
    while (enu.hasMoreElements()) {
        String key = (String) enu.nextElement();
        if (key.contains("ConsumeItem")) {
            String consumeItem = properties.getProperty(key);
            String totalExpenseOfItem = properties.getProperty("TotalExpenseOfItem"
                                        + key.charAt(key.length() - 1));
            map.put(consumeItem, totalExpenseOfItem);
        }
    }
}

Then, introduce how to navigate the template documents and generate the ODF documents.

Navigate ODT document template

Load "Navigation-ODT-Templating.odt" and get the instance of TextDocument. Then traverse the instance of Properties, use the API of TextNavigation and TextSelection to complete the replacement of template document as the line 10-14. Please notice the line 17 and 18, make sure that the chart is shown rightly when open the generated document, so is navigating ODP and ODS document template. At last, generate "Navigation-ODT-Generated.odt" as the line 19.

public void navigateODT() throws Exception {
    Iterator it = properties.entrySet().iterator();
    System.out.println("Navigate ODT document: Navigation-ODT-Templating.odt");
    TextDocument textdoc = (TextDocument) TextDocument.loadDocument("Navigation-ODT-Templating.odt");
    TextNavigation search;
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        String key = (String) entry.getKey();
        String value = (String) entry.getValue();
        search = new TextNavigation(key, textdoc);
        while (search.hasNext()) {
            TextSelection item = (TextSelection) search.nextSelection();
            item.replaceWith(value);
        }
    }
    // remove ObjectReplacements/ and Thumbnails/
    textdoc.getPackage().removePackageDocument("ObjectReplacements/");
    textdoc.getPackage().removePackageDocument("Thumbnails/");
    textdoc.save("Navigation-ODT-Generated.odt");
    System.out.println("...\nNavigation is over, and Navigation-ODT-Generated.odt is generated");
}

Navigate ODP document template

Unlike the ODT document, the chart of ODP document is an embedded document. Before navigate the chart, get the embedded document as the line 2 and 3. The chart comes from a table, and we could change the chart by setting the cell value of the table as the line 13-15. The line 15 uses the Class DefaultCellValueAdapter to update the cell value and value type.

    // set the cell value in the table of embedderdocument
    List<Document> embeddedDocuments = pdoc.getEmbeddedDocuments();
    Document embeddedDocument = embeddedDocuments.get(0);
    Table table = embeddedDocument.getTableList().get(0);
    String consumeItem;
    String totalExpenseOfItem;
    int index = 0;
    Iterator<String> it = map.keySet().iterator();
    while (it.hasNext()) {
        index++;
        consumeItem = (String) it.next();
        totalExpenseOfItem = (String) map.get(consumeItem);
        table.getColumnByIndex(0).getCellByIndex(index).setDisplayText(consumeItem);
        table.getColumnByIndex(1).getCellByIndex(index).setDisplayText(totalExpenseOfItem, new DefaultCellValueAdapter());
    }

Navigate ODS document template

Unlike the ODT document, use the Class CellSelection to complete the replacement. Class CellSelection is the subclass of Class TextSelection. Use the method "advancedReplaceWith" to update the cell value and value type.

    search = new TextNavigation(key, ssdoc);
    while (search.hasNext()) {
        CellSelection item = (CellSelection) search.nextSelection();
        item.advancedReplaceWith(value, new DefaultCellValueAdapter());
    }

Download

Powered by Simple API version 0.3.5.
You can download the code of this demo from here.


Powered by the Apache CMS.

Apache "ODF Toolkit" is an effort undergoing incubation at The Apache Software Foundation (ASF), sponsored by the Apache Incubator. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

Copyright © 2011 The Apache Software Foundation Licensed under the Apache License, Version 2.0. Contact Us
Apache and the Apache feather logos are trademarks of The Apache Software Foundation.
Other names appearing on the site may be trademarks of their respective owners.