use of org.eclipse.smarthome.core.items.ActiveItem in project smarthome by eclipse.
the class ItemResource method createOrUpdateItem.
/**
* Create or Update an item by supplying an item bean.
*
* @param itemname
* @param item the item bean.
* @return
*/
@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}")
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Adds a new item to the registry or updates the existing item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 201, message = "Item created."), @ApiResponse(code = 400, message = "Item null."), @ApiResponse(code = 404, message = "Item not found."), @ApiResponse(code = 405, message = "Item not editable.") })
public Response createOrUpdateItem(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @ApiParam(value = "item data", required = true) GroupItemDTO item) {
final Locale locale = LocaleUtil.getLocale(language);
// If we didn't get an item bean, then return!
if (item == null) {
return Response.status(Status.BAD_REQUEST).build();
}
ActiveItem newItem = createActiveItem(item);
if (newItem == null) {
logger.warn("Received HTTP PUT request at '{}' with an invalid item type '{}'.", uriInfo.getPath(), item.type);
return Response.status(Status.BAD_REQUEST).build();
}
// Save the item
if (getItem(itemname) == null) {
// item does not yet exist, create it
managedItemProvider.add(newItem);
return getItemResponse(Status.CREATED, newItem, locale, null);
} else if (managedItemProvider.get(itemname) != null) {
// item already exists as a managed item, update it
managedItemProvider.update(newItem);
return getItemResponse(Status.OK, newItem, locale, null);
} else {
// Item exists but cannot be updated
logger.warn("Cannot update existing item '{}', because is not managed.", itemname);
return JSONResponse.createErrorResponse(Status.METHOD_NOT_ALLOWED, "Cannot update non-managed Item " + itemname);
}
}
use of org.eclipse.smarthome.core.items.ActiveItem in project smarthome by eclipse.
the class ItemResource method addTag.
@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}/tags/{tag}")
@ApiOperation(value = "Adds a tag to an item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item not found."), @ApiResponse(code = 405, message = "Item not editable.") })
public Response addTag(@PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @PathParam("tag") @ApiParam(value = "tag", required = true) String tag) {
Item item = getItem(itemname);
if (item == null) {
logger.info("Received HTTP PUT request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
return Response.status(Status.NOT_FOUND).build();
}
if (managedItemProvider.get(itemname) == null) {
return Response.status(Status.METHOD_NOT_ALLOWED).build();
}
((ActiveItem) item).addTag(tag);
managedItemProvider.update(item);
return Response.ok(null, MediaType.TEXT_PLAIN).build();
}
use of org.eclipse.smarthome.core.items.ActiveItem in project smarthome by eclipse.
the class ItemResource method removeTag.
@DELETE
@RolesAllowed({ Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}/tags/{tag}")
@ApiOperation(value = "Removes a tag from an item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item not found."), @ApiResponse(code = 405, message = "Item not editable.") })
public Response removeTag(@PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @PathParam("tag") @ApiParam(value = "tag", required = true) String tag) {
Item item = getItem(itemname);
if (item == null) {
logger.info("Received HTTP DELETE request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
return Response.status(Status.NOT_FOUND).build();
}
if (managedItemProvider.get(itemname) == null) {
return Response.status(Status.METHOD_NOT_ALLOWED).build();
}
((ActiveItem) item).removeTag(tag);
managedItemProvider.update(item);
return Response.ok(null, MediaType.TEXT_PLAIN).build();
}
use of org.eclipse.smarthome.core.items.ActiveItem 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);
}
Aggregations