Commit b287ebca authored by Alex Igoshin's avatar Alex Igoshin
Browse files

Merged genesys2/anno-swt into master

parents d2fdb127 9435063d
......@@ -151,14 +151,24 @@
</dependency>
<dependency>
<groupId>org.genesys2</groupId>
<artifactId>client-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>genesys-client-api</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.6.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -10,23 +10,23 @@
*******************************************************************************/
package org.eclipse.wb.swt;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
/**
* Utility class for managing OS resources associated with SWT controls such as colors, fonts, images, etc.
......
......@@ -48,7 +48,7 @@ public class RowConverter {
continue;
}
Object rowValue = convertCellValue(row[i], column.isMultiple(), column.getSeparator(), column.getPattern());
Object rowValue = convertCellValue(row[i], column.isMultiple(), column.getSeparator(), column.getPattern(), column.getGroupPattern());
Object mapValue = map.get(columnDef.getRdfTerm());
if (mapValue == null) {
......@@ -91,7 +91,7 @@ public class RowConverter {
return map;
}
private static Object convertCellValue(Object value, boolean splitString, String separator, String pattern) {
private static Object convertCellValue(Object value, boolean splitString, String separator, String pattern, String groupPattern) {
if (value != null) {
if (value instanceof String) {
String stringValue = ((String) value);
......@@ -102,7 +102,7 @@ public class RowConverter {
String[] split = stringValue.split(separator);
for (int i = split.length - 1; i >= 0; i--) {
if (pattern != null) {
split[i] = valueFromPattern(split[i].trim(), pattern);
split[i] = valueFromPattern(split[i].trim(), pattern, groupPattern);
} else {
split[i] = split[i].trim();
}
......@@ -110,7 +110,7 @@ public class RowConverter {
return split;
}
return valueFromPattern(stringValue.trim(), pattern);
return valueFromPattern(stringValue.trim(), pattern, groupPattern);
}
}
......@@ -118,7 +118,7 @@ public class RowConverter {
return value;
}
private static String valueFromPattern(String trim, String pattern) {
private static String valueFromPattern(String trim, String pattern, String groupPattern) {
if (StringUtils.isBlank(pattern)) {
return trim;
}
......@@ -126,6 +126,13 @@ public class RowConverter {
Matcher matcher = pat.matcher(trim);
if (matcher.find()) {
String res = matcher.groupCount() == 0 ? matcher.group() : matcher.group(1);
if (StringUtils.isNotBlank(groupPattern)) {
res = groupPattern;
if (res.contains("$1"))
res = res.replaceAll("\\$1", matcher.group(1));
if (res.contains("$2"))
res = res.replaceAll("\\$2", matcher.group(2));
}
// System.err.println("Pattern=" + pattern + " groups=" +
// matcher.groupCount() + " res=" + res);
// throw new RuntimeException("foobar");
......
......@@ -79,18 +79,39 @@ import org.eclipse.wb.swt.SWTResourceManager;
import org.genesys2.anno.model.ColumnDef;
import org.genesys2.anno.model.Settings;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import swing2swt.layout.BorderLayout;
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Component
public class AppWindow {
private static final Logger _log = Logger.getLogger(AppWindow.class);
private DataBindingContext m_bindingContext;
private DwcaBuilder builder = new DwcaBuilder();
private ColumnDefs columnDefs = AppConfig.instance.getColumnDefs();
private Settings settings = AppConfig.instance.getSettings();
@Autowired
protected ApplicationContext applicationContext;
@Autowired
protected DataSourceLoader dataSourceLoader;
@Autowired
private DwcaBuilder builder;
@Autowired
private ColumnDefs columnDefs;
@Autowired
private Settings settings;
private ExecutorService threadPool = Executors.newFixedThreadPool(4);
private static class ViewerLabelProvider extends LabelProvider {
@Override
public Image getImage(Object element) {
_log.debug("getImage " + element);
if (element instanceof DataSourceFile) {
......@@ -102,6 +123,7 @@ public class AppWindow {
return super.getImage(element);
}
@Override
public String getText(Object element) {
_log.debug("getText " + element);
if (element instanceof DataSourceFile) {
......@@ -115,12 +137,15 @@ public class AppWindow {
}
private static class TreeContentProvider implements ITreeContentProvider {
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
@Override
public void dispose() {
}
@Override
public Object[] getElements(Object inputElement) {
_log.debug("getElements " + inputElement + " " + inputElement.getClass());
if (inputElement instanceof DwcaBuilder) {
......@@ -135,6 +160,7 @@ public class AppWindow {
return getChildren(inputElement);
}
@Override
public Object[] getChildren(Object parentElement) {
_log.debug("getChildren " + parentElement + " " + parentElement.getClass());
......@@ -150,10 +176,12 @@ public class AppWindow {
return new Object[] { "Item 0", "Item 1", "Item 2" };
}
@Override
public Object getParent(Object element) {
return null;
}
@Override
public boolean hasChildren(Object element) {
_log.debug("hasChildren " + element + " " + element.getClass());
......@@ -172,14 +200,15 @@ public class AppWindow {
}
}
private DataBindingContext m_bindingContext;
protected Shell shlGenesysMagic;
private MenuItem mntmQuit;
protected DataSourceLoader dataSourceLoader = AppConfig.instance.getDataSourceLoader();
private static Image archiveIcon;
private static Image spreadsheetIcon;
private static Image openPackageIcon;
private static Image automapColumnsIcon;
private static Image pushDataIcon;
private static Image spreadsheetIcon16;
static Image pushDataIcon;
private static Image loadWorkspaceIcon;
private static Image saveWorkspaceIcon;
private static Image settingsIcon;
......@@ -196,12 +225,38 @@ public class AppWindow {
* @param args
*/
public static void main(String[] args) {
Shell workspaceShell = new Shell();
WorkspaceDialog workspaceDialog = new WorkspaceDialog(workspaceShell);
Object workspacePath = workspaceDialog.open();
workspaceShell.dispose();
if (workspacePath == null) {
_log.error("No workspace selected. Exiting now!");
return;
}
_log.info("Using workspace: " + workspacePath);
System.setProperty("genesys.magic.workspace", (String) workspacePath);
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AppWindow appWindow = (AppWindow) ctx.getBean("appWindow");
appWindow.execute();
_log.info("Closing Spring context");
ctx.close();
_log.info("All tidied up.");
// Workaround for hanging derby.rawStoreDeamon
System.exit(0);
}
private void execute() {
Display display = Display.getDefault();
final AppWindow that = this;
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
@Override
public void run() {
try {
AppWindow window = new AppWindow();
window.open();
that.open();
} catch (Exception e) {
e.printStackTrace();
}
......@@ -233,6 +288,7 @@ public class AppWindow {
AppWindow.archiveIcon = ImageDescriptor.createFromURL(this.getClass().getResource("/icon/archive.png")).createImage();
AppWindow.openPackageIcon = ImageDescriptor.createFromURL(this.getClass().getResource("/icon/openpackage.png")).createImage();
AppWindow.spreadsheetIcon = ImageDescriptor.createFromURL(this.getClass().getResource("/icon/spreadsheet.png")).createImage();
AppWindow.spreadsheetIcon16 = ImageDescriptor.createFromURL(this.getClass().getResource("/icon/spreadsheet16.png")).createImage();
AppWindow.automapColumnsIcon = ImageDescriptor.createFromURL(this.getClass().getResource("/icon/automap.png")).createImage();
AppWindow.pushDataIcon = ImageDescriptor.createFromURL(this.getClass().getResource("/icon/pushdata.png")).createImage();
AppWindow.loadWorkspaceIcon = ImageDescriptor.createFromURL(this.getClass().getResource("/icon/load.png")).createImage();
......@@ -252,11 +308,6 @@ public class AppWindow {
*/
protected void createContents() {
shlGenesysMagic = new Shell();
shlGenesysMagic.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
doShutdown();
}
});
shlGenesysMagic.setMinimumSize(new Point(100, 22));
shlGenesysMagic.setSize(744, 528);
shlGenesysMagic.setText("Genesys Magic");
......@@ -422,6 +473,7 @@ public class AppWindow {
sashForm.setWeights(new int[] { 3, 6, 3 });
treeViewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent e) {
IStructuredSelection thisSelection = (IStructuredSelection) e.getSelection();
Object selectedNode = thisSelection.getFirstElement();
......@@ -441,7 +493,7 @@ public class AppWindow {
}
}
CTabItem newSheetTab = SheetTabFactory.createComposite(tabFolder, currentSheet);
CTabItem newSheetTab = createTab(tabFolder, currentSheet);
// Load stuff
threadPool.execute(new Runnable() {
......@@ -535,11 +587,37 @@ public class AppWindow {
m_bindingContext = initDataBindings();
}
protected CTabItem createTab(CTabFolder tabFolder, DataSourceSheet 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);
tbtmOther.setControl(sheetDisplay);
tbtmOther.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
sheetDisplay.dispose();
}
});
return tbtmOther;
}
private void doSaveWorkSpace() {
FileDialog dialog = new FileDialog(shlGenesysMagic, SWT.SAVE);
dialog.setFilterNames(new String[] { "JSON Format" });
dialog.setFilterExtensions(new String[] { "*.json" });
dialog.setFileName("workspace.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) {
......@@ -571,18 +649,13 @@ public class AppWindow {
treeViewer.refresh();
}
protected void doShutdown() {
_log.info("Shutting down.");
AppConfig.shutdown();
}
protected void doPush() {
final DataSourceSheet dss = getCurrentDataSourceSheet();
if (dss == null) {
return;
}
PushDialog pushDialog = new PushDialog(shlGenesysMagic, SWT.NONE);
PushDialog pushDialog = (PushDialog) applicationContext.getBean("pushDialog", shlGenesysMagic, SWT.NONE);
pushDialog.setDataSourceSheet(dss);
pushDialog.setColumnDefs(this.columnDefs);
......@@ -590,7 +663,7 @@ public class AppWindow {
}
protected void doSettings() {
SettingsDialog settingsDialog = new SettingsDialog(this.settings, shlGenesysMagic, SWT.NONE);
SettingsDialog settingsDialog = (SettingsDialog) applicationContext.getBean("settingsDialog", shlGenesysMagic, SWT.NONE);
settingsDialog.open();
}
......
......@@ -27,7 +27,9 @@ import java.util.Set;
import org.apache.log4j.Logger;
import org.genesys2.anno.parser.RowReader;
import org.springframework.stereotype.Service;
@Service
public class DataSourceLoaderImpl implements DataSourceLoader {
private static final Logger _log = Logger.getLogger(DataSourceLoaderImpl.class);
......
......@@ -164,6 +164,7 @@ public class DataSourceSheet extends AbstractModelObject {
}
public boolean isCsv() {
System.err.println(this.getClass() + " " + (this instanceof CsvDataSourceSheet));
return this instanceof CsvDataSourceSheet;
}
......
......@@ -28,6 +28,7 @@ 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.parser.CsvDataSourceSheet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -35,11 +36,16 @@ import org.json.JSONTokener;
public class DwcaBuilder extends AbstractModelObject {
private List<DataSourceFile> fileRoots = new ArrayList<DataSourceFile>();
private String workspaceFilename = null;
public List<DataSourceFile> getFileRoots() {
return fileRoots;
}
public String getWorkspaceFilename() {
return workspaceFilename;
}
public void addDataSource(DataSourceFile dataSource) {
if (dataSource == null)
return;
......@@ -52,141 +58,177 @@ public class DwcaBuilder extends AbstractModelObject {
firePropertyChange("fileRoots", null, this);
}
public void saveWorkspace(String filePath,Settings sourceSettings) throws JSONException, IOException {
JSONObject main = new JSONObject();
JSONArray files = new JSONArray();
JSONObject oauthSettings = new JSONObject();
oauthSettings.put("accessToken", sourceSettings.getOauthSettings().getAccessToken());
oauthSettings.put("apiUrl", sourceSettings.getOauthSettings().getApiUrl());
oauthSettings.put("authorizationEndpoint", sourceSettings.getOauthSettings().getAuthorizationEndpoint());
oauthSettings.put("clientKey", sourceSettings.getOauthSettings().getClientKey());
oauthSettings.put("clientSecret", sourceSettings.getOauthSettings().getClientSecret());
oauthSettings.put("refreshToken", sourceSettings.getOauthSettings().getRefreshToken());
oauthSettings.put("serverUrl", sourceSettings.getOauthSettings().getServerUrl());
oauthSettings.put("tokenEndpoint", sourceSettings.getOauthSettings().getTokenEndpoint());
JSONObject settings = new JSONObject();
settings.put("oauthSettings",oauthSettings);
for (DataSourceFile sourceFile : getFileRoots()) {
String path = sourceFile.getFile().getPath();
JSONObject file = new JSONObject();
JSONArray sheets = new JSONArray();
file.put("path", path);
file.put("sheets", sheets);
files.put(file);
for (DataSourceSheet sourceSheet : sourceFile.getSheets()) {
JSONObject sheet = new JSONObject();
JSONArray columns = new JSONArray();
String name = sourceSheet.getSheetName();
sheet.put("name", name);
sheet.put("columns", columns);
sheets.put(sheet);
for (Column sourceColumn : sourceSheet.getColumns()) {
JSONObject column= new JSONObject();
sourceColumn.setDataType(ColumnDataType.TEXT);
column.put("name",sourceColumn.getPreferredName());
column.put("rdfTerm",sourceColumn.getRdfTerm());
column.put("description",sourceColumn.getDescription());
column.put("multiple", sourceColumn.isMultiple());
column.put("dataType",sourceColumn.getDataType());
column.put("pattern",sourceColumn.getPattern());
column.put("separator",sourceColumn.getSeparator());
columns.put(column);
}
}
}
main.put("settings",settings);
main.put("files", files);
main.put("dateCreated", System.currentTimeMillis());
main.put("version", 1);
FileWriter fileWriter = new FileWriter(filePath);
fileWriter.write(main.toString());
fileWriter.flush();
fileWriter.close();
System.out.println(main.toString());
}
public void loadWorkspace(String filePath,Settings settings) throws FileNotFoundException, JSONException {
fileRoots.clear();
FileReader reader = new FileReader(filePath);
JSONTokener tokener = new JSONTokener(reader);
JSONObject root = new JSONObject(tokener);
JSONArray files = root.getJSONArray("files");
for (int i = 0; i < files.length(); i++) {
String path = files.getJSONObject(i).getString("path");
File file = new File(path);
JSONArray sheets = files.getJSONObject(i).getJSONArray("sheets");
DataSourceFile sourceFile = new DataSourceFile();
List<DataSourceSheet> sourceSheetList = new ArrayList<DataSourceSheet>();
for (int j = 0; j < sheets.length(); j++) {
DataSourceSheet sourceSheet = new DataSourceSheet(file);
sourceSheet.setSheetName(sheets.getJSONObject(j).getString("name"));
JSONArray columns = sheets.getJSONObject(j).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("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);
public void saveWorkspace(String filePath, Settings sourceSettings) throws JSONException, IOException {
JSONObject main = new JSONObject();
JSONArray files = new JSONArray();
JSONObject oauthSettings = new JSONObject();
oauthSettings.put("accessToken", sourceSettings.getOauthSettings().getAccessToken());
oauthSettings.put("apiUrl", sourceSettings.getOauthSettings().getApiUrl());
oauthSettings.put("authorizationEndpoint", sourceSettings.getOauthSettings().getAuthorizationEndpoint());
oauthSettings.put("clientKey", sourceSettings.getOauthSettings().getClientKey());
oauthSettings.put("clientSecret", sourceSettings.getOauthSettings().getClientSecret());
oauthSettings.put("refreshToken", sourceSettings.getOauthSettings().getRefreshToken());
oauthSettings.put("serverUrl", sourceSettings.getOauthSettings().getServerUrl());
oauthSettings.put("tokenEndpoint", sourceSettings.getOauthSettings().getTokenEndpoint());
JSONObject settings = new JSONObject();
settings.put("oauthSettings", oauthSettings);
for (DataSourceFile sourceFile : getFileRoots()) {
String path = sourceFile.getFile().getPath();
JSONObject file = new JSONObject();
JSONArray sheets = new JSONArray();
file.put("path", path);