Commit b2d02af3 authored by Matija Obreza's avatar Matija Obreza
Browse files

Spring context and beans

parent d13f857b
......@@ -159,6 +159,16 @@
<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>
......
......@@ -79,15 +79,35 @@ 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 {
......@@ -180,13 +200,14 @@ 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 spreadsheetIcon16;
static Image pushDataIcon;
private static Image loadWorkspaceIcon;
private static Image saveWorkspaceIcon;
......@@ -204,13 +225,22 @@ public class AppWindow {
* @param args
*/
public static void main(String[] args) {
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.");
}
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();
}
......@@ -242,6 +272,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();
......@@ -261,12 +292,6 @@ public class AppWindow {
*/
protected void createContents() {
shlGenesysMagic = new Shell();
shlGenesysMagic.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
doShutdown();
}
});
shlGenesysMagic.setMinimumSize(new Point(100, 22));
shlGenesysMagic.setSize(744, 528);
shlGenesysMagic.setText("Genesys Magic");
......@@ -452,7 +477,7 @@ public class AppWindow {
}
}
CTabItem newSheetTab = SheetTabFactory.createComposite(tabFolder, currentSheet);
CTabItem newSheetTab = createTab(tabFolder, currentSheet);
// Load stuff
threadPool.execute(new Runnable() {
......@@ -546,6 +571,26 @@ 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" });
......@@ -582,18 +627,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);
......@@ -601,7 +641,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;
}
......
......@@ -36,6 +36,7 @@ import org.genesys2.client.oauth.GenesysApiException;
import org.genesys2.client.oauth.GenesysClient;
import org.genesys2.client.oauth.OAuthAuthenticationException;
import org.genesys2.client.oauth.PleaseRetryException;
import org.springframework.beans.factory.annotation.Autowired;
import swing2swt.layout.BorderLayout;
......@@ -44,9 +45,12 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
public class PushDialog extends Dialog {
private static final Logger _log = Logger.getLogger(PushDialog.class);
protected static final int BATCH_SIZE = 25;
private Settings settings = AppConfig.instance.getSettings();
protected DataSourceLoader dataSourceLoader = AppConfig.instance.getDataSourceLoader();
@Autowired
private Settings settings;
@Autowired
protected DataSourceLoader dataSourceLoader;
BlockingQueue<Runnable> linkedBlockingDeque = new LinkedBlockingDeque<Runnable>(100);
private ThreadPoolExecutor executorService = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS, linkedBlockingDeque, new ThreadPoolExecutor.CallerRunsPolicy());
......@@ -166,7 +170,7 @@ public class PushDialog extends Dialog {
if (count % 100 == 0)
_log.info("Examining data at row " + count);
for (Object[] row : rows) {
count++;
try {
......@@ -211,7 +215,7 @@ public class PushDialog extends Dialog {
OAuthSettings oauthSettings = settings.getOauthSettings();
_log.info("Starting push to " + oauthSettings.getServerUrl());
final GenesysClient genesysClient = AppConfig.createGenesysClient(oauthSettings);
final GenesysClient genesysClient = SpringConfig.createGenesysClient(oauthSettings);
try {
// Ping the server
genesysClient.me();
......@@ -305,7 +309,7 @@ public class PushDialog extends Dialog {
} catch (UnsupportedDataFormatException e) {
_log.error(e.getMessage(), e);
} catch (Throwable e) {
_log.error(e.getMessage(), e);
_log.error(e.getMessage(), e);
} finally {
_log.info("Done.");
}
......
......@@ -29,6 +29,7 @@ import org.genesys2.client.oauth.GenesysApiException;
import org.genesys2.client.oauth.GenesysClient;
import org.genesys2.client.oauth.OAuthAuthenticationException;
import org.genesys2.client.oauth.PleaseRetryException;
import org.springframework.beans.factory.annotation.Autowired;
import swing2swt.layout.BorderLayout;
......@@ -36,7 +37,9 @@ public class SettingsDialog extends Dialog {
private static final Logger _log = Logger.getLogger(SettingsDialog.class);
private DataBindingContext m_bindingContext;
@Autowired
private Settings settings;
protected Object result;
protected Shell shell;
private Text txtGenesysURL;
......@@ -54,9 +57,8 @@ public class SettingsDialog extends Dialog {
* @param parent
* @param style
*/
public SettingsDialog(Settings settings, Shell parent, int style) {
public SettingsDialog(Shell parent, int style) {
super(parent, style);
this.settings = settings;
setText("Settings");
}
......@@ -218,7 +220,7 @@ public class SettingsDialog extends Dialog {
GenesysClient genesysClient = null;
try {
genesysClient = AppConfig.createGenesysClient(oauthSettings);
genesysClient = SpringConfig.createGenesysClient(oauthSettings);
} catch (IllegalArgumentException e) {
txtClientKey.forceFocus();
return;
......
......@@ -69,16 +69,26 @@ import org.eclipse.swt.widgets.Text;
import org.genesys2.anno.converter.RowConverter;
import org.genesys2.anno.model.Column;
import org.genesys2.anno.model.ColumnDef;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import swing2swt.layout.BorderLayout;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SheetDisplay extends Composite {
private static final Logger _log = Logger.getLogger(SheetDisplay.class);
private DataBindingContext m_bindingContext;
protected DataSourceLoader dataSourceLoader = AppConfig.instance.getDataSourceLoader();
@Autowired
protected DataSourceLoader dataSourceLoader;
@Autowired
private ColumnDefs columnDefs;
private static final ObjectMapper mapper = new ObjectMapper();
private static class ContentProvider implements IStructuredContentProvider {
......@@ -434,7 +444,7 @@ public class SheetDisplay extends Composite {
DataSourceSheet dataSourceSheet = getDataSourceSheet();
if (dataSourceSheet == null)
return;
Map<String, ?> map1 = RowConverter.toMap(dataSourceSheet, row, AppConfig.instance.getColumnDefs());
Map<String, ?> map1 = RowConverter.toMap(dataSourceSheet, row, columnDefs);
try {
final ObjectNode jsonObject = RowConverter.toJson(map1);
final String prettyJson=mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
......
/**
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.anno.gui;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
public final class SheetTabFactory {
private static Image spreadsheetIcon;
static {
spreadsheetIcon = ImageDescriptor.createFromURL(
SheetTabFactory.class.getResource("/icon/spreadsheet16.png"))
.createImage();
}
/**
* @param currentSheet
* @wbp.factory
*/
public static CTabItem createComposite(CTabFolder tabFolder,
DataSourceSheet currentSheet) {
CTabItem tbtmOther = new CTabItem(tabFolder, SWT.NONE);
tbtmOther.setShowClose(true);
tbtmOther.setText(currentSheet.getSheetName());
tbtmOther.setImage(spreadsheetIcon);
final SheetDisplay sheetDisplay = new SheetDisplay(currentSheet, tabFolder, SWT.NONE);
tbtmOther.setControl(sheetDisplay);
tbtmOther.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
sheetDisplay.dispose();
}
});
return tbtmOther;
}
}
......@@ -3,51 +3,78 @@ package org.genesys2.anno.gui;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.genesys2.anno.model.OAuthSettings;
import org.genesys2.anno.model.Settings;
import org.genesys2.anno.parser.CsvDataSourceParser;
import org.genesys2.anno.parser.XlsxDataSourceParser;
import org.genesys2.client.oauth.GenesysClient;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Scope;
/**
* App config with services
*
* @author matijaobreza
* Main Spring configuration
*
*/
public class AppConfig {
public static final AppConfig instance = new AppConfig();
private DataSourceLoaderImpl dataSourceLoader;
private ExecutorService executorService = Executors.newFixedThreadPool(5);
private ColumnDefs columnDefs = new ColumnDefs();
private Settings settings = new Settings();
private AppConfig() {
dataSourceLoader = new DataSourceLoaderImpl();
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "org.genesys2.anno.gui" })
public class SpringConfig {
@Bean
public DataSourceLoader dataSourceLoader() {
DataSourceLoaderImpl dataSourceLoader = new DataSourceLoaderImpl();
dataSourceLoader.registerParser(new XlsxDataSourceParser());
dataSourceLoader.registerParser(new CsvDataSourceParser());
}
public DataSourceLoaderImpl getDataSourceLoader() {
return dataSourceLoader;
}
@Bean
public ExecutorService getExecutorService() {
return executorService;
}
public static void shutdown() {
instance.executorService.shutdown();
return Executors.newFixedThreadPool(5);
}
@Bean
public ColumnDefs getColumnDefs() {
return columnDefs;
System.err.println("Making columnDefs");
return new ColumnDefs();
}
@Bean
public Settings getSettings() {
return settings;
System.err.println("Creating new settings");
return new Settings();
}
@Bean
public DwcaBuilder dwcaBuilder() {
return new DwcaBuilder();
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public SheetDisplay sheetDisplay(DataSourceSheet dataSourceSheet, Composite parent, int style) {
return new SheetDisplay(dataSourceSheet, parent, style);
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public PushDialog pushDialog(Shell shell, int style) {
return new PushDialog(shell, style);
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public SettingsDialog settingsDialog(Shell shell, int style) {
return new SettingsDialog(shell, style);
}
// Not a bean
public static GenesysClient createGenesysClient(OAuthSettings oauthSettings) {
GenesysClient genesysClient = new GenesysClient();
genesysClient.setBaseUrl(oauthSettings.getServerUrl());
......
# Loggers
log4j.rootLogger=WARN, stdout
log4j.rootLogger=DEBUG, stdout
log4j.logger.org.genesys2.anno=DEBUG
# Appender: stdout
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment