Commit 11a465a0 authored by Matija Obreza's avatar Matija Obreza
Browse files

Start "trial-run" when Push dialog opens

parent 3393dcbe
......@@ -179,7 +179,7 @@ public class AppWindow {
private static Image spreadsheetIcon;
private static Image openPackageIcon;
private static Image automapColumnsIcon;
private static Image pushDataIcon;
static Image pushDataIcon;
private static Image loadWorkspaceIcon;
private static Image saveWorkspaceIcon;
private static Image settingsIcon;
......
......@@ -22,7 +22,6 @@ import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
......@@ -40,13 +39,10 @@ import org.genesys2.client.oauth.PleaseRetryException;
import swing2swt.layout.BorderLayout;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class PushDialog extends Dialog {
private static final Logger _log = Logger.getLogger(PushDialog.class);
private static final ObjectMapper mapper = new ObjectMapper();
protected static final int BATCH_SIZE = 25;
private Settings settings = AppConfig.instance.getSettings();
......@@ -93,7 +89,7 @@ public class PushDialog extends Dialog {
shell.layout();
// create worker
createWorker();
doTrialRun();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
......@@ -104,47 +100,6 @@ public class PushDialog extends Dialog {
return result;
}
/**
* Initializes a streamed reader for {@link #dataSourceSheet} and generates
* JSON
*/
private void createWorker() {
this.executorService.execute(new Runnable() {
@Override
public void run() {
try {
List<Object[]> rows = dataSourceLoader.loadDataRows(dataSourceSheet, 200);
_log.info("Loaded rows " + rows.size());
List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
int counter = 0, total = rows.size();
for (Object[] row : rows) {
counter++;
list.add(RowConverter.toMap(dataSourceSheet, row, columnDefs));
_log.info("Converted row " + counter + " of " + total);
}
final JsonNode genesysJson = RowConverter.toGenesysJson(list);
final String x = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(genesysJson);
_log.info("Generated JSON: " + x);
} catch (final Throwable e) {
_log.error(e.getMessage(), e);
shell.getDisplay().syncExec(new Runnable() {
@Override
public void run() {
MessageBox mb = new MessageBox(shell);
mb.setText("Error loading rows");
mb.setMessage(e.getMessage());
mb.open();
}
});
}
}
});
}
/**
* Create contents of the dialog.
*/
......@@ -170,12 +125,9 @@ public class PushDialog extends Dialog {
shell.setText(getText());
shell.setLayout(new BorderLayout(0, 0));
ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
toolBar.setLayoutData(BorderLayout.NORTH);
ToolItem tltmReload = new ToolItem(toolBar, SWT.NONE);
tltmReload.setText("Reload");
ToolItem tltmPushToGenesys = new ToolItem(toolBar, SWT.NONE);
tltmPushToGenesys.addSelectionListener(new SelectionAdapter() {
@Override
......@@ -184,12 +136,75 @@ public class PushDialog extends Dialog {
}
});
tltmPushToGenesys.setText("Upload");
tltmPushToGenesys.setImage(AppWindow.pushDataIcon);
txtJson = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
txtJson.setLayoutData(BorderLayout.CENTER);
}
protected void doTrialRun() {
_log.info("Decoding data...");
try {
final RowReader rowReader = dataSourceLoader.createRowReader(dataSourceSheet);
rowReader.setSkipRows(dataSourceSheet.getHeaderRowIndex() + 1);
executorService.execute(new Runnable() {
@Override
public void run() {
int count = 0;
try {
do {
List<Object[]> rows = rowReader.readRows(10);
if (rows.size() == 0) {
_log.info("Exhausted data source.");
break;
}
if (count % 100 == 0)
_log.info("Examining data at row " + count);
for (Object[] row : rows) {
count++;
try {
Map<String, ?> accnMap = RowConverter.toMap(dataSourceSheet, row, columnDefs);
ObjectNode accnJson = RowConverter.toJson(accnMap);
final String instCode = accnJson.get("instCode").textValue();
} catch (Throwable e) {
_log.warn("Error in row " + count + ". " + e.getMessage() + ": " + ArrayUtils.toString(row));
}
}
Thread.sleep(10);
} while (true);
_log.info("Done queuing parse jobs!");
} catch (IOException e) {
_log.error(e.getMessage(), e);
} catch (InterruptedException e) {
_log.info("Execution was interrupted");
} finally {
try {
rowReader.close();
} catch (IOException e) {
_log.error("Failed to close rowReader: " + e.getMessage(), e);
}
}
}
});
} catch (IOException e) {
_log.error(e.getMessage(), e);
} catch (UnsupportedDataFormatException e) {
_log.error(e.getMessage(), e);
} finally {
_log.info("Done.");
}
}
protected void doPush() {
OAuthSettings oauthSettings = settings.getOauthSettings();
......@@ -287,6 +302,8 @@ public class PushDialog extends Dialog {
_log.error(e.getMessage(), e);
} catch (UnsupportedDataFormatException e) {
_log.error(e.getMessage(), e);
} catch (Throwable e) {
_log.error(e.getMessage(), e);
} finally {
_log.info("Done.");
}
......
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