From a356ec797caf8869177411dab49cd78b9037ec44 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Mon, 4 May 2020 17:54:23 +0530 Subject: [PATCH 1/2] 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 15747cb21c3..9e786f1deb2 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 5f748b32c2b..baa9b4a2c45 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 00000000000..5f609c0890b --- /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; + } +} From 33c495ad351469a5a853a0bfe58576ab7c715074 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Tue, 5 May 2020 11:10:55 +0530 Subject: [PATCH 2/2] Add license header to DeviceFilter bean --- .../carbon/device/mgt/common/DeviceFilters.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 index 5f609c0890b..ff2e8ebd1bd 100644 --- 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 @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.carbon.device.mgt.common; import java.io.Serializable;