diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat/FileFormatResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat/FileFormatResource.java index 4db9eb1384..142f274b45 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat/FileFormatResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat/FileFormatResource.java @@ -249,9 +249,9 @@ public class FileFormatResource implements RESTResource { return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE) .entity("Unsupported media type '" + acceptHeader + "'!").build(); } - Collection items = null; + List items; if (itemNames == null || itemNames.isEmpty()) { - items = itemRegistry.getAll(); + items = getAllItemsSorted(); } else { items = new ArrayList<>(); for (String itemname : itemNames) { @@ -264,7 +264,7 @@ public class FileFormatResource implements RESTResource { } } 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(); } @@ -290,9 +290,9 @@ public class FileFormatResource implements RESTResource { return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE) .entity("Unsupported media type '" + acceptHeader + "'!").build(); } - Collection things = null; + List things; if (thingUIDs == null || thingUIDs.isEmpty()) { - things = thingRegistry.getAll(); + things = getAllThingsSorted(); } else { try { things = getThingsOrDiscoveryResult(thingUIDs); @@ -301,7 +301,7 @@ public class FileFormatResource implements RESTResource { } } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - generator.generateFileFormat(outputStream, sortThings(things), hideDefaultParameters); + generator.generateFileFormat(outputStream, things, hideDefaultParameters); return Response.ok(new String(outputStream.toByteArray())).build(); } @@ -326,14 +326,15 @@ public class FileFormatResource implements RESTResource { } /* - * 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 sortItems(Collection items) { + private List getAllItemsSorted() { + Collection items = itemRegistry.getAll(); List groups = items.stream().filter(item -> item instanceof GroupItem).sorted((item1, item2) -> { return item1.getName().compareTo(item2.getName()); }).toList(); @@ -387,12 +388,13 @@ public class FileFormatResource implements RESTResource { } /* - * 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 sortThings(Collection things) { + private List getAllThingsSorted() { + Collection things = thingRegistry.getAll(); List thingTree = new ArrayList<>(); Set bindings = things.stream().map(thing -> thing.getUID().getBindingId()).collect(Collectors.toSet()); for (String binding : bindings.stream().sorted().toList()) { diff --git a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/fileconverter/DslItemFileConverter.java b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/fileconverter/DslItemFileConverter.java index 35b974f566..0d19cd5423 100644 --- a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/fileconverter/DslItemFileConverter.java +++ b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/fileconverter/DslItemFileConverter.java @@ -82,6 +82,9 @@ public class DslItemFileConverter extends AbstractItemFileGenerator { @Override public synchronized void generateFileFormat(OutputStream out, List items, Collection 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()), diff --git a/bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/fileconverter/DslThingFileConverter.java b/bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/fileconverter/DslThingFileConverter.java index d1825447e3..728b8cc801 100644 --- a/bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/fileconverter/DslThingFileConverter.java +++ b/bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/fileconverter/DslThingFileConverter.java @@ -76,6 +76,9 @@ public class DslThingFileConverter extends AbstractThingFileGenerator { @Override public synchronized void generateFileFormat(OutputStream out, List things, boolean hideDefaultParameters) { + if (things.isEmpty()) { + return; + } ThingModel model = ThingFactory.eINSTANCE.createThingModel(); Set handledThings = new HashSet<>(); for (Thing thing : things) {