Search in sources :

Example 6 with Database

use of com.healthmarketscience.jackcess.Database in project eol-globi-data by jhpoelen.

the class StudyImporterForADWTest method readMDB.

@Test
public void readMDB() throws URISyntaxException, IOException {
    URI uri = getClass().getResource("spire/econetvis.mdb").toURI();
    assertThat(uri, is(notNullValue()));
    Database db = DatabaseBuilder.open(new File(uri));
    assertThat(db.getFileFormat(), is(Database.FileFormat.V2000));
    String[] tableNames = new String[] { "attribute_types", "common_names", "entities", "habitats", "links", "localities", "metastudies", "part_mapping_new", "part_qualifiers", "studies", "study_habitat", "study_local", "taxon", "taxon_attributes" };
    Set<String> expectedSet = new HashSet<String>();
    Collections.addAll(expectedSet, tableNames);
    Set<String> actualTableNames = db.getTableNames();
    assertThat(actualTableNames.size(), is(not(0)));
    assertThat("expected tables names [" + Arrays.toString(tableNames) + "] to be present", CollectionUtils.subtract(expectedSet, actualTableNames).size(), is(0));
    Table studies = db.getTable("studies");
    for (Map<String, Object> study : studies) {
        assertNotNull(study.get("reference"));
    }
    List<String> expectedColumnNames = Arrays.asList("study_id", "entity1", "entity2", "link_strength", "link_type", "table_ref", "link_number");
    assertColumnNames(expectedColumnNames, db.getTable("links"));
    expectedColumnNames = Arrays.asList("id", "latinname", "commonname", "parent", "webinfo", "moreinfo", "numchildren", "pos", "classification", "pictures", "sounds", "specimens", "idx", "extinct", "rank");
    assertColumnNames(expectedColumnNames, db.getTable("taxon"));
    Table taxonTable = db.getTable("taxon");
    int numberOfTaxa = 0;
    while (taxonTable.getNextRow() != null) {
        numberOfTaxa++;
    }
    assertThat(numberOfTaxa, is(198301));
    Table links = db.getTable("links");
    int numberOfLinks = 0;
    while (links.getNextRow() != null) {
        numberOfLinks++;
    }
    assertThat(numberOfLinks, is(18189));
}
Also used : Table(com.healthmarketscience.jackcess.Table) Database(com.healthmarketscience.jackcess.Database) URI(java.net.URI) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with Database

use of com.healthmarketscience.jackcess.Database in project pentaho-kettle by pentaho.

the class AccessOutputDialog method getTableName.

private void getTableName() {
    AccessOutputMeta meta = new AccessOutputMeta();
    getInfo(meta);
    Database database = null;
    // New class: SelectTableDialog
    try {
        String realFilename = transMeta.environmentSubstitute(meta.getFilename());
        FileObject fileObject = KettleVFS.getFileObject(realFilename, transMeta);
        File file = FileUtils.toFile(fileObject.getURL());
        if (!file.exists() || !file.isFile()) {
            throw new KettleException(BaseMessages.getString(PKG, "AccessOutputMeta.Exception.FileDoesNotExist", realFilename));
        }
        database = Database.open(file);
        Set<String> set = database.getTableNames();
        String[] tablenames = set.toArray(new String[set.size()]);
        EnterSelectionDialog dialog = new EnterSelectionDialog(shell, tablenames, BaseMessages.getString(PKG, "AccessOutputDialog.Dialog.SelectATable.Title"), BaseMessages.getString(PKG, "AccessOutputDialog.Dialog.SelectATable.Message"));
        String tablename = dialog.open();
        if (tablename != null) {
            wTablename.setText(tablename);
        }
    } catch (Throwable e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "AccessOutputDialog.UnableToGetListOfTables.Title"), BaseMessages.getString(PKG, "AccessOutputDialog.UnableToGetListOfTables.Message"), e);
    } finally {
        // Don't forget to close the bugger.
        try {
            if (database != null) {
                database.close();
            }
        } catch (Exception e) {
        // Ignore close errors
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Database(com.healthmarketscience.jackcess.Database) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) AccessOutputMeta(org.pentaho.di.trans.steps.accessoutput.AccessOutputMeta) EnterSelectionDialog(org.pentaho.di.ui.core.dialog.EnterSelectionDialog) KettleException(org.pentaho.di.core.exception.KettleException)

Example 8 with Database

use of com.healthmarketscience.jackcess.Database in project pentaho-kettle by pentaho.

the class AccessOutputMeta method getRequiredFields.

public RowMetaInterface getRequiredFields(VariableSpace space) throws KettleException {
    String realFilename = space.environmentSubstitute(filename);
    File file = new File(realFilename);
    Database db = null;
    try {
        if (!file.exists() || !file.isFile()) {
            throw new KettleException(BaseMessages.getString(PKG, "AccessOutputMeta.Exception.FileDoesNotExist", realFilename));
        }
        // open the database and get the table
        db = Database.open(file);
        String realTablename = space.environmentSubstitute(tablename);
        Table table = db.getTable(realTablename);
        if (table == null) {
            throw new KettleException(BaseMessages.getString(PKG, "AccessOutputMeta.Exception.TableDoesNotExist", realTablename));
        }
        RowMetaInterface layout = getLayout(table);
        return layout;
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "AccessOutputMeta.Exception.ErrorGettingFields"), e);
    } finally {
        try {
            if (db != null) {
                db.close();
            }
        } catch (IOException e) {
            throw new KettleException(BaseMessages.getString(PKG, "AccessOutputMeta.Exception.ErrorClosingDatabase"), e);
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Table(com.healthmarketscience.jackcess.Table) Database(com.healthmarketscience.jackcess.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) IOException(java.io.IOException) File(java.io.File) KettleException(org.pentaho.di.core.exception.KettleException) SQLException(java.sql.SQLException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 9 with Database

use of com.healthmarketscience.jackcess.Database in project pentaho-kettle by pentaho.

the class AccessInputDialog method get.

private void get() {
    RowMetaInterface fields = new RowMeta();
    try {
        AccessInputMeta meta = new AccessInputMeta();
        getInfo(meta);
        // Check if a table name is specified
        if (!checkInputTableName(meta)) {
            return;
        }
        FileInputList inputList = meta.getFiles(transMeta);
        if (inputList.getFiles().size() > 0) {
            // Open the file (only first file)...
            Database d = Database.open(new File(AccessInputMeta.getFilename(inputList.getFile(0))), true);
            String realTableName = transMeta.environmentSubstitute(meta.getTableName());
            Table t = null;
            if (realTableName.startsWith(AccessInputMeta.PREFIX_SYSTEM)) {
                t = d.getSystemTable(realTableName);
            } else {
                t = d.getTable(realTableName);
            }
            // Get the list of columns
            List<Column> col = t.getColumns();
            int nr = col.size();
            for (int i = 0; i < nr; i++) {
                Column c = col.get(i);
                ValueMetaInterface field = AccessInputMeta.getValueMeta(c);
                if (field != null && fields.indexOfValue(field.getName()) < 0) {
                    fields.addValueMeta(field);
                }
            }
        }
    } catch (Exception e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), BaseMessages.getString(PKG, "AccessInputDialog.ErrorReadingFile.DialogMessage", e.toString()), e);
    }
    if (fields.size() > 0) {
        // Clear Fields Grid
        wFields.removeAll();
        for (int j = 0; j < fields.size(); j++) {
            ValueMetaInterface field = fields.getValueMeta(j);
            wFields.add(new String[] { field.getName(), field.getName(), field.getTypeDesc(), "", "-1", "", "", "", "", "none", "N" });
        }
        wFields.removeEmptyRows();
        wFields.setRowNums();
        wFields.optWidth(true);
    } else {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_WARNING);
        mb.setMessage(BaseMessages.getString(PKG, "AccessInputDialog.UnableToFindFields.DialogTitle"));
        mb.setText(BaseMessages.getString(PKG, "AccessInputDialog.UnableToFindFields.DialogMessage"));
        mb.open();
    }
}
Also used : Table(com.healthmarketscience.jackcess.Table) RowMeta(org.pentaho.di.core.row.RowMeta) AccessInputMeta(org.pentaho.di.trans.steps.accessinput.AccessInputMeta) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) KettleException(org.pentaho.di.core.exception.KettleException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) MessageBox(org.eclipse.swt.widgets.MessageBox) Column(com.healthmarketscience.jackcess.Column) Database(com.healthmarketscience.jackcess.Database) File(java.io.File) FileInputList(org.pentaho.di.core.fileinput.FileInputList)

