Java Code Examples for org.pentaho.metastore.api.IMetaStore#namespaceExists()

The following examples show how to use org.pentaho.metastore.api.IMetaStore#namespaceExists() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseMetaStoreUtil.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static void createDatabaseElement( IMetaStore metaStore, DatabaseMeta databaseMeta ) throws MetaStoreException {

    // If the Pentaho namespace doesn't exist, create it!
    //
    if ( !metaStore.namespaceExists( PentahoDefaults.NAMESPACE ) ) {
      metaStore.createNamespace( PentahoDefaults.NAMESPACE );
    }

    // If the database connection element type doesn't exist, create it
    //
    IMetaStoreElementType elementType =
      metaStore.getElementTypeByName(
        PentahoDefaults.NAMESPACE, PentahoDefaults.DATABASE_CONNECTION_ELEMENT_TYPE_NAME );
    if ( elementType == null ) {
      elementType = populateDatabaseElementType( metaStore );
      metaStore.createElementType( PentahoDefaults.NAMESPACE, elementType );
    }

    // populate an element, store it.
    //
    IMetaStoreElement databaseElement = populateDatabaseElement( metaStore, databaseMeta );

    // Store the element physically
    //
    metaStore.createElement( PentahoDefaults.NAMESPACE, databaseElement.getElementType(), databaseElement );
  }
 
Example 2
Source File: DatabaseMetaStoreUtil.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static IMetaStoreElement populateDatabaseElement( IMetaStore metaStore, DatabaseMeta databaseMeta ) throws MetaStoreException {

    if ( !metaStore.namespaceExists( PentahoDefaults.NAMESPACE ) ) {
      throw new MetaStoreException( "Namespace '" + PentahoDefaults.NAMESPACE + "' doesn't exist." );
    }

    // If the data type doesn't exist, error out...
    //
    IMetaStoreElementType elementType =
      metaStore.getElementTypeByName(
        PentahoDefaults.NAMESPACE, PentahoDefaults.DATABASE_CONNECTION_ELEMENT_TYPE_NAME );
    if ( elementType == null ) {
      throw new MetaStoreException( "Unable to find the database connection type" );
    }

    elementType = populateDatabaseElementType( metaStore );

    // generate a new database element and populate it with metadata
    //
    IMetaStoreElement element = metaStore.newElement( elementType, databaseMeta.getName(), null );

    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_PLUGIN_ID, databaseMeta.getPluginId() ) );

    element.setName( databaseMeta.getName() );

    element.addChild( metaStore
      .newAttribute( MetaStoreConst.DB_ATTR_ID_DESCRIPTION, databaseMeta.getDescription() ) );
    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_ACCESS_TYPE, databaseMeta
      .getAccessTypeDesc() ) );
    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_HOSTNAME, databaseMeta.getHostname() ) );
    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_PORT, databaseMeta
      .getDatabasePortNumberString() ) );
    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_DATABASE_NAME, databaseMeta
      .getDatabaseName() ) );
    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_USERNAME, databaseMeta.getUsername() ) );
    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_PASSWORD, metaStore
      .getTwoWayPasswordEncoder().encode( databaseMeta.getPassword() ) ) );
    element
      .addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_SERVERNAME, databaseMeta.getServername() ) );
    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_DATA_TABLESPACE, databaseMeta
      .getDataTablespace() ) );
    element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_INDEX_TABLESPACE, databaseMeta
      .getIndexTablespace() ) );

    IMetaStoreAttribute attributesChild = metaStore.newAttribute( MetaStoreConst.DB_ATTR_ID_ATTRIBUTES, null );
    element.addChild( attributesChild );

    // Now add a list of all the attributes set on the database connection...
    //
    Properties attributes = databaseMeta.getAttributes();
    Enumeration<Object> keys = databaseMeta.getAttributes().keys();
    while ( keys.hasMoreElements() ) {
      String code = (String) keys.nextElement();
      String attribute = (String) attributes.get( code );
      // Add it to the attributes child
      //
      attributesChild.addChild( metaStore.newAttribute( code, attribute ) );
    }

    // Extra information for 3rd-party tools:
    //
    // The driver class
    //
    element
      .addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_DRIVER_CLASS, databaseMeta.getDriverClass() ) );

    // The URL
    //
    try {
      element.addChild( metaStore.newAttribute( MetaStoreConst.DB_ATTR_JDBC_URL, databaseMeta.getURL() ) );
    } catch ( Exception e ) {
      throw new MetaStoreException( "Unable to assemble URL from database '" + databaseMeta.getName() + "'", e );
    }

    return element;
  }
 
Example 3
Source File: PurRepositoryIT.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testMetaStoreElements() throws MetaStoreException {
  // Set up a namespace
  //
  String ns = PentahoDefaults.NAMESPACE;
  IMetaStore metaStore = repository.getMetaStore();
  if ( !metaStore.namespaceExists( ns ) ) {
    metaStore.createNamespace( ns );
  }

  // And an element type
  //
  IMetaStoreElementType elementType = metaStore.newElementType( ns );
  elementType.setName( PentahoDefaults.KETTLE_DATA_SERVICE_ELEMENT_TYPE_NAME );
  elementType.setDescription( PentahoDefaults.KETTLE_DATA_SERVICE_ELEMENT_TYPE_DESCRIPTION );
  metaStore.createElementType( ns, elementType );

  // Now we play with elements...
  //
  IMetaStoreElement oneElement = populateElement( metaStore, elementType, "Element One" );
  metaStore.createElement( ns, elementType, oneElement );

  IMetaStoreElement verifyOneElement = metaStore.getElement( ns, elementType, oneElement.getId() );
  assertNotNull( verifyOneElement );
  validateElement( verifyOneElement, "Element One" );

  assertEquals( 1, metaStore.getElements( ns, elementType ).size() );

  IMetaStoreElement twoElement = populateElement( metaStore, elementType, "Element Two" );
  metaStore.createElement( ns, elementType, twoElement );

  IMetaStoreElement verifyTwoElement = metaStore.getElement( ns, elementType, twoElement.getId() );
  assertNotNull( verifyTwoElement );

  assertEquals( 2, metaStore.getElements( ns, elementType ).size() );

  try {
    metaStore.deleteElementType( ns, elementType );
    fail( "Delete element type failed to properly detect element dependencies" );
  } catch ( MetaStoreDependenciesExistsException e ) {
    List<String> ids = e.getDependencies();
    assertEquals( 2, ids.size() );
    assertTrue( ids.contains( oneElement.getId() ) );
    assertTrue( ids.contains( twoElement.getId() ) );
  }

  metaStore.deleteElement( ns, elementType, oneElement.getId() );

  assertEquals( 1, metaStore.getElements( ns, elementType ).size() );

  metaStore.deleteElement( ns, elementType, twoElement.getId() );

  assertEquals( 0, metaStore.getElements( ns, elementType ).size() );
}