Commit 9e09c0e7 authored by igoshin's avatar igoshin
Browse files

#14051 seems finished

parent 69e6fc23
......@@ -14,6 +14,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.genesys2.anno.gui.ColumnDefs;
import org.genesys2.anno.gui.DataSourceSheet;
import org.genesys2.anno.gui.IDataSourceSheet;
import org.genesys2.anno.model.Column;
import org.genesys2.anno.model.ColumnDef;
import org.genesys2.anno.predefined.GenesysJSON;
......@@ -35,7 +36,7 @@ public class RowConverter {
private static final ObjectMapper mapper = new ObjectMapper();
private static final GenesysJSON genesysJson = new GenesysJSON();
public static Map<String, ?> toMap(DataSourceSheet dataSourceSheet, Object[] row, ColumnDefs columnDefs) {
public static Map<String, ?> toMap(IDataSourceSheet dataSourceSheet, Object[] row, ColumnDefs columnDefs) {
HashMap<String, Object> map = new HashMap<String, Object>(columnDefs.getColumnDefs().size());
_log.debug("Converting " + ArrayUtils.toString(row));
......@@ -158,7 +159,7 @@ public class RowConverter {
return null;
}
public static List<Map<String, ?>> toMap(DataSourceSheet dataSourceSheet, List<Object[]> rows, ColumnDefs columnDefs) {
public static List<Map<String, ?>> toMap(IDataSourceSheet dataSourceSheet, List<Object[]> rows, ColumnDefs columnDefs) {
List<Map<String, ?>> list = new ArrayList<Map<String, ?>>(rows.size());
for (Object[] row : rows) {
list.add(toMap(dataSourceSheet, row, columnDefs));
......
......@@ -388,7 +388,7 @@ public class AppWindow {
public void widgetSelected(SelectionEvent arg0) {
Object selectedObject = ((IStructuredSelection) treeViewer.getSelection()).getFirstElement();
if (selectedObject instanceof JdbcDataSource) {
addJdbcQuery((JdbcDataSource)selectedObject);
addJdbcQuery((JdbcDataSource) selectedObject);
}
if (selectedObject instanceof DataSourceFile) {
addSourceFile();
......@@ -417,8 +417,8 @@ public class AppWindow {
Object selectedObject = iterator.next();
_log.debug("Remove Sel " + selectedObject);
if (selectedObject instanceof DataSourceFile) {
builder.removeDataSource((DataSourceFile) selectedObject);
if (selectedObject instanceof IDataSource) {
builder.removeDataSource((IDataSource) selectedObject);
tableViewer.refresh();
}
}
......@@ -426,7 +426,7 @@ public class AppWindow {
treeViewer.refresh();
}
});
mntmRemoveFile.setText("Remove file");
mntmRemoveFile.setText("Remove");
filesTreeMenu.addMenuListener(new MenuAdapter() {
@Override
......@@ -436,7 +436,7 @@ public class AppWindow {
Object selectedObject = iterator.next();
_log.debug("Sel " + selectedObject);
if (selectedObject instanceof DataSourceFile)
if (selectedObject instanceof IDataSource)
enabled = true;
}
......@@ -515,7 +515,6 @@ public class AppWindow {
i++;
}
}
CTabItem newSheetTab = createTab(tabFolder, currentSheet);
// Load stuff
......@@ -538,8 +537,10 @@ public class AppWindow {
});
tabFolder.setSelection(newSheetTab);
}else if(selectedNode instanceof JdbcDataSourceSheet){
System.out.println("jdbc");
} else if (selectedNode instanceof JdbcDataSourceSheet) {
final JdbcDataSourceSheet currentSheet = (JdbcDataSourceSheet) selectedNode;
CTabItem newSheetTab= createTab(tabFolder, currentSheet);
tabFolder.setSelection(newSheetTab);
}
}
......@@ -621,14 +622,14 @@ public class AppWindow {
m_bindingContext = initDataBindings();
}
protected CTabItem createTab(CTabFolder tabFolder, DataSourceSheet currentSheet) {
protected CTabItem createTab(CTabFolder tabFolder, 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, SWT.NONE);
final SheetDisplay sheetDisplay = (SheetDisplay) applicationContext.getBean("sheetDisplay", currentSheet, tabFolder,treeViewer, SWT.NONE);
tbtmOther.setControl(sheetDisplay);
tbtmOther.addDisposeListener(new DisposeListener() {
......@@ -684,7 +685,7 @@ public class AppWindow {
}
protected void doPush() {
final DataSourceSheet dss = getCurrentDataSourceSheet();
final IDataSourceSheet dss = getCurrentDataSourceSheet();
if (dss == null) {
return;
}
......@@ -707,7 +708,7 @@ public class AppWindow {
}
protected void doAutomap() {
final DataSourceSheet dss = getCurrentDataSourceSheet();
final IDataSourceSheet dss = getCurrentDataSourceSheet();
if (dss == null) {
return;
}
......@@ -722,18 +723,20 @@ public class AppWindow {
});
}
public DataSourceSheet getCurrentDataSourceSheet() {
public IDataSourceSheet getCurrentDataSourceSheet() {
CTabItem currentTab = tabFolder.getSelection();
if (currentTab == null) {
_log.info("No tab selected");
return null;
}
final DataSourceSheet dss = ((SheetDisplay) currentTab.getControl()).getDataSourceSheet();
final IDataSourceSheet dss = ((SheetDisplay) currentTab.getControl()).getDataSourceSheet();
return dss;
}
public void addJdbcQuery(JdbcDataSource dataSource) {
dataSource.addSheet(new JdbcDataSourceSheet());
JdbcDataSourceSheet dataSourceSheet= new JdbcDataSourceSheet();
dataSourceSheet.setDatabaseSettings(dataSource.getSettings());
dataSource.addSheet(dataSourceSheet);
treeViewer.refresh();
}
......
......@@ -20,10 +20,10 @@ import org.genesys2.anno.model.Column;
import org.genesys2.anno.parser.CsvDataSourceSheet;
public class DSW extends AbstractModelObject {
private DataSourceSheet dataSourceSheet;
private IDataSourceSheet dataSourceSheet;
private Column selectedColumn = null;
public DataSourceSheet getDataSourceSheet() {
public IDataSourceSheet getDataSourceSheet() {
return dataSourceSheet;
}
......@@ -31,7 +31,7 @@ public class DSW extends AbstractModelObject {
return (CsvDataSourceSheet) (dataSourceSheet instanceof CsvDataSourceSheet ? dataSourceSheet : null);
}
public void setDataSourceSheet(DataSourceSheet dataSourceSheet) {
public void setDataSourceSheet(IDataSourceSheet dataSourceSheet) {
this.dataSourceSheet = dataSourceSheet;
firePropertyChange("dataSourceSheet", null, dataSourceSheet);
}
......
......@@ -29,9 +29,9 @@ public interface DataSourceLoader {
DataSourceFile loadDataSource(File file);
List<Object[]> loadRows(DataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException, FileNotFoundException, IOException;
List<Object[]> loadRows(IDataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException, FileNotFoundException, IOException;
List<Object[]> loadDataRows(DataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException, FileNotFoundException, IOException;
RowReader createRowReader(DataSourceSheet sheet) throws IOException, UnsupportedDataFormatException;
RowReader createRowReader(IDataSourceSheet sheet) throws IOException, UnsupportedDataFormatException;
}
......@@ -76,7 +76,7 @@ public class DataSourceLoaderImpl implements DataSourceLoader {
}
@Override
public List<Object[]> loadRows(DataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException, FileNotFoundException, IOException {
public List<Object[]> loadRows(IDataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException, FileNotFoundException, IOException {
// Iterate over parsers and see if something comes out
for (DataSourceParser parser : parsers) {
try {
......@@ -108,7 +108,7 @@ public class DataSourceLoaderImpl implements DataSourceLoader {
}
@Override
public RowReader createRowReader(DataSourceSheet sheet) throws IOException, UnsupportedDataFormatException {
public RowReader createRowReader(IDataSourceSheet sheet) throws IOException, UnsupportedDataFormatException {
// Iterate over parsers and see if something comes out
for (DataSourceParser parser : parsers) {
try {
......
......@@ -31,8 +31,8 @@ public interface DataSourceParser {
Collection<? extends DataSourceSheet> findSheets(File sourceFile)
throws UnsupportedDataFormatException;
List<Object[]> loadRows(DataSourceSheet sheet, int maxRows, int startAt) throws UnsupportedDataFormatException, FileNotFoundException, IOException;
List<Object[]> loadRows(IDataSourceSheet sheet, int maxRows, int startAt) throws UnsupportedDataFormatException, FileNotFoundException, IOException;
RowReader createRowReader(DataSourceSheet dataSourceSheet) throws UnsupportedDataFormatException, IOException;
RowReader createRowReader(IDataSourceSheet dataSourceSheet) throws UnsupportedDataFormatException, IOException;
}
......@@ -75,6 +75,7 @@ public class DataSourceSheet extends AbstractModelObject implements IDataSourceS
firePropertyChange("columnTerms", null, columnTerms);
}
@Override
public void setHeadersIncluded(boolean headersIncluded) {
_log.debug("Set headers included :" + headersIncluded);
this.headersIncluded = headersIncluded;
......@@ -84,17 +85,20 @@ public class DataSourceSheet extends AbstractModelObject implements IDataSourceS
public boolean isHeadersIncluded() {
return headersIncluded;
}
@Override
public int getHeaderRowIndex() {
return headerRowIndex;
}
@Override
public void setHeaderRowIndex(int headerRowIndex) {
_log.debug("Set header row index:" + headerRowIndex);
this.headerRowIndex = headerRowIndex;
firePropertyChange("headerRowIndex", null, headerRowIndex);
}
@Override
public void updateData(List<Object[]> rows) {
int columnCount = 0;
for (Object[] row : rows) {
......@@ -147,6 +151,7 @@ public class DataSourceSheet extends AbstractModelObject implements IDataSourceS
firePropertyChange("columns", null, columns);
}
@Override
public List<Object[]> getSampleData() {
return sampleData;
}
......@@ -156,6 +161,7 @@ public class DataSourceSheet extends AbstractModelObject implements IDataSourceS
return this.columns;
}
@Override
public void setColumns(List<Column> columns) {
this.columns = columns;
firePropertyChange("columns", null, this.columns);
......@@ -165,11 +171,18 @@ public class DataSourceSheet extends AbstractModelObject implements IDataSourceS
return this.columns.get(columnIndex);
}
@Override
public boolean isCsv() {
System.err.println(this.getClass() + " " + (this instanceof CsvDataSourceSheet));
return this instanceof CsvDataSourceSheet;
}
@Override
public boolean isSqlQuery() {
return false;
}
@Override
public void automap(ColumnDefs columnDefs) {
for (ColumnDef columnDef : columnDefs.getColumnDefs()) {
_log.debug("Looking at " + columnDef.getPreferredName());
......@@ -182,4 +195,15 @@ public class DataSourceSheet extends AbstractModelObject implements IDataSourceS
}
}
}
@Override
public String getQuery() {
// Auto-generated method stub
return null;
}
@Override
public void setQuery(String query) {
// Auto-generated method stub
}
}
......@@ -47,6 +47,7 @@ public class DatabaseDialog extends Dialog {
private Text txtDbUrl;
private Text txtDbUserName;
private Text txtDbPassword;
private Combo comboType;
/**
* Create the dialog.
......@@ -98,7 +99,20 @@ public class DatabaseDialog extends Dialog {
grpDbConfiguration.setLayout(new GridLayout(2, false));
grpDbConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
grpDbConfiguration.setText("Database configuration");
/*Datasource type*/
Label lblType = new Label(grpDbConfiguration, SWT.NONE);
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*/
Label lblName = new Label(grpDbConfiguration, SWT.NONE);
lblName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
......@@ -175,7 +189,7 @@ public class DatabaseDialog extends Dialog {
scrolledComposite.setContent(composite);
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
m_bindingContext = initDataBindings();
}
private void doConnect() {
......@@ -197,13 +211,19 @@ public class DatabaseDialog extends Dialog {
int button = dialog.open();
if (button == SWT.OK) {
builder.addDataSource(new JdbcDataSource(txtName.getText()));
treeViewer.refresh();
this.shell.close();
ConnectionUtils.close(connection);
doConnection(connection);
}
}
private void doConnection(Connection connection) {
JdbcDataSource jdbcDataSource = new JdbcDataSource(txtName.getText());
jdbcDataSource.setSettings(databaseSettings);
builder.addDataSource(jdbcDataSource);
treeViewer.refresh();
this.shell.close();
ConnectionUtils.close(connection);
}
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
......
......@@ -119,6 +119,16 @@ public class DwcaBuilder extends AbstractModelObject {
csv.put("unixEol", csvSheet.isUnixEol());
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);
......@@ -192,6 +202,20 @@ public class DwcaBuilder extends AbstractModelObject {
sourceSheet = csvSourceSheet;
} else if (!sourceJ.has("path")){
sourceSheet=new JdbcDataSourceSheet();
if (sheetJ.has("query")){
sourceSheet.setQuery(sheetJ.getString("query"));
}
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);
}
......
......@@ -3,19 +3,40 @@ package org.genesys2.anno.gui;
import org.genesys2.anno.model.Column;
import java.io.File;
import java.util.List;
public interface IDataSourceSheet {
public String getQuery();
public void setQuery(String query);
public void setSheetName(String sheetName);
public String getSheetName();
void setHeadersIncluded(boolean headersIncluded);
public boolean isHeadersIncluded();
void setHeaderRowIndex(int headerRowIndex);
public int getHeaderRowIndex();
public void updateData(List<Object[]> rows);
public List<Object[]> getSampleData();
public List<Column> getColumns();
public void setColumns(List<Column> columns);
public boolean isCsv();
public boolean isSqlQuery();
public File getSourceFile();
public void automap(ColumnDefs columnDefs);
}
package org.genesys2.anno.gui;
import org.genesys2.anno.model.DatabaseSettings;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class JdbcDataSource extends AbstractModelObject implements IDataSource {
private DatabaseSettings settings;
public JdbcDataSource(String name) {
this.name = name;
}
......@@ -49,4 +53,12 @@ public class JdbcDataSource extends AbstractModelObject implements IDataSource {
public String toString() {
return this.name;
}
public void setSettings(DatabaseSettings settings) {
this.settings = settings;
}
public DatabaseSettings getSettings() {
return settings;
}
}
......@@ -2,46 +2,164 @@ package org.genesys2.anno.gui;
import org.genesys2.anno.model.Column;
import org.genesys2.anno.model.DatabaseSettings;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class JdbcDataSourceSheet implements IDataSourceSheet {
public class JdbcDataSourceSheet extends AbstractModelObject implements IDataSourceSheet {
private String queryName="Unnamed query";
private String sheetName = "Unnamed query";
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;
}
@Override
public void setQuery(String query) {
this.query = query;
}
@Override
public void setSheetName(String sheetName) {
queryName=sheetName;
this.sheetName = sheetName;
}
@Override
public String getSheetName() {
return this.queryName;
return this.sheetName;
}
@Override
public void setHeadersIncluded(boolean headersIncluded) {
this.headersIncluded = headersIncluded;
firePropertyChange("headersIncluded", null, headersIncluded);
}
@Override
public boolean isHeadersIncluded() {
return false;
return this.headersIncluded;
}
@Override
public void setHeaderRowIndex(int headerRowIndex) {
this.headerRowIndex = headerRowIndex;
firePropertyChange("headerRowIndex", null, headerRowIndex);
}
@Override
public int getHeaderRowIndex() {
return 0;
return this.headerRowIndex;
}
@Override
public void updateData(List<Object[]> rows) {
int columnCount = 0;
for (Object[] row : rows) {
if (row != null)
columnCount = Math.max(columnCount, row.length);
}
Object[] headers = null;
if (headersIncluded) {
try {
this.sampleData = rows.subList(headerRowIndex + 1, rows.size());
Object[] headerRow = rows.get(headerRowIndex);
headers = headerRow;
} catch (IndexOutOfBoundsException e) {
}catch (IllegalArgumentException e){
}
} else {
this.sampleData = rows;
}
// Make sure we have the Columns in the list
ensureColumns(columnCount, headers);
firePropertyChange("sampleData", null, this.sampleData);
}
private void ensureColumns(int columnCount, Object[] headerRow) {
boolean changed = false;
for (int i = columns.size() - 1; i >= 0 && i > columnCount; i--) {
columns.remove(i);
changed = true;
}
for (int i = columns.size(); i < columnCount; i++) {
columns.add(new Column());
changed = true;
}
for (int i = columns.size() - 1; i >= 0; i--) {
String headerColumnName = (headerRow != null && headerRow.length > i ? String.valueOf(headerRow[i]) : null);
Column c = columns.get(i);
c.setPreferredName(headerColumnName == null ? "Column " + i : headerColumnName);
}
// fire change
if (changed)
firePropertyChange("columns", null, columns);
}