Search in sources :

Example 1 with PersistedItem

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.");
        }
    }
}
Also used : PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) Entry(java.util.Map.Entry) Reference(org.osgi.service.component.annotations.Reference)

Example 2 with PersistedItem

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);
    }
}
Also used : PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) StorageService(org.eclipse.smarthome.core.storage.StorageService) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with PersistedItem

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);
    }
}
Also used : PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) StorageService(org.eclipse.smarthome.core.storage.StorageService) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with PersistedItem

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;
}
Also used : PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem)

Example 5 with PersistedItem

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;
}
Also used : PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem)

Aggregations

PersistedItem (org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem)5 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 StringItem (org.eclipse.smarthome.core.library.items.StringItem)2 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)2 StorageService (org.eclipse.smarthome.core.storage.StorageService)2 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)2 Test (org.junit.Test)2 Entry (java.util.Map.Entry)1 Reference (org.osgi.service.component.annotations.Reference)1