Java Code Examples for org.pentaho.di.core.database.DatabaseMeta#getName()

The following examples show how to use org.pentaho.di.core.database.DatabaseMeta#getName() . 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: KettleFileRepository.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Read all the databases from the repository, insert into the has databases object, overwriting optionally
 *
 * @param TransMeta
 *          The transformation to load into.
 * @param overWriteShared
 *          if an object with the same name exists, overwrite
 * @throws KettleException
 */
public void readDatabases( HasDatabasesInterface transMeta, boolean overWriteShared ) throws KettleException {
  try {
    ObjectId[] dbids = getDatabaseIDs( false );
    for ( int i = 0; i < dbids.length; i++ ) {
      DatabaseMeta databaseMeta = loadDatabaseMeta( dbids[i], null ); // reads last version
      if ( transMeta instanceof VariableSpace ) {
        databaseMeta.shareVariablesWith( (VariableSpace) transMeta );
      }

      DatabaseMeta check = transMeta.findDatabase( databaseMeta.getName() ); // Check if there already is one in the
                                                                             // transformation
      if ( check == null || overWriteShared ) { // We only add, never overwrite database connections.
        if ( databaseMeta.getName() != null ) {
          transMeta.addOrReplaceDatabase( databaseMeta );
          if ( !overWriteShared ) {
            databaseMeta.setChanged( false );
          }
        }
      }
    }
  } catch ( KettleException e ) {
    throw e;
  }
}
 
Example 2
Source File: JobEntrySQLDialog.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Copy information from the meta-data input to the dialog fields.
 */
public void getData() {
  wName.setText( Const.nullToEmpty( jobEntry.getName() ) );
  wSQL.setText( Const.nullToEmpty( jobEntry.getSQL() ) );
  DatabaseMeta dbinfo = jobEntry.getDatabase();
  if ( dbinfo != null && dbinfo.getName() != null ) {
    wConnection.setText( dbinfo.getName() );
  } else {
    wConnection.setText( "" );
  }

  wUseSubs.setSelection( jobEntry.getUseVariableSubstitution() );
  wSQLFromFile.setSelection( jobEntry.getSQLFromFile() );
  wSendOneStatement.setSelection( jobEntry.isSendOneStatement() );

  wFilename.setText( Const.nullToEmpty( jobEntry.getSQLFilename() ) );

  wName.selectAll();
  wName.setFocus();
}
 
Example 3
Source File: SpoonDBDelegate.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public void dupeConnection( HasDatabasesInterface hasDatabasesInterface, DatabaseMeta databaseMeta ) {
  String name = databaseMeta.getName();
  int pos = hasDatabasesInterface.indexOfDatabase( databaseMeta );
  if ( databaseMeta != null ) {
    DatabaseMeta databaseMetaCopy = (DatabaseMeta) databaseMeta.clone();
    String dupename = BaseMessages.getString( PKG, "Spoon.Various.DupeName" ) + name;
    databaseMetaCopy.setName( dupename );

    getDatabaseDialog().setDatabaseMeta( databaseMetaCopy );

    String newname = getDatabaseDialog().open();
    if ( newname != null ) { // null: CANCEL

      databaseMetaCopy.verifyAndModifyDatabaseName( hasDatabasesInterface.getDatabases(), name );
      hasDatabasesInterface.addDatabase( pos + 1, databaseMetaCopy );
      spoon.addUndoNew(
        (UndoInterface) hasDatabasesInterface, new DatabaseMeta[] { (DatabaseMeta) databaseMetaCopy.clone() },
        new int[] { pos + 1 } );
      saveConnection( databaseMetaCopy, Const.VERSION_COMMENT_EDIT_VERSION );
      refreshTree();
    }
  }
}
 
Example 4
Source File: SharedDatabaseUtil.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Add a database to the list of shared databases in ~/.kettle/shared.xml
 *
 * @param databaseMeta
 * @throws KettleException in case there is an error
 * @throws KettleObjectExistsException if a database with the same name already exists
 */
