Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe Commits: 8784925f by Tony Chemit at 2020-12-30T22:23:57+01:00 Fix NavigationService Rest APi . be better to use - - - - - 8 changed files: - models/dto/src/main/java/fr/ird/observe/dto/data/TripAware.java - server/core/src/main/java/fr/ird/observe/server/ObserveWebApplicationContext.java - server/core/src/main/java/fr/ird/observe/server/injector/DateInjector.java - services/client-impl/src/main/java/fr/ird/observe/services/client/ObserveServiceFactoryClient.java - toolkit/dto/src/main/java/fr/ird/observe/gson/DtoReferenceAdapter.java - + toolkit/dto/src/main/java/fr/ird/observe/gson/GSonTypedContract.java - + toolkit/dto/src/main/java/fr/ird/observe/gson/GSonTypedContractAdapter.java - toolkit/dto/src/main/java/fr/ird/observe/gson/MultimapAdapterSupport.java Changes: ===================================== models/dto/src/main/java/fr/ird/observe/dto/data/TripAware.java ===================================== @@ -25,6 +25,7 @@ package fr.ird.observe.dto.data; import fr.ird.observe.dto.WithStartEndDate; import fr.ird.observe.dto.referential.common.ProgramReference; import fr.ird.observe.dto.referential.common.VesselReference; +import fr.ird.observe.gson.GSonTypedContract; import java.util.Comparator; import java.util.Date; @@ -37,10 +38,9 @@ import java.util.stream.Collectors; * * @author Tony Chemit - dev@tchemit.fr */ -public interface TripAware extends WithStartEndDate { +public interface TripAware extends WithStartEndDate, GSonTypedContract { String PROPERTY_OBSERVATIONS_AVAILABILITY = "observationsAvailability"; - String PROPERTY_LOGBOOK_AVAILABILITY = "logbookAvailability"; Predicate<String> IS_SEINE_ID = TripAware::isSeineId; Predicate<String> IS_LONGLINE_ID = TripAware::isLonglineId; ===================================== server/core/src/main/java/fr/ird/observe/server/ObserveWebApplicationContext.java ===================================== @@ -24,6 +24,8 @@ package fr.ird.observe.server; import com.google.common.collect.ImmutableList; import com.google.gson.GsonBuilder; +import fr.ird.observe.dto.data.TripAware; +import fr.ird.observe.gson.GSonTypedContractAdapter; import fr.ird.observe.gson.ObserveDataSourceConfigurationAdapter; import fr.ird.observe.gson.ObserveDtoGsonSupplier; import fr.ird.observe.server.configuration.ServerConfig; @@ -149,7 +151,10 @@ public class ObserveWebApplicationContext extends ApplicationContext { @Override protected GsonBuilder getGsonBuilder(boolean prettyPrint) { return super.getGsonBuilder(prettyPrint) - .registerTypeAdapter(ObserveDataSourceConfiguration.class, new ObserveDataSourceConfigurationAdapter()); + .registerTypeAdapter(ObserveDataSourceConfiguration.class, new ObserveDataSourceConfigurationAdapter()) + .registerTypeAdapter(TripAware.class, new GSonTypedContractAdapter<TripAware>(){ + + }); } }; ===================================== server/core/src/main/java/fr/ird/observe/server/injector/DateInjector.java ===================================== @@ -51,7 +51,7 @@ public class DateInjector implements ExecutorParametersInjectorHandler.Injector public Object getValue(Mapping mapping, Call call, String name, Class<?> type, Type generic) { Date result = null; - if (type.equals(Date.class)) { + if (type.equals(Date.class) && call.getParameterTree().getObject()!=null) { Call.ParameterTree parameterTree = call.getParameterTree().getObject().get(name); ===================================== services/client-impl/src/main/java/fr/ird/observe/services/client/ObserveServiceFactoryClient.java ===================================== @@ -23,6 +23,8 @@ package fr.ird.observe.services.client; */ import com.google.gson.GsonBuilder; +import fr.ird.observe.dto.data.TripAware; +import fr.ird.observe.gson.GSonTypedContractAdapter; import fr.ird.observe.gson.ObserveDataSourceConfigurationAdapter; import fr.ird.observe.gson.ObserveDataSourceConnectionAdapter; import fr.ird.observe.gson.ObserveDtoGsonSupplier; @@ -59,7 +61,8 @@ public class ObserveServiceFactoryClient extends ObserveServiceFactorySupport im return super.getGsonBuilder(prettyPrint) .registerTypeAdapter(ObserveDataSourceConnection.class, new ObserveDataSourceConnectionAdapter()) - .registerTypeAdapter(ObserveDataSourceConfiguration.class, new ObserveDataSourceConfigurationAdapter()); + .registerTypeAdapter(ObserveDataSourceConfiguration.class, new ObserveDataSourceConfigurationAdapter()) + .registerTypeAdapter(TripAware.class, new GSonTypedContractAdapter<>()); } }); ===================================== toolkit/dto/src/main/java/fr/ird/observe/gson/DtoReferenceAdapter.java ===================================== @@ -34,6 +34,7 @@ import fr.ird.observe.dto.reference.DtoReference; import java.lang.reflect.Type; /** + * FIXME Replace this by {@link GSonTypedContract} * Created on 06/09/2020. * * @author Tony Chemit - dev@tchemit.fr ===================================== toolkit/dto/src/main/java/fr/ird/observe/gson/GSonTypedContract.java ===================================== @@ -0,0 +1,34 @@ +package fr.ird.observe.gson; + +/*- + * #%L + * ObServe Toolkit :: Dto + * %% + * Copyright (C) 2008 - 2020 IRD, Code Lutin, 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% + */ + +/** + * Add this contract on any contract to be able to serialize and deserialize with Gson, the adapter will do the math. + * <p> + * Created on 30/12/2020. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 8.0.3 + */ +public interface GSonTypedContract { +} ===================================== toolkit/dto/src/main/java/fr/ird/observe/gson/GSonTypedContractAdapter.java ===================================== @@ -0,0 +1,59 @@ +package fr.ird.observe.gson; + +/*- + * #%L + * ObServe Toolkit :: Dto + * %% + * Copyright (C) 2008 - 2020 IRD, Code Lutin, 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.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 java.lang.reflect.Type; +import java.util.Map; + +/** + * Created on 30/12/2020. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 8.0.3 + */ +public class GSonTypedContractAdapter<O extends GSonTypedContract> implements JsonSerializer<O>, JsonDeserializer<O> { + + @Override + public JsonElement serialize(O src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject result = new JsonObject(); + result.add("type", context.serialize(src.getClass())); + result.add("data", context.serialize(src)); + return result; + } + + @Override + public O deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject jsonObject = json.getAsJsonObject(); + Class<O> type = context.deserialize(jsonObject.get("type"), Class.class); + return context.deserialize(jsonObject.get("data"), type); + } + +} ===================================== toolkit/dto/src/main/java/fr/ird/observe/gson/MultimapAdapterSupport.java ===================================== @@ -25,9 +25,11 @@ package fr.ird.observe.gson; import com.google.common.collect.Multimap; import com.google.common.reflect.TypeParameter; import com.google.common.reflect.TypeToken; +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; @@ -35,6 +37,8 @@ import com.google.gson.JsonSerializer; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -63,18 +67,56 @@ public abstract class MultimapAdapterSupport<M extends Multimap> implements Json Type valueType = actualTypeArguments[1]; Type type2 = multimapOf(TypeToken.of(keyType), TypeToken.of(valueType)).getType(); - Map map = context.deserialize(json, type2); M result = createMultimap(); - for (Object key : map.keySet()) { - List multimapValues = (List) map.get(key); - result.putAll(key, multimapValues); + if (!json.isJsonArray()) { + + Map map = context.deserialize(json, type2); + + for (Object key : map.keySet()) { + List multimapValues = (List) map.get(key); + result.putAll(key, multimapValues); + } + } else { + JsonArray asJsonArray = json.getAsJsonArray(); + Iterator<JsonElement> iterator = asJsonArray.iterator(); + while (iterator.hasNext()) { + JsonObject entryObject = (JsonObject) iterator.next(); + Object key = context.deserialize(entryObject.get("key"), keyType); + JsonArray valuesArray = (JsonArray) entryObject.get("value"); + Iterator<JsonElement> valuesIterator = valuesArray.iterator(); + List entryValues = new LinkedList(); + while (valuesIterator.hasNext()) { + JsonElement value = valuesIterator.next(); + entryValues.add(context.deserialize(value, valueType)); + } + result.putAll(key, entryValues); + } } return result; } @Override - public JsonElement serialize(M src, Type typeOfSrc, JsonSerializationContext context) { - return context.serialize(src.asMap()); + public JsonElement serialize(M src, Type type, JsonSerializationContext context) { + Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments(); + Type keyType = actualTypeArguments[0]; + Type valueType = actualTypeArguments[1]; + + JsonArray result = new JsonArray(); + for (Object o : src.asMap().entrySet()) { + Map.Entry entry = (Map.Entry) o; + Object key = entry.getKey(); + Collection<?> value = (Collection<?>) entry.getValue(); + JsonObject entryObject = new JsonObject(); + entryObject.add("key", context.serialize(key)); + JsonArray valuesObject = new JsonArray(); + for (Object o1 : value) { + valuesObject.add(context.serialize(o1, valueType)); + } + entryObject.add("value", valuesObject); + result.add(entryObject); + } + + return result; } } View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/commit/8784925f1a42b43f81a41a5b68... -- View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/commit/8784925f1a42b43f81a41a5b68... You're receiving this email because of your account on gitlab.com.