Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe Commits: fa6a51cc by Tony Chemit at 2023-12-01T18:19:29+01:00 introduce ColumnRendererConsumer and ColumnRendererParameters API (add gson adapter) - See #2811 - - - - - 515c32c1 by Tony Chemit at 2023-12-01T18:19:29+01:00 Add DataMatrix gson adapter (to reduce format) - See #2811 - - - - - 5 changed files: - client/datasource/actions/src/main/i18n/templates/reportHtmlExport_en_GB.ftl - client/datasource/actions/src/main/i18n/templates/reportHtmlExport_es_ES.ftl - client/datasource/actions/src/main/i18n/templates/reportHtmlExport_fr_FR.ftl - + toolkit/api-report/src/main/java/fr/ird/observe/report/json/ReportColumnRenderersParametersAdapter.java - + toolkit/api/src/main/java/fr/ird/observe/spi/json/java4all/DataMatrixAdapter.java Changes: ===================================== client/datasource/actions/src/main/i18n/templates/reportHtmlExport_en_GB.ftl ===================================== @@ -273,6 +273,23 @@ }, 50); } + function deserializeJson(json) { + let height = json.height; + let width = json.width; + let data = json.rows; + let result = new Array(height); + for (let row = 0; row < height; row++) { + let cells = data[row].split('||'); + let realRow = new Array(width); + result[row]=realRow; + for (let column = 0; column < width; column++) { + let rowElement = cells[column]; + realRow[column] = rowElement ==='$'?null:rowElement; + } + } + return result; + } + <#list .data_model.columnRendererFunctions as key> ${key}</#list> @@ -348,6 +365,7 @@ ${key}</#list> <script type="application/javascript"> const json = ${.data_model.json}; + json.data.data = deserializeJson(json.data); const gridContainerParent = document.getElementById("wrapperParent"); const searchOption = document.getElementById("search"); ===================================== client/datasource/actions/src/main/i18n/templates/reportHtmlExport_es_ES.ftl ===================================== @@ -273,6 +273,23 @@ }, 50); } + function deserializeJson(json) { + let height = json.height; + let width = json.width; + let data = json.rows; + let result = new Array(height); + for (let row = 0; row < height; row++) { + let cells = data[row].split('||'); + let realRow = new Array(width); + result[row]=realRow; + for (let column = 0; column < width; column++) { + let rowElement = cells[column]; + realRow[column] = rowElement ==='$'?null:rowElement; + } + } + return result; + } + <#list .data_model.columnRendererFunctions as key> ${key}</#list> @@ -347,6 +364,7 @@ ${key}</#list> <script type="application/javascript"> const json = ${.data_model.json}; + json.data.data = deserializeJson(json.data); const gridContainerParent = document.getElementById("wrapperParent"); const searchOption = document.getElementById("search"); ===================================== client/datasource/actions/src/main/i18n/templates/reportHtmlExport_fr_FR.ftl ===================================== @@ -274,6 +274,23 @@ }, 50); } + function deserializeJson(json) { + let height = json.height; + let width = json.width; + let data = json.rows; + let result = new Array(height); + for (let row = 0; row < height; row++) { + let cells = data[row].split('||'); + let realRow = new Array(width); + result[row]=realRow; + for (let column = 0; column < width; column++) { + let rowElement = cells[column]; + realRow[column] = rowElement ==='$'?null:rowElement; + } + } + return result; + } + <#list .data_model.columnRendererFunctions as key> ${key}</#list> @@ -348,6 +365,7 @@ ${key}</#list> <script type="application/javascript"> const json = ${.data_model.json}; + json.data.data = deserializeJson(json.data); const gridContainerParent = document.getElementById("wrapperParent"); const searchOption = document.getElementById("search"); ===================================== toolkit/api-report/src/main/java/fr/ird/observe/report/json/ReportColumnRenderersParametersAdapter.java ===================================== @@ -0,0 +1,84 @@ +package fr.ird.observe.report.json; + +/*- + * #%L + * ObServe Toolkit :: API :: Report + * %% + * Copyright (C) 2008 - 2023 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import com.google.auto.service.AutoService; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import fr.ird.observe.report.ColumnRendererParameters; +import fr.ird.observe.report.ReportColumnRenderersParameters; +import io.ultreia.java4all.lang.Objects2; +import io.ultreia.java4all.util.json.JsonAdapter; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +/** + * Created at 01/12/2023. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 9.3.0 + */ +@AutoService(JsonAdapter.class) +public class ReportColumnRenderersParametersAdapter implements JsonDeserializer<ReportColumnRenderersParameters>, JsonSerializer<ReportColumnRenderersParameters>, JsonAdapter { + + private static final String TYPE = "type"; + private static final String VALUE = "value"; + + @Override + public Class<?> type() { + return ReportColumnRenderersParameters.class; + } + + @Override + public ReportColumnRenderersParameters deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { + JsonArray columnRendererParametersJson = json.getAsJsonArray(); + List<ColumnRendererParameters> list = new ArrayList<>(columnRendererParametersJson.size()); + for (JsonElement itemJson : columnRendererParametersJson) { + JsonObject asJsonObject = itemJson.getAsJsonObject(); + String key = context.deserialize(asJsonObject.get(TYPE), String.class); + Class<ColumnRendererParameters> itemType = Objects2.forName(key); + list.add(context.deserialize(asJsonObject.get(VALUE), itemType)); + } + return new ReportColumnRenderersParameters(list); + } + + @Override + public JsonElement serialize(ReportColumnRenderersParameters src, Type typeOfSrc, JsonSerializationContext context) { + JsonArray result = new JsonArray(); + for (ColumnRendererParameters columnRendererParameter : src.getColumnRendererParameters()) { + JsonObject item = new JsonObject(); + item.addProperty(TYPE, columnRendererParameter.getClass().getName()); + item.add(VALUE, context.serialize(columnRendererParameter)); + result.add(item); + } + return result; + } +} ===================================== toolkit/api/src/main/java/fr/ird/observe/spi/json/java4all/DataMatrixAdapter.java ===================================== @@ -0,0 +1,103 @@ +package fr.ird.observe.spi.json.java4all; + +/*- + * #%L + * ObServe Toolkit :: API + * %% + * Copyright (C) 2008 - 2023 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import com.google.auto.service.AutoService; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import io.ultreia.java4all.util.json.JsonAdapter; +import io.ultreia.java4all.util.matrix.DataMatrix; + +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.stream.Collectors; + +/** + * Created at 01/12/2023. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 9.3.0 + */ +@AutoService(JsonAdapter.class) +public class DataMatrixAdapter implements JsonDeserializer<DataMatrix>, JsonSerializer<DataMatrix>, JsonAdapter { + + private static final String WIDTH = "width"; + private static final String HEIGHT = "height"; + private static final String X = "x"; + private static final String Y = "y"; + private static final String ROWS = "rows"; + + @Override + public Class<?> type() { + return DataMatrix.class; + } + + @Override + public DataMatrix deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { + JsonObject dataMatrixAsJsonObject = json.getAsJsonObject(); + + DataMatrix result = new DataMatrix(); + result.setWidth(context.deserialize(dataMatrixAsJsonObject.get(WIDTH), int.class)); + result.setHeight(context.deserialize(dataMatrixAsJsonObject.get(HEIGHT), int.class)); + result.setX(context.deserialize(dataMatrixAsJsonObject.get(X), int.class)); + result.setY(context.deserialize(dataMatrixAsJsonObject.get(Y), int.class)); + JsonArray rows = dataMatrixAsJsonObject.getAsJsonArray(ROWS); + Object[][] data = new Object[result.getHeight()][result.getWidth()]; + int index = 0; + for (JsonElement rowElement : rows) { + String[] deserialize = rowElement.getAsString().split("\\|\\|"); + Object[] row = new Object[deserialize.length]; + for (int i = 0; i < deserialize.length; i++) { + String s = deserialize[i]; + row[i] = s.equals("$") ? null : s; + } + data[index++] = row; + } + result.setData(data); + return result; + } + + @Override + public JsonElement serialize(DataMatrix src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject result = new JsonObject(); + result.addProperty(WIDTH, src.getWidth()); + result.addProperty(HEIGHT, src.getHeight()); + result.addProperty(X, src.getX()); + result.addProperty(Y, src.getY()); + JsonArray rows = new JsonArray(src.getHeight()); + result.add(ROWS, rows); + for (Object[] row : src.getData()) { + rows.add(new JsonPrimitive(Arrays.stream(row).map(d -> d == null ? "$" : d.toString()).collect(Collectors.joining("||")))); + } + + return result; + } +} + View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/compare/7e08d2ef3db60c7595a97ff4b... -- View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/compare/7e08d2ef3db60c7595a97ff4b... You're receiving this email because of your account on gitlab.com.