use of org.eclipse.smarthome.core.items.dto.GroupItemDTO in project smarthome by eclipse.
the class ItemResource method createOrUpdateItems.
/**
* Create or Update a list of items by supplying a list of item beans.
*
* @param items the list of item beans.
* @return array of status information for each item bean
*/
@PUT
@RolesAllowed({ Role.ADMIN })
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Adds a list of items to the registry or updates the existing items.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 400, message = "Item list is null.") })
public Response createOrUpdateItems(@ApiParam(value = "array of item data", required = true) GroupItemDTO[] items) {
// If we didn't get an item list bean, then return!
if (items == null) {
return Response.status(Status.BAD_REQUEST).build();
}
List<GroupItemDTO> wrongTypes = new ArrayList<>();
List<ActiveItem> activeItems = new ArrayList<>();
for (GroupItemDTO item : items) {
ActiveItem newItem = createActiveItem(item);
if (newItem == null) {
wrongTypes.add(item);
} else {
activeItems.add(newItem);
}
}
List<ActiveItem> createdItems = new ArrayList<>();
List<ActiveItem> updatedItems = new ArrayList<>();
List<ActiveItem> failedItems = new ArrayList<>();
for (ActiveItem activeItem : activeItems) {
String itemName = activeItem.getName();
if (getItem(itemName) == null) {
// item does not yet exist, create it
managedItemProvider.add(activeItem);
createdItems.add(activeItem);
} else if (managedItemProvider.get(itemName) != null) {
// item already exists as a managed item, update it
managedItemProvider.update(activeItem);
updatedItems.add(activeItem);
} else {
// Item exists but cannot be updated
logger.warn("Cannot update existing item '{}', because it is not managed.", itemName);
failedItems.add(activeItem);
}
}
// build response
List<JsonObject> responseList = new ArrayList<>();
for (GroupItemDTO item : wrongTypes) {
responseList.add(buildStatusObject(item.name, "error", "Received HTTP PUT request at '" + uriInfo.getPath() + "' with an invalid item type '" + item.type + "'."));
}
for (ActiveItem item : failedItems) {
responseList.add(buildStatusObject(item.getName(), "error", "Cannot update non-managed item"));
}
for (ActiveItem item : createdItems) {
responseList.add(buildStatusObject(item.getName(), "created", null));
}
for (ActiveItem item : updatedItems) {
responseList.add(buildStatusObject(item.getName(), "updated", null));
}
return JSONResponse.createResponse(Status.OK, responseList, null);
}
use of org.eclipse.smarthome.core.items.dto.GroupItemDTO in project smarthome by eclipse.
the class ItemResourceOSGiTest method addMultipleItems.
@Test
public void addMultipleItems() throws IOException {
List<GroupItemDTO> itemList = new ArrayList<>();
GroupItemDTO[] items = new GroupItemDTO[] {};
GroupItemDTO item1DTO = new GroupItemDTO();
item1DTO.name = "item1";
item1DTO.type = "Switch";
item1DTO.label = "item1Label";
itemList.add(item1DTO);
GroupItemDTO item2DTO = new GroupItemDTO();
item2DTO.name = "item2";
item2DTO.type = "Rollershutter";
item2DTO.label = "item2Label";
itemList.add(item2DTO);
items = itemList.toArray(items);
Response response = itemResource.createOrUpdateItems(items);
String jsonResponse = IOUtils.toString((InputStream) response.getEntity());
List<String> statusCodes = JsonPath.read(jsonResponse, "$..status");
// expect 2x created
assertThat(statusCodes.size(), is(2));
assertThat(statusCodes.get(0), is("created"));
assertThat(statusCodes.get(1), is("created"));
itemList.clear();
item1DTO.label = "item1LabelNew";
itemList.add(item1DTO);
item2DTO.type = "WrongType";
itemList.add(item2DTO);
items = itemList.toArray(items);
response = itemResource.createOrUpdateItems(items);
jsonResponse = IOUtils.toString((InputStream) response.getEntity());
statusCodes = JsonPath.read(jsonResponse, "$..status");
// expect error and updated
assertThat(statusCodes.size(), is(2));
assertThat(statusCodes.get(0), is("error"));
assertThat(statusCodes.get(1), is("updated"));
}
Aggregations