Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/765 immutable flat path dto #425

Merged
merged 11 commits into from
Mar 14, 2023
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
Note: version releases in the 0.x.y range may introduce breaking changes.

## [unreleased]
### Added
### Fixed
### Added
### Changed

- FlatPathDto is now immutable ([#425](https://github.com/ehrbase/openEHR_SDK/pull/425))

### Fixed

## [1.24.0]
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ValueNode;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -101,9 +109,8 @@ private static Map<String, Object> convertFlatToStructured(Map<FlatPathDto, Json
Map<FlatPathDto, Map<FlatPathDto, JsonNode>> sharedStartMap = flatMap.entrySet().stream()
.collect(Collectors.groupingBy(
e -> {
FlatPathDto startFlatPathDto = new FlatPathDto();
startFlatPathDto.setName(e.getKey().getName());
startFlatPathDto.setCount(e.getKey().getCount());
FlatPathDto startFlatPathDto = new FlatPathDto(
e.getKey().getName(), null, e.getKey().getCount(), null);
return startFlatPathDto;
},
LinkedHashMap::new,
Expand All @@ -112,30 +119,18 @@ private static Map<String, Object> convertFlatToStructured(Map<FlatPathDto, Json
Map<String, Object> structured = new LinkedHashMap<>();

sharedStartMap.forEach((k, v) -> {
if (!structured.containsKey(k.getName())) {
structured.put(k.getName(), new ArrayList<>());
}

List<Object> values = (List<Object>) structured.get(k.getName());

LinkedHashMap<String, Object> subMap = new LinkedHashMap<>();

// find sub entries
Map<String, Object> subEntriesMap =
convertFlatToStructured((Map<FlatPathDto, JsonNode>) v.entrySet().stream()
.filter(e -> e.getKey().getChild() != null)
.collect(Collectors.toMap(
e -> FlatPathDto.removeStart(e.getKey(), new FlatPathDto(k)),
Map.Entry::getValue,
(u, t) -> u,
LinkedHashMap::new)));

if (!subEntriesMap.isEmpty()) {
subMap.putAll(subEntriesMap);
}
Map<String, Object> subMap = convertFlatToStructured((Map<FlatPathDto, JsonNode>) v.entrySet().stream()
.filter(e -> e.getKey().getChild() != null)
.collect(Collectors.toMap(
e -> FlatPathDto.removeStart(e.getKey(), k),
Map.Entry::getValue,
(u, t) -> u,
LinkedHashMap::new)));

// find attributes
Map<String, Object> collect = v.entrySet().stream()
Map<String, Object> attributes = v.entrySet().stream()
.filter(e -> e.getKey().getChild() == null)
.collect(Collectors.toMap(
e -> Optional.ofNullable(e.getKey().getAttributeName())
Expand All @@ -145,19 +140,18 @@ private static Map<String, Object> convertFlatToStructured(Map<FlatPathDto, Json
(u, j) -> u,
LinkedHashMap::new));

List<Object> values = (List<Object>) structured.computeIfAbsent(k.getName(), n -> new ArrayList<>());

// singe valued Attributes have no name
if (collect.size() == 1 && collect.containsKey("") && subMap.isEmpty()) {
values.add(collect.entrySet().stream()
.findAny()
.map(Map.Entry::getValue)
.orElse(""));
} else if (!collect.isEmpty()) {

if (collect.containsKey("")) {
collect.put("|value", collect.get(""));
collect.remove("");
if (attributes.size() == 1 && attributes.containsKey("") && subMap.isEmpty()) {
values.add(attributes.values().stream().findAny().orElse(""));

} else {
if (attributes.containsKey("")) {
subMap.put("|value", attributes.get(""));
attributes.remove("");
}
subMap.putAll(collect);
subMap.putAll(attributes);
}

if (!subMap.isEmpty()) {
Expand Down
Loading