Search in sources :

Example 1 with GroupItemDTO

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);
}
Also used : GroupItemDTO(org.eclipse.smarthome.core.items.dto.GroupItemDTO) ArrayList(java.util.ArrayList) ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) JsonObject(com.google.gson.JsonObject) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with GroupItemDTO

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"));
}
Also used : Response(javax.ws.rs.core.Response) GroupItemDTO(org.eclipse.smarthome.core.items.dto.GroupItemDTO) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 GroupItemDTO (org.eclipse.smarthome.core.items.dto.GroupItemDTO)2 JsonObject (com.google.gson.JsonObject)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 InputStream (java.io.InputStream)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Response (javax.ws.rs.core.Response)1 ActiveItem (org.eclipse.smarthome.core.items.ActiveItem)1 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)1 Test (org.junit.Test)1