public static void addSharedDatabase(DatabaseMeta databaseMeta) throws KettleObjectExistsException, KettleException {
  // First verify existence...
  //
  List<DatabaseMeta> sharedDatabases = loadSharedDatabases();
  DatabaseMeta found = DatabaseMeta.findDatabase(sharedDatabases, databaseMeta.getName());
  if (found!=null) {
    throw new KettleObjectExistsException("A database with name '"+databaseMeta.getName()+"' already exists in the shared databases list.");
  }
  try {
    SharedObjects sharedObjects = new SharedObjects();
    sharedObjects.storeObject(databaseMeta);
    sharedObjects.saveToFile();
  } catch(Exception e) {
    throw new KettleException("It was not possible to add database '"+databaseMeta.getName()+"' to the shared.xml file");
  }
}
 
Example 5
Source File: KettleDatabaseRepositoryJobDelegate.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Read the database connections in the repository and add them to this job if they are not yet present.
 *
 * @param jobMeta
 *          the job to put the database connections in
 * @param overWriteShared
 *          set to true if you want to overwrite shared connections while loading.
 * @throws KettleException
 */
public void readDatabases( JobMeta jobMeta, boolean overWriteShared ) throws KettleException {
  try {
    ObjectId[] dbids = repository.getDatabaseIDs( false );
    for ( int i = 0; i < dbids.length; i++ ) {
      DatabaseMeta databaseMeta = repository.loadDatabaseMeta( dbids[i], null ); // reads last version
      databaseMeta.shareVariablesWith( jobMeta );

      // See if there already is one in the transformation
      //
      DatabaseMeta check = jobMeta.findDatabase( databaseMeta.getName() );

      // We only add, never overwrite database connections.
      //
      if ( check == null || overWriteShared ) {
        if ( databaseMeta.getName() != null ) {
          jobMeta.addOrReplaceDatabase( databaseMeta );
          if ( !overWriteShared ) {
            databaseMeta.setChanged( false );
          }
        }
      }
    }
    jobMeta.setChanged( false );
  } catch ( KettleDatabaseException dbe ) {
    throw new KettleException(
      BaseMessages.getString( PKG, "JobMeta.Log.UnableToReadDatabaseIDSFromRepository" ), dbe );
  } catch ( KettleException ke ) {
    throw new KettleException(
      BaseMessages.getString( PKG, "JobMeta.Log.UnableToReadDatabasesFromRepository" ), ke );
  }
}
 
Example 6
Source File: KettleDatabaseRepositoryTransDelegate.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Read all the databases from the repository, insert into the TransMeta object, overwriting optionally
 *
 * @param transMeta
 *          The transformation to load into.
 * @param overWriteShared
 *          if an object with the same name exists, overwrite
 * @throws KettleException
 */
public void readDatabases( TransMeta transMeta, boolean overWriteShared ) throws KettleException {
  try {
    ObjectId[] dbids = repository.getDatabaseIDs( false );
    for ( int i = 0; i < dbids.length; i++ ) {
      DatabaseMeta databaseMeta = repository.loadDatabaseMeta( dbids[i], null ); // reads last version
      databaseMeta.shareVariablesWith( transMeta );

      DatabaseMeta check = transMeta.findDatabase( databaseMeta.getName() ); // Check if there already is one in the
                                                                             // transformation
      if ( check == null || overWriteShared ) { // We only add, never overwrite database connections.
        if ( databaseMeta.getName() != null ) {
          transMeta.addOrReplaceDatabase( databaseMeta );
          if ( !overWriteShared ) {
            databaseMeta.setChanged( false );
          }
        }
      }
    }
    transMeta.clearChangedDatabases();
  } catch ( KettleDatabaseException dbe ) {
    throw new KettleException( BaseMessages.getString(
      PKG, "TransMeta.Log.UnableToReadDatabaseIDSFromRepository" ), dbe );
  } catch ( KettleException ke ) {
    throw new KettleException(
      BaseMessages.getString( PKG, "TransMeta.Log.UnableToReadDatabasesFromRepository" ), ke );
  }
}
 
