BLOG

Posts Tagged ‘OFBiz Product List’

OFBiz Tutorial – Implementing a Product PDF export

Tuesday, February 23rd, 2010

In our last post of the tutorial series OFBiz Tutorial – Implementing a Product CSV export we have learned how to quickly implement a CSV export of a list of rows.

In this post we will perform similar steps in order to create a PDF version of the “Product List” screen.
PDF exports are supported out of the box by the OFBiz widgets.
In this excercise we will:

  1. add to the product list screen a link, close to the CSV export link we have created in our last post, that will generate the PDF export
  2. add to the controller.xml file the request and view entries for the new PDF export screen
  3. implement the new screen definition for the PDF export screen; the screen will reuse the same form used by html screen but with a different decorator (more suited for PDF output)

Adding a link element

We have already learned how to create a link in a screen. We add it after the “CSV Export” link:

1
2
3
4
<container style="button-bar">
    <link target="ProductListExport" text="CSV Export" style="buttontext"/>
    <link target="ProductListPDF" text="PDF Export" style="buttontext"/>
</container>
  • the new link is inside of the same “container” element of the “CSV Export” link; we want the two links rendered close to each other
  • the link element is defined in the same way we have defined the other link

Controller entries for a PDF export screen

1
2
3
4
5
6
<request-map uri="ProductListPDF">  
    <security https="true" auth="true"/> 
    <response name="success" type="view" value="ProductListPDFScreen"/>
</request-map> 
... 
<view-map name="ProductListPDFScreen" type="screenfop" content-type="application/pdf" page="component://hwm/widget/HwmScreens.xml#ProductListPDF"/>

The entries are very similar to the ones we have implemented in a previous post for the product list screen. There are just a couple of things to notice:

  • as usual, the request-map uri must match with the target of the link element in the screen (”ProductListPDF”)
  • in the view-map, the type is now “screenfop” (instead of “screen”) because we have to invoke the “fop screen renderer” to generate a PDF output
  • in the view-map we have also set the content-type to “application/pdf” as useful metadata information for the browser

Screen definition for PDF export screens

There isn’t anything special in a screen definition for a PDF export; the screen is defined in the same way of a normal screen, except for a couple of details:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<screen name="ProductListPDF">  
    <section>  
        <actions>  
            <set field="viewSize" value="10000"/>  
            <set field="pageLayoutName" value="main-page-landscape"/>  
            <entity-condition entity-name="Product" list="products">  
                <order-by field-name="productId"/>  
            </entity-condition>  
        </actions>  
        <widgets>  
            <decorator-screen name="FoReportDecorator" location="component://common/widget/CommonScreens.xml">  
                <decorator-section name="body">  
                    <container>  
                        <label text="Finished Products" style="h1"/>  
                    </container>  
                    <include-form name="ListProducts" location="component://hwm/widget/HwmForms.xml"/>  
                </decorator-section>  
            </decorator-screen>  
        </widgets>  
    </section> 
</screen>
  • the screen definition is mostly identical to the “product list screen” except for the decorator element: we are now using the FoReportDecorator that is one of the screen decorators available out of the box in OFBiz (it renders simple PDF reports with a page-of-pages footer, logo etc.)
  • in particular, the entity-condition element is the same and it is used to select the list of products for the export
  • the form that is included is the same of the “product list screen”: we will not have to re-implement it because the widget renderer will take care of rendering it into the proper output (PDF or CSV or html)
  • as we already did for the CSV screen, the “set” field operation, where we set “viewSize” to “10000″, is an easy way to “disable” pagination; we actually define the top limit of our product export to 10000 records here, but of course we can use a different value
  • there is an additional set operation here (for “pageLayoutName”), to set the PDF layout to landscape

Conclusion

The exercise is complete and we can test the product PDF by clicking the “PDF Export” link (there is no need to restart OFBiz).

Implementing the PDF report ended up being very similar to implementing the CSV export (or HTML screen): in OFBiz the same patterns, tools and languages can be reused to dramatically improve productivity.

- Jacopo

Jacopo Cappellato is VP of Technology at HotWax Media and has been involved with the OFBiz project since 2003. He is an OFBiz Project Committer and a member of both the OFBiz Project Management Committee and the Apache Software Foundation.Jacopo Cappellato - OFBiz Developer - OFBiz Expert

