public List<SaikuCube> getCubes(String connectionName) {
   OlapConnection olapcon = connections.getOlapConnection(connectionName);
   List<SaikuCube> cubes = new ArrayList<SaikuCube>();
   if (olapcon != null) {
     try {
       for (Catalog cat : olapcon.getOlapCatalogs()) {
         for (Schema schem : cat.getSchemas()) {
           for (Cube cub : schem.getCubes()) {
             cubes.add(
                 new SaikuCube(
                     connectionName,
                     cub.getUniqueName(),
                     cub.getName(),
                     cub.getCaption(),
                     cat.getName(),
                     schem.getName(),
                     cub.isVisible()));
           }
         }
       }
     } catch (OlapException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
   Collections.sort(cubes, new SaikuCubeCaptionComparator());
   return cubes;
 }
 public Cube getNativeCube(SaikuCube cube) throws SaikuOlapException {
   try {
     OlapConnection con = connections.getOlapConnection(cube.getConnectionName());
     if (con != null) {
       for (Database db : con.getOlapDatabases()) {
         Catalog cat = db.getCatalogs().get(cube.getCatalogName());
         if (cat != null) {
           for (Schema schema : cat.getSchemas()) {
             if (schema.getName().equals(cube.getSchemaName())) {
               for (Cube cub : schema.getCubes()) {
                 if (cub.getName().equals(cube.getName())
                     || cub.getUniqueName().equals(cube.getUniqueName())) {
                   return cub;
                 }
               }
             }
           }
         }
       }
     }
   } catch (Exception e) {
     throw new SaikuOlapException("Cannot get native cube for ( " + cube + " )", e);
   }
   throw new SaikuOlapException("Cannot get native cube for ( " + cube + " )");
 }
  /**
   * Adds a catalog and its children to the cache. Do not use directly. This must be called with a
   * write lock on the cache.
   *
   * @param catalogName The name of the catalog to load in cache.
   */
  private void addCatalogToCache(IPentahoSession session, String catalogName) {

    final IOlapService.Catalog catalog =
        new Catalog(catalogName, new ArrayList<IOlapService.Schema>());

    OlapConnection connection = null;

    try {

      connection = getConnection(catalogName, session);

      for (org.olap4j.metadata.Schema schema4j : connection.getOlapSchemas()) {

        connection.setSchema(schema4j.getName());

        final IOlapService.Schema schema =
            new Schema(
                schema4j.getName(),
                catalog,
                new ArrayList<IOlapService.Cube>(),
                new ArrayList<String>(connection.getAvailableRoleNames()));

        for (org.olap4j.metadata.Cube cube4j : schema4j.getCubes()) {
          schema.cubes.add(new IOlapService.Cube(cube4j.getName(), cube4j.getCaption(), schema));
        }

        catalog.schemas.add(schema);
      }

      // We're done.
      getCache(session).add(catalog);

    } catch (OlapException e) {

      LOG.warn("Failed to initialize the olap connection cache for catalog " + catalogName, e);

    } finally {
      try {
        if (connection != null) {
          connection.close();
        }
      } catch (SQLException e) {
        LOG.warn("Failed to gracefully close an olap connection to catalog " + catalogName, e);
      }
    }
  }
  /** @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;
  }
  public SaikuConnection getConnection(String connectionName) throws SaikuOlapException {
    OlapConnection olapcon = connections.getOlapConnection(connectionName);
    SaikuConnection connection = null;
    if (olapcon != null) {
      List<SaikuCatalog> catalogs = new ArrayList<SaikuCatalog>();
      try {
        for (Catalog cat : olapcon.getOlapCatalogs()) {
          List<SaikuSchema> schemas = new ArrayList<SaikuSchema>();
          for (Schema schem : cat.getSchemas()) {
            List<SaikuCube> cubes = new ArrayList<SaikuCube>();
            for (Cube cub : schem.getCubes()) {
              cubes.add(
                  new SaikuCube(
                      connectionName,
                      cub.getUniqueName(),
                      cub.getName(),
                      cub.getCaption(),
                      cat.getName(),
                      schem.getName(),
                      cub.isVisible()));
            }
            Collections.sort(cubes, new SaikuCubeCaptionComparator());
            schemas.add(new SaikuSchema(schem.getName(), cubes));
          }
          if (schemas.size() == 0) {
            OlapDatabaseMetaData olapDbMeta = olapcon.getMetaData();
            ResultSet cubesResult = olapDbMeta.getCubes(cat.getName(), null, null);

            try {
              List<SaikuCube> cubes = new ArrayList<SaikuCube>();
              while (cubesResult.next()) {

                cubes.add(
                    new SaikuCube(
                        connectionName,
                        cubesResult.getString("CUBE_NAME"),
                        cubesResult.getString("CUBE_NAME"),
                        cubesResult.getString("CUBE_NAME"),
                        cubesResult.getString("CATALOG_NAME"),
                        cubesResult.getString("SCHEMA_NAME")));
              }
              Collections.sort(cubes, new SaikuCubeCaptionComparator());
              schemas.add(new SaikuSchema("", cubes));
            } catch (SQLException e) {
              throw new OlapException(e.getMessage(), e);
            } finally {
              try {
                cubesResult.close();
              } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
            }
          }
          Collections.sort(schemas);
          catalogs.add(new SaikuCatalog(cat.getName(), schemas));
        }
      } catch (OlapException e) {
        throw new SaikuOlapException(
            "Error getting objects of connection (" + connectionName + ")", e);
      }
      Collections.sort(catalogs);
      connection = new SaikuConnection(connectionName, catalogs);
      return connection;
    }
    throw new SaikuOlapException("Cannot find connection: (" + connectionName + ")");
  }