Example 7
Source File: BaseStepDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
String showDbDialogUnlessCancelledOrValid( DatabaseMeta changing, DatabaseMeta origin ) {
  changing.shareVariablesWith( transMeta );
  DatabaseDialog cid = getDatabaseDialog( shell );
  cid.setDatabaseMeta( changing );
  cid.setModalDialog( true );

  if ( cid.getDatabaseMeta() == null ) {
    return changing.getName();
  }

  String name = null;
  boolean repeat = true;
  while ( repeat ) {
    name = cid.open();
    if ( name == null ) {
      // Cancel was pressed
      repeat = false;
    } else {
      name = name.trim();
      DatabaseMeta same = transMeta.findDatabase( name );
      if ( same == null || same == origin ) {
        // OK was pressed and input is valid
        repeat = false;
      } else {
        showDbExistsDialog( changing );
      }
    }
  }
  return name;
}
 
Example 8
Source File: SpoonDBDelegate.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void editConnection( DatabaseMeta databaseMeta ) {
  HasDatabasesInterface hasDatabasesInterface = spoon.getActiveHasDatabasesInterface();
  if ( hasDatabasesInterface == null ) {
    return; // program error, exit just to make sure.
  }

  String originalName = databaseMeta.getName();
  getDatabaseDialog().setDatabaseMeta( databaseMeta );
  getDatabaseDialog().setDatabases( hasDatabasesInterface.getDatabases() );
  String newname = getDatabaseDialog().open();
  if ( !Utils.isEmpty( newname ) ) { // null: CANCEL
    databaseMeta.setName( originalName );

    databaseMeta = getDatabaseDialog().getDatabaseMeta();
    if ( !newname.equals( originalName )
        && databaseMeta.findDatabase( hasDatabasesInterface.getDatabases(), newname ) != null ) {
      databaseMeta.setName( newname.trim() );
      DatabaseDialog.showDatabaseExistsDialog( spoon.getShell(), databaseMeta );
      databaseMeta.setName( originalName );
      databaseMeta.setDisplayName( originalName );
      return;
    }
    databaseMeta.setName( newname.trim() );
    databaseMeta.setDisplayName( newname.trim() );
    saveConnection( databaseMeta, Const.VERSION_COMMENT_EDIT_VERSION );
    if ( databaseMeta.isShared() ) {
      sharedObjectSyncUtil.synchronizeConnections( databaseMeta, originalName );
    }

    saveConnection( databaseMeta, Const.VERSION_COMMENT_EDIT_VERSION );
    if ( databaseMeta.isShared() ) {
      sharedObjectSyncUtil.synchronizeConnections( databaseMeta, originalName );
    }

    refreshTree();
  }
  spoon.setShellText();
}
 
Example 9
Source File: SAPConnectionParamsHelper.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static SAPConnectionParams getFromDatabaseMeta( DatabaseMeta sapConnection ) {
  String name = sapConnection.getName();
  String host = sapConnection.environmentSubstitute( sapConnection.getHostname() );
  String sysnr =
    sapConnection.environmentSubstitute( sapConnection.getAttributes().getProperty( "SAPSystemNumber" ) );
  String client = sapConnection.environmentSubstitute( sapConnection.getAttributes().getProperty( "SAPClient" ) );
  String user = sapConnection.environmentSubstitute( sapConnection.getUsername() );
  String password = sapConnection.environmentSubstitute( sapConnection.getPassword() );
  String lang = "";
  return new SAPConnectionParams( name, host, sysnr, client, user, password, lang );
}
 
Example 10
Source File: TransDelegate.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Insert all the databases from the repository into the TransMeta object, overwriting optionally
 * 
 * @param TransMeta
 *          The transformation to load into.
 * @param overWriteShared
 *          if an object with the same name exists, overwrite
 * @throws KettleException
 */
protected void readDatabases( TransMeta transMeta, boolean overWriteShared, List<DatabaseMeta> databaseMetas ) {
  for ( DatabaseMeta databaseMeta : databaseMetas ) {
    if ( overWriteShared || transMeta.findDatabase( databaseMeta.getName() ) == null ) {
      if ( databaseMeta.getName() != null ) {
        databaseMeta.shareVariablesWith( transMeta );
        transMeta.addOrReplaceDatabase( databaseMeta );
        if ( !overWriteShared ) {
          databaseMeta.setChanged( false );
        }
      }
    }
  }
  transMeta.clearChangedDatabases();
}
 
Example 11
Source File: JobDelegate.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Insert all the databases from the repository into the JobMeta object, overwriting optionally
 * 
 * @param jobMeta
 *          The transformation to load into.
 * @param overWriteShared
 *          if an object with the same name exists, overwrite
 */
