geoportal-data-entry-app/src/main/java/org/gcube/portlets/user/geoportaldataentry/client/ui/report/ReportTemplateToHTML.java

159 lines
4.3 KiB
Java

package org.gcube.portlets.user.geoportaldataentry.client.ui.report;
import com.github.gwtbootstrap.client.ui.Paragraph;
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
Paragraph reportJSON;
boolean toJSONERROR = false;
/**
* Instantiates a new report template to HTML.
*
* @param reportAsJSON the report as JSON
*/
public ReportTemplateToHTML(String reportAsJSON) {
initWidget(uiBinder.createAndBindUi(this));
vpContainer.setVisible(false);
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>" + toPrettyPrintJSON(reportAsJSON) + "</pre>"));
}
}
public static native String toPrettyPrintJSON(String jsonData)/*-{
console.log(jsonData);
var objJSON = JSON.parse(jsonData);
return $wnd.prettyPrintJson.toHtml(objJSON);
}-*/;
/**
* 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;
}
}-*/;
}