Commit 0d17cc34 authored by Matija Obreza's avatar Matija Obreza

Read Access databases (EURISCO)

parent 9dc860d8
......@@ -170,6 +170,16 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>com.healthmarketscience.jackcess</groupId>
<artifactId>jackcess</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -35,7 +35,16 @@ import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.*;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
......@@ -55,7 +64,19 @@ import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.wb.swt.SWTResourceManager;
import org.genesys2.anno.model.ColumnDef;
import org.genesys2.anno.model.Settings;
......@@ -508,6 +529,13 @@ public class AppWindow {
IDataSource currentSource = (IDataSource) ((TreeSelection) thisSelection).getPaths()[0].getFirstSegment();
if (selectedNode instanceof JdbcDataSource) {
// Open db props
DatabaseDialog databaseDialog = (DatabaseDialog) applicationContext.getBean("databaseDialog", shlGenesysMagic, treeViewer, SWT.NONE);
databaseDialog.setDatabaseSettings(((JdbcDataSource) selectedNode).getSettings());
databaseDialog.open();
}
if (selectedNode instanceof IDataSourceSheet) {
final IDataSourceSheet currentSheet = (IDataSourceSheet) selectedNode;
_log.debug("Doubleclick " + currentSheet);
......
......@@ -21,6 +21,7 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.eclipse.core.databinding.DataBindingContext;
......@@ -51,8 +52,8 @@ import org.genesys2.anno.model.JdbcDrivers;
import org.genesys2.anno.util.ConnectionUtils;
import org.genesys2.anno.util.DownloadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import swing2swt.layout.BorderLayout;
public class DatabaseDialog extends Dialog {
......@@ -62,8 +63,7 @@ public class DatabaseDialog extends Dialog {
@Value("${genesys.magic.workspace}")
private String workspacePath;
@Autowired
private DatabaseSettings databaseSettings;
private DatabaseSettings databaseSettings = new DatabaseSettings();
@Autowired
private DwcaBuilder builder;
......@@ -82,6 +82,7 @@ public class DatabaseDialog extends Dialog {
private Text txtDbUserName;
private Text txtDbPassword;
private Combo comboType;
private Text txtDbProperties;
/**
* Create the dialog.
......@@ -92,8 +93,13 @@ public class DatabaseDialog extends Dialog {
public DatabaseDialog(Shell parent, TreeViewer treeViewer, int style) {
super(parent, style);
this.treeViewer = treeViewer;
this.databaseSettings=new DatabaseSettings();
setText("Add Database");
}
public void setDatabaseSettings(DatabaseSettings databaseSettings) {
this.databaseSettings = databaseSettings;
}
/**
* Open the dialog.
......@@ -118,7 +124,7 @@ public class DatabaseDialog extends Dialog {
*/
private void createContents() {
shell = new Shell(getParent(), SWT.SHELL_TRIM);
shell.setSize(674, 281);
shell.setSize(673, 384);
shell.setText(getText());
shell.setLayout(new BorderLayout(0, 0));
......@@ -184,6 +190,16 @@ public class DatabaseDialog extends Dialog {
txtDbPassword = new Text(grpDbConfiguration, SWT.BORDER | SWT.PASSWORD);
txtDbPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblProperties = new Label(grpDbConfiguration, SWT.NONE);
lblProperties.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
lblProperties.setText("Additional JDBC properties");
lblProperties.setAlignment(SWT.RIGHT);
txtDbProperties = new Text(grpDbConfiguration, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
GridData gd_txtDbProperties = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
gd_txtDbProperties.heightHint = 60;
txtDbProperties.setLayoutData(gd_txtDbProperties);
Composite compositeFooter = new Composite(composite, SWT.NONE);
compositeFooter.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false, 1, 1));
......@@ -212,6 +228,7 @@ public class DatabaseDialog extends Dialog {
composite.pack();
scrolledComposite.setContent(composite);
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
m_bindingContext = initDataBindings();
}
......@@ -241,8 +258,14 @@ public class DatabaseDialog extends Dialog {
Connection connection = null;
String message = "N/A";
try {
connection = ConnectionUtils.getConnection(databaseSettings.getDriverClassName(), this.txtDbUrl.getText(), this.txtDbUserName.getText(), this.txtDbPassword.getText());
Properties props = new Properties();
props.put("user", this.txtDbUserName.getText());
props.put("password", this.txtDbPassword.getText());
props.put("charSet", "windows-1250");
try {
connection = ConnectionUtils.getConnection(databaseSettings.getDriverClassName(), this.txtDbUrl.getText(), props);
} catch (ClassNotFoundException e) {
message = "Could not load JDBC driver " + e.getMessage();
} catch (SQLException e) {
......@@ -276,11 +299,10 @@ public class DatabaseDialog extends Dialog {
this.shell.close();
ConnectionUtils.close(connection);
}
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue ovTextName = WidgetProperties.text(SWT.Modify).observe(this.txtName);
IObservableValue ovTextName = WidgetProperties.text(SWT.Modify).observe(txtName);
IObservableValue ovName = BeanProperties.value("dataSourceName").observe(databaseSettings);
bindingContext.bindValue(ovTextName, ovName, null, null);
//
......@@ -296,6 +318,10 @@ public class DatabaseDialog extends Dialog {
IObservableValue ovDbPassword = BeanProperties.value("password").observe(databaseSettings);
bindingContext.bindValue(ovTextDbPassword, ovDbPassword, null, null);
//
IObservableValue ovTextDbProperties = WidgetProperties.text(SWT.Modify).observe(txtDbProperties);
IObservableValue ovDbProperties = BeanProperties.value("properties").observe(databaseSettings);
bindingContext.bindValue(ovTextDbProperties, ovDbProperties, null, null);
//
return bindingContext;
}
}
......@@ -105,6 +105,7 @@ public class DwcaBuilder extends AbstractModelObject {
dataBaseSettings.put("user", ((JdbcDataSource) sourceFile).getSettings().getUser());
dataBaseSettings.put("password", ((JdbcDataSource) sourceFile).getSettings().getPassword());
dataBaseSettings.put("driver", ((JdbcDataSource) sourceFile).getSettings().getDriverClassName());
dataBaseSettings.put("properties", ((JdbcDataSource) sourceFile).getSettings().getProperties());
file.put("dataBaseSettings", dataBaseSettings);
file.put("name", sourceFile.getFileName());
......@@ -193,10 +194,14 @@ public class DwcaBuilder extends AbstractModelObject {
sourceFile = new JdbcDataSource(sourceJ.getString("name"));
JSONObject dataBaseSettingsJ = sourceJ.getJSONObject("dataBaseSettings");
DatabaseSettings databaseSettings = new DatabaseSettings();
databaseSettings.setDataSourceName(sourceFile.getFileName());
databaseSettings.setUrl(dataBaseSettingsJ.getString("url"));
databaseSettings.setUser(dataBaseSettingsJ.getString("user"));
databaseSettings.setPassword(dataBaseSettingsJ.getString("password"));
databaseSettings.setDriverClassName(dataBaseSettingsJ.getString("driver"));
if (dataBaseSettingsJ.has("properties")) {
databaseSettings.setProperties(dataBaseSettingsJ.getString("properties"));
}
((JdbcDataSource) sourceFile).setSettings(databaseSettings);
}
......
......@@ -20,11 +20,12 @@ import java.net.MalformedURLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.genesys2.anno.model.DatabaseSettings;
import org.genesys2.anno.model.JdbcDrivers;
import org.genesys2.anno.model.OAuthSettings;
import org.genesys2.anno.model.Settings;
......@@ -46,8 +47,6 @@ import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Main Spring configuration
*
......@@ -91,11 +90,6 @@ public class SpringConfig {
return new Settings();
}
@Bean
public DatabaseSettings getDatabaseSettings() {
return new DatabaseSettings();
}
@Bean
public DwcaBuilder dwcaBuilder() {
return new DwcaBuilder();
......
......@@ -15,6 +15,9 @@
*/
package org.genesys2.anno.model;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.anno.gui.AbstractModelObject;
public class DatabaseSettings extends AbstractModelObject {
......@@ -25,6 +28,7 @@ public class DatabaseSettings extends AbstractModelObject {
private String dataSourceName = "";
private String user = "";
private String password = "";
private String properties = "";
public String getUrl() {
return url;
......@@ -75,4 +79,23 @@ public class DatabaseSettings extends AbstractModelObject {
this.driverClassName = driverClassName;
firePropertyChange("driverClassName", null, this.driverClassName);
}
public String getProperties() {
return properties;
}
public void setProperties(String properties) {
this.properties = properties;
}
public Properties asProperties() {
Properties p = new Properties();
p.put("user", this.user);
p.put("password", this.password);
for (String propLine : StringUtils.split(this.properties, '\n')) {
String[] x = propLine.trim().split("=");
p.put(x[0].trim(), x[1].trim());
}
return p;
}
}
......@@ -24,15 +24,17 @@ public class JdbcDrivers extends AbstractModelObject {
private List<JdbcDriver> jdbcDrivers = new ArrayList<JdbcDriver>();
private String mySqlDownloadUrl = "http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar";
private String msSqlDownloadUrl = "http://download1058.mediafire.com/8x31owqyfiog/3ycmaelhoot/sqljdbc4.jar";
private String postgreSqlDownloadUrl = "http://central.maven.org/maven2/org/postgresql/postgresql/9.3-1102-jdbc41/postgresql-9.3-1102-jdbc41.jar";
private static final String mySqlDownloadUrl = "http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar";
private static final String msSqlDownloadUrl = "http://download1058.mediafire.com/8x31owqyfiog/3ycmaelhoot/sqljdbc4.jar";
private static final String postgreSqlDownloadUrl = "http://central.maven.org/maven2/org/postgresql/postgresql/9.3-1102-jdbc41/postgresql-9.3-1102-jdbc41.jar";
private static final String accessDownloadUrl = "http://repo1.maven.org/maven2/net/sf/ucanaccess/ucanaccess/3.0.6/ucanaccess-3.0.6.jar";
private String jdbcOdbcDownloadUrl = "";
{
jdbcDrivers.add(new JdbcDriver("MySQL", "com.mysql.jdbc.Driver", mySqlDownloadUrl, "jdbc:mysql://localhost:3306/DATABASE"));
jdbcDrivers.add(new JdbcDriver("MsSQL", "com.microsoft.sqlserver.jdbc.SQLServerDriver", msSqlDownloadUrl, "jdbc:sqlserver://localhost\\INSTANCE:1433;databaseName=DATABASE"));
jdbcDrivers.add(new JdbcDriver("PostgreSQL", "org.postgresql.Driver", postgreSqlDownloadUrl, "jdbc:postgresql://localhost:5432/DATABASE"));
jdbcDrivers.add(new JdbcDriver("MS Access", "net.ucanaccess.jdbc.UcanaccessDriver", accessDownloadUrl, "jdbc:ucanaccess://PATH-TO-ACCDB"));
jdbcDrivers.add(new JdbcDriver("JDBC ODBC", "sun.jdbc.odbc.JdbcOdbcDriver", jdbcOdbcDownloadUrl, "jdbc:odbc://JDBC-ODBC-CONNECTSTRING"));
}
......
......@@ -40,10 +40,8 @@ public class JdbcRowReader implements RowReader {
this.query = query;
String url = databaseSettings.getUrl();
String driver = databaseSettings.getDriverClassName();
String user = databaseSettings.getUser();
String password = databaseSettings.getPassword();
try {
this.conn = ConnectionUtils.getConnection(driver, url, user, password);
this.conn = ConnectionUtils.getConnection(driver, url, databaseSettings.asProperties());
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
......
......@@ -35,13 +35,11 @@ public class JDBCRowReader {
public List<Object[]> getRows(DatabaseSettings databaseSettings, String query, int limit) throws SQLException, ClassNotFoundException {
String url = databaseSettings.getUrl();
String user = databaseSettings.getUser();
String password = databaseSettings.getPassword();
String driverClassName = databaseSettings.getDriverClassName();
List<Object[]> rows = new ArrayList<Object[]>();
Connection conn = ConnectionUtils.getConnection(driverClassName, url, user, password);
Connection conn = ConnectionUtils.getConnection(driverClassName, url, databaseSettings.asProperties());
try {
PreparedStatement ps = conn.prepareStatement(query);
......
......@@ -38,10 +38,13 @@ public class ConnectionUtils {
_instance = this;
}
public static Connection getConnection(String driverClassName, String url, String user, String password) throws ClassNotFoundException, SQLException {
public static Connection getConnection(String driverClassName, String url, Properties props) throws ClassNotFoundException, SQLException {
Connection con = null;
for (Object key : props.keySet()) {
System.err.println("DB " + key + " = " + props.get(key));
}
try {
con = DriverManager.getConnection(url, user, password);
con = DriverManager.getConnection(url, props);
con.setReadOnly(true);
return con;
} catch (Throwable e) {
......@@ -58,7 +61,7 @@ public class ConnectionUtils {
DriverManager.registerDriver(new DriverShim(d));
System.out.println("Driver registered");
con = DriverManager.getConnection(url, user, password);
con = DriverManager.getConnection(url, props);
con.setReadOnly(true);
System.out.println("Got connection " + con);
......
......@@ -15,8 +15,6 @@
*/
package org.genesys2.anno.util;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
......@@ -24,10 +22,13 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
public class DownloadUtils {
private static final Logger _log = Logger.getLogger(DownloadUtils.class);
private static final int BUFFER_SIZE = 4096;
// private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDir) throws IOException {
URL url = new URL(fileURL);
......
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