Commit 69e6fc23 authored by igoshin's avatar igoshin
Browse files

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

parent 2b05eba8
/target /target
/bin /bin
/.idea
\ No newline at end of file
...@@ -19,6 +19,7 @@ package org.genesys2.anno.gui; ...@@ -19,6 +19,7 @@ package org.genesys2.anno.gui;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
...@@ -53,14 +54,7 @@ import org.eclipse.swt.dnd.DragSource; ...@@ -53,14 +54,7 @@ import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter; import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent; import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.ControlAdapter; import org.eclipse.swt.events.*;
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.graphics.Image; import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Control;
...@@ -125,7 +119,7 @@ public class AppWindow { ...@@ -125,7 +119,7 @@ public class AppWindow {
@Override @Override
public String getText(Object element) { public String getText(Object element) {
_log.debug("getText " + element); // _log.debug("getText " + element);
if (element instanceof DataSourceFile) { if (element instanceof DataSourceFile) {
return ((DataSourceFile) element).getFile().getName(); return ((DataSourceFile) element).getFile().getName();
} }
...@@ -170,9 +164,15 @@ public class AppWindow { ...@@ -170,9 +164,15 @@ public class AppWindow {
if (parentElement instanceof DataSourceFile) { if (parentElement instanceof DataSourceFile) {
return ((DataSourceFile) parentElement).getSheets().toArray(); return ((DataSourceFile) parentElement).getSheets().toArray();
} }
if (parentElement instanceof JdbcDataSource) {
return ((JdbcDataSource) parentElement).getSheets().toArray();
}
if (parentElement instanceof DataSourceSheet) { if (parentElement instanceof DataSourceSheet) {
return null; return null;
} }
if (parentElement instanceof JdbcDataSourceSheet) {
return null;
}
return new Object[] { "Item 0", "Item 1", "Item 2" }; return new Object[] { "Item 0", "Item 1", "Item 2" };
} }
...@@ -183,7 +183,7 @@ public class AppWindow { ...@@ -183,7 +183,7 @@ public class AppWindow {
@Override @Override
public boolean hasChildren(Object element) { public boolean hasChildren(Object element) {
_log.debug("hasChildren " + element + " " + element.getClass()); // _log.debug("hasChildren " + element + " " + element.getClass());
if (element instanceof DwcaBuilder) { if (element instanceof DwcaBuilder) {
return ((DwcaBuilder) element).getFileRoots().size() > 0; return ((DwcaBuilder) element).getFileRoots().size() > 0;
...@@ -191,10 +191,15 @@ public class AppWindow { ...@@ -191,10 +191,15 @@ public class AppWindow {
if (element instanceof DataSourceFile) { if (element instanceof DataSourceFile) {
return ((DataSourceFile) element).getSheets().size() > 0; return ((DataSourceFile) element).getSheets().size() > 0;
} }
if (element instanceof JdbcDataSource) {
return ((JdbcDataSource) element).getSheets().size() > 0;
}
if (element instanceof DataSourceSheet) { if (element instanceof DataSourceSheet) {
return false; return false;
} }
if (element instanceof JdbcDataSourceSheet) {
return false;
}
return getChildren(element).length > 0; return getChildren(element).length > 0;
} }
...@@ -236,7 +241,6 @@ public class AppWindow { ...@@ -236,7 +241,6 @@ public class AppWindow {
} }
_log.info("Using workspace: " + workspacePath); _log.info("Using workspace: " + workspacePath);
System.setProperty("genesys.magic.workspace", (String) workspacePath); System.setProperty("genesys.magic.workspace", (String) workspacePath);
System.setProperty("genesys.magic.workspace.json", workspacePath+"/workspace.json");
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AppWindow appWindow = (AppWindow) ctx.getBean("appWindow"); AppWindow appWindow = (AppWindow) ctx.getBean("appWindow");
...@@ -378,14 +382,32 @@ public class AppWindow { ...@@ -378,14 +382,32 @@ public class AppWindow {
treeViewer.setContentProvider(new TreeContentProvider()); treeViewer.setContentProvider(new TreeContentProvider());
treeViewer.setInput(builder); treeViewer.setInput(builder);
MenuItem mntmAddFile = new MenuItem(filesTreeMenu, SWT.NONE); final MenuItem mntmAddFile = new MenuItem(filesTreeMenu, SWT.NONE);
mntmAddFile.addSelectionListener(new SelectionAdapter() { mntmAddFile.addSelectionListener(new SelectionAdapter() {
@Override @Override
public void widgetSelected(SelectionEvent arg0) { public void widgetSelected(SelectionEvent arg0) {
addSourceFile(); Object selectedObject = ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
} if (selectedObject instanceof JdbcDataSource) {
}); addJdbcQuery((JdbcDataSource)selectedObject);
mntmAddFile.setText("Add file..."); }
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); final MenuItem mntmRemoveFile = new MenuItem(filesTreeMenu, SWT.NONE);
mntmRemoveFile.addSelectionListener(new SelectionAdapter() { mntmRemoveFile.addSelectionListener(new SelectionAdapter() {
...@@ -516,7 +538,9 @@ public class AppWindow { ...@@ -516,7 +538,9 @@ public class AppWindow {
}); });
tabFolder.setSelection(newSheetTab); tabFolder.setSelection(newSheetTab);
} }else if(selectedNode instanceof JdbcDataSourceSheet){
System.out.println("jdbc");
}
} }
}); });
...@@ -593,7 +617,7 @@ public class AppWindow { ...@@ -593,7 +617,7 @@ public class AppWindow {
doDatabase(); doDatabase();
} }
}); });
tltmDatabase.setText("Database"); tltmDatabase.setText("Add database");
m_bindingContext = initDataBindings(); m_bindingContext = initDataBindings();
} }
...@@ -618,35 +642,27 @@ public class AppWindow { ...@@ -618,35 +642,27 @@ public class AppWindow {
} }
private void doSaveWorkSpace() { private void doSaveWorkSpace() {
String workspacePath=System.getProperty("genesys.magic.workspace.json"); FileDialog dialog = new FileDialog(shlGenesysMagic, SWT.SAVE);
try { dialog.setFilterNames(new String[] { "JSON Format" });
builder.saveWorkspace(workspacePath, settings); dialog.setFilterExtensions(new String[] { "*.json" });
} catch (JSONException e) { if (builder.getWorkspaceFilename() == null) {
_log.error(e.getMessage()); dialog.setFileName("workspace.json");
} catch (IOException e) { } else {
_log.error(e.getMessage()); File file = new File(builder.getWorkspaceFilename());
} dialog.setFileName(file.getName());
// FileDialog dialog = new FileDialog(shlGenesysMagic, SWT.SAVE); dialog.setFilterPath(file.getParent());
// dialog.setFilterNames(new String[] { "JSON Format" }); }
// dialog.setFilterExtensions(new String[] { "*.json" }); String path = dialog.open();
// if (builder.getWorkspaceFilename() == null) {
// dialog.setFileName("workspace.json"); if (path != null) {
// } else { try {
// File file = new File(builder.getWorkspaceFilename()); builder.saveWorkspace(path, settings);
// dialog.setFileName(file.getName()); } catch (JSONException e) {
// dialog.setFilterPath(file.getParent()); _log.error(e.getMessage());
// } } catch (IOException e) {
// String path = dialog.open(); _log.error(e.getMessage());
// }
// 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() { private void doLoadWorkSpace() {
...@@ -686,7 +702,7 @@ public class AppWindow { ...@@ -686,7 +702,7 @@ public class AppWindow {
} }
protected void doDatabase() { protected void doDatabase() {
DatabaseDialog databaseDialog = (DatabaseDialog) applicationContext.getBean("databaseDialog", shlGenesysMagic, SWT.NONE); DatabaseDialog databaseDialog = (DatabaseDialog) applicationContext.getBean("databaseDialog",shlGenesysMagic,treeViewer, SWT.NONE);
databaseDialog.open(); databaseDialog.open();
} }
...@@ -716,6 +732,11 @@ public class AppWindow { ...@@ -716,6 +732,11 @@ public class AppWindow {
return dss; return dss;
} }
public void addJdbcQuery(JdbcDataSource dataSource) {
dataSource.addSheet(new JdbcDataSourceSheet());
treeViewer.refresh();
}
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" });
......
...@@ -20,28 +20,33 @@ import java.io.File; ...@@ -20,28 +20,33 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class DataSourceFile extends AbstractModelObject { public class DataSourceFile extends AbstractModelObject implements IDataSource {
private File file; private File file;
private List<DataSourceSheet> sheets = new ArrayList<DataSourceSheet>(); private List<IDataSourceSheet> sheets = new ArrayList<IDataSourceSheet>();
@Override
public File getFile() { public File getFile() {
return file; return file;
} }
@Override
public void setFile(File file) { public void setFile(File file) {
this.file = file; this.file = file;
firePropertyChange("fileName", null, file); firePropertyChange("fileName", null, file);
} }
@Override
public String getFileName() { public String getFileName() {
return this.file==null ? null : this.file.getName(); return this.file==null ? null : this.file.getName();
} }
public List<DataSourceSheet> getSheets() { @Override
public List<IDataSourceSheet> getSheets() {
return sheets; return sheets;
} }
public void setSheets(List<DataSourceSheet> sheets) { @Override
public void setSheets(List<IDataSourceSheet> sheets) {
this.sheets = sheets; this.sheets = sheets;
firePropertyChange("sheets", null, sheets); firePropertyChange("sheets", null, sheets);
} }
......
/** /**
* Copyright 2014 Global Crop Diversity Trust * Copyright 2014 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -27,7 +27,7 @@ import org.genesys2.anno.model.Column; ...@@ -27,7 +27,7 @@ import org.genesys2.anno.model.Column;
import org.genesys2.anno.model.ColumnDef; import org.genesys2.anno.model.ColumnDef;
import org.genesys2.anno.parser.CsvDataSourceSheet; 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 static final Logger _log = Logger.getLogger(DataSourceSheet.class);
private File sourceFile; private File sourceFile;
...@@ -47,6 +47,7 @@ public class DataSourceSheet extends AbstractModelObject { ...@@ -47,6 +47,7 @@ public class DataSourceSheet extends AbstractModelObject {
return sourceFile; return sourceFile;
} }
@Override
public String getSheetName() { public String getSheetName() {
return sheetName; return sheetName;
} }
...@@ -79,11 +80,11 @@ public class DataSourceSheet extends AbstractModelObject { ...@@ -79,11 +80,11 @@ public class DataSourceSheet extends AbstractModelObject {
this.headersIncluded = headersIncluded; this.headersIncluded = headersIncluded;
firePropertyChange("headersIncluded", null, headersIncluded); firePropertyChange("headersIncluded", null, headersIncluded);
} }
@Override
public boolean isHeadersIncluded() { public boolean isHeadersIncluded() {
return headersIncluded; return headersIncluded;
} }
@Override
public int getHeaderRowIndex() { public int getHeaderRowIndex() {
return headerRowIndex; return headerRowIndex;
} }
...@@ -150,6 +151,7 @@ public class DataSourceSheet extends AbstractModelObject { ...@@ -150,6 +151,7 @@ public class DataSourceSheet extends AbstractModelObject {
return sampleData; return sampleData;
} }
@Override
public List<Column> getColumns() { public List<Column> getColumns() {
return this.columns; return this.columns;
} }
......
...@@ -6,6 +6,8 @@ import org.eclipse.core.databinding.beans.BeanProperties; ...@@ -6,6 +6,8 @@ import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties; import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.MessageDialog; 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.SWT;
import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionAdapter;
...@@ -32,8 +34,13 @@ public class DatabaseDialog extends Dialog { ...@@ -32,8 +34,13 @@ public class DatabaseDialog extends Dialog {
@Autowired @Autowired
private DatabaseSettings databaseSettings; private DatabaseSettings databaseSettings;
@Autowired
private DwcaBuilder builder;
protected Object result; protected Object result;
protected Shell shell; protected Shell shell;
private TreeViewer treeViewer;
private Text txtName;
private Text txtDbHost; private Text txtDbHost;
private Text txtDbPort; private Text txtDbPort;
private Text txtDbName; private Text txtDbName;
...@@ -47,8 +54,9 @@ public class DatabaseDialog extends Dialog { ...@@ -47,8 +54,9 @@ public class DatabaseDialog extends Dialog {
* @param parent * @param parent
* @param style * @param style
*/ */
public DatabaseDialog(Shell parent, int style) { public DatabaseDialog(Shell parent, TreeViewer treeViewer, int style) {
super(parent, style); super(parent, style);
this.treeViewer = treeViewer;
setText("Add Database"); setText("Add Database");
} }
...@@ -91,7 +99,14 @@ public class DatabaseDialog extends Dialog { ...@@ -91,7 +99,14 @@ public class DatabaseDialog extends Dialog {
grpDbConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); grpDbConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
grpDbConfiguration.setText("Database configuration"); 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*/ /*Host*/
Label lblDbHost = new Label(grpDbConfiguration, SWT.NONE); Label lblDbHost = new Label(grpDbConfiguration, SWT.NONE);
lblDbHost.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); lblDbHost.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
...@@ -182,54 +197,20 @@ public class DatabaseDialog extends Dialog { ...@@ -182,54 +197,20 @@ public class DatabaseDialog extends Dialog {
int button = dialog.open(); int button = dialog.open();
if (button == SWT.OK) { if (button == SWT.OK) {
try { builder.addDataSource(new JdbcDataSource(txtName.getText()));
saveDataSourceToWorkspace(); treeViewer.refresh();
} catch (JSONException e) { this.shell.close();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
ConnectionUtils.close(connection); 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() { protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext(); 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 ovTextDbHost = WidgetProperties.text(SWT.Modify).observe(this.txtDbHost);
IObservableValue ovDbHost = BeanProperties.value("host").observe(databaseSettings); IObservableValue ovDbHost = BeanProperties.value("host").observe(databaseSettings);
bindingContext.bindValue(ovTextDbHost, ovDbHost, null, null); bindingContext.bindValue(ovTextDbHost, ovDbHost, null, null);
......
...@@ -34,28 +34,25 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -34,28 +34,25 @@ import org.springframework.beans.factory.annotation.Autowired;
public class DwcaBuilder extends AbstractModelObject { public class DwcaBuilder extends AbstractModelObject {
@Autowired private List<IDataSource> fileRoots = new ArrayList<IDataSource>();
private DatabaseSettings databaseSettings;
private List<DataSourceFile> fileRoots = new ArrayList<DataSourceFile>();
private String workspaceFilename = null; private String workspaceFilename = null;
public List<DataSourceFile> getFileRoots() { public List<IDataSource> getFileRoots() {
return fileRoots; return fileRoots;
} }
public String getWorkspaceFilename() { public String getWorkspaceFilename() {