192 lines
5.2 KiB
Java
192 lines
5.2 KiB
Java
package org.gcube.portlets.user.geoportaldataentry.client.ui.report;
|
|
|
|
import org.gcube.portlets.user.geoportaldataentry.client.ui.utils.ExternalLib;
|
|
|
|
import com.github.gwtbootstrap.client.ui.AccordionGroup;
|
|
import com.github.gwtbootstrap.client.ui.Paragraph;
|
|
import com.github.gwtbootstrap.client.ui.constants.IconType;
|
|
import com.github.gwtbootstrap.client.ui.event.HiddenEvent;
|
|
import com.github.gwtbootstrap.client.ui.event.HiddenHandler;
|
|
import com.github.gwtbootstrap.client.ui.event.ShowEvent;
|
|
import com.github.gwtbootstrap.client.ui.event.ShowHandler;
|
|
import com.google.gwt.core.client.GWT;
|
|
import com.google.gwt.json.client.JSONArray;
|
|
import com.google.gwt.json.client.JSONObject;
|
|
import com.google.gwt.json.client.JSONParser;
|
|
import com.google.gwt.json.client.JSONValue;
|
|
import com.google.gwt.uibinder.client.UiBinder;
|
|
import com.google.gwt.uibinder.client.UiField;
|
|
import com.google.gwt.user.client.ui.Composite;
|
|
import com.google.gwt.user.client.ui.HTML;
|
|
import com.google.gwt.user.client.ui.VerticalPanel;
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
|
|
/**
|
|
* The Class ReportTemplateToHTML.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
*
|
|
* Jan 25, 2021
|
|
*/
|
|
public class ReportTemplateToHTML extends Composite {
|
|
|
|
private static ReportTemplateToHTMLUiBinder uiBinder = GWT.create(ReportTemplateToHTMLUiBinder.class);
|
|
|
|
/**
|
|
* The Interface ReportTemplateToHTMLUiBinder.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
*
|
|
* Jan 25, 2021
|
|
*/
|
|
interface ReportTemplateToHTMLUiBinder extends UiBinder<Widget, ReportTemplateToHTML> {
|
|
}
|
|
|
|
@UiField
|
|
VerticalPanel vpContainer;
|
|
|
|
@UiField
|
|
VerticalPanel htmlContainer;
|
|
|
|
@UiField
|
|
AccordionGroup showReportAsJSON;
|
|
|
|
@UiField
|
|
Paragraph reportJSON;
|
|
|
|
boolean toJSONERROR = false;
|
|
|
|
/**
|
|
* Instantiates a new report template to HTML.
|
|
*
|
|
* @param reportAsJSON the report as JSON
|
|
*/
|
|
public ReportTemplateToHTML(String reportAsJSON, boolean openJSONReport) {
|
|
initWidget(uiBinder.createAndBindUi(this));
|
|
vpContainer.setVisible(false);
|
|
showReportAsJSON.setDefaultOpen(openJSONReport);
|
|
//showReportAsJSON.setIconPosition(IconPosition.RIGHT);
|
|
|
|
if(openJSONReport) {
|
|
showReportAsJSON.setIcon(IconType.ARROW_DOWN);
|
|
}else {
|
|
showReportAsJSON.setIcon(IconType.ARROW_RIGHT);
|
|
}
|
|
|
|
showReportAsJSON.addShowHandler(new ShowHandler() {
|
|
|
|
@Override
|
|
public void onShow(ShowEvent showEvent) {
|
|
showReportAsJSON.setIcon(IconType.ARROW_DOWN);
|
|
|
|
}
|
|
});
|
|
|
|
showReportAsJSON.addHiddenHandler(new HiddenHandler() {
|
|
|
|
@Override
|
|
public void onHidden(HiddenEvent hiddenEvent) {
|
|
showReportAsJSON.setIcon(IconType.ARROW_RIGHT);
|
|
|
|
}
|
|
});
|
|
|
|
|
|
if (reportAsJSON != null) {
|
|
GWT.log("report is: " + reportAsJSON);
|
|
vpContainer.setVisible(true);
|
|
|
|
try {
|
|
JSONValue jsonObj = JSONParser.parse(reportAsJSON);
|
|
JSONObject json = (JSONObject) jsonObj;
|
|
JSONValue valueChildren = json.get("children");
|
|
JSONArray jsonChildren = (JSONArray) valueChildren;
|
|
|
|
String toTableHTML = null;
|
|
for (int i = 0; i < jsonChildren.size(); i++) {
|
|
JSONObject jsO = (JSONObject) jsonChildren.get(i);
|
|
|
|
if (jsO.containsKey("children")) {
|
|
toTableHTML = jsonToHTML(jsO.get("children").toString());
|
|
} else {
|
|
toTableHTML = jsonToHTML(jsO.toString());
|
|
}
|
|
|
|
htmlContainer.add(new HTML(toTableHTML));
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
GWT.log("error: " + e.getMessage());
|
|
}
|
|
reportJSON.add(new HTML("<pre>" + ExternalLib.toPrettyPrintJSON(reportAsJSON) + "</pre>"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Json to HTML.
|
|
*
|
|
* @param jsonTxt the json txt
|
|
* @return the string
|
|
*/
|
|
public static native String jsonToHTML(String jsonTxt)/*-{
|
|
try {
|
|
var jsonObj = JSON.parse(jsonTxt);
|
|
|
|
if (jsonObj.length == undefined)
|
|
jsonObj = [ jsonObj ]
|
|
//console.log(jsonObj.length)
|
|
|
|
// EXTRACT VALUE FOR HTML HEADER.
|
|
var col = [];
|
|
for (var i = 0; i < jsonObj.length; i++) {
|
|
for ( var key in jsonObj[i]) {
|
|
//console.log('key json' +key)
|
|
if (col.indexOf(key) === -1) {
|
|
col.push(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
// CREATE DYNAMIC TABLE.
|
|
var table = document.createElement("table");
|
|
try {
|
|
table.classList.add("my-html-table");
|
|
|
|
} catch (e) {
|
|
console.log('invalid css add', e);
|
|
}
|
|
|
|
// ADD JSON DATA TO THE TABLE AS ROWS.
|
|
for (var i = 0; i < col.length; i++) {
|
|
tr = table.insertRow(-1);
|
|
var firstCell = tr.insertCell(-1);
|
|
//firstCell.style.cssText="font-weight: bold; text-align: center; vertical-align: middle;";
|
|
firstCell.innerHTML = col[i];
|
|
for (var j = 0; j < jsonObj.length; j++) {
|
|
var tabCell = tr.insertCell(-1);
|
|
var theValue = jsonObj[j][col[i]];
|
|
//console.log("the value: "+theValue);
|
|
if (Object.prototype.toString.call(theValue) === '[object Array]') {
|
|
var formattedValueArray = "";
|
|
for (var k = 0; k < theValue.length; k++) {
|
|
var theValueArray = theValue[k];
|
|
//console.log(theValueArray);
|
|
formattedValueArray += theValueArray + "<br>";
|
|
}
|
|
tabCell.innerHTML = formattedValueArray;
|
|
} else {
|
|
tabCell.innerHTML = theValue;
|
|
}
|
|
}
|
|
}
|
|
|
|
return table.outerHTML;
|
|
} catch (e) {
|
|
console.log('invalid json', e);
|
|
return jsonTxt;
|
|
}
|
|
|
|
}-*/;
|
|
}
|