protected void readDatabases( JobMeta jobMeta, boolean overWriteShared, List<DatabaseMeta> databaseMetas ) {
  for ( DatabaseMeta databaseMeta : databaseMetas ) {
    if ( overWriteShared || jobMeta.findDatabase( databaseMeta.getName() ) == null ) {
      if ( databaseMeta.getName() != null ) {
        databaseMeta.shareVariablesWith( jobMeta );
        jobMeta.addOrReplaceDatabase( databaseMeta );
        if ( !overWriteShared ) {
          databaseMeta.setChanged( false );
        }
      }
    }
  }
  jobMeta.clearChanged();
}
 
Example 12
Source File: StreamToJobNodeConverter.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
JobMeta filterPrivateDatabases( JobMeta jobMeta ) {
  Set<String> privateDatabases = jobMeta.getPrivateDatabases();
  if ( privateDatabases != null ) {
    // keep only private transformation databases
    for ( Iterator<DatabaseMeta> it = jobMeta.getDatabases().iterator(); it.hasNext(); ) {
      DatabaseMeta databaseMeta = it.next();
      String databaseName = databaseMeta.getName();
      if ( !privateDatabases.contains( databaseName ) && !jobMeta.isDatabaseConnectionUsed( databaseMeta ) ) {
        it.remove();
      }
    }
  }
  return jobMeta;
}
 
Example 13
Source File: StreamToTransNodeConverter.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
TransMeta filterPrivateDatabases( TransMeta transMeta ) {
  Set<String> privateDatabases = transMeta.getPrivateDatabases();
  if ( privateDatabases != null ) {
    // keep only private transformation databases
    for ( Iterator<DatabaseMeta> it = transMeta.getDatabases().iterator(); it.hasNext(); ) {
      DatabaseMeta databaseMeta = it.next();
      String databaseName = databaseMeta.getName();
      if ( !privateDatabases.contains( databaseName ) && !transMeta.isDatabaseConnectionUsed( databaseMeta ) ) {
        it.remove();
      }
    }
  }
  return transMeta;
}
 
Example 14
Source File: JobEntryCheckDbConnectionsDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void addDatabases() {
  connections = new String[jobMeta.nrDatabases()];
  for ( int i = 0; i < jobMeta.nrDatabases(); i++ ) {
    DatabaseMeta ci = jobMeta.getDatabase( i );
    connections[i] = ci.getName();
  }
}
 
Example 15
Source File: XulDatabaseDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
private JdbcConnectionDefinition convertDbMeta( final DatabaseMeta meta ) throws KettleDatabaseException {
  if ( meta.getAccessType() == DatabaseMeta.TYPE_ACCESS_JNDI ) {
    return new JndiConnectionDefinition( meta.getName(),
      meta.getDatabaseName(),
      meta.getDatabaseInterface().getPluginName(), null, null );
  } else {
    final Map<String, String> map = meta.getExtraOptions();
    final Properties properties = new Properties();
    final Iterator<Map.Entry<String, String>> entryIterator = map.entrySet().iterator();
    while ( entryIterator.hasNext() ) {
      final Map.Entry<String, String> entry = entryIterator.next();
      final String key = entry.getKey();
      final String realKey = key.substring( meta.getPluginId().length() + 1 );
      final String value = entry.getValue();
      if ( DatabaseMeta.EMPTY_OPTIONS_STRING.equals( value ) ) {
        properties.put( realKey, "" );
      } else {
        properties.put( realKey, value );
      }
    }

    meta.getAttributes().keySet().stream()
      .map( key -> ( (String) key ) )
      .filter( key -> !key.startsWith( BaseDatabaseMeta.ATTRIBUTE_PREFIX_EXTRA_OPTION ) )
      .forEach( key -> properties.put( OTHER_PREFIX + key, meta.getAttributes().getProperty( key ) ) );


    return new DriverConnectionDefinition(
      meta.getName(),
      meta.getDriverClass(),
      meta.getURL(),
      meta.getUsername(),
      meta.getPassword(),
      meta.getHostname(),
      meta.getDatabaseName(),
      meta.getDatabaseInterface().getPluginId(),
      meta.getDatabasePortNumberString(),
      properties
    );
  }
}
 
Example 16
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;
  }