Commit 2b05eba8 authored by igoshin's avatar igoshin
Browse files

#14051 enabled save datasource to workspace

parent 104e55aa
......@@ -60,6 +60,12 @@
<version>4.3</version>
<scope>provided</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>org.eclipse.swt.gtk.linux</groupId>-->
<!--<artifactId>x86_64</artifactId>-->
<!--<version>3.3.0-v3346</version>-->
<!--</dependency>-->
<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv</artifactId>
......@@ -169,7 +175,13 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -236,6 +236,7 @@ public class AppWindow {
}
_log.info("Using workspace: " + workspacePath);
System.setProperty("genesys.magic.workspace", (String) workspacePath);
System.setProperty("genesys.magic.workspace.json", workspacePath+"/workspace.json");
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AppWindow appWindow = (AppWindow) ctx.getBean("appWindow");
......@@ -584,7 +585,16 @@ public class AppWindow {
});
tltmPush.setText("Push");
tltmPush.setImage(pushDataIcon);
m_bindingContext = initDataBindings();
ToolItem tltmDatabase = new ToolItem(toolBar, SWT.NONE);
tltmDatabase.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
doDatabase();
}
});
tltmDatabase.setText("Database");
m_bindingContext = initDataBindings();
}
protected CTabItem createTab(CTabFolder tabFolder, DataSourceSheet currentSheet) {
......@@ -608,27 +618,35 @@ public class AppWindow {
}
private void doSaveWorkSpace() {
FileDialog dialog = new FileDialog(shlGenesysMagic, SWT.SAVE);
dialog.setFilterNames(new String[] { "JSON Format" });
dialog.setFilterExtensions(new String[] { "*.json" });
if (builder.getWorkspaceFilename() == null) {
dialog.setFileName("workspace.json");
} else {
File file = new File(builder.getWorkspaceFilename());
dialog.setFileName(file.getName());
dialog.setFilterPath(file.getParent());
}
String path = dialog.open();
if (path != null) {
try {
builder.saveWorkspace(path, settings);
} catch (JSONException e) {
_log.error(e.getMessage());
} catch (IOException e) {
_log.error(e.getMessage());
}
}
String workspacePath=System.getProperty("genesys.magic.workspace.json");
try {
builder.saveWorkspace(workspacePath, settings);
} catch (JSONException e) {
_log.error(e.getMessage());
} catch (IOException e) {
_log.error(e.getMessage());
}
// FileDialog dialog = new FileDialog(shlGenesysMagic, SWT.SAVE);
// dialog.setFilterNames(new String[] { "JSON Format" });
// dialog.setFilterExtensions(new String[] { "*.json" });
// if (builder.getWorkspaceFilename() == null) {
// dialog.setFileName("workspace.json");
// } else {
// File file = new File(builder.getWorkspaceFilename());
// dialog.setFileName(file.getName());
// dialog.setFilterPath(file.getParent());
// }
// String path = dialog.open();
//
// if (path != null) {
// try {
// builder.saveWorkspace(path, settings);
// } catch (JSONException e) {
// _log.error(e.getMessage());
// } catch (IOException e) {
// _log.error(e.getMessage());
// }
// }
}
private void doLoadWorkSpace() {
......@@ -667,6 +685,11 @@ public class AppWindow {
settingsDialog.open();
}
protected void doDatabase() {
DatabaseDialog databaseDialog = (DatabaseDialog) applicationContext.getBean("databaseDialog", shlGenesysMagic, SWT.NONE);
databaseDialog.open();
}
protected void doAutomap() {
final DataSourceSheet dss = getCurrentDataSourceSheet();
if (dss == null) {
......
package org.genesys2.anno.gui;
import org.apache.log4j.Logger;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;
import org.genesys2.anno.model.DatabaseSettings;
import org.genesys2.anno.util.ConnectionUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.beans.factory.annotation.Autowired;
import swing2swt.layout.BorderLayout;
import java.io.*;
import java.sql.Connection;
public class DatabaseDialog extends Dialog {
private static final Logger _log = Logger.getLogger(DatabaseDialog.class);
private DataBindingContext m_bindingContext;
@Autowired
private DatabaseSettings databaseSettings;
protected Object result;
protected Shell shell;
private Text txtDbHost;
private Text txtDbPort;
private Text txtDbName;
private Text txtDbUrl;
private Text txtDbUserName;
private Text txtDbPassword;
/**
* Create the dialog.
*
* @param parent
* @param style
*/
public DatabaseDialog(Shell parent, int style) {
super(parent, style);
setText("Add Database");
}
/**
* Open the dialog.
*
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return result;
}
/**
* Create contents of the dialog.
*/
private void createContents() {
shell = new Shell(getParent(), SWT.SHELL_TRIM);
shell.setSize(665, 445);
shell.setText(getText());
shell.setLayout(new BorderLayout(0, 0));
ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
Composite composite = new Composite(scrolledComposite, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
Group grpDbConfiguration = new Group(composite, SWT.NONE);
grpDbConfiguration.setLayout(new GridLayout(2, false));
grpDbConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
grpDbConfiguration.setText("Database configuration");
/*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));
lblDbUrl.setAlignment(SWT.RIGHT);
lblDbUrl.setText("Connection URL");
txtDbUrl = new Text(grpDbConfiguration, SWT.BORDER);
txtDbUrl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
/*User*/
Label lblDbUserName = new Label(grpDbConfiguration, SWT.NONE);
lblDbUserName.setAlignment(SWT.RIGHT);
lblDbUserName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblDbUserName.setText("User");
txtDbUserName = new Text(grpDbConfiguration, SWT.BORDER);
txtDbUserName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
/*Password*/
Label lblDbPassword = new Label(grpDbConfiguration, SWT.NONE);
lblDbPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblDbPassword.setText("Password");
lblDbPassword.setAlignment(SWT.RIGHT);
txtDbPassword = new Text(grpDbConfiguration, SWT.BORDER);
txtDbPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Composite compositeFooter = new Composite(composite, SWT.NONE);
GridData gdFooter = new GridData(SWT.CENTER, SWT.TOP, true, false, 1, 1);
compositeFooter.setLayoutData(gdFooter);
compositeFooter.setLayout(new RowLayout(SWT.HORIZONTAL));
Button btnConnect = new Button(compositeFooter, SWT.NONE);
btnConnect.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
doConnect();
}
});
btnConnect.setText("Connect");
composite.pack();
scrolledComposite.setContent(composite);
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
m_bindingContext = initDataBindings();
}
private void doConnect() {
Connection connection = ConnectionUtils.getConnection(this.txtDbUrl.getText(), this.txtDbUserName.getText(), this.txtDbPassword.getText());
MessageBox dialog = null;
if (connection == null) {
dialog = new MessageBox(shell, SWT.ICON_ERROR | SWT.CANCEL);
dialog.setText("Connection");
dialog.setMessage("Connection failed");
} else {
dialog = new MessageBox(shell, SWT.ICON_WORKING | SWT.OK | SWT.CANCEL);
dialog.setText("Connection");
dialog.setMessage("Connection successful!\nDo you want to add connection to workspace?");
}
int button = dialog.open();
if (button == SWT.OK) {
try {
saveDataSourceToWorkspace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
ConnectionUtils.close(connection);
}
}
private void saveDataSourceToWorkspace() throws JSONException, IOException {
String workspacePath = System.getProperty("genesys.magic.workspace.json");
File file = new File(workspacePath);
if (!file.exists()) {
file.createNewFile();
FileWriter fileWriter = new FileWriter(workspacePath);
fileWriter.write("{}");
fileWriter.flush();
fileWriter.close();
}
FileReader reader = new FileReader(workspacePath);
JSONTokener tokener = new JSONTokener(reader);
JSONObject databaseSettings = new JSONObject();
databaseSettings.put("host", this.databaseSettings.getHost());
databaseSettings.put("port", this.databaseSettings.getPort());
databaseSettings.put("name", this.databaseSettings.getName());
databaseSettings.put("url", this.databaseSettings.getUrl());
databaseSettings.put("user", this.databaseSettings.getUser());
databaseSettings.put("password", this.databaseSettings.getPassword());
JSONObject root = new JSONObject(tokener);
root.put("databaseSettings", databaseSettings);
FileWriter fileWriter = new FileWriter(workspacePath);
fileWriter.write(root.toString());
fileWriter.flush();
fileWriter.close();
}
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
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);
//
IObservableValue ovTextDbUser = WidgetProperties.text(SWT.Modify).observe(txtDbUserName);
IObservableValue ovDbUser = BeanProperties.value("user").observe(databaseSettings);
bindingContext.bindValue(ovTextDbUser, ovDbUser, null, null);
//
IObservableValue ovTextDbPassword = WidgetProperties.text(SWT.Modify).observe(txtDbPassword);
IObservableValue ovDbPassword = BeanProperties.value("password").observe(databaseSettings);
bindingContext.bindValue(ovTextDbPassword, ovDbPassword, null, null);
//
return bindingContext;
}
}
......@@ -24,17 +24,19 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.genesys2.anno.model.Column;
import org.genesys2.anno.model.ColumnDataType;
import org.genesys2.anno.model.OAuthSettings;
import org.genesys2.anno.model.Settings;
import org.genesys2.anno.model.*;
import org.genesys2.anno.parser.CsvDataSourceSheet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.beans.factory.annotation.Autowired;
public class DwcaBuilder extends AbstractModelObject {
@Autowired
private DatabaseSettings databaseSettings;
private List<DataSourceFile> fileRoots = new ArrayList<DataSourceFile>();
private String workspaceFilename = null;
......@@ -59,8 +61,18 @@ public class DwcaBuilder extends AbstractModelObject {
}
public void saveWorkspace(String filePath, Settings sourceSettings) throws JSONException, IOException {
JSONObject main = new JSONObject();
File workspaceFile = new File(filePath);
if (!workspaceFile.exists()) {
workspaceFile.createNewFile();
FileWriter fileWriter = new FileWriter(filePath);
fileWriter.write("{}");
fileWriter.flush();
fileWriter.close();
}
FileReader reader = new FileReader(filePath);
JSONTokener tokener = new JSONTokener(reader);
JSONObject main = new JSONObject(tokener);
JSONArray files = new JSONArray();
JSONObject oauthSettings = new JSONObject();
......@@ -148,85 +160,98 @@ public class DwcaBuilder extends AbstractModelObject {
JSONTokener tokener = new JSONTokener(reader);
JSONObject root = new JSONObject(tokener);
JSONArray files = root.getJSONArray("files");
for (int i = 0; i < files.length(); i++) {
JSONObject sourceJ = files.getJSONObject(i);
String path = sourceJ.getString("path");
File file = new File(path);
JSONArray sheets = sourceJ.getJSONArray("sheets");
DataSourceFile sourceFile = new DataSourceFile();
List<DataSourceSheet> sourceSheetList = new ArrayList<DataSourceSheet>();
for (int j = 0; j < sheets.length(); j++) {
JSONObject sheetJ = sheets.getJSONObject(j);
DataSourceSheet sourceSheet = null;
if (sheetJ.has("csv")) {
CsvDataSourceSheet csvSourceSheet = new CsvDataSourceSheet(file);
JSONObject csvJ = sheetJ.getJSONObject("csv");
csvSourceSheet.setCharset(csvJ.getString("charset"));
csvSourceSheet.setDelimiterChar(csvJ.getString("delimiterChar").charAt(0));
csvSourceSheet.setQuoteChar(csvJ.getString("quoteChar").charAt(0));
csvSourceSheet.setUnixEol(csvJ.getBoolean("unixEol"));
sourceSheet = csvSourceSheet;
} else {
sourceSheet = new DataSourceSheet(file);
}
sourceSheet.setSheetName(sheetJ.getString("name"));
JSONArray columns = sheetJ.getJSONArray("columns");
List<Column> columnList = new ArrayList<Column>();
for (int x = 0; x < columns.length(); x++) {
JSONObject column = columns.getJSONObject(x);
Column sourceColumn = new Column();
if (column.has("name"))
sourceColumn.setPreferredName(column.getString("name"));
if (column.has("rdfTerm"))
sourceColumn.setRdfTerm(column.getString("rdfTerm"));
if (column.has("description"))
sourceColumn.setDescription(column.getString("description"));
if (column.has("pattern"))
sourceColumn.setPattern(column.getString("pattern"));
if (column.has("separator"))
sourceColumn.setSeparator(column.getString("separator"));
if (column.has("groupPattern"))
sourceColumn.setGroupPattern(column.getString("groupPattern"));
if (column.has("dataType"))
sourceColumn.setDataType(ColumnDataType.valueOf(column.getString("dataType")));
if (column.has("multiple"))
sourceColumn.setMultiple(column.getBoolean("multiple"));
columnList.add(sourceColumn);
}
sourceSheet.setColumns(columnList);
sourceSheetList.add(sourceSheet);
}
sourceFile.setFile(file);
sourceFile.setSheets(sourceSheetList);
addDataSource(sourceFile);
}
JSONObject jsonSettings = root.getJSONObject("settings").getJSONObject("oauthSettings");
OAuthSettings oAuthSettings = new OAuthSettings();
oAuthSettings.setAccessToken(jsonSettings.getString("accessToken"));
oAuthSettings.setApiUrl(jsonSettings.getString("apiUrl"));
oAuthSettings.setAuthorizationEndpoint(jsonSettings.getString("authorizationEndpoint"));
oAuthSettings.setClientKey(jsonSettings.getString("clientKey"));
oAuthSettings.setClientSecret(jsonSettings.getString("clientSecret"));
oAuthSettings.setRefreshToken(jsonSettings.getString("refreshToken"));
oAuthSettings.setServerUrl(jsonSettings.getString("serverUrl"));
oAuthSettings.setTokenEndpoint(jsonSettings.getString("tokenEndpoint"));
settings.setOauthSettings(oAuthSettings);
if (root.has("files")) {
JSONArray files = root.getJSONArray("files");
for (int i = 0; i < files.length(); i++) {
JSONObject sourceJ = files.getJSONObject(i);
String path = sourceJ.getString("path");
File file = new File(path);
JSONArray sheets = sourceJ.getJSONArray("sheets");
DataSourceFile sourceFile = new DataSourceFile();
List<DataSourceSheet> sourceSheetList = new ArrayList<DataSourceSheet>();
for (int j = 0; j < sheets.length(); j++) {
JSONObject sheetJ = sheets.getJSONObject(j);
DataSourceSheet sourceSheet = null;
if (sheetJ.has("csv")) {
CsvDataSourceSheet csvSourceSheet = new CsvDataSourceSheet(file);
JSONObject csvJ = sheetJ.getJSONObject("csv");
csvSourceSheet.setCharset(csvJ.getString("charset"));
csvSourceSheet.setDelimiterChar(csvJ.getString("delimiterChar").charAt(0));
csvSourceSheet.setQuoteChar(csvJ.getString("quoteChar").charAt(0));
csvSourceSheet.setUnixEol(csvJ.getBoolean("unixEol"));
sourceSheet = csvSourceSheet;
} else {
sourceSheet = new DataSourceSheet(file);
}
sourceSheet.setSheetName(sheetJ.getString("name"));
JSONArray columns = sheetJ.getJSONArray("columns");
List<Column> columnList = new ArrayList<Column>();
for (int x = 0; x < columns.length(); x++) {
JSONObject column = columns.getJSONObject(x);
Column sourceColumn = new Column();
if (column.has("name"))
sourceColumn.setPreferredName(column.getString("name"));
if (column.has("rdfTerm"))
sourceColumn.setRdfTerm(column.getString("rdfTerm"));
if (column.has("description"))
sourceColumn.setDescription(column.getString("description"));
if (column.has("pattern"))
sourceColumn.setPattern(column.getString("pattern"));
if (column.has("separator"))
sourceColumn.setSeparator(column.getString("separator"));
if (column.has("groupPattern"))
sourceColumn.setGroupPattern(column.getString("groupPattern"));
if (column.has("dataType"))
sourceColumn.setDataType(ColumnDataType.valueOf(column.getString("dataType")));
if (column.has("multiple"))
sourceColumn.setMultiple(column.getBoolean("multiple"));
columnList.add(sourceColumn);
}
sourceSheet.setColumns(columnList);
sourceSheetList.add(sourceSheet);
}
sourceFile.setFile(file);
sourceFile.setSheets(sourceSheetList);
addDataSource(sourceFile);
}