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;
}
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();
}
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();
}
}
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
}
}
}
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);
}
}
}
Aggregations