Skip to content

Commit

Permalink
Merge pull request #18 from openepcis/bugfix/OEPCIS-715-Convert-to-JS…
Browse files Browse the repository at this point in the history
…ON-still-has-empty-context

Bugfix "convert to json still has empty context"
  • Loading branch information
sboeckelmann authored Sep 26, 2023
2 parents 194c8e1 + fbb5f43 commit 5985e23
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/main/java/io/openepcis/convert/VersionTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,7 +63,7 @@ public class VersionTransformer {

private final XMLEventValueTransformer xmlEventValueTransformer;

private Optional<Function<Object, Object>> epcisEventMapper = Optional.empty();
private Optional<BiFunction<Object, List<Object>, Object>> epcisEventMapper = Optional.empty();

public VersionTransformer(final ExecutorService executorService, final JAXBContext jaxbContext) {
this.executorService = executorService;
Expand All @@ -72,7 +74,7 @@ public VersionTransformer(final ExecutorService executorService, final JAXBConte
this.xmlEventValueTransformer = new XMLEventValueTransformer(jaxbContext);
}

private VersionTransformer(VersionTransformer parent, Function<Object, Object> eventMapper) {
private VersionTransformer(VersionTransformer parent, BiFunction<Object,List<Object>, Object> eventMapper) {
this.executorService = parent.executorService;
this.xmlVersionTransformer = parent.xmlVersionTransformer;
this.jsonToXmlConverter = parent.jsonToXmlConverter.mapWith(eventMapper);
Expand Down Expand Up @@ -399,7 +401,7 @@ private InputStream fromXmlToXml(final InputStream inputDocument) {
}
}

public final VersionTransformer mapWith(final Function<Object, Object> mapper) {
public final VersionTransformer mapWith(final BiFunction<Object, List<Object>, Object> mapper) {
return new VersionTransformer(this, mapper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@
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 {

public JSONEventValueTransformer() {
}

private JSONEventValueTransformer(Function<Object, Object> epcisEventMapper) {
private JSONEventValueTransformer(BiFunction<Object, List<Object>, Object> epcisEventMapper) {
this.epcisEventMapper = Optional.ofNullable(epcisEventMapper);
}

Expand Down Expand Up @@ -108,7 +110,7 @@ public void convert(InputStream jsonStream, EventHandler<? extends EPCISEventCol
// Close JSONParser after reading all events
}

public final JSONEventValueTransformer mapWith(final Function<Object, Object> mapper) {
public final JSONEventValueTransformer mapWith(final BiFunction<Object, List<Object>,Object> mapper) {
return new JSONEventValueTransformer(mapper);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/openepcis/convert/json/JsonEventParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -55,7 +56,7 @@ public abstract class JsonEventParser {
protected final DefaultJsonSchemaNamespaceURIResolver defaultJsonSchemaNamespaceURIResolver =
DefaultJsonSchemaNamespaceURIResolver.getContext();

protected Optional<Function<Object, Object>> epcisEventMapper = Optional.empty();
protected Optional<BiFunction<Object, List<Object>, Object>> epcisEventMapper = Optional.empty();

// Variable to ensure whether provided InputStream is EPCIS document or single event
boolean isDocument = false;
Expand Down Expand Up @@ -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<String, String> 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;
}
Expand Down Expand Up @@ -183,9 +184,8 @@ protected void eventTraverser(
&& EPCISEvent.class.isAssignableFrom(xmlSupport.getClass())) {
final Map<String, String> 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);
xmlSupport = epcisEventMapper.get().apply(xmlSupport, List.of(swappedNamespace));
}

if(isMarshallingRequired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +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.Function;
import java.util.function.BiFunction;

/**
* Class for handling the conversion of EPCIS 2.0 events in JSON-LD format to EPCIS 2.0 XML format.
Expand Down Expand Up @@ -75,7 +76,7 @@ public JsonToXmlConverter(final JAXBContext jaxbContext) {
}

private JsonToXmlConverter(
final JsonToXmlConverter parent, Function<Object, Object> epcisEventMapper) {
final JsonToXmlConverter parent, BiFunction<Object,List<Object>, Object> epcisEventMapper) {
this(parent.jaxbContext);
this.epcisEventMapper = Optional.ofNullable(epcisEventMapper);
}
Expand Down Expand Up @@ -209,7 +210,7 @@ public void convert(
// Close JSONParser after reading all events
}

public final JsonToXmlConverter mapWith(final Function<Object, Object> mapper) {
public final JsonToXmlConverter mapWith(final BiFunction<Object, List<Object>,Object> mapper) {
return new JsonToXmlConverter(this, mapper);
}
}
6 changes: 3 additions & 3 deletions src/main/java/io/openepcis/convert/xml/XMLEventParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,7 +51,7 @@ public abstract class XMLEventParser {
protected final DefaultJsonSchemaNamespaceURIResolver namespaceResolver =
DefaultJsonSchemaNamespaceURIResolver.getContext();

protected Optional<Function<Object, Object>> epcisEventMapper = Optional.empty();
protected Optional<BiFunction<Object, List<Object>, Object>> epcisEventMapper = Optional.empty();

protected static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance();

Expand Down Expand Up @@ -124,9 +125,8 @@ 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<String, String> 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);
event = epcisEventMapper.get().apply(event, List.of(swappedMap));
}
return event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -50,7 +52,7 @@ public XMLEventValueTransformer(final JAXBContext jaxbContext) {
}

private XMLEventValueTransformer(
final XMLEventValueTransformer parent, Function<Object, Object> epcisEventMapper) {
final XMLEventValueTransformer parent, BiFunction<Object, List<Object>, Object> epcisEventMapper) {
this(parent.jaxbContext);
this.epcisEventMapper = Optional.ofNullable(epcisEventMapper);
}
Expand Down Expand Up @@ -202,7 +204,7 @@ public void convert(InputStream xmlStream, EventHandler<? extends EPCISEventColl
}

}
public XMLEventValueTransformer mapWith(Function<Object, Object> mapper) {
public XMLEventValueTransformer mapWith(BiFunction<Object, List<Object>, Object> mapper) {
return new XMLEventValueTransformer(this, mapper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +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.Function;
import java.util.function.BiFunction;

/**
* Class for handling the conversion of EPCIS 2.0 events in XML format to EPCIS 2.0 JSON format. It
Expand All @@ -63,7 +64,7 @@ public XmlToJsonConverter(final JAXBContext jaxbContext) {
}

private XmlToJsonConverter(
final XmlToJsonConverter parent, Function<Object, Object> epcisEventMapper) {
final XmlToJsonConverter parent, BiFunction<Object, List<Object>, Object> epcisEventMapper) {
this(parent.jaxbContext);
this.epcisEventMapper = Optional.ofNullable(epcisEventMapper);
}
Expand Down Expand Up @@ -245,7 +246,7 @@ public void convert(
}
}

public final XmlToJsonConverter mapWith(final Function<Object, Object> mapper) {
public final XmlToJsonConverter mapWith(final BiFunction<Object, List<Object>, Object> mapper) {
return new XmlToJsonConverter(this, mapper);
}
}

0 comments on commit 5985e23

Please sign in to comment.