Skip to content

[REST] Fix API to get file format when applied to a subset of elements #4786

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ public Response createFileFormatForItems(final @Context HttpHeaders httpHeaders,
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
.entity("Unsupported media type '" + acceptHeader + "'!").build();
}
Collection<Item> items = null;
List<Item> items;
if (itemNames == null || itemNames.isEmpty()) {
items = itemRegistry.getAll();
items = getAllItemsSorted();
} else {
items = new ArrayList<>();
for (String itemname : itemNames) {
Expand All @@ -264,7 +264,7 @@ public Response createFileFormatForItems(final @Context HttpHeaders httpHeaders,
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
generator.generateFileFormat(outputStream, sortItems(items), getMetadata(items), hideDefaultParameters);
generator.generateFileFormat(outputStream, items, getMetadata(items), hideDefaultParameters);
return Response.ok(new String(outputStream.toByteArray())).build();
}

Expand All @@ -290,9 +290,9 @@ public Response createFileFormatForThings(final @Context HttpHeaders httpHeaders
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
.entity("Unsupported media type '" + acceptHeader + "'!").build();
}
Collection<Thing> things = null;
List<Thing> things;
if (thingUIDs == null || thingUIDs.isEmpty()) {
things = thingRegistry.getAll();
things = getAllThingsSorted();
} else {
try {
things = getThingsOrDiscoveryResult(thingUIDs);
Expand All @@ -301,7 +301,7 @@ public Response createFileFormatForThings(final @Context HttpHeaders httpHeaders
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
generator.generateFileFormat(outputStream, sortThings(things), hideDefaultParameters);
generator.generateFileFormat(outputStream, things, hideDefaultParameters);
return Response.ok(new String(outputStream.toByteArray())).build();
}

Expand All @@ -326,14 +326,15 @@ private Collection<Metadata> getMetadata(Collection<Item> items) {
}

/*
* Sort the items in such a way:
* Get all items from registry sorted in such a way:
* - group items are before non group items
* - group items are sorted to have as much as possible ancestors before their children
* - items not linked to a channel are before items linked to a channel
* - items linked to a channel are grouped by thing UID
* - items linked to the same thing UID are sorted by item name
*/
private List<Item> sortItems(Collection<Item> items) {
private List<Item> getAllItemsSorted() {
Collection<Item> items = itemRegistry.getAll();
List<Item> groups = items.stream().filter(item -> item instanceof GroupItem).sorted((item1, item2) -> {
return item1.getName().compareTo(item2.getName());
}).toList();
Expand Down Expand Up @@ -387,12 +388,13 @@ private void fillGroupTree(List<Item> groups, Item item) {
}

/*
* Sort the things in such a way:
* Get all things from registry sorted in such a way:
* - things are grouped by binding, sorted by natural order of binding name
* - all things of a binding are sorted to follow the tree, that is bridge thing is before its sub-things
* - all things of a binding at a certain tree depth are sorted by thing UID
*/
private List<Thing> sortThings(Collection<Thing> things) {
private List<Thing> getAllThingsSorted() {
Collection<Thing> things = thingRegistry.getAll();
List<Thing> thingTree = new ArrayList<>();
Set<String> bindings = things.stream().map(thing -> thing.getUID().getBindingId()).collect(Collectors.toSet());
for (String binding : bindings.stream().sorted().toList()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public String getFileFormatGenerator() {
@Override
public synchronized void generateFileFormat(OutputStream out, List<Item> items, Collection<Metadata> metadata,
boolean hideDefaultParameters) {
if (items.isEmpty()) {
return;
}
ItemModel model = ItemsFactory.eINSTANCE.createItemModel();
for (Item item : items) {
model.getItems().add(buildModelItem(item, getChannelLinks(metadata, item.getName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public String getFileFormatGenerator() {

@Override
public synchronized void generateFileFormat(OutputStream out, List<Thing> things, boolean hideDefaultParameters) {
if (things.isEmpty()) {
return;
}
ThingModel model = ThingFactory.eINSTANCE.createThingModel();
Set<Thing> handledThings = new HashSet<>();
for (Thing thing : things) {
Expand Down
Loading