/** @see org.pivot4j.analytics.datasource.DataSourceManager#getCubes(java.lang.String) */
  @Override
  public List<CubeInfo> getCubes(String catalogName) {
    if (catalogName == null) {
      throw new NullArgumentException("catalogName");
    }

    SimpleDataSourceInfo definition = getDefinition(catalogName);

    if (definition == null) {
      throw new IllegalArgumentException(
          "Data source with the given name does not exist : " + catalogName);
    }

    OlapDataSource dataSource = createDataSource(definition);

    List<CubeInfo> cubes = new LinkedList<CubeInfo>();

    OlapConnection connection = null;

    try {
      connection = dataSource.getConnection();

      for (Cube cube : connection.getOlapSchema().getCubes()) {
        if (cube.isVisible()) {
          cubes.add(new CubeInfo(cube.getName(), cube.getCaption(), cube.getDescription()));
        }
      }
    } catch (SQLException e) {
      throw new PivotException(e);
    } finally {
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
          throw new PivotException(e);
        }
      }
    }

    return cubes;
  }
Example #2
0
  public static void main(String[] args) throws Exception {

    // Load the driver
    Class.forName("mondrian.olap4j.MondrianOlap4jDriver");

    // Connect
    final Connection connection =
        DriverManager.getConnection(
            "jdbc:mondrian:" // Driver ident
                + "Jdbc=jdbc:hsqldb:file:foodmart/foodmart;" // Relational DB
                + "Catalog=file:foodmart/FoodMart.xml;"); // Catalog

    // We are dealing with an olap connection. we must unwrap it.
    final OlapConnection olapConnection = connection.unwrap(OlapConnection.class);

    // Get a cube object.
    Cube salesCube = olapConnection.getOlapSchema().getCubes().get("Sales");

    // Build a query object.
    Query myQuery = new Query("myQuery", salesCube);

    // Lookup some dimensions
    QueryDimension productDim = myQuery.getDimension("Product");
    QueryDimension storeDim = myQuery.getDimension("Store");
    QueryDimension timeDim = myQuery.getDimension("Time");

    // Place dimensions on some axis
    myQuery.getAxis(Axis.COLUMNS).addDimension(productDim);
    myQuery.getAxis(Axis.ROWS).addDimension(storeDim);
    myQuery.getAxis(Axis.FILTER).addDimension(timeDim);

    // Including a member by metadata
    Member year1997 =
        salesCube.lookupMember(IdentifierNode.ofNames("Time", "1997").getSegmentList());
    timeDim.include(year1997);

    // Including a member by name parts
    productDim.include(
        Selection.Operator.CHILDREN,
        IdentifierNode.ofNames("Product", "Drink", "Beverages").getSegmentList());

    // We can also exclude members
    productDim.exclude(
        IdentifierNode.ofNames("Product", "Drink", "Beverages", "Carbonated Beverages")
            .getSegmentList());

    // Validate this query
    myQuery.validate();

    // Print!
    System.out.println("/********************* QUERY ***********************/");

    System.out.println(myQuery.getSelect().toString());

    System.out.println("/********************* QUERY ***********************/");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println(" ");

    System.out.println("/********************* RESULTS ***********************/");

    RectangularCellSetFormatter formatter = new RectangularCellSetFormatter(false);

    PrintWriter writer = new PrintWriter(System.out);

    formatter.format(myQuery.execute(), writer);

    writer.flush();
    System.out.println("/********************* RESULTS ***********************/");
  }