Commit 69e6fc23 authored by igoshin's avatar igoshin

#14051 correct save to workspace.json, adding new sql query

parent 2b05eba8
/target
/bin
/.idea
\ No newline at end of file
......@@ -19,6 +19,7 @@ package org.genesys2.anno.gui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
......@@ -53,14 +54,7 @@ import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
......@@ -125,7 +119,7 @@ public class AppWindow {
@Override
public String getText(Object element) {
_log.debug("getText " + element);
// _log.debug("getText " + element);
if (element instanceof DataSourceFile) {
return ((DataSourceFile) element).getFile().getName();
}
......@@ -170,9 +164,15 @@ public class AppWindow {
if (parentElement instanceof DataSourceFile) {
return ((DataSourceFile) parentElement).getSheets().toArray();
}
if (parentElement instanceof JdbcDataSource) {
return ((JdbcDataSource) parentElement).getSheets().toArray();
}
if (parentElement instanceof DataSourceSheet) {
return null;
}
if (parentElement instanceof JdbcDataSourceSheet) {
return null;
}
return new Object[] { "Item 0", "Item 1", "Item 2" };
}
......@@ -183,7 +183,7 @@ public class AppWindow {
@Override
public boolean hasChildren(Object element) {
_log.debug("hasChildren " + element + " " + element.getClass());
// _log.debug("hasChildren " + element + " " + element.getClass());
if (element instanceof DwcaBuilder) {
return ((DwcaBuilder) element).getFileRoots().size() > 0;
......@@ -191,10 +191,15 @@ public class AppWindow {
if (element instanceof DataSourceFile) {
return ((DataSourceFile) element).getSheets().size() > 0;
}
if (element instanceof JdbcDataSource) {
return ((JdbcDataSource) element).getSheets().size() > 0;
}
if (element instanceof DataSourceSheet) {
return false;
}
if (element instanceof JdbcDataSourceSheet) {
return false;
}
return getChildren(element).length > 0;
}
......@@ -236,7 +241,6 @@ 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");
......@@ -378,14 +382,32 @@ public class AppWindow {
treeViewer.setContentProvider(new TreeContentProvider());
treeViewer.setInput(builder);
MenuItem mntmAddFile = new MenuItem(filesTreeMenu, SWT.NONE);
mntmAddFile.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
addSourceFile();
}
});
mntmAddFile.setText("Add file...");
final MenuItem mntmAddFile = new MenuItem(filesTreeMenu, SWT.NONE);
mntmAddFile.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
Object selectedObject = ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
if (selectedObject instanceof JdbcDataSource) {
addJdbcQuery((JdbcDataSource)selectedObject);
}
if (selectedObject instanceof DataSourceFile) {
addSourceFile();
}
}
});
filesTreeMenu.addMenuListener(new MenuAdapter() {
@Override
public void menuShown(MenuEvent e) {
Object selectedObject = ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
if (selectedObject instanceof DataSourceFile) {
mntmAddFile.setText("Add file");
}
if (selectedObject instanceof JdbcDataSource) {
mntmAddFile.setText("Add SQL Query");
}
}
});
final MenuItem mntmRemoveFile = new MenuItem(filesTreeMenu, SWT.NONE);
mntmRemoveFile.addSelectionListener(new SelectionAdapter() {
......@@ -516,7 +538,9 @@ public class AppWindow {
});
tabFolder.setSelection(newSheetTab);
}
}else if(selectedNode instanceof JdbcDataSourceSheet){
System.out.println("jdbc");
}
}
});
......@@ -593,7 +617,7 @@ public class AppWindow {
doDatabase();
}
});
tltmDatabase.setText("Database");
tltmDatabase.setText("Add database");
m_bindingContext = initDataBindings();
}
......@@ -618,35 +642,27 @@ public class AppWindow {
}
private void doSaveWorkSpace() {
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());
// }
// }
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() {
......@@ -686,7 +702,7 @@ public class AppWindow {
}
protected void doDatabase() {
DatabaseDialog databaseDialog = (DatabaseDialog) applicationContext.getBean("databaseDialog", shlGenesysMagic, SWT.NONE);
DatabaseDialog databaseDialog = (DatabaseDialog) applicationContext.getBean("databaseDialog",shlGenesysMagic,treeViewer, SWT.NONE);
databaseDialog.open();
}
......@@ -716,6 +732,11 @@ public class AppWindow {
return dss;
}
public void addJdbcQuery(JdbcDataSource dataSource) {
dataSource.addSheet(new JdbcDataSourceSheet());
treeViewer.refresh();
}
public void addSourceFile() {
FileDialog fd = new FileDialog(shlGenesysMagic, SWT.MULTI);
fd.setFilterNames(new String[] { "Supported source files", "Excel files", "CSV files" });
......
......@@ -20,28 +20,33 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class DataSourceFile extends AbstractModelObject {
public class DataSourceFile extends AbstractModelObject implements IDataSource {
private File file;
private List<DataSourceSheet> sheets = new ArrayList<DataSourceSheet>();
private List<IDataSourceSheet> sheets = new ArrayList<IDataSourceSheet>();
@Override
public File getFile() {
return file;
}
@Override
public void setFile(File file) {
this.file = file;
firePropertyChange("fileName", null, file);
}
@Override
public String getFileName() {
return this.file==null ? null : this.file.getName();
}
public List<DataSourceSheet> getSheets() {
@Override
public List<IDataSourceSheet> getSheets() {
return sheets;
}
public void setSheets(List<DataSourceSheet> sheets) {
@Override
public void setSheets(List<IDataSourceSheet> sheets) {
this.sheets = sheets;
firePropertyChange("sheets", null, sheets);
}
......
/**
* Copyright 2014 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.
......@@ -27,7 +27,7 @@ import org.genesys2.anno.model.Column;
import org.genesys2.anno.model.ColumnDef;
import org.genesys2.anno.parser.CsvDataSourceSheet;
public class DataSourceSheet extends AbstractModelObject {
public class DataSourceSheet extends AbstractModelObject implements IDataSourceSheet {
private static final Logger _log = Logger.getLogger(DataSourceSheet.class);
private File sourceFile;
......@@ -47,6 +47,7 @@ public class DataSourceSheet extends AbstractModelObject {
return sourceFile;
}
@Override
public String getSheetName() {
return sheetName;
}
......@@ -79,11 +80,11 @@ public class DataSourceSheet extends AbstractModelObject {
this.headersIncluded = headersIncluded;
firePropertyChange("headersIncluded", null, headersIncluded);
}
@Override
public boolean isHeadersIncluded() {
return headersIncluded;
}
@Override
public int getHeaderRowIndex() {
return headerRowIndex;
}
......@@ -150,6 +151,7 @@ public class DataSourceSheet extends AbstractModelObject {
return sampleData;
}
@Override
public List<Column> getColumns() {
return this.columns;
}
......
......@@ -6,6 +6,8 @@ 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.jface.internal.databinding.provisional.viewers.ViewerLabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
......@@ -32,8 +34,13 @@ public class DatabaseDialog extends Dialog {
@Autowired
private DatabaseSettings databaseSettings;
@Autowired
private DwcaBuilder builder;
protected Object result;
protected Shell shell;
private TreeViewer treeViewer;
private Text txtName;
private Text txtDbHost;
private Text txtDbPort;
private Text txtDbName;
......@@ -47,8 +54,9 @@ public class DatabaseDialog extends Dialog {
* @param parent
* @param style
*/
public DatabaseDialog(Shell parent, int style) {
public DatabaseDialog(Shell parent, TreeViewer treeViewer, int style) {
super(parent, style);
this.treeViewer = treeViewer;
setText("Add Database");
}
......@@ -91,7 +99,14 @@ public class DatabaseDialog extends Dialog {
grpDbConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
grpDbConfiguration.setText("Database configuration");
/*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);
lblName.setText("Datasource name");
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));
......@@ -182,54 +197,20 @@ public class DatabaseDialog extends Dialog {
int button = dialog.open();
if (button == SWT.OK) {
try {
saveDataSourceToWorkspace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
builder.addDataSource(new JdbcDataSource(txtName.getText()));
treeViewer.refresh();
this.shell.close();
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 ovTextName = WidgetProperties.text(SWT.Modify).observe(this.txtName);
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);
......
......@@ -34,28 +34,25 @@ import org.springframework.beans.factory.annotation.Autowired;
public class DwcaBuilder extends AbstractModelObject {
@Autowired
private DatabaseSettings databaseSettings;
private List<DataSourceFile> fileRoots = new ArrayList<DataSourceFile>();
private List<IDataSource> fileRoots = new ArrayList<IDataSource>();
private String workspaceFilename = null;
public List<DataSourceFile> getFileRoots() {
public List<IDataSource> getFileRoots() {
return fileRoots;
}
public String getWorkspaceFilename() {
return workspaceFilename;
}
public String getWorkspaceFilename() {
return workspaceFilename;
}
public void addDataSource(DataSourceFile dataSource) {
public void addDataSource(IDataSource dataSource) {
if (dataSource == null)
return;
fileRoots.add(dataSource);
firePropertyChange("fileRoots", null, this);
}
public void removeDataSource(DataSourceFile dataSource) {
public void removeDataSource(IDataSource dataSource) {
fileRoots.remove(dataSource);
firePropertyChange("fileRoots", null, this);
}
......@@ -88,18 +85,21 @@ public class DwcaBuilder extends AbstractModelObject {
JSONObject settings = new JSONObject();
settings.put("oauthSettings", oauthSettings);
for (DataSourceFile sourceFile : getFileRoots()) {
String path = sourceFile.getFile().getPath();
for (IDataSource sourceFile : getFileRoots()) {
JSONObject file = new JSONObject();
JSONArray sheets = new JSONArray();
JSONObject file = new JSONObject();
JSONArray sheets = new JSONArray();
if (sourceFile instanceof DataSourceFile){
String path = sourceFile.getFile().getPath();
file.put("path", path);
}else if (sourceFile instanceof JdbcDataSource){
file.put("name",sourceFile.getFileName());
}
file.put("path", path);
file.put("sheets", sheets);
files.put(file);
for (DataSourceSheet sourceSheet : sourceFile.getSheets()) {
for (IDataSourceSheet sourceSheet : sourceFile.getSheets()) {
JSONObject sheet = new JSONObject();
JSONArray columns = new JSONArray();
......@@ -165,18 +165,23 @@ public class DwcaBuilder extends AbstractModelObject {
for (int i = 0; i < files.length(); i++) {
JSONObject sourceJ = files.getJSONObject(i);
String path = sourceJ.getString("path");
File file = new File(path);
IDataSource sourceFile = null;
File file = null;
if (sourceJ.has("path")) {
String path = sourceJ.getString("path");
file = new File(path);
sourceFile=new DataSourceFile();
sourceFile.setFile(file);
}else {
sourceFile =new JdbcDataSource(sourceJ.getString("name"));
}
JSONArray sheets = sourceJ.getJSONArray("sheets");
DataSourceFile sourceFile = new DataSourceFile();
List<DataSourceSheet> sourceSheetList = new ArrayList<DataSourceSheet>();
List<IDataSourceSheet> sourceSheetList = new ArrayList<IDataSourceSheet>();
for (int j = 0; j < sheets.length(); j++) {
JSONObject sheetJ = sheets.getJSONObject(j);
DataSourceSheet sourceSheet = null;
IDataSourceSheet sourceSheet = null;
if (sheetJ.has("csv")) {
CsvDataSourceSheet csvSourceSheet = new CsvDataSourceSheet(file);
JSONObject csvJ = sheetJ.getJSONObject("csv");
......@@ -185,7 +190,9 @@ public class DwcaBuilder extends AbstractModelObject {
csvSourceSheet.setQuoteChar(csvJ.getString("quoteChar").charAt(0));
csvSourceSheet.setUnixEol(csvJ.getBoolean("unixEol"));
sourceSheet = csvSourceSheet;
} else {
} else if (!sourceJ.has("path")){
sourceSheet=new JdbcDataSourceSheet();
}else {
sourceSheet = new DataSourceSheet(file);
}
sourceSheet.setSheetName(sheetJ.getString("name"));
......@@ -221,7 +228,6 @@ public class DwcaBuilder extends AbstractModelObject {
sourceSheetList.add(sourceSheet);
}
sourceFile.setFile(file);
sourceFile.setSheets(sourceSheetList);
addDataSource(sourceFile);
......@@ -243,16 +249,6 @@ public class DwcaBuilder extends AbstractModelObject {
settings.setOauthSettings(oAuthSettings);
}
if(root.has("databaseSettings")){
JSONObject jsonDatabaseSettings=root.getJSONObject("databaseSettings");
databaseSettings.setHost(jsonDatabaseSettings.getString("host"));
databaseSettings.setPort(jsonDatabaseSettings.getString("port"));
databaseSettings.setName(jsonDatabaseSettings.getString("name"));
databaseSettings.setUrl(jsonDatabaseSettings.getString("url"));
databaseSettings.setUser(jsonDatabaseSettings.getString("user"));
databaseSettings.setPassword(jsonDatabaseSettings.getString("password"));
}
this.workspaceFilename = filePath;
}
......
package org.genesys2.anno.gui;
import java.io.File;
import java.util.List;
public interface IDataSource {
public File getFile();
public void setFile(File file);
public String getFileName();
public List<IDataSourceSheet> getSheets();
void setSheets(List<IDataSourceSheet> sheets);
}
package org.genesys2.anno.gui;
import org.genesys2.anno.model.Column;
import java.util.List;
public interface IDataSourceSheet {
public void setSheetName(String sheetName);
public String getSheetName();
public boolean isHeadersIncluded();
public int getHeaderRowIndex();
public List<Column> getColumns();
public void setColumns(List<Column> columns);
}
package org.genesys2.anno.gui;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class JdbcDataSource extends AbstractModelObject implements IDataSource {
public JdbcDataSource(String name) {
this.name = name;
}
private String name;
private List<IDataSourceSheet> dataSourceSheets= new ArrayList<IDataSourceSheet>();
@Override
public String getFileName(){
return this.name;
}
@Override
public File getFile() {
/*Not applicable for jdbc datasource*/
return null;
}
@Override
public void setFile(File file) {
/*Not applicable for jdbc datasource*/
}
@Override
public List<IDataSourceSheet> getSheets() {
return dataSourceSheets;
}
@Override
public void setSheets(List<IDataSourceSheet> sheets) {
this.dataSourceSheets=sheets;
firePropertyChange("sheets", null, sheets);
}
public void addSheet(JdbcDataSourceSheet sourceSheet){
dataSourceSheets.add(sourceSheet);
}
@Override
public String toString() {
return this.name;
}
}
package org.genesys2.anno.gui;
import org.genesys2.anno.model.Column;
import java.util.ArrayList;
import java.util.List;
public class JdbcDataSourceSheet implements IDataSourceSheet {
private String queryName="Unnamed query";
@Override
public void setSheetName(String sheetName) {
queryName=sheetName;
}
@Override
public String getSheetName() {
return this.queryName;
}
@Override
public boolean isHeadersIncluded() {
return false;
}
@Override
public int getHeaderRowIndex() {
return 0;
}
@Override
public List<Column> getColumns() {
return new ArrayList<Column>();
}