Skip to content

Commit

Permalink
Fix namespace "channel" is added to item metadata (openhab#2957)
Browse files Browse the repository at this point in the history
* Fix namespace "channel" is added to item metadata

Signed-off-by: Jan N. Klug <github@klug.nrw>
GitOrigin-RevId: c96ce5b
  • Loading branch information
J-N-K authored and splatch committed Jul 12, 2023
1 parent a6c5765 commit 27f0117
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public GenericItemProvider(final @Reference ModelRepository modelRepository,
rank = 0;
}

itemFactorys.forEach(itemFactory -> dispatchBindingsPerItemType(null, itemFactory.getSupportedItemTypes()));
itemFactorys.forEach(itemFactory -> dispatchBindingsPerItemType(itemFactory.getSupportedItemTypes()));

// process models which are already parsed by modelRepository:
for (String modelName : modelRepository.getAllModelNamesOfType("items")) {
Expand All @@ -131,7 +131,7 @@ public Integer getRank() {
@Reference(cardinality = ReferenceCardinality.AT_LEAST_ONE, policy = ReferencePolicy.DYNAMIC)
public void addItemFactory(ItemFactory factory) {
itemFactorys.add(factory);
dispatchBindingsPerItemType(null, factory.getSupportedItemTypes());
dispatchBindingsPerItemType(factory.getSupportedItemTypes());
}

/**
Expand All @@ -147,10 +147,11 @@ public void removeItemFactory(ItemFactory factory) {
public void addBindingConfigReader(BindingConfigReader reader) {
if (!bindingConfigReaders.containsKey(reader.getBindingType())) {
bindingConfigReaders.put(reader.getBindingType(), reader);
genericMetaDataProvider.removeMetadataByNamespace(reader.getBindingType());
dispatchBindingsPerType(reader, new String[] { reader.getBindingType() });
} else {
logger.warn("Attempted to register a second BindingConfigReader of type '{}'."
+ " The primaraly reader will remain active!", reader.getBindingType());
+ " The previous reader will remain active!", reader.getBindingType());
}
}

Expand Down Expand Up @@ -206,7 +207,7 @@ private void processBindingConfigsFromModel(String modelName, EventType type) {
// create items and read new binding configuration
if (!EventType.REMOVED.equals(type)) {
for (ModelItem modelItem : model.getItems()) {
genericMetaDataProvider.removeMetadata(modelItem.getName());
genericMetaDataProvider.removeMetadataByItemName(modelItem.getName());
Item item = createItemFromModelItem(modelItem);
if (item != null) {
internalDispatchBindings(modelName, item, modelItem.getBindings());
Expand All @@ -221,7 +222,7 @@ private void processBindingConfigsFromModel(String modelName, EventType type) {
}

private @Nullable Item createItemFromModelItem(ModelItem modelItem) {
Item item = null;
Item item;
if (modelItem instanceof ModelGroupItem) {
ModelGroupItem modelGroupItem = (ModelGroupItem) modelItem;
Item baseItem;
Expand Down Expand Up @@ -249,7 +250,7 @@ private void processBindingConfigsFromModel(String modelName, EventType type) {
return null;
}
}
if (item != null && item instanceof ActiveItem) {
if (item instanceof ActiveItem) {
String label = modelItem.getLabel();
String format = extractFormat(label);
if (format != null) {
Expand Down Expand Up @@ -287,14 +288,14 @@ private void assignTags(ModelItem modelItem, ActiveItem item) {
private GroupItem applyGroupFunction(Item baseItem, ModelGroupItem modelGroupItem, ModelGroupFunction function) {
GroupFunctionDTO dto = new GroupFunctionDTO();
dto.name = function.getName();
dto.params = modelGroupItem.getArgs().toArray(new String[modelGroupItem.getArgs().size()]);
dto.params = modelGroupItem.getArgs().toArray(new String[0]);

GroupFunction groupFunction = ItemDTOMapper.mapFunction(baseItem, dto);

return new GroupItem(modelGroupItem.getName(), baseItem, groupFunction);
}

private void dispatchBindingsPerItemType(@Nullable BindingConfigReader reader, String[] itemTypes) {
private void dispatchBindingsPerItemType(String[] itemTypes) {
for (String modelName : modelRepository.getAllModelNamesOfType("items")) {
ItemModel model = (ItemModel) modelRepository.getModel(modelName);
if (model != null) {
Expand All @@ -304,7 +305,7 @@ private void dispatchBindingsPerItemType(@Nullable BindingConfigReader reader, S
if (type != null && itemType.equals(ItemUtil.getMainItemType(type))) {
Item item = createItemFromModelItem(modelItem);
if (item != null) {
internalDispatchBindings(reader, modelName, item, modelItem.getBindings());
internalDispatchBindings(null, modelName, item, modelItem.getBindings());
}
}
}
Expand Down Expand Up @@ -426,7 +427,7 @@ public void modelChanged(String modelName, EventType type) {
private void notifyAndCleanup(Item oldItem) {
notifyListenersAboutRemovedElement(oldItem);
this.stateDescriptionFragments.remove(oldItem.getName());
genericMetaDataProvider.removeMetadata(oldItem.getName());
genericMetaDataProvider.removeMetadataByItemName(oldItem.getName());
}

protected boolean hasItemChanged(Item item1, Item item2) {
Expand Down Expand Up @@ -455,7 +456,7 @@ private boolean hasGroupItemChanged(Item item1, Item item2) {
return false;
}

if ((gItem1 != null && gItem2 == null) || (gItem1 == null && gItem2 != null)) {
if (gItem1 == null || gItem2 == null) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,27 @@ public void addMetadata(String bindingType, String itemName, String value,
}

/**
* Removes all meta-data for a given item name
* Removes all meta-data for a given namespace
*
* @param itemName
* @param namespace the namespace
*/
public void removeMetadata(String itemName) {
public void removeMetadataByNamespace(String namespace) {
Set<Metadata> toBeRemoved;
try {
lock.writeLock().lock();
toBeRemoved = metadata.stream().filter(MetadataPredicates.hasNamespace(namespace)).collect(toSet());
metadata.removeAll(toBeRemoved);
} finally {
lock.writeLock().unlock();
}
toBeRemoved.forEach(this::notifyListenersAboutRemovedElement);
}

/**
* Removes all meta-data for a given item
*
* @param itemName the item name
*/public void removeMetadataByItemName(String itemName) {
Set<Metadata> toBeRemoved;
try {
lock.writeLock().lock();
Expand All @@ -85,7 +101,6 @@ public void removeMetadata(String itemName) {
notifyListenersAboutRemovedElement(m);
}
}

@Override
public Collection<Metadata> getAll() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,30 @@ public void testAddMetadata() {
@Test
public void testRemoveMetadataNonExistentItem() {
GenericMetadataProvider provider = new GenericMetadataProvider();
provider.removeMetadata("nonExistentItem");
provider.removeMetadataByItemName("nonExistentItem");
}

@Test
public void testRemoveMetadata() {
public void testRemoveMetadataByItemName() {
GenericMetadataProvider provider = new GenericMetadataProvider();
provider.addMetadata("other", "item", "value", null);
provider.addMetadata("binding", "item", "value", null);
provider.addMetadata("binding", "other", "value", null);
assertEquals(3, provider.getAll().size());

provider.removeMetadata("item");
provider.removeMetadataByItemName("item");
assertEquals(1, provider.getAll().size());
}

@Test
public void testRemoveMetadataByNamespace() {
GenericMetadataProvider provider = new GenericMetadataProvider();
provider.addMetadata("other", "item", "value", null);
provider.addMetadata("binding", "item", "value", null);
provider.addMetadata("binding", "other", "value", null);
assertEquals(3, provider.getAll().size());

provider.removeMetadataByNamespace("binding");
assertEquals(1, provider.getAll().size());
}
}

0 comments on commit 27f0117

Please sign in to comment.