Commit a8135f51 authored by igoshin's avatar igoshin

#14360 seems finished.

parent 0cb1db86
......@@ -35,15 +35,7 @@ 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.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.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
......@@ -507,7 +499,9 @@ public class AppWindow {
IStructuredSelection thisSelection = (IStructuredSelection) e.getSelection();
Object selectedNode = thisSelection.getFirstElement();
if (selectedNode instanceof IDataSourceSheet) {
IDataSource currentSource= (IDataSource) ((TreeSelection) thisSelection).getPaths()[0].getFirstSegment();
if (selectedNode instanceof IDataSourceSheet) {
final IDataSourceSheet currentSheet = (IDataSourceSheet) selectedNode;
_log.debug("Doubleclick " + currentSheet);
int i = 0;
......@@ -521,7 +515,7 @@ public class AppWindow {
i++;
}
}
CTabItem newSheetTab = createTab(tabFolder, currentSheet);
CTabItem newSheetTab = createTab(tabFolder,currentSource, currentSheet);
// Load stuff
if (selectedNode instanceof DataSourceSheet) {
......@@ -625,14 +619,14 @@ public class AppWindow {
m_bindingContext = initDataBindings();
}
protected CTabItem createTab(CTabFolder tabFolder, IDataSourceSheet currentSheet) {
protected CTabItem createTab(CTabFolder tabFolder, IDataSource currentSource, IDataSourceSheet currentSheet) {
CTabItem tbtmOther = new CTabItem(tabFolder, SWT.NONE);
tbtmOther.setShowClose(true);
tbtmOther.setText(currentSheet.getSheetName());
tbtmOther.setImage(spreadsheetIcon16);
final SheetDisplay sheetDisplay = (SheetDisplay) applicationContext.getBean("sheetDisplay", currentSheet, tabFolder, treeViewer, SWT.NONE);
final SheetDisplay sheetDisplay = (SheetDisplay) applicationContext.getBean("sheetDisplay",currentSource, currentSheet, tabFolder, treeViewer, SWT.NONE);
tbtmOther.setControl(sheetDisplay);
tbtmOther.addDisposeListener(new DisposeListener() {
......@@ -692,9 +686,12 @@ public class AppWindow {
if (dss == null) {
return;
}
final IDataSource dataSource = getCurrentDataSource();
PushDialog pushDialog = (PushDialog) applicationContext.getBean("pushDialog", shlGenesysMagic, SWT.NONE);
pushDialog.setDataSourceSheet(dss);
pushDialog.setDataSource(dataSource);
pushDialog.setColumnDefs(this.columnDefs);
pushDialog.open();
......@@ -736,9 +733,20 @@ public class AppWindow {
return dss;
}
public IDataSource getCurrentDataSource() {
CTabItem currentTab = tabFolder.getSelection();
if (currentTab == null) {
_log.info("No tab selected");
return null;
}
IStructuredSelection thisSelection = (IStructuredSelection) tabFolder.getSelection();
IDataSource currentSource= (IDataSource) ((TreeSelection) thisSelection).getPaths()[0].getFirstSegment();
return currentSource;
}
public void addJdbcQuery(JdbcDataSource dataSource) {
JdbcDataSourceSheet dataSourceSheet = new JdbcDataSourceSheet();
dataSourceSheet.setDatabaseSettings(dataSource.getSettings());
dataSource.addSheet(dataSourceSheet);
treeViewer.refresh();
}
......
......@@ -33,5 +33,5 @@ public interface DataSourceLoader {
List<Object[]> loadDataRows(DataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException, FileNotFoundException, IOException;
RowReader createRowReader(IDataSourceSheet sheet) throws IOException, UnsupportedDataFormatException;
RowReader createRowReader(IDataSourceSheet sheet,IDataSource dataSource) throws IOException, UnsupportedDataFormatException;
}
......@@ -114,17 +114,13 @@ public class DataSourceLoaderImpl implements DataSourceLoader {
}
@Override
public RowReader createRowReader(IDataSourceSheet sheet) throws IOException, UnsupportedDataFormatException {
public RowReader createRowReader(IDataSourceSheet sheet,IDataSource dataSource) throws IOException, UnsupportedDataFormatException {
// Iterate over parsers and see if something comes out
if (sheet instanceof JdbcDataSourceSheet) {
JdbcDataSourceSheet jdbcSheet = (JdbcDataSourceSheet) sheet;
DatabaseSettings databaseSettings = jdbcSheet.getDatabaseSettings();
String url = databaseSettings.getUrl();
String user = databaseSettings.getUser();
String password = databaseSettings.getPassword();
return jdbcRowReader.createRowReader(jdbcSheet.getQuery(), url, user, password);
DatabaseSettings databaseSettings = ((JdbcDataSource)dataSource).getSettings();
return jdbcRowReader.createRowReader(jdbcSheet.getQuery(),databaseSettings);
}
for (DataSourceParser parser : parsers) {
......
package org.genesys2.anno.gui;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.eclipse.core.databinding.DataBindingContext;
......@@ -27,21 +31,34 @@ import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.genesys2.anno.model.DatabaseSettings;
import org.genesys2.anno.model.JdbcDriver;
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 {
private static final Logger _log = Logger.getLogger(DatabaseDialog.class);
private DataBindingContext m_bindingContext;
@Value("${genesys.magic.workspace}")
private String workspacePath;
@Autowired
private DatabaseSettings databaseSettings;
@Autowired
private DwcaBuilder builder;
@Autowired
private JdbcDrivers drivers;
@Autowired
private ExtraClassLoader extraClassLoader;
protected Object result;
protected Shell shell;
private TreeViewer treeViewer;
......@@ -109,16 +126,21 @@ public class DatabaseDialog extends Dialog {
lblType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblType.setText("Datasource type");
comboType = new Combo(grpDbConfiguration, SWT.NONE);
comboType.setItems(new String[] { "MySQL", "MSSQL" });
comboType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
comboType.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
m_bindingContext = initDataBindings();
}
});
/* Datasource name */
comboType = new Combo(grpDbConfiguration, SWT.NONE);
comboType.setItems(getDriversNames(drivers.getJdbcDrivers()));
comboType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
comboType.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
List<JdbcDriver> jdbcDrivers=drivers.getJdbcDrivers();
JdbcDriver driver=jdbcDrivers.get(jdbcDrivers.indexOf(new JdbcDriver(comboType.getText())));
databaseSettings.setUrl(driver.getPredefinedConnectUrl());
databaseSettings.setDriverClassName(driver.getDriverClassName());
m_bindingContext = initDataBindings();
}
});
/* Datasource name */
Label lblName = new Label(grpDbConfiguration, SWT.NONE);
lblName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblName.setAlignment(SWT.RIGHT);
......@@ -126,30 +148,6 @@ public class DatabaseDialog extends Dialog {
txtName = new Text(grpDbConfiguration, SWT.BORDER);
txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
/* Host */
Label lblDbHost = new Label(grpDbConfiguration, SWT.NONE);
lblDbHost.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblDbHost.setAlignment(SWT.RIGHT);
lblDbHost.setText("Host");
txtDbHost = new Text(grpDbConfiguration, SWT.BORDER);
txtDbHost.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
/* Port */
Label lblDbPort = new Label(grpDbConfiguration, SWT.NONE);
lblDbPort.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblDbPort.setAlignment(SWT.RIGHT);
lblDbPort.setText("Port");
txtDbPort = new Text(grpDbConfiguration, SWT.BORDER);
txtDbPort.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
/* Database */
Label lblDbName = new Label(grpDbConfiguration, SWT.NONE);
lblDbName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblDbName.setAlignment(SWT.RIGHT);
lblDbName.setText("Database");
txtDbName = new Text(grpDbConfiguration, SWT.BORDER);
txtDbName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
/* Connection URL */
Label lblDbUrl = new Label(grpDbConfiguration, SWT.NONE);
lblDbUrl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
......@@ -180,11 +178,22 @@ public class DatabaseDialog extends Dialog {
compositeFooter.setLayoutData(gdFooter);
compositeFooter.setLayout(new RowLayout(SWT.HORIZONTAL));
Button btnDownloadFile = new Button(compositeFooter, SWT.NONE);
btnDownloadFile.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
List<JdbcDriver> jdbcDrivers=drivers.getJdbcDrivers();
JdbcDriver driver=jdbcDrivers.get(jdbcDrivers.indexOf(new JdbcDriver(comboType.getText())));
doDownload(driver);
}
});
btnDownloadFile.setText("Download driver");
Button btnConnect = new Button(compositeFooter, SWT.NONE);
btnConnect.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
doConnect();
doConnect();
}
});
btnConnect.setText("Connect");
......@@ -195,12 +204,32 @@ public class DatabaseDialog extends Dialog {
}
private String[] getDriversNames(List<JdbcDriver> drivers) {
String[] array = new String[drivers.size()];
List<String> titles = new ArrayList<String>();
for (JdbcDriver driver : drivers) {
titles.add(driver.getTitle());
}
return titles.toArray(array);
}
private void doDownload(JdbcDriver driver) {
String jdbcPath = workspacePath + "/jdbc";
try {
DownloadUtils.downloadFile(driver.getDownloadUrl(), jdbcPath);
extraClassLoader.addJars(new File(jdbcPath));
} catch (IOException e) {
_log.error(e.getMessage());
}
}
private void doConnect() {
Connection connection = null;
String message = "N/A";
try {
connection = ConnectionUtils.getConnection(this.txtDbUrl.getText(), this.txtDbUserName.getText(), this.txtDbPassword.getText());
connection = ConnectionUtils.getConnection(databaseSettings.getDriverClassName(),this.txtDbUrl.getText(), this.txtDbUserName.getText(), this.txtDbPassword.getText());
} catch (ClassNotFoundException e) {
message = "Could not load JDBC driver " + e.getMessage();
} catch (SQLException e) {
......@@ -242,18 +271,6 @@ public class DatabaseDialog extends Dialog {
IObservableValue ovName = BeanProperties.value("dataSourceName").observe(databaseSettings);
bindingContext.bindValue(ovTextName, ovName, null, null);
//
IObservableValue ovTextDbHost = WidgetProperties.text(SWT.Modify).observe(this.txtDbHost);
IObservableValue ovDbHost = BeanProperties.value("host").observe(databaseSettings);
bindingContext.bindValue(ovTextDbHost, ovDbHost, null, null);
//
IObservableValue ovTextDbPort = WidgetProperties.text(SWT.Modify).observe(txtDbPort);
IObservableValue ovDbPort = BeanProperties.value("port").observe(databaseSettings);
bindingContext.bindValue(ovTextDbPort, ovDbPort, null, null);
//
IObservableValue ovTextDbName = WidgetProperties.text(SWT.Modify).observe(txtDbName);
IObservableValue ovDbName = BeanProperties.value("name").observe(databaseSettings);
bindingContext.bindValue(ovTextDbName, ovDbName, null, null);
//
IObservableValue ovTextDbUrl = WidgetProperties.text(SWT.Modify).observe(txtDbUrl);
IObservableValue ovDbUrl = BeanProperties.value("url").observe(databaseSettings);
bindingContext.bindValue(ovTextDbUrl, ovDbUrl, null, null);
......
......@@ -104,6 +104,7 @@ public class DwcaBuilder extends AbstractModelObject {
dataBaseSettings.put("url", ((JdbcDataSource) sourceFile).getSettings().getUrl());
dataBaseSettings.put("user", ((JdbcDataSource) sourceFile).getSettings().getUser());
dataBaseSettings.put("password", ((JdbcDataSource) sourceFile).getSettings().getPassword());
dataBaseSettings.put("driver", ((JdbcDataSource) sourceFile).getSettings().getDriverClassName());
file.put("dataBaseSettings", dataBaseSettings);
file.put("name", sourceFile.getFileName());
......@@ -133,14 +134,7 @@ public class DwcaBuilder extends AbstractModelObject {
sheet.put("csv", csv);
}
if (sourceSheet instanceof JdbcDataSourceSheet) {
JSONObject databaseSettings = new JSONObject();
databaseSettings.put("url", ((JdbcDataSourceSheet) sourceSheet).getDatabaseSettings().getUrl());
databaseSettings.put("user", ((JdbcDataSourceSheet) sourceSheet).getDatabaseSettings().getUser());
databaseSettings.put("password", ((JdbcDataSourceSheet) sourceSheet).getDatabaseSettings().getPassword());
sheet.put("query", sourceSheet.getQuery());
sheet.put("dataBaseSettings", databaseSettings);
}
sheets.put(sheet);
......@@ -202,6 +196,7 @@ public class DwcaBuilder extends AbstractModelObject {
databaseSettings.setUrl(dataBaseSettingsJ.getString("url"));
databaseSettings.setUser(dataBaseSettingsJ.getString("user"));
databaseSettings.setPassword(dataBaseSettingsJ.getString("password"));
databaseSettings.setDriverClassName(dataBaseSettingsJ.getString("driver"));
((JdbcDataSource) sourceFile).setSettings(databaseSettings);
}
......@@ -227,14 +222,7 @@ public class DwcaBuilder extends AbstractModelObject {
if (sheetJ.has("name")) {
sourceSheet.setSheetName(sheetJ.getString("name"));
}
if (sheetJ.has("dataBaseSettings")) {
JSONObject dataBaseSettingsJ = sheetJ.getJSONObject("dataBaseSettings");
DatabaseSettings databaseSettings = new DatabaseSettings();
databaseSettings.setUrl(dataBaseSettingsJ.getString("url"));
databaseSettings.setUser(dataBaseSettingsJ.getString("user"));
databaseSettings.setPassword(dataBaseSettingsJ.getString("password"));
((JdbcDataSourceSheet) sourceSheet).setDatabaseSettings(databaseSettings);
}
} else {
sourceSheet = new DataSourceSheet(file);
}
......@@ -246,23 +234,15 @@ public class DwcaBuilder extends AbstractModelObject {
List<Object[]> rows=null;
try {
rows = dataSourceLoader.loadRows(sourceSheet, 200);
sourceSheet.updateData(rows);
} catch (NullPointerException e){
//ignore
} catch (UnsupportedDataFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sourceSheet.updateData(rows);
List<Column> loadedColumns = sourceSheet.getColumns();
List<Object[]> rows=null;
try {
rows = dataSourceLoader.loadRows(sourceSheet, 200);
} catch (UnsupportedDataFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sourceSheet.updateData(rows);
List<Column> loadedColumns = sourceSheet.getColumns();
for (int x = 0; x < columns.length(); x++) {
......
......@@ -15,19 +15,9 @@ public class JdbcDataSourceSheet extends AbstractModelObject implements IDataSou
private boolean headersIncluded = true;
private int headerRowIndex = 0;
private String query;
private DatabaseSettings databaseSettings;
private List<Object[]> sampleData;
private List<Column> columns = new ArrayList<Column>();
public DatabaseSettings getDatabaseSettings() {
return databaseSettings;
}
public void setDatabaseSettings(DatabaseSettings databaseSettings) {
this.databaseSettings = databaseSettings;
firePropertyChange("databaseSettings", null, databaseSettings);
}
@Override
public String getQuery() {
return query;
......
......@@ -62,8 +62,9 @@ public class PushDialog extends Dialog {
private IDataSourceSheet dataSourceSheet;
private ColumnDefs columnDefs;
private SwtLogAppender logAppender;
private IDataSource dataSource;
/**
/**
* Create the dialog.
*
* @param parent
......@@ -187,7 +188,7 @@ public class PushDialog extends Dialog {
protected void doTrialRun() {
_log.info("Decoding data...");
try {
final RowReader rowReader = dataSourceLoader.createRowReader(dataSourceSheet);
final RowReader rowReader = dataSourceLoader.createRowReader(dataSourceSheet,dataSource);
rowReader.setSkipRows(dataSourceSheet.getHeaderRowIndex() + 1);
executorService.execute(new Runnable() {
......@@ -255,7 +256,7 @@ public class PushDialog extends Dialog {
// Ping the server
genesysClient.me();
final RowReader rowReader = dataSourceLoader.createRowReader(dataSourceSheet);
final RowReader rowReader = dataSourceLoader.createRowReader(dataSourceSheet,dataSource);
rowReader.setSkipRows(dataSourceSheet.getHeaderRowIndex() + 1);
executorService.execute(new Runnable() {
......@@ -396,4 +397,7 @@ public class PushDialog extends Dialog {
});
}
public void setDataSource(IDataSource dataSource) {
this.dataSource = dataSource;
}
}
......@@ -160,6 +160,7 @@ public class SheetDisplay extends Composite {
private Text txtGroupPattern;
private Text txtQueryName;
private TreeViewer treeViewer;
private IDataSource dataSource;
/**
* Create the composite.
......@@ -169,10 +170,11 @@ public class SheetDisplay extends Composite {
* @param parent
* @param style
*/
public SheetDisplay(final IDataSourceSheet dataSourceSheet, final Composite parent, final TreeViewer treeViewer, int style) {
public SheetDisplay(IDataSource dataSource,final IDataSourceSheet dataSourceSheet, final Composite parent, final TreeViewer treeViewer, int style) {
super(parent, SWT.NONE);
this.dsw.setDataSourceSheet(dataSourceSheet);
this.treeViewer = treeViewer;
this.dataSource=dataSource;
addDisposeListener(new DisposeListener() {
@Override
......@@ -512,25 +514,22 @@ public class SheetDisplay extends Composite {
});
}
private void executeSqlQuery(String query, DSW dsw) {
IDataSourceSheet currentSheet = dsw.getDataSourceSheet();
DatabaseSettings databaseSettings = ((JdbcDataSourceSheet) currentSheet).getDatabaseSettings();
String url = databaseSettings.getUrl();
String user = databaseSettings.getUser();
String password = databaseSettings.getPassword();
final List<Object[]> rows;
try {
rows = jdbcRowReader.getRows(query, url, user, password, 300);
if (rows != null) {
currentSheet.updateData(rows);
}
} catch (Throwable e) {
showMessageBox("SQL error", e);
_log.error(e.getMessage());
}
}
private void executeSqlQuery(String query, DSW dsw) {
IDataSourceSheet currentSheet = dsw.getDataSourceSheet();
DatabaseSettings databaseSettings = ((JdbcDataSource)dataSource).getSettings();
final List<Object[]> rows;
try {
rows = jdbcRowReader.getRows(databaseSettings, query, 300);
if (rows != null) {
currentSheet.updateData(rows);
}
} catch (Throwable e) {
showMessageBox("SQL error", e);
_log.error(e.getMessage());
}
}
protected void doDoubleClickOnRow(Object[] row) {
IDataSourceSheet dataSourceSheet = getDataSourceSheet();
......
......@@ -10,6 +10,7 @@ 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;
import org.genesys2.anno.parser.CsvDataSourceParser;
......@@ -50,6 +51,11 @@ public class SpringConfig {
return dataSourceLoader;
}
@Bean
public JdbcDrivers jdbcDrivers() {
return new JdbcDrivers();
}
@Bean
public ExecutorService getExecutorService() {
return Executors.newFixedThreadPool(5);
......@@ -85,8 +91,8 @@ public class SpringConfig {
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public SheetDisplay sheetDisplay(IDataSourceSheet dataSourceSheet, Composite parent, TreeViewer treeViewer, int style) {
return new SheetDisplay(dataSourceSheet, parent, treeViewer, style);
public SheetDisplay sheetDisplay(IDataSource dataSource,IDataSourceSheet dataSourceSheet, Composite parent, TreeViewer treeViewer, int style) {
return new SheetDisplay(dataSource,dataSourceSheet, parent, treeViewer, style);
}
@Bean
......
......@@ -4,48 +4,13 @@ import org.genesys2.anno.gui.AbstractModelObject;
public class DatabaseSettings extends AbstractModelObject {
private String driverClassName="";
private String type = "";
private String host = "localhost";
private String port = "3306";
private String name = "";
private String url = "jdbc:mysql://" + host + ":" + port + "/" + name;
private String url = "";
private String dataSourceName = "";
private String user = "";
private String password = "";
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
String url = "jdbc:mysql://" + host + ":" + port + "/" + name;
setUrl(url);
firePropertyChange("host", null, this.host);
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
String url = "jdbc:mysql://" + host + ":" + port + "/" + name;
setUrl(url);
firePropertyChange("port", null, this.port);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
String url = "jdbc:mysql://" + host + ":" + port + "/" + name;
setUrl(url);
firePropertyChange("name", null, this.name);
}
public String getUrl() {
return url;
}
......@@ -86,4 +51,13 @@ public class DatabaseSettings extends AbstractModelObject {
public void setType(String type) {
this.type = type;
}