Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe
Commits:
-
8784925f
by Tony Chemit at 2020-12-30T22:23:57+01:00
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:
| ... | ... | @@ -25,6 +25,7 @@ package fr.ird.observe.dto.data; |
| 25 | 25 |
import fr.ird.observe.dto.WithStartEndDate;
|
| 26 | 26 |
import fr.ird.observe.dto.referential.common.ProgramReference;
|
| 27 | 27 |
import fr.ird.observe.dto.referential.common.VesselReference;
|
| 28 |
+import fr.ird.observe.gson.GSonTypedContract;
|
|
| 28 | 29 |
|
| 29 | 30 |
import java.util.Comparator;
|
| 30 | 31 |
import java.util.Date;
|
| ... | ... | @@ -37,10 +38,9 @@ import java.util.stream.Collectors; |
| 37 | 38 |
*
|
| 38 | 39 |
* @author Tony Chemit - dev@tchemit.fr
|
| 39 | 40 |
*/
|
| 40 |
-public interface TripAware extends WithStartEndDate {
|
|
| 41 |
+public interface TripAware extends WithStartEndDate, GSonTypedContract {
|
|
| 41 | 42 |
|
| 42 | 43 |
String PROPERTY_OBSERVATIONS_AVAILABILITY = "observationsAvailability";
|
| 43 |
- |
|
| 44 | 44 |
String PROPERTY_LOGBOOK_AVAILABILITY = "logbookAvailability";
|
| 45 | 45 |
Predicate<String> IS_SEINE_ID = TripAware::isSeineId;
|
| 46 | 46 |
Predicate<String> IS_LONGLINE_ID = TripAware::isLonglineId;
|
| ... | ... | @@ -24,6 +24,8 @@ package fr.ird.observe.server; |
| 24 | 24 |
|
| 25 | 25 |
import com.google.common.collect.ImmutableList;
|
| 26 | 26 |
import com.google.gson.GsonBuilder;
|
| 27 |
+import fr.ird.observe.dto.data.TripAware;
|
|
| 28 |
+import fr.ird.observe.gson.GSonTypedContractAdapter;
|
|
| 27 | 29 |
import fr.ird.observe.gson.ObserveDataSourceConfigurationAdapter;
|
| 28 | 30 |
import fr.ird.observe.gson.ObserveDtoGsonSupplier;
|
| 29 | 31 |
import fr.ird.observe.server.configuration.ServerConfig;
|
| ... | ... | @@ -149,7 +151,10 @@ public class ObserveWebApplicationContext extends ApplicationContext { |
| 149 | 151 |
@Override
|
| 150 | 152 |
protected GsonBuilder getGsonBuilder(boolean prettyPrint) {
|
| 151 | 153 |
return super.getGsonBuilder(prettyPrint)
|
| 152 |
- .registerTypeAdapter(ObserveDataSourceConfiguration.class, new ObserveDataSourceConfigurationAdapter());
|
|
| 154 |
+ .registerTypeAdapter(ObserveDataSourceConfiguration.class, new ObserveDataSourceConfigurationAdapter())
|
|
| 155 |
+ .registerTypeAdapter(TripAware.class, new GSonTypedContractAdapter<TripAware>(){
|
|
| 156 |
+ |
|
| 157 |
+ });
|
|
| 153 | 158 |
|
| 154 | 159 |
}
|
| 155 | 160 |
};
|
| ... | ... | @@ -51,7 +51,7 @@ public class DateInjector implements ExecutorParametersInjectorHandler.Injector |
| 51 | 51 |
public Object getValue(Mapping mapping, Call call, String name, Class<?> type, Type generic) {
|
| 52 | 52 |
Date result = null;
|
| 53 | 53 |
|
| 54 |
- if (type.equals(Date.class)) {
|
|
| 54 |
+ if (type.equals(Date.class) && call.getParameterTree().getObject()!=null) {
|
|
| 55 | 55 |
|
| 56 | 56 |
Call.ParameterTree parameterTree = call.getParameterTree().getObject().get(name);
|
| 57 | 57 |
|
| ... | ... | @@ -23,6 +23,8 @@ package fr.ird.observe.services.client; |
| 23 | 23 |
*/
|
| 24 | 24 |
|
| 25 | 25 |
import com.google.gson.GsonBuilder;
|
| 26 |
+import fr.ird.observe.dto.data.TripAware;
|
|
| 27 |
+import fr.ird.observe.gson.GSonTypedContractAdapter;
|
|
| 26 | 28 |
import fr.ird.observe.gson.ObserveDataSourceConfigurationAdapter;
|
| 27 | 29 |
import fr.ird.observe.gson.ObserveDataSourceConnectionAdapter;
|
| 28 | 30 |
import fr.ird.observe.gson.ObserveDtoGsonSupplier;
|
| ... | ... | @@ -59,7 +61,8 @@ public class ObserveServiceFactoryClient extends ObserveServiceFactorySupport im |
| 59 | 61 |
|
| 60 | 62 |
return super.getGsonBuilder(prettyPrint)
|
| 61 | 63 |
.registerTypeAdapter(ObserveDataSourceConnection.class, new ObserveDataSourceConnectionAdapter())
|
| 62 |
- .registerTypeAdapter(ObserveDataSourceConfiguration.class, new ObserveDataSourceConfigurationAdapter());
|
|
| 64 |
+ .registerTypeAdapter(ObserveDataSourceConfiguration.class, new ObserveDataSourceConfigurationAdapter())
|
|
| 65 |
+ .registerTypeAdapter(TripAware.class, new GSonTypedContractAdapter<>());
|
|
| 63 | 66 |
|
| 64 | 67 |
}
|
| 65 | 68 |
});
|
| ... | ... | @@ -34,6 +34,7 @@ import fr.ird.observe.dto.reference.DtoReference; |
| 34 | 34 |
import java.lang.reflect.Type;
|
| 35 | 35 |
|
| 36 | 36 |
/**
|
| 37 |
+ * FIXME Replace this by {@link GSonTypedContract}
|
|
| 37 | 38 |
* Created on 06/09/2020.
|
| 38 | 39 |
*
|
| 39 | 40 |
* @author Tony Chemit - dev@tchemit.fr
|
| 1 |
+package fr.ird.observe.gson;
|
|
| 2 |
+ |
|
| 3 |
+/*-
|
|
| 4 |
+ * #%L
|
|
| 5 |
+ * ObServe Toolkit :: Dto
|
|
| 6 |
+ * %%
|
|
| 7 |
+ * Copyright (C) 2008 - 2020 IRD, Code Lutin, Ultreia.io
|
|
| 8 |
+ * %%
|
|
| 9 |
+ * This program is free software: you can redistribute it and/or modify
|
|
| 10 |
+ * it under the terms of the GNU General Public License as
|
|
| 11 |
+ * published by the Free Software Foundation, either version 3 of the
|
|
| 12 |
+ * License, or (at your option) any later version.
|
|
| 13 |
+ *
|
|
| 14 |
+ * This program is distributed in the hope that it will be useful,
|
|
| 15 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 |
+ * GNU General Public License for more details.
|
|
| 18 |
+ *
|
|
| 19 |
+ * You should have received a copy of the GNU General Public
|
|
| 20 |
+ * License along with this program. If not, see
|
|
| 21 |
+ * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 |
+ * #L%
|
|
| 23 |
+ */
|
|
| 24 |
+ |
|
| 25 |
+/**
|
|
| 26 |
+ * Add this contract on any contract to be able to serialize and deserialize with Gson, the adapter will do the math.
|
|
| 27 |
+ * <p>
|
|
| 28 |
+ * Created on 30/12/2020.
|
|
| 29 |
+ *
|
|
| 30 |
+ * @author Tony Chemit - dev@tchemit.fr
|
|
| 31 |
+ * @since 8.0.3
|
|
| 32 |
+ */
|
|
| 33 |
+public interface GSonTypedContract {
|
|
| 34 |
+}
|
| 1 |
+package fr.ird.observe.gson;
|
|
| 2 |
+ |
|
| 3 |
+/*-
|
|
| 4 |
+ * #%L
|
|
| 5 |
+ * ObServe Toolkit :: Dto
|
|
| 6 |
+ * %%
|
|
| 7 |
+ * Copyright (C) 2008 - 2020 IRD, Code Lutin, Ultreia.io
|
|
| 8 |
+ * %%
|
|
| 9 |
+ * This program is free software: you can redistribute it and/or modify
|
|
| 10 |
+ * it under the terms of the GNU General Public License as
|
|
| 11 |
+ * published by the Free Software Foundation, either version 3 of the
|
|
| 12 |
+ * License, or (at your option) any later version.
|
|
| 13 |
+ *
|
|
| 14 |
+ * This program is distributed in the hope that it will be useful,
|
|
| 15 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 |
+ * GNU General Public License for more details.
|
|
| 18 |
+ *
|
|
| 19 |
+ * You should have received a copy of the GNU General Public
|
|
| 20 |
+ * License along with this program. If not, see
|
|
| 21 |
+ * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 |
+ * #L%
|
|
| 23 |
+ */
|
|
| 24 |
+ |
|
| 25 |
+import com.google.gson.JsonDeserializationContext;
|
|
| 26 |
+import com.google.gson.JsonDeserializer;
|
|
| 27 |
+import com.google.gson.JsonElement;
|
|
| 28 |
+import com.google.gson.JsonObject;
|
|
| 29 |
+import com.google.gson.JsonParseException;
|
|
| 30 |
+import com.google.gson.JsonSerializationContext;
|
|
| 31 |
+import com.google.gson.JsonSerializer;
|
|
| 32 |
+ |
|
| 33 |
+import java.lang.reflect.Type;
|
|
| 34 |
+import java.util.Map;
|
|
| 35 |
+ |
|
| 36 |
+/**
|
|
| 37 |
+ * Created on 30/12/2020.
|
|
| 38 |
+ *
|
|
| 39 |
+ * @author Tony Chemit - dev@tchemit.fr
|
|
| 40 |
+ * @since 8.0.3
|
|
| 41 |
+ */
|
|
| 42 |
+public class GSonTypedContractAdapter<O extends GSonTypedContract> implements JsonSerializer<O>, JsonDeserializer<O> {
|
|
| 43 |
+ |
|
| 44 |
+ @Override
|
|
| 45 |
+ public JsonElement serialize(O src, Type typeOfSrc, JsonSerializationContext context) {
|
|
| 46 |
+ JsonObject result = new JsonObject();
|
|
| 47 |
+ result.add("type", context.serialize(src.getClass()));
|
|
| 48 |
+ result.add("data", context.serialize(src));
|
|
| 49 |
+ return result;
|
|
| 50 |
+ }
|
|
| 51 |
+ |
|
| 52 |
+ @Override
|
|
| 53 |
+ public O deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
| 54 |
+ JsonObject jsonObject = json.getAsJsonObject();
|
|
| 55 |
+ Class<O> type = context.deserialize(jsonObject.get("type"), Class.class);
|
|
| 56 |
+ return context.deserialize(jsonObject.get("data"), type);
|
|
| 57 |
+ }
|
|
| 58 |
+ |
|
| 59 |
+}
|
| ... | ... | @@ -25,9 +25,11 @@ package fr.ird.observe.gson; |
| 25 | 25 |
import com.google.common.collect.Multimap;
|
| 26 | 26 |
import com.google.common.reflect.TypeParameter;
|
| 27 | 27 |
import com.google.common.reflect.TypeToken;
|
| 28 |
+import com.google.gson.JsonArray;
|
|
| 28 | 29 |
import com.google.gson.JsonDeserializationContext;
|
| 29 | 30 |
import com.google.gson.JsonDeserializer;
|
| 30 | 31 |
import com.google.gson.JsonElement;
|
| 32 |
+import com.google.gson.JsonObject;
|
|
| 31 | 33 |
import com.google.gson.JsonParseException;
|
| 32 | 34 |
import com.google.gson.JsonSerializationContext;
|
| 33 | 35 |
import com.google.gson.JsonSerializer;
|
| ... | ... | @@ -35,6 +37,8 @@ import com.google.gson.JsonSerializer; |
| 35 | 37 |
import java.lang.reflect.ParameterizedType;
|
| 36 | 38 |
import java.lang.reflect.Type;
|
| 37 | 39 |
import java.util.Collection;
|
| 40 |
+import java.util.Iterator;
|
|
| 41 |
+import java.util.LinkedList;
|
|
| 38 | 42 |
import java.util.List;
|
| 39 | 43 |
import java.util.Map;
|
| 40 | 44 |
|
| ... | ... | @@ -63,18 +67,56 @@ public abstract class MultimapAdapterSupport<M extends Multimap> implements Json |
| 63 | 67 |
Type valueType = actualTypeArguments[1];
|
| 64 | 68 |
|
| 65 | 69 |
Type type2 = multimapOf(TypeToken.of(keyType), TypeToken.of(valueType)).getType();
|
| 66 |
- Map map = context.deserialize(json, type2);
|
|
| 67 | 70 |
|
| 68 | 71 |
M result = createMultimap();
|
| 69 |
- for (Object key : map.keySet()) {
|
|
| 70 |
- List multimapValues = (List) map.get(key);
|
|
| 71 |
- result.putAll(key, multimapValues);
|
|
| 72 |
+ if (!json.isJsonArray()) {
|
|
| 73 |
+ |
|
| 74 |
+ Map map = context.deserialize(json, type2);
|
|
| 75 |
+ |
|
| 76 |
+ for (Object key : map.keySet()) {
|
|
| 77 |
+ List multimapValues = (List) map.get(key);
|
|
| 78 |
+ result.putAll(key, multimapValues);
|
|
| 79 |
+ }
|
|
| 80 |
+ } else {
|
|
| 81 |
+ JsonArray asJsonArray = json.getAsJsonArray();
|
|
| 82 |
+ Iterator<JsonElement> iterator = asJsonArray.iterator();
|
|
| 83 |
+ while (iterator.hasNext()) {
|
|
| 84 |
+ JsonObject entryObject = (JsonObject) iterator.next();
|
|
| 85 |
+ Object key = context.deserialize(entryObject.get("key"), keyType);
|
|
| 86 |
+ JsonArray valuesArray = (JsonArray) entryObject.get("value");
|
|
| 87 |
+ Iterator<JsonElement> valuesIterator = valuesArray.iterator();
|
|
| 88 |
+ List entryValues = new LinkedList();
|
|
| 89 |
+ while (valuesIterator.hasNext()) {
|
|
| 90 |
+ JsonElement value = valuesIterator.next();
|
|
| 91 |
+ entryValues.add(context.deserialize(value, valueType));
|
|
| 92 |
+ }
|
|
| 93 |
+ result.putAll(key, entryValues);
|
|
| 94 |
+ }
|
|
| 72 | 95 |
}
|
| 73 | 96 |
return result;
|
| 74 | 97 |
}
|
| 75 | 98 |
|
| 76 | 99 |
@Override
|
| 77 |
- public JsonElement serialize(M src, Type typeOfSrc, JsonSerializationContext context) {
|
|
| 78 |
- return context.serialize(src.asMap());
|
|
| 100 |
+ public JsonElement serialize(M src, Type type, JsonSerializationContext context) {
|
|
| 101 |
+ Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
|
|
| 102 |
+ Type keyType = actualTypeArguments[0];
|
|
| 103 |
+ Type valueType = actualTypeArguments[1];
|
|
| 104 |
+ |
|
| 105 |
+ JsonArray result = new JsonArray();
|
|
| 106 |
+ for (Object o : src.asMap().entrySet()) {
|
|
| 107 |
+ Map.Entry entry = (Map.Entry) o;
|
|
| 108 |
+ Object key = entry.getKey();
|
|
| 109 |
+ Collection<?> value = (Collection<?>) entry.getValue();
|
|
| 110 |
+ JsonObject entryObject = new JsonObject();
|
|
| 111 |
+ entryObject.add("key", context.serialize(key));
|
|
| 112 |
+ JsonArray valuesObject = new JsonArray();
|
|
| 113 |
+ for (Object o1 : value) {
|
|
| 114 |
+ valuesObject.add(context.serialize(o1, valueType));
|
|
| 115 |
+ }
|
|
| 116 |
+ entryObject.add("value", valuesObject);
|
|
| 117 |
+ result.add(entryObject);
|
|
| 118 |
+ }
|
|
| 119 |
+ |
|
| 120 |
+ return result;
|
|
| 79 | 121 |
}
|
| 80 | 122 |
}
|