OFBiz Tutorial – Enhancing the Product list screen

Wednesday, January 27th, 2010

As you become an expert OFBiz developer, you will certainly need to know all about the OFBiz Form Widget! Here at HotWax Media we use the Form Widget regularly, whether as part of a front end e-commerce shopping cart or as part of a back-end ERP administration screen (for example OFBiz screens to handle order management, warehouse management, and inventory management as part of your integrated online retail OFBiz system).

In this post we will explore some of the features offered by the form widget, the layer in the OFBiz framework that enables the implementation of list based user interface elements. We will do this by continuing the exercise we started in our earlier “OFBiz Tutorial” posts. Specifically, this exercise is based on the “hwm” component we have setup in OFBiz Tutorial – Custom Components In OFBiz and on the custom “product list” screen we have built in OFBiz Tutorial – Building A Simple Product List Screen Here is the form widget definition for the list of products that we have created in the last post:

1
2
3
<form name="ListProducts" type="list" list-name="products">
    <auto-fields-entity entity-name="Product" default-field-type="display"/> 
</form>

The auto-fields element was all we needed to retrieve the fields defined in the “Product” entity (entity-name) and render them as “display” fields (default-field-type). The goal of this post is to fine tune the list definition, in order to render only a subset of the fields and also to render them in a richer way; specifically:

  • we want to render the “product id” field as a link to the “product detail screen” in the OFBiz “Catalog” application

  • instead of displaying the id in the “product type id” field, we want to display the description associated to that id
  • we are only interested in the following fields from the “Product” entity: “product id” (as a link), “product type id” (as a description), “internal name”, “description”
  • we want to add two computed fields containing the “quantity on hand” and “available to promise” quantity of the items in inventory

Defining links

In order to transform the “product id” field into a link, we will simply add the field definition right after the “auto-fields” element; in this way, the explicit field type (link) will override the field type defined by the “default-field-type” attribute; here is the updated field definition:

1
2
3
4
5
6
7
8
<form name="ListProducts" type="list" list-name="products">
    <auto-fields-entity entity-name="Product" default-field-type="display"/> 
    <field name="productId" widget-style="buttontext">
        <hyperlink target="/catalog/control/EditProduct" target-type="inter-app" description="${productId}"> 
            <parameter param-name="productId"/> 
        </hyperlink> 
    </field> 
</form>

When you refresh the screen you will immediately see the new form definition in action. Here are some details about the new field definition:

  • the attribute widget-style=”buttontext” sets the style for the field by using one of the many styles available in OFBiz (and defined in a shared CSS stylesheet; you can of course define and use your own styles)

  • the “description” attribute in the “hyperlink” element defines the text rendered as hyperlink; we have used the ${} notation to insert the “productId” variable
  • the “target” attribute in the “hyperlink” element defines the url for the hyperlink; this url is defined in the controller.xml file of the corresponding “catalog” web application (defined in the applications/product component); since the link, from the “hwm” application, points to a different application we use the target-type=”inter-app” attribute
  • the “description” attribute in the “hyperlink” element defines the text rendered as hyperlink; we have used the ${} notation to insert there the “productId” variable
  • the “parameter” element is needed because the EditProduct url requires that a productId parameter is passed to it

Performing lookups to different entities

Instead of displaying the id for the product type, as stored in the Product.productTypeId field, we prefer to display the description associated to that type; for example, instead of “FINISHED_GOOD” we want “Finished Good”. The description is not stored in the Product entity, it is instead in the ProductType entity: we have to use the Product.productTypeId field to perform a table lookup in the ProductType entity (Product.productTypeId = ProductType.productTypeId), and use the content of the ProductType.description field when the form is rendered. The form widget has built-in support for this, the “display-entity” element:

1
2
3
<field name="productTypeId" title="Product Type">
    <display-entity entity-name="ProductType"/>
</field>

When you refresh the screen you will immediately see the new form definition in action. Here are some details about the new field definition:

  • the “entity-name” defines the entity to lookup

  • this is all we have to specify here, because, if not specified, OFBiz assumes that the name of the field (name=”productTypeId”) is the same in both entities and assumes that a “description” field is available in the referenced entity; if the id and description fields in the referenced entity have different names, you can specify them by adding the “description” and “key-field-name” attributes to the “display-entity” element

