Commit 9f7eb593 authored by Matija Obreza's avatar Matija Obreza

Open Ms Access as a file source

parent f87b137c
...@@ -164,7 +164,7 @@ ...@@ -164,7 +164,7 @@
<dependency> <dependency>
<groupId>com.healthmarketscience.jackcess</groupId> <groupId>com.healthmarketscience.jackcess</groupId>
<artifactId>jackcess</artifactId> <artifactId>jackcess</artifactId>
<version>2.1.4</version> <version>2.1.6</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hsqldb</groupId> <groupId>org.hsqldb</groupId>
......
...@@ -316,13 +316,6 @@ public class AppWindow { ...@@ -316,13 +316,6 @@ public class AppWindow {
threadPool.shutdown(); threadPool.shutdown();
} }
public static boolean isMac() {
if (System.getProperty("os.name").equals("Mac OS X")) {
return true;
}
return false;
}
/** /**
* Create contents of the window. * Create contents of the window.
*/ */
...@@ -833,8 +826,8 @@ public class AppWindow { ...@@ -833,8 +826,8 @@ public class AppWindow {
public void addSourceFile() { public void addSourceFile() {
FileDialog fd = new FileDialog(shlGenesysMagic, SWT.MULTI); FileDialog fd = new FileDialog(shlGenesysMagic, SWT.MULTI);
fd.setFilterNames(new String[] { "Supported source files", "Excel files", "CSV files" }); fd.setFilterNames(new String[] { "Supported source files", "Excel files", "CSV files", "MS Access" });
fd.setFilterExtensions(new String[] { "*.xlsx;*.xls;*.csv;*.tab", "*.xlsx;*.xls", "*.csv;*.tab" }); fd.setFilterExtensions(new String[] { "*.xlsx;*.xls;*.csv;*.tab;*.accdb", "*.xlsx;*.xls", "*.csv;*.tab", "*.accdb" });
String response = fd.open(); String response = fd.open();
if (response != null) { if (response != null) {
......
...@@ -30,6 +30,7 @@ import org.genesys2.anno.model.JdbcDrivers; ...@@ -30,6 +30,7 @@ import org.genesys2.anno.model.JdbcDrivers;
import org.genesys2.anno.model.OAuthSettings; import org.genesys2.anno.model.OAuthSettings;
import org.genesys2.anno.model.Settings; import org.genesys2.anno.model.Settings;
import org.genesys2.anno.parser.CsvDataSourceParser; import org.genesys2.anno.parser.CsvDataSourceParser;
import org.genesys2.anno.parser.MsAccessDataSourceParser;
import org.genesys2.anno.parser.XlsxDataSourceParser; import org.genesys2.anno.parser.XlsxDataSourceParser;
import org.genesys2.anno.reader.JDBCRowReader; import org.genesys2.anno.reader.JDBCRowReader;
import org.genesys2.anno.util.ConnectionUtils; import org.genesys2.anno.util.ConnectionUtils;
...@@ -65,6 +66,7 @@ public class SpringConfig { ...@@ -65,6 +66,7 @@ public class SpringConfig {
DataSourceLoaderImpl dataSourceLoader = new DataSourceLoaderImpl(); DataSourceLoaderImpl dataSourceLoader = new DataSourceLoaderImpl();
dataSourceLoader.registerParser(new XlsxDataSourceParser()); dataSourceLoader.registerParser(new XlsxDataSourceParser());
dataSourceLoader.registerParser(new CsvDataSourceParser()); dataSourceLoader.registerParser(new CsvDataSourceParser());
dataSourceLoader.registerParser(new MsAccessDataSourceParser());
return dataSourceLoader; return dataSourceLoader;
} }
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.anno.parser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.genesys2.anno.gui.DataSourceSheet;
import org.genesys2.anno.gui.IDataSourceSheet;
import org.genesys2.anno.gui.UnsupportedDataFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
/**
* The MsAccess DataSourceParser.
*
* @author Matija Obreza
*/
public class MsAccessDataSourceParser extends BasicDataSourceParser {
private static final Logger _log = LoggerFactory.getLogger(MsAccessDataSourceParser.class);
private final String[] supportedExtensions = new String[] { ".accdb" };
@Override
public String[] getSupportedExtensions() {
return supportedExtensions;
}
@Override
public Collection<? extends DataSourceSheet> findSheets(File sourceFile) throws UnsupportedDataFormatException {
try {
try (Database db = DatabaseBuilder.open(sourceFile)) {
_log.debug("CHARSET: " + db.getCharset());
db.getTableNames().stream().forEach(tableName -> System.out.println("Table: " + tableName));
return db.getTableNames().stream().sorted().map(tableName -> {
DataSourceSheet sheetDataSource = new DataSourceSheet(sourceFile);
sheetDataSource.setSheetName(tableName);
_log.debug("Creating sheet for table {}", tableName);
return sheetDataSource;
}).collect(Collectors.toList());
}
} catch (Throwable e) {
throw new UnsupportedDataFormatException(e.getMessage(), e);
}
}
@Override
public List<Object[]> loadRows(IDataSourceSheet dataSourceSheet, int maxRows, int startAt) throws UnsupportedDataFormatException, FileNotFoundException, IOException {
File sourceFile = dataSourceSheet.getSourceFile();
String sheetName = dataSourceSheet.getSheetName();
_log.debug("Reading {} from {}", sheetName, sourceFile);
List<Object[]> rows = new ArrayList<Object[]>(maxRows);
try {
try (Database db = DatabaseBuilder.open(sourceFile)) {
_log.debug("CHARSET: " + db.getCharset());
Table table = db.getTable(sheetName);
rows.add(table.getColumns().stream().map(column -> column.getName()).toArray());
Cursor cursor = table.getDefaultCursor();
cursor.moveNextRows(startAt);
for (int pos = maxRows; pos > 0; pos--) {
Row r = cursor.getNextRow();
if (r != null) {
rows.add(r.values().toArray());
}
}
}
return rows;
} catch (Throwable e) {
throw new UnsupportedDataFormatException(e.getMessage(), e);
}
}
@Override
public RowReader createRowReader(IDataSourceSheet dataSourceSheet) throws UnsupportedDataFormatException, IOException {
File sourceFile = dataSourceSheet.getSourceFile();
String sheetName = dataSourceSheet.getSheetName();
return new MsAccessRowReader(sourceFile, sheetName);
}
}
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.anno.parser;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
/**
* The Ms Access RowReader.
*
* @author Matija Obreza
*/
public class MsAccessRowReader implements RowReader {
private static final Logger _log = LoggerFactory.getLogger(MsAccessRowReader.class);
private Database db;
private Table table;
private Cursor cursor;
/**
* Instantiates a new ms access row reader.
*
* @param sourceFile
* the source file
* @param sheetName
* the sheet name
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public MsAccessRowReader(File sourceFile, String sheetName) throws IOException {
this.db = DatabaseBuilder.open(sourceFile);
_log.debug("CHARSET: " + db.getCharset());
this.table = db.getTable(sheetName);
this.cursor = table.getDefaultCursor();
}
/// This is called in case data starts beyond 1st row
@Override
public void setSkipRows(int skipRows) {
_log.info("Skipping rows {}", skipRows);
cursor.reset();
// try {
// cursor.moveNextRows(skipRows);
// } catch (IOException e) {
// _log.error("Could not skip rows", e);
// }
}
@Override
public int getRowCount() {
return table.getRowCount();
}
@Override
public void close() throws IOException {
db.close();
}
@Override
public List<Object[]> readRows(int rowsToRead) throws IOException {
List<Object[]> rows = new ArrayList<Object[]>(rowsToRead);
int rowCount = 0;
while ((rowCount < rowsToRead) && cursor.moveToNextRow()) {
rowCount++;
Row r = cursor.getCurrentRow();
if (r != null) {
rows.add(r.values().toArray());
}
}
return rows;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment