/** * */ package org.gcube.portlets.user.performfishanalytics.client.viewannualbinder; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.gcube.portlets.user.performfishanalytics.client.DataMinerAlgorithms; import org.gcube.portlets.user.performfishanalytics.client.PerformFishAnalyticsServiceAsync; import org.gcube.portlets.user.performfishanalytics.client.controllers.PerformFishAnalyticsController; import org.gcube.portlets.user.performfishanalytics.client.event.CallAlgorithmEvent; import org.gcube.portlets.user.performfishanalytics.client.view.util.CorrelationValueToColourUtil; import org.gcube.portlets.user.performfishanalytics.shared.FileContentType; import org.gcube.portlets.user.performfishanalytics.shared.KPI; import org.gcube.portlets.user.performfishanalytics.shared.OutputFile; import org.gcube.portlets.user.performfishanalytics.shared.csv.CSVFile; import org.gcube.portlets.user.performfishanalytics.shared.csv.CSVRow; import org.gcube.portlets.user.performfishanalytics.shared.dataminer.DataMinerResponse; import com.github.gwtbootstrap.client.ui.Alert; import com.github.gwtbootstrap.client.ui.ControlGroup; import com.github.gwtbootstrap.client.ui.ListBox; import com.github.gwtbootstrap.client.ui.constants.AlertType; import com.github.gwtbootstrap.client.ui.constants.ControlGroupType; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.logical.shared.AttachEvent; import com.google.gwt.event.shared.HandlerManager; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.ComplexPanel; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; /** * The Class AnalyticsPanelResult. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Jan 29, 2019 */ public class AnalyticsAnnualPanelResult extends Composite { /** * */ private static AnalyticsPanelResultUiBinder uiBinder = GWT.create(AnalyticsPanelResultUiBinder.class); /** * The Interface AnalyticsPanelResultUiBinder. * * @author Francesco Mangiacrapa at ISTI-CNR * (francesco.mangiacrapa@isti.cnr.it) Jan 30, 2019 */ interface AnalyticsPanelResultUiBinder extends UiBinder { } @UiField HTMLPanel field_html_panel; @UiField VerticalPanel field_parameters_container; @UiField VerticalPanel uib_vp_correlation_results_container; @UiField VerticalPanel field_unary_algorithm_container; @UiField VerticalPanel field_binary_algorithm; @UiField VerticalPanel field_binary_algorithm_container; @UiField VerticalPanel field_unary_algorithm; @UiField Label uib_label_focus_id; @UiField ControlGroup cg_focus_id_correlation; @UiField ListBox field_list_focus_id_correlation; private Map dataInputParameters; private DataMinerResponse dmResponse; private Map> kpiMapPointers = new HashMap>(); private List selectedKPIs; private List selectedAreas; // private HandlerManager theEventBus; private Map csvGenerated = new HashMap(); /** * Because this class has a default constructor, it can be used as a binder * template. In other words, it can be used in other *.ui.xml files as * follows: * Hello! Note that * depending on the widget that is used, it may be necessary to implement * HasHTML instead of HasText. */ public AnalyticsAnnualPanelResult(HandlerManager eventBus) { initWidget(uiBinder.createAndBindUi(this)); // this.theEventBus = eventBus; } /** * Adds the selected areas. * * @param listAreas * the list areas */ public void addSelectedAreas(List listAreas) { this.selectedAreas = listAreas; } /** * Gets the data miner response. * * @return the data miner response */ public DataMinerResponse getDataMinerResponse() { return dmResponse; } /** * Gets the data input parameters. * * @return the data input parameters */ public Map getDataInputParameters() { return dataInputParameters; } /** * Show alert. * * @param error * the error * @param type * the type * @param closable * the closable * @param panel * the panel */ private void showAlert(String error, AlertType type, boolean closable, ComplexPanel panel) { Alert alert = new Alert(error); alert.setType(type); alert.setClose(closable); alert.getElement().getStyle().setMargin(10, Unit.PX); panel.add(alert); } /** * Adds the selected kp is. * * @param selectedKPIs * the selected kp is */ public void addSelectedKPIs(List selectedKPIs) { this.selectedKPIs = selectedKPIs; } public KPI getKPIForName(String name) { GWT.log("Searching KPI name: " + name + " into: "); for (KPI kpi : selectedKPIs) { GWT.log("selected: " + kpi.getName()); } KPI foundKPI = null; String purgedName = name.trim(); for (KPI kpi : selectedKPIs) { String purgedKPIName = kpi.getName().trim(); if (purgedKPIName.compareToIgnoreCase(purgedName) == 0) { foundKPI = kpi; break; } } GWT.log("FOUND KPI: " + foundKPI); return foundKPI; } /** * Adds the parameters. * * @param keyToGet * the key to get * @param parameters * the parameters * @param toShowBatchTypeValue * label to show batch type value */ public void addParameters(String keyToGet, Map> parameters, String toShowBatchTypeValue) { final FlexTable flexTable = new FlexTable(); flexTable.setStyleName("colgrouptable"); try { List dataInputs = parameters.get(keyToGet); if (dataInputs == null || dataInputs.isEmpty()) return; dataInputParameters = new HashMap(); String theDataInputs = dataInputs.get(0); String[] splittedParams = theDataInputs.split(";"); for (String splitParam : splittedParams) { try { String[] keyvalue = splitParam.split("="); dataInputParameters.put(keyvalue[0], keyvalue[1]); } catch (Exception e) { } } flexTable.setWidget(0, 0, new HTML("Batch Type:")); flexTable.setWidget(0, 1, new HTML(toShowBatchTypeValue)); //flexTable.setWidget(1, 0, new HTML("Level:")); //flexTable.setWidget(1, 1, new HTML(dataInputParameters.get(PerformFishAnalyticsConstant.DM_SCALEP_PARAM))); String KPINames = ""; for (KPI kpi : selectedKPIs) { KPINames += kpi.getName() + ", "; } KPINames = KPINames.substring(0, KPINames.length() - 2); flexTable.setWidget(2, 0, new HTML("KPIs:")); flexTable.setWidget(2, 1, new HTML(KPINames)); } catch (Exception e) { // silent } field_parameters_container.add(flexTable); } /** * Adds the list batch ids. * * @param listBatchesID * the list batches id */ public void addListBatchIds(List listBatchesID) { for (String batchID : listBatchesID) { field_list_focus_id_correlation.addItem(batchID, batchID); } } /** * Adds the results. * * @param dmResponse * the dm response */ public void addResults(DataMinerResponse dmResponse) { this.dmResponse = dmResponse; for (final OutputFile outputFile : dmResponse.getListOutput()) { if (outputFile.getDataType().equals(FileContentType.CSV)) { PerformFishAnalyticsServiceAsync.Util.getInstance().getCSVFile(outputFile, true, new AsyncCallback() { @Override public void onFailure(Throwable caught) { showAlert(caught.getMessage(), AlertType.ERROR, true, uib_vp_correlation_results_container); } @Override public void onSuccess(CSVFile result) { csvGenerated.put(result.getFileName(), result); fillCorrelationMatrix(); } }); } } } /** * Fill correlation matrix. */ private void fillCorrelationMatrix() { if (csvGenerated.size() < 2) return; String corrIndexFilename = null; String corrFilename = null; for (String fileName : csvGenerated.keySet()) { if (fileName.contains("index")) { corrIndexFilename = fileName; } else { corrFilename = fileName; } } GWT.log("Correlation Matrix Index File: " + corrIndexFilename); GWT.log("Correlation Matrix File: " + corrFilename); CSVFile corrIndexCsvFile = csvGenerated.get(corrIndexFilename); CSVFile corrCsvFile = csvGenerated.get(corrFilename); GWT.log("Correlation Matrix Index CSV: " + corrIndexCsvFile); GWT.log("Correlation Matrix CSV: " + corrCsvFile); HorizontalPanel hp = new HorizontalPanel(); final FlexTable flexTable = new FlexTable(); flexTable.setStyleName("fixedtable"); flexTable.getElement().getStyle().setMarginBottom(10, Unit.PX); flexTable.setWidget(0, 0, new Label("")); CSVRow headerRow = corrIndexCsvFile.getHeaderRow(); // HEADER for (int i = 1; i < headerRow.getListValues().size(); i++) { final String headerValue = headerRow.getListValues().get(i); HTML label = new HTML(headerValue); label.getElement().getStyle().setFontSize(16, Unit.PX); final int columnIndex = i; KPI kpi = getKPIForName(headerValue); fillKPIReferenceForIndex(0, columnIndex, Arrays.asList(kpi)); flexTable.setWidget(0, i, label); } // DATA for (int i = 0; i < corrIndexCsvFile.getValueRows().size(); i++) { CSVRow row = corrIndexCsvFile.getValueRows().get(i); CSVRow rowMatrixColor = corrCsvFile.getValueRows().get(i); final int rowIndex = i + 1; // adding +1 for header row for (int j = 0; j < row.getListValues().size(); j++) { final String rowValue = row.getListValues().get(j); final String rowMatrixColorValue = rowMatrixColor.getListValues().get(j); final String theColor = CorrelationValueToColourUtil.getRGBColor(rowMatrixColorValue); final int columnIndex = j; // final Button button = new Button(rowValue); final HTML buttonHTML = new HTML(rowValue); buttonHTML.addStyleName("my-active-html"); // only the first column if (j == 0) { HTML label = new HTML(rowValue); label.getElement().getStyle().setFontSize(16, Unit.PX); flexTable.setWidget(rowIndex, j, label); // rowValue is a KPI name KPI kpi = getKPIForName(rowValue); fillKPIReferenceForIndex(rowIndex, columnIndex, Arrays.asList(kpi)); continue; } else { if (rowIndex == j) { // diagonal HTML dg = new HTML(rowValue); dg.getElement().getStyle().setFontSize(18, Unit.PX); flexTable.setWidget(rowIndex, j, dg); continue; // j > 0 } else { KPI columnKPI = getKPIForName(headerRow.getListValues().get(columnIndex)); // Here the first index is the KPI name KPI rowKPI = getKPIForName(row.getListValues().get(0)); fillKPIReferenceForIndex(rowIndex, columnIndex, Arrays.asList(columnKPI, rowKPI)); buttonHTML.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { // callScatterAndDeaChart(rowIndex, columnIndex, // button); callScatter(rowIndex, columnIndex, buttonHTML); } }); } } if (j > 0) buttonHTML.getElement().getStyle().setColor("#000"); buttonHTML.addAttachHandler(new AttachEvent.Handler() { @Override public void onAttachOrDetach(AttachEvent event) { if (theColor.startsWith("#")) { GWT.log("Setting backgrounf color: " + theColor); buttonHTML.getElement().getParentElement().getStyle().setBackgroundColor(theColor); } } }); flexTable.setWidget(rowIndex, j, buttonHTML); } } hp.add(flexTable); final FlexTable flexLegend = new FlexTable(); flexLegend.setStyleName("simpletable"); Map map = CorrelationValueToColourUtil.getMap(); flexLegend.setWidget(0, 0, new Label("Legend")); flexLegend.setWidget(0, 1, new HTML("")); int i = 1; for (String key : map.keySet()) { final String rgbColor = map.get(key); final HTML theLegendColor = new HTML(""); flexLegend.setWidget(i, 0, theLegendColor); flexLegend.setWidget(i, 1, new HTML(key)); theLegendColor.addAttachHandler(new AttachEvent.Handler() { @Override public void onAttachOrDetach(AttachEvent event) { theLegendColor.getElement().getParentElement().getStyle().setBackgroundColor(rgbColor); } }); i++; } flexLegend.getElement().getStyle().setMarginLeft(15, Unit.PX); hp.add(flexLegend); uib_vp_correlation_results_container.insert(hp, 0); } /** * Fill kpi reference for index. * * @param rowIndex * the row index * @param columnIndex * the column index * @param listKPI * the list kpi */ private void fillKPIReferenceForIndex(int rowIndex, int columnIndex, List listKPI) { String key = generateKey(rowIndex, columnIndex); kpiMapPointers.put(key, listKPI); } /** * Gets the KPI for indexes. * * @param rowIndex * the row index * @param columnIndex * the column index * @return the KPI for indexes */ public List getKPIForIndexes(int rowIndex, int columnIndex) { String key = generateKey(rowIndex, columnIndex); return kpiMapPointers.get(key); } private String generateKey(int rowIndex, int columnIndex) { return rowIndex + "-" + columnIndex; } /** * Check valid focus id. * * @return the FocusID if it is valid, null otherwise. */ public String checkValidFocusID() { cg_focus_id_correlation.setType(ControlGroupType.NONE); // CHECK THE FOCUS ID VALUE String focusID = field_list_focus_id_correlation.getSelectedItemText(); if (focusID == null || focusID.isEmpty()) { String msgError = "Could not execute a valid Analysis."; showAlert(msgError, AlertType.ERROR, true, uib_vp_correlation_results_container); cg_focus_id_correlation.setType(ControlGroupType.ERROR); return null; } return focusID; } /** * Call scatter. * * @param rowIndex * the row index * @param columnIndex * the column index * @param button * the button */ private void callScatter(int rowIndex, int columnIndex, HTML button) { String focusID = checkValidFocusID(); if (focusID == null) return; GWT.log("Called ScatterChart at rowIndex: " + rowIndex + ", columnIndex: " + columnIndex); List selectedKPI = getKPIForIndexes(rowIndex, columnIndex); GWT.log("Selected KPI: " + selectedKPI); PerformFishAnalyticsController.eventBus .fireEvent(new CallAlgorithmEvent(DataMinerAlgorithms.SCATTER, focusID, selectedKPI, null)); } }