Displaying just the fields we need

We now realize that we don’t need most of the fields of the “Product” entity; for this reason it is easier to remove the “auto-fields” element and simply add the field definition for the two more fields we want to display (”internal name” and “description”). Here is the new form definition:

1
2
3
4
5
6
7
8
9
10
11
12
<form name="ListProducts" type="list" list-name="products">
    <field name="productId" widget-style="buttontext"> 
        <hyperlink target="/catalog/control/EditProduct" description="${productId}" target-type="inter-app">
            <parameter param-name="productId"/>
        </hyperlink>
    </field>
    <field name="productTypeId" title="Product Type">
        <display-entity entity-name="ProductType"/>
    </field> 
    <field name="internalName"><display/></field>
    <field name="description"><display/></field>
</form>

Displaying fields computed by services

This is the most interesting part of this exercise. We want to add two additional fields to the list: one with the count of units physically in the warehouse for the product, and one with the units available in warehouse for sales (i.e. units not reserved by sales orders). This information is not stored in the Product or other entities, and computing the units is not an easy task, because, in order to enable multi warehouse support, material tracking and quality control, the OFBiz data model for inventory is rather complex. However there is a standard service (a reusable business logic) specifically designed to count inventory: the “getProductInventoryAvailable” service. In its simplest form the service takes as input a productId and returns as output a map containing two fields, “quantityOnHandTotal” and “availableToPromiseTotal”; this is exactly what we need. But we have to call this service for each record in the list, get the result from the service and use it to populate the two new columns. Again, the form widget assists us with built-in support for service calls, using the “row-actions” and “service” elements:

1
2
3
4
5
6
7
8
9
 
<form name="ListProducts" type="list" list-name="products">
    <row-actions> 
        <service service-name="getProductInventoryAvailable" result-map="inventoryAvailableMap">
            <field-map field-name="productId" from-field="productId"/>
        </service>
    </row-actions> 
... 
</form>
  • all the actions inside the “row-actions” section are executed before each row is rendered; this tag is similar to the “actions” tag that we have used in a previous post, with the only difference that “actions” is executed once before the list is rendered, while “row-actions” is executed before each and every row in the list; a form definition can also contain both action elements
  • the “service” action is a convenient way to invoke a service: the service name is specified with the “service-name” attribute, the “field-map” elements are used to pass the input parameters to the service, the “result-map” attribute defines the name of the output map

We can now add the two new computed fields in the following way:

1
2
3
4
5
6
<field name="qoh" entry-name="inventoryAvailableMap.quantityOnHandTotal"> 
    <display/>
</field> 
<field name="atp" entry-name="inventoryAvailableMap.availableToPromiseTotal">
    <display/>
</field>

the “entry-name” attribute is used to explicitly specify the source of the content: the service result map that we have named “inventoryAvailableMap”, and the “quantityOnHandTotal” and “availableToPromiseTotal” fields.

Conclusion

Here is the final result of our programming efforts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<form name="ListProducts" type="list" list-name="products">
    <row-actions>
        <service service-name="getProductInventoryAvailable" result-map="inventoryAvailableMap">
            <field-map field-name="productId" from-field="productId"/>
        </service>
    </row-actions>
    <field name="productId" widget-style="buttontext">
        <hyperlink target="/catalog/control/EditProduct" description="${productId}" target-type="inter-app">
            <parameter param-name="productId"/>
        </hyperlink>
    </field>
    <field name="productTypeId" title="Product Type">
        <display-entity entity-name="ProductType"/>
    </field>
    <field name="internalName"><display/></field>
    <field name="description"><display/></field>
    <field name="qoh" entry-name="inventoryAvailableMap.quantityOnHandTotal">
        <display/>
    </field>
    <field name="atp" entry-name="inventoryAvailableMap.availableToPromiseTotal">
        <display/>
    </field>
</form>

and here is how the list is rendered:

The final result

Our form is now completed and in the next post we will reuse it to build a CSV (Comma Separated Value) export.

- Jacopo

Jacopo Cappellato is VP of Technology at HotWax Media and has been involved with the OFBiz project since 2003. He is an OFBiz Project Committer and a member of both the OFBiz Project Management Committee and the Apache Software Foundation.

Jacopo Cappellato - OFBiz Developer - OFBiz Expert