From a356ec797caf8869177411dab49cd78b9037ec44 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Mon, 4 May 2020 17:54:23 +0530 Subject: [PATCH] Add API to retrieve device filters --- .../service/api/DeviceManagementService.java | 39 ++++++++++++++++++ .../impl/DeviceManagementServiceImpl.java | 33 +++++++++++++++ .../device/mgt/common/DeviceFilters.java | 40 +++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceFilters.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 15747cb21c..9e786f1deb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -2188,4 +2188,43 @@ public interface DeviceManagementService { value = "The device identifier") @PathParam("id") String deviceId, OperationStatusBean operationStatusBean); + + @GET + @Path("/filters") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Retrieving filters of device.", + notes = "Provides filters in devices of Entgra IoT Server which can be used in UI for filtering.", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the device filters.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 500, + message = "Error occurred while getting the version data.", + response = ErrorResponse.class) + }) + Response getDeviceFilters(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 5f748b32c2..baa9b4a2c4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -44,6 +44,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceFilters; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.Feature; @@ -1274,4 +1275,36 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } } + + @GET + @Path("/filters") + @Override + public Response getDeviceFilters() { + try { + List deviceTypeNames = new ArrayList<>(); + List ownershipNames = new ArrayList<>(); + List statusNames = new ArrayList<>(); + List deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceTypes(); + for (DeviceType deviceType : deviceTypes) { + deviceTypeNames.add(deviceType.getName()); + } + EnrolmentInfo.OwnerShip[] ownerShips = EnrolmentInfo.OwnerShip.values(); + for (EnrolmentInfo.OwnerShip ownerShip : ownerShips) { + ownershipNames.add(ownerShip.name()); + } + EnrolmentInfo.Status[] statuses = EnrolmentInfo.Status.values(); + for (EnrolmentInfo.Status status : statuses) { + statusNames.add(status.name()); + } + DeviceFilters deviceFilters = new DeviceFilters(); + deviceFilters.setDeviceTypes(deviceTypeNames); + deviceFilters.setOwnerships(ownershipNames); + deviceFilters.setStatuses(statusNames); + return Response.status(Response.Status.OK).entity(deviceFilters).build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred white retrieving device types to be used in device filters."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceFilters.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceFilters.java new file mode 100644 index 0000000000..5f609c0890 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceFilters.java @@ -0,0 +1,40 @@ +package org.wso2.carbon.device.mgt.common; + +import java.io.Serializable; +import java.util.List; + +/** + * This class carries information related to device filtering values which will be used in the UI to filter devices. + */ +public class DeviceFilters implements Serializable { + + private static final long serialVersionUID = -5249449134602406387L; + + private List deviceTypes; + private List ownerships; + private List statuses; + + public List getDeviceTypes() { + return deviceTypes; + } + + public void setDeviceTypes(List deviceTypes) { + this.deviceTypes = deviceTypes; + } + + public List getOwnerships() { + return ownerships; + } + + public void setOwnerships(List ownerships) { + this.ownerships = ownerships; + } + + public List getStatuses() { + return statuses; + } + + public void setStatuses(List statuses) { + this.statuses = statuses; + } +}