use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class PersistenceManagerImpl method appliesToItem.
/**
* Checks if a given persistence configuration entry is relevant for an item
*
* @param config the persistence configuration entry
* @param item to check if the configuration applies to
* @return true, if the configuration applies to the item
*/
private boolean appliesToItem(SimpleItemConfiguration config, Item item) {
for (SimpleConfig itemCfg : config.getItems()) {
if (itemCfg instanceof SimpleAllConfig) {
return true;
}
if (itemCfg instanceof SimpleItemConfig) {
SimpleItemConfig singleItemConfig = (SimpleItemConfig) itemCfg;
if (item.getName().equals(singleItemConfig.getItem())) {
return true;
}
}
if (itemCfg instanceof SimpleGroupConfig) {
SimpleGroupConfig groupItemCfg = (SimpleGroupConfig) itemCfg;
String groupName = groupItemCfg.getGroup();
try {
Item gItem = itemRegistry.getItem(groupName);
if (gItem instanceof GroupItem) {
GroupItem groupItem = (GroupItem) gItem;
if (groupItem.getAllMembers().contains(item)) {
return true;
}
}
} catch (Exception e) {
}
}
}
return false;
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class ItemResource method addMember.
@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{itemName: [a-zA-Z_0-9]*}/members/{memberItemName: [a-zA-Z_0-9]*}")
@ApiOperation(value = "Adds a new member to a group item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item or member item not found or item is not of type group item."), @ApiResponse(code = 405, message = "Member item is not editable.") })
public Response addMember(@PathParam("itemName") @ApiParam(value = "item name", required = true) String itemName, @PathParam("memberItemName") @ApiParam(value = "member item name", required = true) String memberItemName) {
try {
Item item = itemRegistry.getItem(itemName);
if (!(item instanceof GroupItem)) {
return Response.status(Status.NOT_FOUND).build();
}
GroupItem groupItem = (GroupItem) item;
Item memberItem = itemRegistry.getItem(memberItemName);
if (!(memberItem instanceof GenericItem)) {
return Response.status(Status.NOT_FOUND).build();
}
if (managedItemProvider.get(memberItemName) == null) {
return Response.status(Status.METHOD_NOT_ALLOWED).build();
}
GenericItem genericMemberItem = (GenericItem) memberItem;
genericMemberItem.addGroupName(groupItem.getName());
managedItemProvider.update(genericMemberItem);
return Response.ok(null, MediaType.TEXT_PLAIN).build();
} catch (ItemNotFoundException e) {
return Response.status(Status.NOT_FOUND).build();
}
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class EnrichedItemDTOMapper method map.
private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, URI uri, boolean drillDown, Predicate<Item> itemFilter, Locale locale) {
String state = item.getState().toFullString();
String transformedState = considerTransformation(state, item.getStateDescription(locale));
if (transformedState != null && transformedState.equals(state)) {
transformedState = null;
}
StateDescription stateDescription = considerTransformation(item.getStateDescription(locale));
String link = null != uri ? uri.toASCIIString() + ItemResource.PATH_ITEMS + "/" + itemDTO.name : null;
EnrichedItemDTO enrichedItemDTO = null;
if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
EnrichedItemDTO[] memberDTOs;
if (drillDown) {
Collection<EnrichedItemDTO> members = new LinkedHashSet<>();
for (Item member : groupItem.getMembers()) {
if (itemFilter == null || itemFilter.test(member)) {
members.add(map(member, drillDown, itemFilter, uri, locale));
}
}
memberDTOs = members.toArray(new EnrichedItemDTO[members.size()]);
} else {
memberDTOs = new EnrichedItemDTO[0];
}
enrichedItemDTO = new EnrichedGroupItemDTO(itemDTO, memberDTOs, link, state, transformedState, stateDescription);
} else {
enrichedItemDTO = new EnrichedItemDTO(itemDTO, link, state, transformedState, stateDescription);
}
return enrichedItemDTO;
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class EnrichedItemDTOMapperTest method testFiltering.
@Test
public void testFiltering() {
GroupItem group = new GroupItem("TestGroup");
GroupItem subGroup = new GroupItem("TestSubGroup");
GenericItem switchItem = itemFactory.createItem(CoreItemFactory.SWITCH, "TestSwitch");
GenericItem numberItem = itemFactory.createItem(CoreItemFactory.NUMBER, "TestNumber");
GenericItem stringItem = itemFactory.createItem(CoreItemFactory.STRING, "TestString");
if (switchItem != null && numberItem != null && stringItem != null) {
group.addMember(subGroup);
group.addMember(switchItem);
group.addMember(numberItem);
subGroup.addMember(stringItem);
}
EnrichedGroupItemDTO dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, false, null, URI.create(""), null);
assertThat(dto.members.length, is(0));
dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, null, URI.create(""), null);
assertThat(dto.members.length, is(3));
assertThat(((EnrichedGroupItemDTO) dto.members[0]).members.length, is(1));
dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, i -> i.getType().equals(CoreItemFactory.NUMBER), URI.create(""), null);
assertThat(dto.members.length, is(1));
dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, i -> i.getType().equals(CoreItemFactory.NUMBER) || i instanceof GroupItem, URI.create(""), null);
assertThat(dto.members.length, is(2));
assertThat(((EnrichedGroupItemDTO) dto.members[0]).members.length, is(0));
dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, i -> i.getType().equals(CoreItemFactory.NUMBER) || i instanceof GroupItem, URI.create(""), null);
assertThat(dto.members.length, is(2));
assertThat(((EnrichedGroupItemDTO) dto.members[0]).members.length, is(0));
dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, i -> i.getType().equals(CoreItemFactory.NUMBER) || i.getType().equals(CoreItemFactory.STRING) || i instanceof GroupItem, URI.create(""), null);
assertThat(dto.members.length, is(2));
assertThat(((EnrichedGroupItemDTO) dto.members[0]).members.length, is(1));
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class ItemUIRegistryImpl method getDynamicGroupChildren.
/**
* This method creates a list of children for a group dynamically.
* If there are no explicit children defined in a sitemap, the children
* can thus be created on the fly by iterating over the members of the group item.
*
* @param group The group widget to get children for
* @return a list of default widgets provided for the member items
*/
private EList<Widget> getDynamicGroupChildren(Group group) {
EList<Widget> children = new BasicEList<Widget>();
String itemName = group.getItem();
try {
if (itemName != null) {
Item item = getItem(itemName);
if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
for (Item member : groupItem.getMembers()) {
Widget widget = getDefaultWidget(member.getClass(), member.getName());
if (widget != null) {
widget.setItem(member.getName());
children.add(widget);
}
}
} else {
logger.warn("Item '{}' is not a group.", item.getName());
}
} else {
logger.warn("Group does not specify an associated item - ignoring it.");
}
} catch (ItemNotFoundException e) {
logger.warn("Dynamic group with label '{}' will be ignored, because its item '{}' does not exist.", group.getLabel(), itemName);
}
return children;
}
Aggregations