From acd333ac7825c4c3860468b569da2d0fd9cd9d92 Mon Sep 17 00:00:00 2001 From: Hardik Shetty Date: Wed, 27 Nov 2024 13:19:35 +0100 Subject: [PATCH] Added Springboot Serialization, Refactored dataset API --- pom.xml | 7 ++++++ .../dataset/AdapterConfigSerializer.java | 2 ++ .../org/aksw/gerbil/web/MainController.java | 24 +++++++++---------- .../aksw/gerbil/web/config/AdapterList.java | 8 ++----- .../gerbil/web/config/AdapterManager.java | 7 +++--- .../aksw/gerbil/web/config/RootConfig.java | 12 ++++++++-- src/main/webapp/WEB-INF/views/config.jsp | 2 +- .../ErrorCountingAnnotatorDecoratorTest.java | 4 +++- 8 files changed, 40 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 48a9076e..001cc681 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,13 @@ + + + org.springframework.boot + spring-boot-starter + 2.7.15 + + org.aksw diff --git a/src/main/java/org/aksw/gerbil/dataset/AdapterConfigSerializer.java b/src/main/java/org/aksw/gerbil/dataset/AdapterConfigSerializer.java index 62f2b370..328326de 100644 --- a/src/main/java/org/aksw/gerbil/dataset/AdapterConfigSerializer.java +++ b/src/main/java/org/aksw/gerbil/dataset/AdapterConfigSerializer.java @@ -2,11 +2,13 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import org.aksw.gerbil.datatypes.AbstractAdapterConfiguration; import java.io.IOException; +@JsonSerialize(using = AdapterConfigSerializer.class) public class AdapterConfigSerializer extends StdSerializer { diff --git a/src/main/java/org/aksw/gerbil/web/MainController.java b/src/main/java/org/aksw/gerbil/web/MainController.java index fd1de805..88f91392 100644 --- a/src/main/java/org/aksw/gerbil/web/MainController.java +++ b/src/main/java/org/aksw/gerbil/web/MainController.java @@ -30,6 +30,7 @@ import org.aksw.gerbil.config.GerbilConfiguration; import org.aksw.gerbil.database.ExperimentDAO; import org.aksw.gerbil.dataid.DataIDGenerator; +import org.aksw.gerbil.dataset.DatasetConfiguration; import org.aksw.gerbil.datatypes.ExperimentTaskConfiguration; import org.aksw.gerbil.datatypes.ExperimentTaskStatus; import org.aksw.gerbil.datatypes.ExperimentType; @@ -303,27 +304,24 @@ public ModelAndView experiment(@RequestParam(value = "id") String id, HttpServle } @RequestMapping("/datasets") - public @ResponseBody Map> datasets(@RequestParam(value = "experimentType") String experimentType) { + public @ResponseBody Map> datasets(@RequestParam(value = "experimentType") String experimentType) { ExperimentType type = null; - Map> response = new TreeMap<>(); - ObjectMapper mapper = new ObjectMapper(); + Map> response = new TreeMap<>(); try { type = ExperimentType.valueOf(experimentType); } catch (IllegalArgumentException e) { LOGGER.warn("Got a request containing a wrong ExperimentType (\"{}\"). Ignoring it.", experimentType); return null; } - List adapterDetailsJsonList = adapterManager.getDatasetDetailsForExperiment(type); - for (String adapterJson : adapterDetailsJsonList) { - try { - Map adapterDetails = mapper.readValue(adapterJson, new TypeReference>() {}); - String name = adapterDetails.get("name"); - String group = adapterDetails.get("group"); - response.computeIfAbsent(group, k -> new ArrayList<>()).add(name); - Collections.sort(response.get(group)); - } catch (IOException e) { - LOGGER.error("Failed to parse adapter details JSON: {}", adapterJson, e); + try { + List datasetConfigurations = adapterManager.getDatasetDetailsForExperiment(type); + for (DatasetConfiguration config : datasetConfigurations) { + response.computeIfAbsent(config.getGroup(), k -> new ArrayList<>()).add(config); } + response.values().forEach(newList -> newList.sort(Comparator.naturalOrder())); + } catch (Exception e) { + LOGGER.error("Error fetching datasets for ExperimentType: {}", experimentType, e); + return null; } return response; } diff --git a/src/main/java/org/aksw/gerbil/web/config/AdapterList.java b/src/main/java/org/aksw/gerbil/web/config/AdapterList.java index d32b5e03..683af9a3 100644 --- a/src/main/java/org/aksw/gerbil/web/config/AdapterList.java +++ b/src/main/java/org/aksw/gerbil/web/config/AdapterList.java @@ -94,17 +94,13 @@ public List getAdapterDetailsForExperiment(ExperimentType type) { List configs = getAdaptersForExperiment(type); List serializedConfigs = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); - SimpleModule module = new SimpleModule(); - module.addSerializer(AbstractAdapterConfiguration.class, new AdapterConfigSerializer(AbstractAdapterConfiguration.class)); - mapper.registerModule(module); for (T config : configs) { - String json = null; try { - json = mapper.writeValueAsString(config); + String json = mapper.writeValueAsString(config); + serializedConfigs.add(json); } catch (JsonProcessingException e) { throw new RuntimeException(e); } - serializedConfigs.add(json); } return serializedConfigs; } diff --git a/src/main/java/org/aksw/gerbil/web/config/AdapterManager.java b/src/main/java/org/aksw/gerbil/web/config/AdapterManager.java index 571d023d..a1ad527d 100644 --- a/src/main/java/org/aksw/gerbil/web/config/AdapterManager.java +++ b/src/main/java/org/aksw/gerbil/web/config/AdapterManager.java @@ -16,8 +16,8 @@ */ package org.aksw.gerbil.web.config; +import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.Set; import org.aksw.gerbil.annotator.AnnotatorConfiguration; @@ -67,8 +67,9 @@ public Set getAnnotatorNamesForExperiment(ExperimentType type) { public Set getDatasetNamesForExperiment(ExperimentType type) { return datasets.getAdapterNamesForExperiment(type); } - public List getDatasetDetailsForExperiment(ExperimentType type) { - return datasets.getAdapterDetailsForExperiment(type); + + public List getDatasetDetailsForExperiment(ExperimentType type) { + return new ArrayList<>(datasets.getAdaptersForExperiment(type)); } public AnnotatorConfiguration getAnnotatorConfig(String name, ExperimentType type) { diff --git a/src/main/java/org/aksw/gerbil/web/config/RootConfig.java b/src/main/java/org/aksw/gerbil/web/config/RootConfig.java index a5584d98..e131b646 100644 --- a/src/main/java/org/aksw/gerbil/web/config/RootConfig.java +++ b/src/main/java/org/aksw/gerbil/web/config/RootConfig.java @@ -25,12 +25,14 @@ import java.util.Set; import org.aksw.gerbil.config.GerbilConfiguration; +import org.aksw.gerbil.dataset.AdapterConfigSerializer; import org.aksw.gerbil.dataset.check.EntityCheckerManager; import org.aksw.gerbil.dataset.check.impl.EntityCheckerManagerImpl; import org.aksw.gerbil.dataset.check.impl.FileBasedCachingEntityCheckerManager; import org.aksw.gerbil.dataset.check.impl.HttpBasedEntityChecker; import org.aksw.gerbil.dataset.check.impl.InMemoryCachingEntityCheckerManager; import org.aksw.gerbil.dataset.check.index.IndexBasedEntityChecker; +import org.aksw.gerbil.datatypes.AbstractAdapterConfiguration; import org.aksw.gerbil.datatypes.ExperimentType; import org.aksw.gerbil.evaluate.EvaluatorFactory; import org.aksw.gerbil.exceptions.GerbilException; @@ -61,6 +63,7 @@ import org.apache.commons.configuration.ConversionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; @@ -80,11 +83,11 @@ * org.aksw.gerbil.web.config searching for other * {@link Configuration}s * - * + * * @author Michael Röder (roeder@informatik.uni-leipzig.de) * @author Lars Wesemann * @author Didier Cherix - * + * */ @org.springframework.context.annotation.Configuration @ComponentScan(basePackages = "org.aksw.gerbil.web.config") @@ -147,6 +150,11 @@ public class RootConfig { return overseer; } + @Bean + public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { + return builder -> builder.serializers(new AdapterConfigSerializer(AbstractAdapterConfiguration.class)); + } + public static @Bean SubClassInferencer createSubClassInferencer() { Model classModel = ModelFactory.createDefaultModel(); String hierarchyFiles[] = GerbilConfiguration.getInstance() diff --git a/src/main/webapp/WEB-INF/views/config.jsp b/src/main/webapp/WEB-INF/views/config.jsp index 5a454cac..4394e725 100644 --- a/src/main/webapp/WEB-INF/views/config.jsp +++ b/src/main/webapp/WEB-INF/views/config.jsp @@ -510,7 +510,7 @@ checkbox.type = 'checkbox'; checkbox.value = item; label.appendChild(checkbox); - label.appendChild(document.createTextNode(item)); + label.appendChild(document.createTextNode(item.name)); optionsContainer.appendChild(label); }); optgroupDiv.appendChild(optionsContainer); diff --git a/src/test/java/org/aksw/gerbil/annotator/decorator/ErrorCountingAnnotatorDecoratorTest.java b/src/test/java/org/aksw/gerbil/annotator/decorator/ErrorCountingAnnotatorDecoratorTest.java index c1cc897c..43d389c5 100644 --- a/src/test/java/org/aksw/gerbil/annotator/decorator/ErrorCountingAnnotatorDecoratorTest.java +++ b/src/test/java/org/aksw/gerbil/annotator/decorator/ErrorCountingAnnotatorDecoratorTest.java @@ -47,6 +47,8 @@ public class ErrorCountingAnnotatorDecoratorTest { + private static String UNGROUPED = "Un Grouped"; + @Test public void testErrorCount() { SimpleLoggingResultStoringDAO4Debugging db = new SimpleLoggingResultStoringDAO4Debugging(); @@ -77,7 +79,7 @@ public static class ErrorCausingAnnotatorConfig extends AbstractAdapterConfigura private int errorsPerHundred; public ErrorCausingAnnotatorConfig(int errorsPerHundred) { - super("Error causing topic system","Un Grouped", false, ExperimentType.ERec); + super("Error causing topic system",UNGROUPED, false, ExperimentType.ERec); this.errorsPerHundred = errorsPerHundred; }