Commit 457b7ea7 authored by Matija Obreza's avatar Matija Obreza

Fixed #5

parent 19e43098
......@@ -153,7 +153,7 @@
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>genesys-client-api</artifactId>
<version>0.0.3-SNAPSHOT</version>
<version>0.0.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
......
......@@ -46,6 +46,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import swing2swt.layout.BorderLayout;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class PushDialog extends Dialog {
......@@ -62,14 +64,15 @@ public class PushDialog extends Dialog {
@Autowired
private Settings settings;
@Autowired
private ObjectMapper objectMapper;
@Autowired
protected DataSourceLoader dataSourceLoader;
BlockingQueue<Runnable> linkedBlockingDeque = new LinkedBlockingDeque<Runnable>(
4);
private ThreadPoolExecutor executorService = new ThreadPoolExecutor(1, 2,
30, TimeUnit.SECONDS, linkedBlockingDeque,
new ThreadPoolExecutor.CallerRunsPolicy());
BlockingQueue<Runnable> linkedBlockingDeque = new LinkedBlockingDeque<Runnable>(4);
private ThreadPoolExecutor executorService = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
linkedBlockingDeque, new ThreadPoolExecutor.CallerRunsPolicy());
protected Object result;
protected Shell shell;
......@@ -81,10 +84,8 @@ public class PushDialog extends Dialog {
private List<Future<?>> futures = new ArrayList<Future<?>>();
static {
deleteDataIcon = ImageDescriptor.createFromURL(
PushDialog.class.getResource("/icon/delete.png")).createImage();
parseDataIcon = ImageDescriptor.createFromURL(
PushDialog.class.getResource("/icon/parse.png")).createImage();
deleteDataIcon = ImageDescriptor.createFromURL(PushDialog.class.getResource("/icon/delete.png")).createImage();
parseDataIcon = ImageDescriptor.createFromURL(PushDialog.class.getResource("/icon/parse.png")).createImage();
}
/**
......@@ -94,8 +95,7 @@ public class PushDialog extends Dialog {
* @param style
*/
public PushDialog(Shell parent, int style) {
super(parent, SWT.DIALOG_TRIM | SWT.MAX | SWT.RESIZE
| SWT.APPLICATION_MODAL);
super(parent, SWT.DIALOG_TRIM | SWT.MAX | SWT.RESIZE | SWT.APPLICATION_MODAL);
setText("Push data to Genesys");
}
......@@ -159,8 +159,7 @@ public class PushDialog extends Dialog {
executorService.shutdown();
executorService.shutdownNow();
event.doit = executorService.getActiveCount() == 0;
_log.warn((executorService.getActiveCount())
+ " jobs are still running, refusing to close.");
_log.warn((executorService.getActiveCount()) + " jobs are still running, refusing to close.");
}
});
shell.setSize(691, 383);
......@@ -184,10 +183,8 @@ public class PushDialog extends Dialog {
tltmPushToGenesys.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (SwtUtil.showYesNoPrompt(getParent(),
"Confirmation required",
"Push data to Genesys server?\n"
+ settings.getOauthSettings().getServerUrl())) {
if (SwtUtil.showYesNoPrompt(getParent(), "Confirmation required", "Push data to Genesys server?\n"
+ settings.getOauthSettings().getServerUrl())) {
_log.info("Upsert confirmed");
doPush(GenesysOp.UPSERT);
} else {
......@@ -202,10 +199,8 @@ public class PushDialog extends Dialog {
tltmRemove.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
if (SwtUtil.showYesNoPrompt(getParent(),
"Confirmation required",
"Delete data from Genesys server?\n"
+ settings.getOauthSettings().getServerUrl())) {
if (SwtUtil.showYesNoPrompt(getParent(), "Confirmation required", "Delete data from Genesys server?\n"
+ settings.getOauthSettings().getServerUrl())) {
_log.info("Delete confirmed");
doPush(GenesysOp.DELETE);
} else {
......@@ -247,8 +242,7 @@ public class PushDialog extends Dialog {
});
tltmRadioItemWarning.setText("WARNING");
txtJson = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
txtJson = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
txtJson.setLayoutData(BorderLayout.CENTER);
}
......@@ -256,8 +250,7 @@ public class PushDialog extends Dialog {
protected void doTrialRun() {
_log.info("Decoding data...");
try {
final RowReader rowReader = dataSourceLoader.createRowReader(
dataSourceSheet, dataSource);
final RowReader rowReader = dataSourceLoader.createRowReader(dataSourceSheet, dataSource);
rowReader.setSkipRows(dataSourceSheet.getHeaderRowIndex() + 1);
Future<?> future = executorService.submit(new Runnable() {
......@@ -279,18 +272,14 @@ public class PushDialog extends Dialog {
for (Object[] row : rows) {
count++;
try {
Map<String, ?> accnMap = RowConverter
.toMap(dataSourceSheet, row,
columnDefs);
ObjectNode accnJson = RowConverter
.toJson(accnMap);
Map<String, ?> accnMap = RowConverter.toMap(dataSourceSheet, row, columnDefs);
ObjectNode accnJson = RowConverter.toJson(accnMap);
// Check for instCode
accnJson.get(Api1Constants.Accession.INSTCODE).textValue();
} catch (Throwable e) {
_log.info("Error in row " + count + ". "
+ e.getMessage() + ": "
_log.info("Error in row " + count + ". " + e.getMessage() + ": "
+ ArrayUtils.toString(row));
}
}
......@@ -309,9 +298,7 @@ public class PushDialog extends Dialog {
try {
rowReader.close();
} catch (IOException e) {
_log.error(
"Failed to close rowReader: "
+ e.getMessage(), e);
_log.error("Failed to close rowReader: " + e.getMessage(), e);
}
}
}
......@@ -332,14 +319,12 @@ public class PushDialog extends Dialog {
OAuthSettings oauthSettings = settings.getOauthSettings();
_log.info("Starting push to " + oauthSettings.getServerUrl());
final GenesysClient genesysClient = SpringConfig
.createGenesysClient(oauthSettings);
final GenesysClient genesysClient = SpringConfig.createGenesysClient(oauthSettings);
try {
// Ping the server
genesysClient.me();
final RowReader rowReader = dataSourceLoader.createRowReader(
dataSourceSheet, dataSource);
final RowReader rowReader = dataSourceLoader.createRowReader(dataSourceSheet, dataSource);
rowReader.setSkipRows(dataSourceSheet.getHeaderRowIndex() + 1);
Future<?> future = executorService.submit(new Runnable() {
......@@ -361,38 +346,28 @@ public class PushDialog extends Dialog {
_log.info("Batch start at row " + count);
for (Object[] row : rows) {
count++;
_log.debug(count + ": "
+ ArrayUtils.toString(row));
Map<String, ?> accnMap = RowConverter.toMap(
dataSourceSheet, row, columnDefs);
_log.debug(count + ": " + ArrayUtils.toString(row));
Map<String, ?> accnMap = RowConverter.toMap(dataSourceSheet, row, columnDefs);
try {
ObjectNode accnJson = RowConverter
.toJson(accnMap);
final String instCode = accnJson.get(
Api1Constants.Accession.INSTCODE).textValue();
ObjectNode accnJson = RowConverter.toJson(accnMap);
final String instCode = accnJson.get(Api1Constants.Accession.INSTCODE).textValue();
List<ObjectNode> instCodeBatch = null;
synchronized (instCodeMap) {
instCodeBatch = instCodeMap
.get(instCode);
instCodeBatch = instCodeMap.get(instCode);
if (instCodeBatch == null) {
instCodeMap
.put(instCode,
instCodeBatch = new ArrayList<ObjectNode>());
instCodeMap.put(instCode, instCodeBatch = new ArrayList<ObjectNode>());
}
}
synchronized (instCodeBatch) {
instCodeBatch.add(accnJson);
if (instCodeBatch.size() >= BATCH_SIZE) {
doAsyncPush(operation,
genesysClient, instCode,
instCodeBatch);
doAsyncPush(operation, genesysClient, instCode, instCodeBatch);
}
}
} catch (GenesysJSONIncompleteException e) {
_log.info("Ignoring incomplete accession "
+ ArrayUtils.toString(row));
_log.info("Ignoring incomplete accession " + ArrayUtils.toString(row));
}
}
......@@ -404,8 +379,7 @@ public class PushDialog extends Dialog {
for (final String instCode : instCodeMap.keySet()) {
List<ObjectNode> accns = instCodeMap.get(instCode);
if (accns.size() > 0) {
doAsyncPush(operation, genesysClient, instCode,
accns);
doAsyncPush(operation, genesysClient, instCode, accns);
}
}
......@@ -415,8 +389,7 @@ public class PushDialog extends Dialog {
_log.error("Error reading row " + rowReader.getRowCount());
_log.error(e.getMessage(), e);
} catch (GenesysJSONException e) {
_log.error("Genesys JSON conversion failed in batch"
+ count);
_log.error("Genesys JSON conversion failed in batch" + count);
_log.error(e.getMessage(), e);
} catch (InterruptedException e) {
_log.info("Execution was interrupted");
......@@ -427,9 +400,7 @@ public class PushDialog extends Dialog {
try {
rowReader.close();
} catch (IOException e) {
_log.error(
"Failed to close rowReader: "
+ e.getMessage(), e);
_log.error("Failed to close rowReader: " + e.getMessage(), e);
}
}
}
......@@ -466,8 +437,7 @@ public class PushDialog extends Dialog {
this.columnDefs = columnDefs;
}
private void doAsyncPush(final GenesysOp operation,
final GenesysClient genesysClient, final String instCode,
private void doAsyncPush(final GenesysOp operation, final GenesysClient genesysClient, final String instCode,
final Collection<ObjectNode> instCodeBatch) {
if (instCodeBatch.size() == 0) {
_log.debug("Nothing to push");
......@@ -475,15 +445,13 @@ public class PushDialog extends Dialog {
}
Future<?> future = executorService.submit(new Runnable() {
final ArrayList<ObjectNode> accns = new ArrayList<ObjectNode>(
instCodeBatch);
final ArrayList<ObjectNode> accns = new ArrayList<ObjectNode>(instCodeBatch);
final GenesysOp op = operation;
@Override
public void run() {
try {
_log.info("Pushing data for instCode=" + instCode
+ " size=" + accns.size());
_log.info("Pushing data for instCode=" + instCode + " size=" + accns.size());
if (_log.isDebugEnabled()) {
for (ObjectNode o : accns) {
_log.debug(o);
......@@ -491,10 +459,9 @@ public class PushDialog extends Dialog {
}
if (op == GenesysOp.UPSERT)
genesysClient.updateAccessions(instCode, accns);
genesysClient.updateAccessions(instCode, objectMapper.writeValueAsString(accns));
else if (op == GenesysOp.DELETE)
genesysClient.deleteAccessions(instCode,
GenesysClient.toArrayNode(accns));
genesysClient.deleteAccessionsByName(instCode, objectMapper.writeValueAsString(accns));
} catch (OAuthAuthenticationException e) {
_log.error(e.getMessage(), e);
......@@ -502,6 +469,8 @@ public class PushDialog extends Dialog {
_log.error(e.getMessage(), e);
} catch (InterruptedException e) {
_log.info("Push was interrupted.");
} catch (JsonProcessingException e) {
_log.error(e.getMessage(), e);
}
}
});
......
......@@ -27,6 +27,7 @@ import org.genesys2.anno.model.OAuthSettings;
import org.genesys2.anno.model.Settings;
import org.genesys2.client.oauth.GenesysApiException;
import org.genesys2.client.oauth.GenesysClient;
import org.genesys2.client.oauth.HttpRedirectException;
import org.genesys2.client.oauth.OAuthAuthenticationException;
import org.genesys2.client.oauth.PleaseRetryException;
import org.scribe.exceptions.OAuthConnectionException;
......@@ -228,14 +229,16 @@ public class SettingsDialog extends Dialog {
}
try {
genesysClient.me();
String res = genesysClient.me();
System.err.println("/me: " + res);
oauthSettings.setAccessToken(genesysClient.getAccessToken().getToken());
SwtUtil.showMessageBox(getParent(), "OAuth", "Tokens are up to date.");
} catch (OAuthConnectionException e) {
_log.error(e.getMessage(), e);
SwtUtil.showMessageBox(getParent(), "Problem connecting to server", "Could not access remote service at:\n" + oauthSettings.getServerUrl() + "\n\n" + e.getMessage());
SwtUtil.showMessageBox(getParent(), "Problem connecting to server", "Could not access remote service at:\n"
+ oauthSettings.getServerUrl() + "\n\n" + e.getMessage());
} catch (OAuthAuthenticationException e) {
_log.warn(e.getMessage());
String authorizationUrl = genesysClient.getAuthorizationUrl(null);
......@@ -252,7 +255,13 @@ public class SettingsDialog extends Dialog {
SwtUtil.showMessageBox(getParent(), "OAuth error", e1.getMessage());
}
}
} catch (HttpRedirectException e) {
_log.error(e.getMessage(), e);
SwtUtil.showMessageBox(
getParent(),
"Server redirect",
"Server is redirecting to a different URL. Check your Genesys Server URL!\n\n"
+ e.getRedirectUrl());
} catch (PleaseRetryException e) {
_log.error("Please retry", e);
} catch (GenesysApiException e) {
......@@ -263,37 +272,59 @@ public class SettingsDialog extends Dialog {
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue observeTextTxtGenesysURLObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtGenesysURL);
IObservableValue oauthSettingsserverUrlSettingsObserveValue = BeanProperties.value("oauthSettings.serverUrl").observe(settings);
bindingContext.bindValue(observeTextTxtGenesysURLObserveWidget, oauthSettingsserverUrlSettingsObserveValue, null, null);
IObservableValue observeTextTxtGenesysURLObserveWidget = WidgetProperties.text(SWT.Modify).observe(
txtGenesysURL);
IObservableValue oauthSettingsserverUrlSettingsObserveValue = BeanProperties.value("oauthSettings.serverUrl")
.observe(settings);
bindingContext.bindValue(observeTextTxtGenesysURLObserveWidget, oauthSettingsserverUrlSettingsObserveValue,
null, null);
//
IObservableValue observeTextTxtAuthorizationEndpointObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtAuthorizationEndpoint);
IObservableValue oauthSettingsauthorizationEndpointSettingsObserveValue = BeanProperties.value("oauthSettings.authorizationEndpoint").observe(settings);
bindingContext.bindValue(observeTextTxtAuthorizationEndpointObserveWidget, oauthSettingsauthorizationEndpointSettingsObserveValue, null, null);
IObservableValue observeTextTxtAuthorizationEndpointObserveWidget = WidgetProperties.text(SWT.Modify).observe(
txtAuthorizationEndpoint);
IObservableValue oauthSettingsauthorizationEndpointSettingsObserveValue = BeanProperties.value(
"oauthSettings.authorizationEndpoint").observe(settings);
bindingContext.bindValue(observeTextTxtAuthorizationEndpointObserveWidget,
oauthSettingsauthorizationEndpointSettingsObserveValue, null, null);
//
IObservableValue observeTextTxtTokenEndpointObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtTokenEndpoint);
IObservableValue oauthSettingstokenEndpointSettingsObserveValue = BeanProperties.value("oauthSettings.tokenEndpoint").observe(settings);
bindingContext.bindValue(observeTextTxtTokenEndpointObserveWidget, oauthSettingstokenEndpointSettingsObserveValue, null, null);
IObservableValue observeTextTxtTokenEndpointObserveWidget = WidgetProperties.text(SWT.Modify).observe(
txtTokenEndpoint);
IObservableValue oauthSettingstokenEndpointSettingsObserveValue = BeanProperties.value(
"oauthSettings.tokenEndpoint").observe(settings);
bindingContext.bindValue(observeTextTxtTokenEndpointObserveWidget,
oauthSettingstokenEndpointSettingsObserveValue, null, null);
//
IObservableValue observeTextTxtApiUrlObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtApiUrl);
IObservableValue oauthSettingsapiUrlSettingsObserveValue = BeanProperties.value("oauthSettings.apiUrl").observe(settings);
bindingContext.bindValue(observeTextTxtApiUrlObserveWidget, oauthSettingsapiUrlSettingsObserveValue, null, null);
IObservableValue oauthSettingsapiUrlSettingsObserveValue = BeanProperties.value("oauthSettings.apiUrl")
.observe(settings);
bindingContext
.bindValue(observeTextTxtApiUrlObserveWidget, oauthSettingsapiUrlSettingsObserveValue, null, null);
//
IObservableValue observeTextTxtAccessTokenObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtAccessToken);
IObservableValue oauthSettingsaccessTokenSettingsObserveValue = BeanProperties.value("oauthSettings.accessToken").observe(settings);
bindingContext.bindValue(observeTextTxtAccessTokenObserveWidget, oauthSettingsaccessTokenSettingsObserveValue, null, null);
IObservableValue observeTextTxtAccessTokenObserveWidget = WidgetProperties.text(SWT.Modify).observe(
txtAccessToken);
IObservableValue oauthSettingsaccessTokenSettingsObserveValue = BeanProperties.value(
"oauthSettings.accessToken").observe(settings);
bindingContext.bindValue(observeTextTxtAccessTokenObserveWidget, oauthSettingsaccessTokenSettingsObserveValue,
null, null);
//
IObservableValue observeTextTxtRefreshTokenObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtRefreshToken);
IObservableValue oauthSettingsrefreshTokenSettingsObserveValue = BeanProperties.value("oauthSettings.refreshToken").observe(settings);
bindingContext.bindValue(observeTextTxtRefreshTokenObserveWidget, oauthSettingsrefreshTokenSettingsObserveValue, null, null);
IObservableValue observeTextTxtRefreshTokenObserveWidget = WidgetProperties.text(SWT.Modify).observe(
txtRefreshToken);
IObservableValue oauthSettingsrefreshTokenSettingsObserveValue = BeanProperties.value(
"oauthSettings.refreshToken").observe(settings);
bindingContext.bindValue(observeTextTxtRefreshTokenObserveWidget,
oauthSettingsrefreshTokenSettingsObserveValue, null, null);
//
IObservableValue observeTextTxtClientKeyObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtClientKey);
IObservableValue oauthSettingsclientKeySettingsObserveValue = BeanProperties.value("oauthSettings.clientKey").observe(settings);
bindingContext.bindValue(observeTextTxtClientKeyObserveWidget, oauthSettingsclientKeySettingsObserveValue, null, null);
IObservableValue oauthSettingsclientKeySettingsObserveValue = BeanProperties.value("oauthSettings.clientKey")
.observe(settings);
bindingContext.bindValue(observeTextTxtClientKeyObserveWidget, oauthSettingsclientKeySettingsObserveValue,
null, null);
//
IObservableValue observeTextTxtClientSecretObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtClientSecret);
IObservableValue oauthSettingsclientSecretSettingsObserveValue = BeanProperties.value("oauthSettings.clientSecret").observe(settings);
bindingContext.bindValue(observeTextTxtClientSecretObserveWidget, oauthSettingsclientSecretSettingsObserveValue, null, null);
IObservableValue observeTextTxtClientSecretObserveWidget = WidgetProperties.text(SWT.Modify).observe(
txtClientSecret);
IObservableValue oauthSettingsclientSecretSettingsObserveValue = BeanProperties.value(
"oauthSettings.clientSecret").observe(settings);
bindingContext.bindValue(observeTextTxtClientSecretObserveWidget,
oauthSettingsclientSecretSettingsObserveValue, null, null);
//
return bindingContext;
}
......
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