Search in sources :

Example 1 with Database

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

the class JobEntryMSAccessBulkLoad method importFile.

private boolean importFile(String sourceFilename, String delimiter, String targetFilename, String tablename, Result result, Job parentJob) {
    boolean retval = false;
    try {
        incrFilesToProcess();
        File sourceDataFile = new File(sourceFilename);
        File targetDbFile = new File(targetFilename);
        // create database if needed
        if (!targetDbFile.exists()) {
            Database.create(targetDbFile);
            logBasic(BaseMessages.getString(PKG, "JobEntryMSAccessBulkLoad.Log.DbCreated", targetFilename));
        } else {
            // Database exists
            Database db = Database.open(targetDbFile);
            logBasic(BaseMessages.getString(PKG, "JobEntryMSAccessBulkLoad.Log.DbOpened", targetFilename));
            // Let's check table
            if (db.getTable(tablename) != null) {
                logBasic(BaseMessages.getString(PKG, "JobEntryMSAccessBulkLoad.Log.TableExists", tablename));
            }
            // close database
            if (db != null) {
                db.close();
            }
            logBasic(BaseMessages.getString(PKG, "JobEntryMSAccessBulkLoad.Log.DbCosed", targetFilename));
        }
        // load data from file
        Database.open(targetDbFile).importFile(tablename, sourceDataFile, delimiter);
        logBasic(BaseMessages.getString(PKG, "JobEntryMSAccessBulkLoad.Log.FileImported", sourceFilename, tablename, targetFilename));
        // add filename to result filename
        if (add_result_filenames) {
            addFileToResultFilenames(sourceFilename, result, parentJob);
        }
        retval = true;
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobEntryMSAccessBulkLoad.Error.LoadingDataToFile", sourceFilename, targetFilename, e.getMessage()));
    }
    if (retval) {
        incrSuccess();
    } else {
        incrErrors();
    }
    return retval;
}
Also used : Database(com.healthmarketscience.jackcess.Database) File(java.io.File) ResultFile(org.pentaho.di.core.ResultFile) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException)

Example 2 with Database

use of com.healthmarketscience.jackcess.Database in project tika by apache.

the class JackcessParser method parse.

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    TikaInputStream tis = TikaInputStream.get(stream);
    Database db = null;
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    String password = null;
    PasswordProvider passwordProvider = context.get(PasswordProvider.class);
    if (passwordProvider != null) {
        password = passwordProvider.getPassword(metadata);
    }
    try {
        if (password == null) {
            //do this to ensure encryption/wrong password exception vs. more generic
            //"need right codec" error message.
            db = new DatabaseBuilder(tis.getFile()).setCodecProvider(new CryptCodecProvider()).setReadOnly(true).open();
        } else {
            db = new DatabaseBuilder(tis.getFile()).setCodecProvider(new CryptCodecProvider(password)).setReadOnly(true).open();
        }
        //just in case
        db.setLinkResolver(IGNORE_LINK_RESOLVER);
        JackcessExtractor ex = new JackcessExtractor(metadata, context, locale);
        ex.parse(db, xhtml);
    } catch (IllegalStateException e) {
        if (e.getMessage() != null && e.getMessage().contains("Incorrect password")) {
            throw new EncryptedDocumentException(e);
        }
        throw e;
    } finally {
        if (db != null) {
            try {
                db.close();
            } catch (IOException e) {
            //swallow = silent close
            }
        }
    }
    xhtml.endDocument();
}
Also used : DatabaseBuilder(com.healthmarketscience.jackcess.DatabaseBuilder) CryptCodecProvider(com.healthmarketscience.jackcess.CryptCodecProvider) EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) Database(com.healthmarketscience.jackcess.Database) TikaInputStream(org.apache.tika.io.TikaInputStream) IOException(java.io.IOException) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) PasswordProvider(org.apache.tika.parser.PasswordProvider)

Example 3 with Database

use of com.healthmarketscience.jackcess.Database in project timbuctoo by HuygensING.

the class MdbLoader method loadData.

@Override
public void loadData(List<Tuple<String, File>> files, Importer importer) throws InvalidFileException, IOException {
    Database database = DatabaseBuilder.open(files.get(0).getRight());
    for (String tableName : database.getTableNames()) {
        importer.startCollection(tableName);
        Table table = database.getTable(tableName);
        List<? extends Column> columns = table.getColumns();
        for (int i = 0; i < columns.size(); i++) {
            importer.registerPropertyName(i, columns.get(i).getName());
        }
        for (Row row : table) {
            importer.startEntity();
            for (int colNum = 0; colNum < columns.size(); colNum++) {
                Object cellValue = row.get(columns.get(colNum).getName());
                if (cellValue == null) {
                    cellValue = "";
                }
                importer.setValue(colNum, "" + cellValue);
            }
            importer.finishEntity();
        }
        importer.finishCollection();
    }
}
Also used : Table(com.healthmarketscience.jackcess.Table) Database(com.healthmarketscience.jackcess.Database) Row(com.healthmarketscience.jackcess.Row)

Example 4 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 5 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)

Aggregations

Database (com.healthmarketscience.jackcess.Database)9 File (java.io.File)7 Table (com.healthmarketscience.jackcess.Table)5 KettleException (org.pentaho.di.core.exception.KettleException)5 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)3 DatabaseBuilder (com.healthmarketscience.jackcess.DatabaseBuilder)2 IOException (java.io.IOException)2 MessageBox (org.eclipse.swt.widgets.MessageBox)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 CryptCodecProvider (com.healthmarketscience.jackcess.CryptCodecProvider)1 Row (com.healthmarketscience.jackcess.Row)1 FileOutputStream (java.io.FileOutputStream)1 URI (java.net.URI)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1