data-miner-manager/src/main/java/org/gcube/portlets/user/dataminermanager/client/computations/ComputationsViewerPanel.java

475 lines
14 KiB
Java

package org.gcube.portlets.user.dataminermanager.client.computations;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.gcube.portlets.user.dataminermanager.client.common.EventBusProvider;
import org.gcube.portlets.user.dataminermanager.client.events.ComputationDataEvent;
import org.gcube.portlets.user.dataminermanager.client.events.DataMinerWorkAreaEvent;
import org.gcube.portlets.user.dataminermanager.client.events.RefreshDataMinerWorkAreaEvent;
import org.gcube.portlets.user.dataminermanager.client.events.UIStateEvent;
import org.gcube.portlets.user.dataminermanager.client.util.UtilsGXT3;
import org.gcube.portlets.user.dataminermanager.shared.data.ComputationData;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.container.HtmlLayoutContainer;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.FieldSet;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
/**
*
* @author Giancarlo Panichi email: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class ComputationsViewerPanel extends FramedPanel {
private VerticalLayoutContainer v;
private ComputationData computationData;
private ArrayList<String> urlList;
private LinkedHashMap<String, String> urlFileNameMap;
public ComputationsViewerPanel() {
super();
Log.debug("ComputationsPanel");
// msgs = GWT.create(ServiceCategoryMessages.class);
init();
bindToEvents();
create();
}
private void init() {
setItemId("ComputationsViewerPanel");
forceLayoutOnResize = true;
setBodyBorder(false);
setBorders(false);
setBodyStyle("backgroundColor:white;");
setHeaderVisible(true);
setResize(true);
setAnimCollapse(false);
setHeadingText("Computations Viewer");
}
private void create() {
v = new VerticalLayoutContainer();
v.setScrollMode(ScrollMode.AUTO);
add(v);
}
private void bindToEvents() {
EventBusProvider.INSTANCE.addHandler(UIStateEvent.TYPE,
new UIStateEvent.UIStateEventHandler() {
@Override
public void onChange(UIStateEvent event) {
Log.debug("Catch UIStateEvent: " + event);
manageStateEvents(event);
}
});
EventBusProvider.INSTANCE.addHandler(DataMinerWorkAreaEvent.TYPE,
new DataMinerWorkAreaEvent.DataMinerWorkAreaEventHandler() {
@Override
public void onChange(DataMinerWorkAreaEvent event) {
Log.debug("Catch DataMinerWorkAreaEvent: " + event);
manageDataMinerWorkAreaEvents(event);
}
});
EventBusProvider.INSTANCE
.addHandler(
RefreshDataMinerWorkAreaEvent.TYPE,
new RefreshDataMinerWorkAreaEvent.RefreshDataMinerWorkAreaEventHandler() {
@Override
public void onRefresh(
RefreshDataMinerWorkAreaEvent event) {
Log.debug("Catch RefreshDataMinerWorkAreaEvent: "
+ event);
manageRefreshDataMinerWorkAreaEvents(event);
}
});
EventBusProvider.INSTANCE.addHandler(ComputationDataEvent.getType(),
new ComputationDataEvent.ComputationDataEventHandler() {
@Override
public void onComputationData(ComputationDataEvent event) {
Log.debug("Catch ComputationDataEvent: " + event);
showOutput(event);
}
});
}
private void manageStateEvents(UIStateEvent event) {
switch (event.getUiStateType()) {
case START:
break;
case WAITING:
break;
case UPDATE:
break;
default:
break;
}
}
private void manageDataMinerWorkAreaEvents(DataMinerWorkAreaEvent event) {
switch (event.getDataMinerWorkAreaEventType()) {
case OPEN:
case UPDATE:
resetPanel();
break;
default:
break;
}
}
private void manageRefreshDataMinerWorkAreaEvents(
RefreshDataMinerWorkAreaEvent event) {
switch (event.getDataMinerWorkAreaElementType()) {
case Computations:
resetPanel();
break;
case InputDataSets:
break;
case OutputDataSets:
break;
default:
break;
}
}
public void resetPanel() {
v.clear();
SimpleContainer sectionTitle = new SimpleContainer();
HtmlLayoutContainer title = new HtmlLayoutContainer(
"<br><center>No computation selected.</center>");
sectionTitle.add(title);
v.add(sectionTitle, new VerticalLayoutData(-1, -1, new Margins(20)));
computationData = null;
forceLayout();
}
private void showOutput(ComputationDataEvent event) {
v.clear();
computationData = event.getComputationData();
addComputationData();
}
private void addComputationData() {
}
private void createView(){
if (computationData == null) {
Log.error("ComputationData is null!");
UtilsGXT3.alert("Error",
"No information on computation is retrieved!");
return;
}
SimpleContainer sectionTitle = new SimpleContainer();
// title
HtmlLayoutContainer title = new HtmlLayoutContainer(
"<center>.: Computation Report of <b>"
+ computationData.getComputationId().getId()
+ "</b></center>");
sectionTitle.add(title, new MarginData());
sectionTitle.getElement().getStyle().setMarginRight(20, Unit.PX);
// v.add(sectionTitle, new VerticalLayoutData(1, -1, new Margins(0)));
v.add(sectionTitle, new VerticalLayoutData(-1, -1, new Margins(10)));
if (computationData.getOutputParameters() != null
&& !computationData.getOutputParameters().isEmpty()) {
VerticalLayoutContainer outputVBox = new VerticalLayoutContainer();
LinkedHashMap<String, String> output = computationData
.getOutputParameters();
for (String key : output.keySet()) {
TextField textField = new TextField();
textField.setValue(output.get(key));
textField.setReadOnly(true);
FieldLabel fieldLabel = new FieldLabel(textField, key);
fieldLabel.setLabelWidth(200);
fieldLabel.setLabelWordWrap(true);
outputVBox.add(fieldLabel, new VerticalLayoutData(1, -1,
new Margins(4)));
}
FieldSet outputFieldSet = new FieldSet();
outputFieldSet.setHeadingText("Output Result");
outputFieldSet.setCollapsible(true);
outputFieldSet.add(outputVBox);
outputFieldSet.getElement().getStyle().setMarginRight(20, Unit.PX);
v.add(outputFieldSet, new VerticalLayoutData(-1, -1,
new Margins(10)));
}
if (computationData.getInputParameters() != null
&& !computationData.getInputParameters().isEmpty()) {
VerticalLayoutContainer inputVBox = new VerticalLayoutContainer();
LinkedHashMap<String, String> input = computationData
.getInputParameters();
for (String key : input.keySet()) {
TextField textField = new TextField();
textField.setValue(input.get(key));
textField.setReadOnly(true);
FieldLabel fieldLabel = new FieldLabel(textField, key);
fieldLabel.setLabelWidth(200);
fieldLabel.setLabelWordWrap(true);
inputVBox.add(fieldLabel, new VerticalLayoutData(1, -1,
new Margins(4)));
}
FieldSet inputFieldSet = new FieldSet();
inputFieldSet.setHeadingText("Input Parameters");
inputFieldSet.setCollapsible(true);
inputFieldSet.add(inputVBox);
inputFieldSet.getElement().getStyle().setMarginRight(20, Unit.PX);
v.add(inputFieldSet,
new VerticalLayoutData(-1, -1, new Margins(10)));
}
VerticalLayoutContainer detailsVBox = new VerticalLayoutContainer();
TextField startDateField = new TextField();
startDateField.setValue(computationData.getStartDate());
startDateField.setReadOnly(true);
FieldLabel startDateLabel = new FieldLabel(startDateField, "Start Date");
startDateLabel.setLabelWidth(200);
startDateLabel.setLabelWordWrap(true);
detailsVBox.add(startDateLabel, new VerticalLayoutData(1, -1,
new Margins(4)));
TextField endDateField = new TextField();
endDateField.setValue(computationData.getEndDate());
endDateField.setReadOnly(true);
FieldLabel endDateLabel = new FieldLabel(endDateField, "End Date");
endDateLabel.setLabelWidth(200);
endDateLabel.setLabelWordWrap(true);
detailsVBox.add(endDateLabel, new VerticalLayoutData(1, -1,
new Margins(4)));
TextField statusField = new TextField();
statusField.setValue(computationData.getStatus());
statusField.setReadOnly(true);
FieldLabel statusLabel = new FieldLabel(statusField, "Status");
statusLabel.setLabelWidth(200);
statusLabel.setLabelWordWrap(true);
detailsVBox.add(statusLabel, new VerticalLayoutData(1, -1, new Margins(
4)));
TextField vreField = new TextField();
vreField.setValue(computationData.getVre());
vreField.setReadOnly(true);
FieldLabel vreLabel = new FieldLabel(vreField, "VRE");
vreLabel.setLabelWidth(200);
vreLabel.setLabelWordWrap(true);
detailsVBox
.add(vreLabel, new VerticalLayoutData(1, -1, new Margins(4)));
FieldSet detailsFieldSet = new FieldSet();
detailsFieldSet.setHeadingText("Computation Details");
detailsFieldSet.setCollapsible(true);
detailsFieldSet.add(detailsVBox);
detailsFieldSet.getElement().getStyle().setMarginRight(20, Unit.PX);
v.add(detailsFieldSet, new VerticalLayoutData(-1, -1, new Margins(10)));
VerticalLayoutContainer operatorVBox = new VerticalLayoutContainer();
TextField operatorNameField = new TextField();
operatorNameField.setValue(computationData.getComputationId()
.getOperatorName());
operatorNameField.setReadOnly(true);
FieldLabel operatorNameLabel = new FieldLabel(operatorNameField,
"Operator Name");
operatorNameLabel.setLabelWidth(200);
operatorNameLabel.setLabelWordWrap(true);
operatorVBox.add(operatorNameLabel, new VerticalLayoutData(1, -1,
new Margins(4)));
TextArea operatorDescriptionField = new TextArea();
operatorDescriptionField.setHeight(40);
operatorDescriptionField.setValue(computationData
.getOperatorDescription());
operatorDescriptionField.setReadOnly(true);
FieldLabel operatorDescriptionLabel = new FieldLabel(
operatorDescriptionField, "Operator Description");
operatorDescriptionLabel.setLabelWidth(200);
operatorDescriptionLabel.setLabelWordWrap(true);
operatorDescriptionLabel.setHeight(50);
operatorVBox.add(operatorDescriptionLabel, new VerticalLayoutData(1,
-1, new Margins(5)));
FieldSet operatorFieldSet = new FieldSet();
operatorFieldSet.setHeadingText("Operator Details");
operatorFieldSet.setCollapsible(true);
operatorFieldSet.setHeight(130);
operatorFieldSet.add(operatorVBox);
operatorFieldSet.getElement().getStyle().setMarginBottom(120, Unit.PX);
operatorFieldSet.getElement().getStyle().setMarginRight(20, Unit.PX);
v.add(operatorFieldSet, new VerticalLayoutData(-1, -1, new Margins(10)));
forceLayout();
}
/**
*
*/
private void createUrlList() {
urlList = new ArrayList<String>();
urlFileNameMap = new LinkedHashMap<String, String>();
if (computationData.getOutputParameters() != null
&& !computationData.getOutputParameters().isEmpty()) {
LinkedHashMap<String, String> output = computationData
.getOutputParameters();
for (String key : output.keySet()) {
String value = output.get(key);
if (value != null && !value.isEmpty() && value.startsWith("http")) {
urlList.add(value);
}
}
}
if (computationData.getInputParameters() != null
&& !computationData.getInputParameters().isEmpty()) {
LinkedHashMap<String, String> input = computationData
.getInputParameters();
for (String key : input.keySet()) {
String value = input.get(key);
if (value != null && !value.isEmpty()&& value.startsWith("http")) {
urlList.add(value);
}
}
}
if (!urlList.isEmpty()) {
retrieveUrlInformation(null);
} else {
}
}
private void retrieveUrlInformation(String url) {
if (url == null) {
if (urlList != null && !urlList.isEmpty()) {
retrieveFileName(urlList.get(0));
} else {
}
} else {
int index=urlList.indexOf(url);
if(index==-1){
} else {
if(index==urlList.size()-1){
} else {
retrieveFileName(urlList.get(index++));
}
}
}
}
private void retrieveFileName(final String url) {
try {
RequestBuilder builder = new RequestBuilder(RequestBuilder.HEAD,
URL.encode(url));
builder.sendRequest(null, new RequestCallback() {
@Override
public void onResponseReceived(Request request,
Response response) {
if (200 == response.getStatusCode()) {
String contentDisposition = response
.getHeader("Content-Disposition");
// ContentDisposition = "attachment; filename=abc.jpg"
if (contentDisposition != null
&& contentDisposition.indexOf("=") != -1) {
String fileName = contentDisposition.split("=")[1]; // getting
// value
// after
// '='
urlFileNameMap.put(url, fileName);
retrieveUrlInformation(url);
} else {
urlFileNameMap.put(url, null);
retrieveUrlInformation(url);
}
} else {
urlFileNameMap.put(url, null);
retrieveUrlInformation(url);
}
}
@Override
public void onError(Request request, Throwable exception) {
Log.error(exception.getLocalizedMessage());
exception.printStackTrace();
urlFileNameMap.put(url, null);
retrieveUrlInformation(url);
}
});
} catch (RequestException e) {
Log.error(e.getLocalizedMessage());
e.printStackTrace();
UtilsGXT3.alert("Error", "Error retrieving file name from public url!");
}
/*
* URL url = new URL("http://somesite/getFile?id=12345");
* HttpURLConnection conn = (HttpURLConnection) url.openConnection();
* conn.setRequestMethod("GET"); conn.setAllowUserInteraction(false);
* conn.setDoInput(true); conn.setDoOutput(true); conn.connect();
*
* String raw = conn.getHeaderField("Content-Disposition"); // raw =
* "attachment; filename=abc.jpg" if (raw != null && raw.indexOf("=") !=
* -1) { String fileName = raw.split("=")[1]; // getting value after '='
* } else { // fall back to random generated file name? }
*/
}
}