From dda27710fbd0ef02dd75dc5a8b029f9052d8d842 Mon Sep 17 00:00:00 2001 From: Siddhant Pandey Date: Fri, 22 Sep 2023 18:48:30 +0530 Subject: [PATCH 1/2] OEPCIS-715:Bug fix: Convert to JSON still has empty @context for EPCISEvent objects --- .../java/io/openepcis/convert/VersionTransformer.java | 8 +++++--- .../convert/json/JSONEventValueTransformer.java | 6 ++++-- .../io/openepcis/convert/json/JsonEventParser.java | 11 ++++++----- .../io/openepcis/convert/json/JsonToXmlConverter.java | 6 ++++-- .../java/io/openepcis/convert/xml/XMLEventParser.java | 7 ++++--- .../convert/xml/XMLEventValueTransformer.java | 6 ++++-- .../io/openepcis/convert/xml/XmlToJsonConverter.java | 6 ++++-- 7 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/openepcis/convert/VersionTransformer.java b/src/main/java/io/openepcis/convert/VersionTransformer.java index 01bf246..e36af4a 100644 --- a/src/main/java/io/openepcis/convert/VersionTransformer.java +++ b/src/main/java/io/openepcis/convert/VersionTransformer.java @@ -38,10 +38,12 @@ import java.io.*; import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.BiFunction; import java.util.function.Function; @Slf4j @@ -61,7 +63,7 @@ public class VersionTransformer { private final XMLEventValueTransformer xmlEventValueTransformer; - private Optional> epcisEventMapper = Optional.empty(); + private Optional, Object>> epcisEventMapper = Optional.empty(); public VersionTransformer(final ExecutorService executorService, final JAXBContext jaxbContext) { this.executorService = executorService; @@ -72,7 +74,7 @@ public VersionTransformer(final ExecutorService executorService, final JAXBConte this.xmlEventValueTransformer = new XMLEventValueTransformer(jaxbContext); } - private VersionTransformer(VersionTransformer parent, Function eventMapper) { + private VersionTransformer(VersionTransformer parent, BiFunction, Object> eventMapper) { this.executorService = parent.executorService; this.xmlVersionTransformer = parent.xmlVersionTransformer; this.jsonToXmlConverter = parent.jsonToXmlConverter.mapWith(eventMapper); @@ -399,7 +401,7 @@ private InputStream fromXmlToXml(final InputStream inputDocument) { } } - public final VersionTransformer mapWith(final Function mapper) { + public final VersionTransformer mapWith(final BiFunction, Object> mapper) { return new VersionTransformer(this, mapper); } diff --git a/src/main/java/io/openepcis/convert/json/JSONEventValueTransformer.java b/src/main/java/io/openepcis/convert/json/JSONEventValueTransformer.java index c49ad92..44778b1 100644 --- a/src/main/java/io/openepcis/convert/json/JSONEventValueTransformer.java +++ b/src/main/java/io/openepcis/convert/json/JSONEventValueTransformer.java @@ -30,9 +30,11 @@ import java.io.IOException; import java.io.InputStream; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; import java.util.function.Function; public class JSONEventValueTransformer extends JsonEventParser implements EventsConverter { @@ -40,7 +42,7 @@ public class JSONEventValueTransformer extends JsonEventParser implements Events public JSONEventValueTransformer() { } - private JSONEventValueTransformer(Function epcisEventMapper) { + private JSONEventValueTransformer(BiFunction, Object> epcisEventMapper) { this.epcisEventMapper = Optional.ofNullable(epcisEventMapper); } @@ -108,7 +110,7 @@ public void convert(InputStream jsonStream, EventHandler mapper) { + public final JSONEventValueTransformer mapWith(final BiFunction,Object> mapper) { return new JSONEventValueTransformer(mapper); } diff --git a/src/main/java/io/openepcis/convert/json/JsonEventParser.java b/src/main/java/io/openepcis/convert/json/JsonEventParser.java index 6c1b19e..36b1788 100644 --- a/src/main/java/io/openepcis/convert/json/JsonEventParser.java +++ b/src/main/java/io/openepcis/convert/json/JsonEventParser.java @@ -45,6 +45,7 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; @@ -55,7 +56,7 @@ public abstract class JsonEventParser { protected final DefaultJsonSchemaNamespaceURIResolver defaultJsonSchemaNamespaceURIResolver = DefaultJsonSchemaNamespaceURIResolver.getContext(); - protected Optional> epcisEventMapper = Optional.empty(); + protected Optional, Object>> epcisEventMapper = Optional.empty(); // Variable to ensure whether provided InputStream is EPCIS document or single event boolean isDocument = false; @@ -84,9 +85,9 @@ protected EPCISEvent processSingleEvent(AtomicInteger sequenceInEventList, JsonP if (epcisEventMapper.isPresent()) { //Change the key value to keep key as localname and value as namespaceURI final Map swappedNamespace = defaultJsonSchemaNamespaceURIResolver.getAllNamespaces().entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); - event.setContextInfo(!swappedNamespace.isEmpty() ? List.of(swappedNamespace) : null); + //event.setContextInfo(!swappedNamespace.isEmpty() ? List.of(swappedNamespace) : null); event.getOpenEPCISExtension().setSequenceInEPCISDoc(sequenceInEventList.incrementAndGet()); - return (EPCISEvent) epcisEventMapper.get().apply(event); + return (EPCISEvent) epcisEventMapper.get().apply(event, List.of(swappedNamespace)); } return event; } @@ -183,9 +184,9 @@ protected void eventTraverser( && EPCISEvent.class.isAssignableFrom(xmlSupport.getClass())) { final Map swappedNamespace = defaultJsonSchemaNamespaceURIResolver.getAllNamespaces().entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); final EPCISEvent epcisEvent = (EPCISEvent) xmlSupport; - epcisEvent.setContextInfo(!swappedNamespace.isEmpty() ? List.of(swappedNamespace) : null); + // epcisEvent.setContextInfo(!swappedNamespace.isEmpty() ? List.of(swappedNamespace) : null); epcisEvent.getOpenEPCISExtension().setSequenceInEPCISDoc(sequenceInEventList.incrementAndGet()); - xmlSupport = epcisEventMapper.get().apply(xmlSupport); + xmlSupport = epcisEventMapper.get().apply(xmlSupport, List.of(swappedNamespace)); } if(isMarshallingRequired) { diff --git a/src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java b/src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java index 9edaf01..288c7ee 100644 --- a/src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java +++ b/src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java @@ -43,9 +43,11 @@ import java.io.InputStream; import java.io.StringWriter; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; import java.util.function.Function; /** @@ -75,7 +77,7 @@ public JsonToXmlConverter(final JAXBContext jaxbContext) { } private JsonToXmlConverter( - final JsonToXmlConverter parent, Function epcisEventMapper) { + final JsonToXmlConverter parent, BiFunction, Object> epcisEventMapper) { this(parent.jaxbContext); this.epcisEventMapper = Optional.ofNullable(epcisEventMapper); } @@ -209,7 +211,7 @@ public void convert( // Close JSONParser after reading all events } - public final JsonToXmlConverter mapWith(final Function mapper) { + public final JsonToXmlConverter mapWith(final BiFunction,Object> mapper) { return new JsonToXmlConverter(this, mapper); } } diff --git a/src/main/java/io/openepcis/convert/xml/XMLEventParser.java b/src/main/java/io/openepcis/convert/xml/XMLEventParser.java index 744d5bd..7bd643e 100644 --- a/src/main/java/io/openepcis/convert/xml/XMLEventParser.java +++ b/src/main/java/io/openepcis/convert/xml/XMLEventParser.java @@ -36,6 +36,7 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -50,7 +51,7 @@ public abstract class XMLEventParser { protected final DefaultJsonSchemaNamespaceURIResolver namespaceResolver = DefaultJsonSchemaNamespaceURIResolver.getContext(); - protected Optional> epcisEventMapper = Optional.empty(); + protected Optional, Object>> epcisEventMapper = Optional.empty(); protected static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance(); @@ -124,9 +125,9 @@ protected Object applyEventMapper(AtomicInteger sequenceInEventList, Object even final EPCISEvent ev = (EPCISEvent) event; //Change the key value to keep key as localname and value as namespaceURI final Map swappedMap = namespaceResolver.getAllNamespaces().entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); - ev.setContextInfo(!swappedMap.isEmpty() ? List.of(swappedMap) : null); + // ev.setContextInfo(!swappedMap.isEmpty() ? List.of(swappedMap) : null); ev.getOpenEPCISExtension().setSequenceInEPCISDoc(sequenceInEventList.incrementAndGet()); - event = epcisEventMapper.get().apply(event); + event = epcisEventMapper.get().apply(event, List.of(swappedMap)); } return event; } diff --git a/src/main/java/io/openepcis/convert/xml/XMLEventValueTransformer.java b/src/main/java/io/openepcis/convert/xml/XMLEventValueTransformer.java index d018f6a..03cbdb1 100644 --- a/src/main/java/io/openepcis/convert/xml/XMLEventValueTransformer.java +++ b/src/main/java/io/openepcis/convert/xml/XMLEventValueTransformer.java @@ -37,9 +37,11 @@ import java.io.InputStream; import java.io.StringWriter; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; import java.util.function.Function; public class XMLEventValueTransformer extends XMLEventParser implements EventsConverter { @@ -50,7 +52,7 @@ public XMLEventValueTransformer(final JAXBContext jaxbContext) { } private XMLEventValueTransformer( - final XMLEventValueTransformer parent, Function epcisEventMapper) { + final XMLEventValueTransformer parent, BiFunction, Object> epcisEventMapper) { this(parent.jaxbContext); this.epcisEventMapper = Optional.ofNullable(epcisEventMapper); } @@ -202,7 +204,7 @@ public void convert(InputStream xmlStream, EventHandler mapper) { + public XMLEventValueTransformer mapWith(BiFunction, Object> mapper) { return new XMLEventValueTransformer(this, mapper); } diff --git a/src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java b/src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java index 716440b..398d33e 100644 --- a/src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java +++ b/src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java @@ -36,9 +36,11 @@ import java.io.IOException; import java.io.InputStream; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; import java.util.function.Function; /** @@ -63,7 +65,7 @@ public XmlToJsonConverter(final JAXBContext jaxbContext) { } private XmlToJsonConverter( - final XmlToJsonConverter parent, Function epcisEventMapper) { + final XmlToJsonConverter parent, BiFunction, Object> epcisEventMapper) { this(parent.jaxbContext); this.epcisEventMapper = Optional.ofNullable(epcisEventMapper); } @@ -245,7 +247,7 @@ public void convert( } } - public final XmlToJsonConverter mapWith(final Function mapper) { + public final XmlToJsonConverter mapWith(final BiFunction, Object> mapper) { return new XmlToJsonConverter(this, mapper); } } From fbb5f436c466d51254ca360a8df966f60b5fdfe2 Mon Sep 17 00:00:00 2001 From: Siddhant Pandey Date: Tue, 26 Sep 2023 14:07:06 +0530 Subject: [PATCH 2/2] OEPCIS-715:Fixed Bug:Convert to JSON still has empty @context for EPCISEvent objects --- src/main/java/io/openepcis/convert/json/JsonEventParser.java | 1 - src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java | 1 - src/main/java/io/openepcis/convert/xml/XMLEventParser.java | 1 - src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java | 1 - 4 files changed, 4 deletions(-) diff --git a/src/main/java/io/openepcis/convert/json/JsonEventParser.java b/src/main/java/io/openepcis/convert/json/JsonEventParser.java index 36b1788..93020d4 100644 --- a/src/main/java/io/openepcis/convert/json/JsonEventParser.java +++ b/src/main/java/io/openepcis/convert/json/JsonEventParser.java @@ -184,7 +184,6 @@ protected void eventTraverser( && EPCISEvent.class.isAssignableFrom(xmlSupport.getClass())) { final Map swappedNamespace = defaultJsonSchemaNamespaceURIResolver.getAllNamespaces().entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); final EPCISEvent epcisEvent = (EPCISEvent) xmlSupport; - // epcisEvent.setContextInfo(!swappedNamespace.isEmpty() ? List.of(swappedNamespace) : null); epcisEvent.getOpenEPCISExtension().setSequenceInEPCISDoc(sequenceInEventList.incrementAndGet()); xmlSupport = epcisEventMapper.get().apply(xmlSupport, List.of(swappedNamespace)); } diff --git a/src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java b/src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java index 288c7ee..7ea27da 100644 --- a/src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java +++ b/src/main/java/io/openepcis/convert/json/JsonToXmlConverter.java @@ -48,7 +48,6 @@ import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiFunction; -import java.util.function.Function; /** * Class for handling the conversion of EPCIS 2.0 events in JSON-LD format to EPCIS 2.0 XML format. diff --git a/src/main/java/io/openepcis/convert/xml/XMLEventParser.java b/src/main/java/io/openepcis/convert/xml/XMLEventParser.java index 7bd643e..b944b3d 100644 --- a/src/main/java/io/openepcis/convert/xml/XMLEventParser.java +++ b/src/main/java/io/openepcis/convert/xml/XMLEventParser.java @@ -125,7 +125,6 @@ protected Object applyEventMapper(AtomicInteger sequenceInEventList, Object even final EPCISEvent ev = (EPCISEvent) event; //Change the key value to keep key as localname and value as namespaceURI final Map swappedMap = namespaceResolver.getAllNamespaces().entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); - // ev.setContextInfo(!swappedMap.isEmpty() ? List.of(swappedMap) : null); ev.getOpenEPCISExtension().setSequenceInEPCISDoc(sequenceInEventList.incrementAndGet()); event = epcisEventMapper.get().apply(event, List.of(swappedMap)); } diff --git a/src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java b/src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java index 398d33e..e6f0cfe 100644 --- a/src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java +++ b/src/main/java/io/openepcis/convert/xml/XmlToJsonConverter.java @@ -41,7 +41,6 @@ import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiFunction; -import java.util.function.Function; /** * Class for handling the conversion of EPCIS 2.0 events in XML format to EPCIS 2.0 JSON format. It