Example 10 with Database

use of com.healthmarketscience.jackcess.Database in project pentaho-kettle by pentaho.

the class AccessInputDialog method getTableName.

private void getTableName() {
    Database accessDatabase = null;
    try {
        AccessInputMeta meta = new AccessInputMeta();
        getInfo(meta);
        FileInputList fileInputList = meta.getFiles(transMeta);
        if (fileInputList.nrOfFiles() > 0) {
            // Check the first file
            if (fileInputList.getFile(0).exists()) {
                // Open the file (only first file) in readOnly ...
                // 
                accessDatabase = Database.open(new File(AccessInputMeta.getFilename(fileInputList.getFile(0))), true);
                // Get user tables
                // 
                Set<String> settables = accessDatabase.getTableNames();
                // Get System tables
                settables.addAll(accessDatabase.getSystemTableNames());
                // Get system tables
                String[] tablenames = settables.toArray(new String[settables.size()]);
                Const.sortStrings(tablenames);
                EnterSelectionDialog dialog = new EnterSelectionDialog(shell, tablenames, BaseMessages.getString(PKG, "AccessInputDialog.Dialog.SelectATable.Title"), BaseMessages.getString(PKG, "AccessInputDialog.Dialog.SelectATable.Message"));
                String tablename = dialog.open();
                if (tablename != null) {
                    wTable.setText(tablename);
                }
            } else {
                // The file not exists !
                throw new KettleException(BaseMessages.getString(PKG, "AccessInputMeta.Exception.FileDoesNotExist", KettleVFS.getFilename(fileInputList.getFile(0))));
            }
        } else {
            // No file specified
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
            mb.setMessage(BaseMessages.getString(PKG, "AccessInputDialog.FilesMissing.DialogMessage"));
            mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
            mb.open();
        }
    } catch (Throwable e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "AccessInputDialog.UnableToGetListOfTables.Title"), BaseMessages.getString(PKG, "AccessInputDialog.UnableToGetListOfTables.Message"), e);
    } finally {
        // Don't forget to close the bugger.
        try {
            if (accessDatabase != null) {
                accessDatabase.close();
            }
        } catch (Exception e) {
        // Ignore close errors
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) AccessInputMeta(org.pentaho.di.trans.steps.accessinput.AccessInputMeta) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) KettleException(org.pentaho.di.core.exception.KettleException) MessageBox(org.eclipse.swt.widgets.MessageBox) Database(com.healthmarketscience.jackcess.Database) File(java.io.File) FileInputList(org.pentaho.di.core.fileinput.FileInputList) EnterSelectionDialog(org.pentaho.di.ui.core.dialog.EnterSelectionDialog)

Aggregations

Database (com.healthmarketscience.jackcess.Database)11 File (java.io.File)9 Table (com.healthmarketscience.jackcess.Table)7 KettleException (org.pentaho.di.core.exception.KettleException)5 DatabaseBuilder (com.healthmarketscience.jackcess.DatabaseBuilder)3 URI (java.net.URI)3 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 MessageBox (org.eclipse.swt.widgets.MessageBox)2 Term (org.eol.globi.domain.Term)2 TermImpl (org.eol.globi.domain.TermImpl)2 Test (org.junit.Test)2 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)2 FileInputList (org.pentaho.di.core.fileinput.FileInputList)2 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 AccessInputMeta (org.pentaho.di.trans.steps.accessinput.AccessInputMeta)2 EnterSelectionDialog (org.pentaho.di.ui.core.dialog.EnterSelectionDialog)2 Column (com.healthmarketscience.jackcess.Column)1