Menu

Home

Cesar Garcia

Hello OLAP World!!

This tutorial describes the initial setup for the smallest OLAP Faces project. This is the outline of the basic steps:

  1. Include the dependencies for JSF 2 (if you do use a Java EE Container), olap4j y OLAP FACES in your WEB-INF/lib.
  2. Add to your web.xml the JSF Servlet and configure its mapping.
  3. Create a Facelets page containing an OLAP Faces cellSetTable to show a CellSet
  4. Create a Managed Bean providing the CellSet to display and bind the cellSetTable to this bean.

Step 1: Include dependencies in your project

I use Maven to build my projects, so this is the relevant part of my pom.xml

<dependencies>
    <!-- JSF API -->
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- OLAP Faces -->
    <dependency>
        <groupId>cgalesanco</groupId>
            <artifactId>olap4j-faces</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>cgalesanco</groupId>
        <artifactId>olap4j-query</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
     ...
</dependencies>

Step 3 Create a Facelts page with a cellSetTable

This is the sample page (hello.xhtml)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
xmlns:olap="http://cgalesanco.es/faces/olap4j">

<h:head>
    <title>OLAP Faces Hello World page</title>
</h:head>
<h:body>
<!-- 
    A table to show a CellSet uses the 'var' EL expression to provide
    information about the item to be drawn.
-->
<olap:cellSetTable value="#{hello.cellSet}" var="cell">
    <!-- Row axis formatting -->
    <olap:rowAxis>
      <olap:header>
        <!-- Content to be drawn as the header of QueryDimension columns: 
                  the hierarchy caption -->
     <f:facet name="header">
       <h:outputText value="#{cell.hierarchy.caption}" />
     </f:facet>
     <!-- Content to be drawn for the cells showing information about 
                 a member in a hierarchy -->
     <h:outputText value="#{cell.member.caption}" />
        </olap:header>
      </olap:rowAxis>
</olap:cellSetTable>


In this page I make a reference to the OLAP Faces taglib, referencing its namespace. And then add a <olap:cellsettable> tag. This tag has two attributes:</olap:cellsettable>

  • value: that must evaluate to an olap4j CellSet
  • var: a variable name that will contain the information about the current cell (like the 'var' attribute for the standard h:dataTable.

The complete use case allows description of the query row axis, the query column axis and the data cells. By now we can only define the row axis (the columns axis is rendered as a single row containing every leaf member in the cellset, and for the data cell, the component draws its "formattedValue".

The axis rendering has a main part and a "header" facet. That facet is used to render the headers of every QueryDimension, and the main part renders every member in the query axis.

For the headers, we are rendering #{cell.hierarchy.caption} the caption property of the hierarchy used for that dimension.

For the members, we are rendering #{cell.member.caption} the caption of each member. #{cell.member} and #{cell.hierarchy} return the standar olap4j objects Member and Hierarchy.

Now we need to implement the "hello" managed bean.

Setp 4: The hello managed bean

:::::java

@ManagedBean(name="hello")
@RequestScoped
public class HelloBean {

private OlapConnection cn;
private CellSet cs;

@PostConstruct
public void init() throws SQLException, ClassNotFoundException {
    Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
    Connection jdbcCn = DriverManager.getConnection("jdbc:mondrian:"

            + "JdbcDrivers=com.mysql.jdbc.Driver;"
            + "Jdbc=jdbc:mysql://localhost/foodmart;"
            + "JdbcUser=root;JdbcPassword=root;"
            + "Catalog=file:/users/cesar/FoodMart.xml");
    cn = jdbcCn.unwrap(OlapConnection.class);
}

@PreDestroy
public void tearDown() throws SQLException {
    if ( cn != null )
        cn.close();
}

public CellSet getCellSet() throws OlapException {
    if ( cs == null )
        cs = cn.createStatement().executeOlapQuery(
            "SELECT {[Measures].[Unit Sales],[Measures].[Store Sales]} ON COLUMNS," +
            " CrossJoin({[Promotions].[All Promotions],[Promotions].[All Promotions].Children}," +
            "           {[Promotion Media].[All Media],[Promotion Media].[All Media].Children}) ON ROWS " +
            "FROM [Sales]");
    return cs;
}

}


MongoDB Logo MongoDB