use of org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem in project smarthome by eclipse.
the class ManagedItemProvider method addItemFactory.
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addItemFactory(ItemFactory itemFactory) {
itemFactories.add(itemFactory);
if (failedToCreate.size() > 0) {
// retry failed creation attempts
Iterator<Entry<String, PersistedItem>> iterator = failedToCreate.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, PersistedItem> entry = iterator.next();
String itemName = entry.getKey();
PersistedItem persistedItem = entry.getValue();
Item item = itemFactory.createItem(persistedItem.itemType, itemName);
if (item != null && item instanceof ActiveItem) {
iterator.remove();
configureItem(persistedItem, (ActiveItem) item);
notifyListenersAboutAddedElement(item);
} else {
logger.debug("The added item factory '{}' still could not instantiate item '{}'.", itemFactory, itemName);
}
}
if (failedToCreate.isEmpty()) {
logger.info("Finished loading the items which could not have been created before.");
}
}
}
use of org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem in project smarthome by eclipse.
the class ManagedItemProviderOSGiTest method assertItemsAreAddedAsGroupMembersOnDeferredCreation.
@SuppressWarnings("null")
@Test
public void assertItemsAreAddedAsGroupMembersOnDeferredCreation() throws ItemNotFoundException {
StorageService storageService = getService(StorageService.class);
assertThat(storageService, is(notNullValue()));
Storage<PersistedItem> storage = storageService.getStorage(Item.class.getName());
StrangeItem item = new StrangeItem("SomeStrangeItem");
GroupItem groupItem = new GroupItem("SomeGroupItem");
item.addGroupName(groupItem.getName());
groupItem.addMember(item);
String itemKey = item.getUID();
// put items into the storage that cannot be handled (yet)
storage.put(itemKey, itemProvider.toPersistableElement(item));
itemProvider.add(groupItem);
// start without the appropriate item factory - it only creates the group item
assertThat(itemProvider.getAll().size(), is(1));
assertThat(itemRegistry.getItems().size(), is(1));
assertThat(itemProvider.get("SomeStrangeItem"), is(nullValue()));
assertThat(itemProvider.get("SomeGroupItem"), is(notNullValue()));
assertThat(itemRegistry.getItem("SomeGroupItem"), is(notNullValue()));
try {
assertThat(itemRegistry.getItem("SomeStrangeItem"), is(nullValue()));
fail("the item is not (yet) expected to be there");
} catch (ItemNotFoundException e) {
// all good
}
// now register the item factory. Both items should be there...
StrangeItemFactory factory = new StrangeItemFactory();
registerService(factory);
try {
assertThat(itemProvider.getAll().size(), is(2));
assertThat(itemRegistry.getItems().size(), is(2));
Item item1 = itemRegistry.get("SomeStrangeItem");
Item item2 = itemRegistry.get("SomeGroupItem");
assertThat(itemRegistry.get("SomeStrangeItem"), is(notNullValue()));
assertThat(item1, is(notNullValue()));
assertThat(item1.getGroupNames().size(), is(1));
assertThat(((GroupItem) item2).getMembers().size(), is(1));
} finally {
unregisterService(factory);
}
}
use of org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem in project smarthome by eclipse.
the class ManagedItemProviderOSGiTest method assertItemsAreThereOnceTheFactoryGetsAdded.
@Test
public void assertItemsAreThereOnceTheFactoryGetsAdded() throws ItemNotFoundException {
StorageService storageService = getService(StorageService.class);
assertThat(storageService, is(notNullValue()));
Storage<PersistedItem> storage = storageService.getStorage(Item.class.getName());
StrangeItem item = new StrangeItem("SomeStrangeItem");
String key = item.getUID();
// put an item into the storage that cannot be handled (yet)
storage.put(key, itemProvider.toPersistableElement(item));
// start without the appropriate item factory - it's going to fail silently, leaving a debug log
assertThat(itemProvider.getAll().size(), is(0));
assertThat(itemRegistry.getItems().size(), is(0));
assertThat(itemProvider.get("SomeStrangeItem"), is(nullValue()));
try {
assertThat(itemRegistry.getItem("SomeStrangeItem"), is(nullValue()));
fail("the item is not (yet) expected to be there");
} catch (ItemNotFoundException e) {
// all good
}
// now register the item factory. The item should be there...
StrangeItemFactory factory = new StrangeItemFactory();
registerService(factory);
try {
assertThat(itemProvider.getAll().size(), is(1));
assertThat(itemRegistry.getItems().size(), is(1));
assertThat(itemProvider.get("SomeStrangeItem"), is(notNullValue()));
assertThat(itemRegistry.getItem("SomeStrangeItem"), is(notNullValue()));
} finally {
unregisterService(factory);
}
}
use of org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem in project smarthome by eclipse.
the class ManagedItemProvider method toElement.
@Override
protected Item toElement(String itemName, PersistedItem persistedItem) {
Item item = null;
if (persistedItem.itemType.equals(ITEM_TYPE_GROUP)) {
if (persistedItem.baseItemType != null) {
Item baseItem = createItem(persistedItem.baseItemType, itemName);
if (persistedItem.functionName != null) {
GroupFunction function = getGroupFunction(persistedItem, baseItem);
item = new GroupItem(itemName, baseItem, function);
} else {
item = new GroupItem(itemName, baseItem, new GroupFunction.Equality());
}
} else {
item = new GroupItem(itemName);
}
} else {
item = createItem(persistedItem.itemType, itemName);
}
if (item != null && item instanceof ActiveItem) {
configureItem(persistedItem, (ActiveItem) item);
}
if (item == null) {
failedToCreate.put(itemName, persistedItem);
logger.debug("Couldn't restore item '{}' of type '{}' ~ there is no appropriate ItemFactory available.", itemName, persistedItem.itemType);
}
return item;
}
use of org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem in project smarthome by eclipse.
the class ManagedItemProvider method toPersistableElement.
@Override
protected PersistedItem toPersistableElement(Item item) {
PersistedItem persistedItem = new PersistedItem(item instanceof GroupItem ? ITEM_TYPE_GROUP : toItemFactoryName(item));
if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
String baseItemType = null;
Item baseItem = groupItem.getBaseItem();
if (baseItem != null) {
baseItemType = toItemFactoryName(baseItem);
}
persistedItem.baseItemType = baseItemType;
addFunctionToPersisedItem(persistedItem, groupItem);
}
persistedItem.label = item.getLabel();
persistedItem.groupNames = new ArrayList<>(item.getGroupNames());
persistedItem.tags = new HashSet<>(item.getTags());
persistedItem.category = item.getCategory();
return persistedItem;
}
Aggregations