From d6845cd40a0438356fd38c959b748502b8b5f943 Mon Sep 17 00:00:00 2001 From: charitha Date: Sat, 26 Oct 2019 01:03:44 +0530 Subject: [PATCH 1/2] Add new API endpoints - Change device status as bulk - Get total device count - Add group with different owner --- .../service/api/DeviceManagementService.java | 35 +++------ .../admin/DeviceManagementAdminService.java | 55 +++++++++++++ .../admin/GroupManagementAdminService.java | 77 ++++++++++++++++++- .../impl/DeviceManagementServiceImpl.java | 38 +++------ .../DeviceManagementAdminServiceImpl.java | 32 +++++++- .../GroupManagementAdminServiceImpl.java | 28 ++++++- .../DeviceManagementProviderService.java | 6 +- .../DeviceManagementProviderServiceImpl.java | 17 ++-- 8 files changed, 217 insertions(+), 71 deletions(-) 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 3531154a6e..f80d3326fe 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 @@ -1713,16 +1713,16 @@ public interface DeviceManagementService { @Valid OperationRequest operationRequest); @GET - @Path("/status/count/{tenantDomain}/{type}/{status}") + @Path("/type/{type}/status/{status}/count") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", value = "Get Device Count with status", - notes = "Get specified device count with status.", + notes = "Get specified device count with type and status.", tags = "Device Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:details") + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") }) } ) @@ -1730,7 +1730,7 @@ public interface DeviceManagementService { value = { @ApiResponse( code = 200, - message = "OK. \n Successfully fetched the details of the device.", + message = "OK. \n Successfully fetched the count of matching devices.", response = int.class, responseHeaders = { @ResponseHeader( @@ -1764,13 +1764,6 @@ public interface DeviceManagementService { response = ErrorResponse.class) }) Response getDeviceCountByStatus( - @ApiParam( - name = "tenantDomain", - value = "The tenant doamin.", - required = true) - @PathParam("tenantDomain") - @Size(max = 45) - String tenantDomain, @ApiParam( name = "type", value = "The device type name, such as ios, android, windows or fire-alarm.", @@ -1788,17 +1781,16 @@ public interface DeviceManagementService { @GET - @Path("/status/ids/{tenantDomain}/{type}/{status}") + @Path("/type/{type}/status/{status}/ids") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", - value = "Getting Details of a Device", - notes = "Get the details of a device by specifying the device type and device identifier and optionally " + - "the owner.", + value = "Getting ids of devices with specified type and status", + notes = "Get the ids of a device by specifying the device type and status.", tags = "Device Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:details") + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") }) } ) @@ -1840,13 +1832,6 @@ public interface DeviceManagementService { response = ErrorResponse.class) }) Response getDeviceIdentifiersByStatus( - @ApiParam( - name = "tenantDomain", - value = "The tenant domain.", - required = true) - @PathParam("tenantDomain") - @Size(max = 45) - String tenantDomain, @ApiParam( name = "type", value = "The device type name, such as ios, android, windows or fire-alarm.", @@ -1863,7 +1848,7 @@ public interface DeviceManagementService { String status); @PUT - @Path("/status/update/{tenantDomain}/{type}/{status}") + @Path("/type/{type}/status/{status}") @ApiOperation( produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON, @@ -1915,8 +1900,6 @@ public interface DeviceManagementService { response = ErrorResponse.class) }) Response bulkUpdateDeviceStatus( - @ApiParam(name = "tenantDomain", value = "The tenant domain.", required = true) - @PathParam("tenantDomain") String tenantDomain, @ApiParam(name = "type", value = "The device type, such as ios, android or windows.", required = true) @PathParam("type") String type, @ApiParam(name = "status", value = "The device type, such as ios, android or windows.", required = true) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java index 1ac3169189..29d8a2cb43 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceManagementAdminService.java @@ -45,9 +45,11 @@ import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import io.swagger.annotations.ResponseHeader; +import org.apache.axis2.transport.http.HTTPConstants; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; @@ -198,6 +200,59 @@ public interface DeviceManagementAdminService { defaultValue = "5") @QueryParam("limit") int limit); + @Path("/count") + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "Get the count of devices.", + notes = "Returns count of all devices enrolled with the system.", + tags = "Device Management Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin:devices:view") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the device count.", + response = Integer.class, + 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 has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 404, + message = "No groups found.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the device count.", + response = ErrorResponse.class) + }) + Response getDeviceCount(@ApiParam( + name = "status", + value = "status of group of which count should be retrieved") + @QueryParam("status") + String status); + + @PUT @Path("/device-owner") @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java index c42739f296..304a00d380 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java @@ -33,10 +33,12 @@ import io.swagger.annotations.ResponseHeader; import org.apache.axis2.transport.http.HTTPConstants; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; +import javax.validation.Valid; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -75,6 +77,12 @@ import javax.ws.rs.core.Response; description = "", key = "perm:admin-groups:count", permissions = {"/device-mgt/admin/groups/view"} + ), + @Scope( + name = "Add groups", + description = "", + key = "perm:admin-groups:add", + permissions = {"/device-mgt/admin/groups/add"} ) } ) @@ -166,7 +174,7 @@ public interface GroupManagementAdminService { ) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK. \n Successfully fetched the device group count.", - response = DeviceGroupList.class, + response = Integer.class, responseHeaders = { @ResponseHeader( name = "Content-Type", @@ -202,4 +210,71 @@ public interface GroupManagementAdminService { @QueryParam("status") String status); + @POST + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_POST, + value = "Adding a New Device Group", + notes = "Add device group with the current user as the owner.", + tags = "Device Group Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin-groups:add") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 201, + message = "Created. \n Device group has successfully been created", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The URL of the added group."), + @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 has been modified the last time" + + ".\n" + "Used by caches, or in conditional requests.") + } + ), + @ApiResponse( + code = 303, + message = "See Other. \n Source can be retrieved from the URL specified at the Location " + + "header.", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The Source URL of the document.")}), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 401, + message = "Unauthorized. \n Current logged in user is not authorized to add device groups.", + response = ErrorResponse.class), + @ApiResponse( + code = 415, + message = "Unsupported media type. \n The entity of the request was in a not supported " + + "format."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while adding a new device group.", + response = ErrorResponse.class) + }) + Response createGroup(@ApiParam( + name = "group", + value = "Define the group object with data.", + required = true) + @Valid DeviceGroup group); + } 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 ef89eb6ef8..c058b1ac69 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 @@ -973,66 +973,48 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @GET @Override - @Path("/status/count/{tenantDomain}/{type}/{status}") - public Response getDeviceCountByStatus(@PathParam("tenantDomain") String tenantDomain, @PathParam("type") String type, @PathParam("status") String status) { + @Path("/type/{type}/status/{status}/count") + public Response getDeviceCountByStatus(@PathParam("type") String type, @PathParam("status") String status) { int deviceCount; try { - int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); - deviceCount = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceCountOfTypeByStatus(tenantId, type, status); + deviceCount = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceCountOfTypeByStatus(type, status); return Response.status(Response.Status.OK).entity(deviceCount).build(); } catch (DeviceManagementException e) { String errorMessage = "Error while retrieving device count."; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); - } catch (UserStoreException e) { - String errorMessage = "Error resolving tenant Domain"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); } } @GET @Override - @Path("/status/ids/{tenantDomain}/{type}/{status}") - public Response getDeviceIdentifiersByStatus(@PathParam("tenantDomain") String tenantDomain, @PathParam("type") String type, @PathParam("status") String status) { + @Path("/type/{type}/status/{status}/ids") + public Response getDeviceIdentifiersByStatus(@PathParam("type") String type, @PathParam("status") String status) { List deviceIds; try { - int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); - deviceIds = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceIdentifiersByStatus(tenantId, type, status); + deviceIds = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceIdentifiersByStatus(type, status); return Response.status(Response.Status.OK).entity(deviceIds.toArray(new String[0])).build(); } catch (DeviceManagementException e) { String errorMessage = "Error while obtaining list of devices"; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); - } catch (UserStoreException e) { - String errorMessage = "Error resolving tenant Domain"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); } } @PUT @Override - @Path("/status/update/{tenantDomain}/{type}/{status}") - public Response bulkUpdateDeviceStatus(@PathParam("tenantDomain") String tenantDomain, @PathParam("type") String type, - @PathParam("status") String status, @Valid List deviceList) { + @Path("/type/{type}/status/{status}") + public Response bulkUpdateDeviceStatus(@PathParam("type") String type, @PathParam("status") String status, + @Valid List deviceList) { try { - int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); - DeviceMgtAPIUtils.getDeviceManagementService().bulkUpdateDeviceStatus(tenantId, type, deviceList, status); + DeviceMgtAPIUtils.getDeviceManagementService().bulkUpdateDeviceStatus(type, deviceList, status); } catch (DeviceManagementException e) { String errorMessage = "Error while updating device status in bulk."; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); - } catch (UserStoreException e) { - String errorMessage = "Error resolving tenant Domain"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); } return Response.status(Response.Status.OK).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java index 4b30cb4112..9533c43a42 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java @@ -40,10 +40,10 @@ import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; -import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.exceptions.UserNotFoundException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; @@ -53,7 +53,13 @@ import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.validation.constraints.Size; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; @@ -107,6 +113,26 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } } + @Override + @Path("/count") + @GET + public Response getDeviceCount(@QueryParam("status") String status) { + int deviceCount; + try { + if (status == null) { + deviceCount = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceCount(); + } else { + deviceCount = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceCount(EnrolmentInfo.Status.valueOf(status)); + } + } catch (DeviceManagementException e) { + String msg = "Error occurred while fetching device count."; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + return Response.status(Response.Status.OK).entity(deviceCount).build(); + } + @PUT @Override @Path("/device-owner") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java index dcb0238204..1971890b43 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java @@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.solr.common.StringUtils; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.GroupManagementAdminService; @@ -36,6 +38,10 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class); + private static final String DEFAULT_ADMIN_ROLE = "admin"; + private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", + "/permission/device-mgt/user/groups"}; + @Override public Response getGroups(String name, String owner, int offset, int limit, String status) { try { @@ -84,4 +90,24 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + + @Override + public Response createGroup(DeviceGroup group) { + if (group == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + group.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE); + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); + return Response.status(Response.Status.CREATED).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while adding new group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (GroupAlreadyExistException e) { + String msg = "Group already exists with name " + group.getName() + "."; + log.warn(msg); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 43bb5ba713..8765ed22e5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -752,11 +752,11 @@ public interface DeviceManagementProviderService { List findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength) throws DeviceManagementException; - int getDeviceCountOfTypeByStatus(int tenantId, String deviceType, String deviceStatus) throws DeviceManagementException; + int getDeviceCountOfTypeByStatus(String deviceType, String deviceStatus) throws DeviceManagementException; - List getDeviceIdentifiersByStatus(int tenantId, String deviceType, String deviceStatus) throws DeviceManagementException; + List getDeviceIdentifiersByStatus(String deviceType, String deviceStatus) throws DeviceManagementException; - boolean bulkUpdateDeviceStatus(int tenantId, String deviceType, List deviceList, String status) throws DeviceManagementException; + boolean bulkUpdateDeviceStatus(String deviceType, List deviceList, String status) throws DeviceManagementException; boolean updateEnrollment(String owner, List deviceIdentifiers) throws DeviceManagementException, UserNotFoundException, InvalidDeviceException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index a012fff0c8..2669d292bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -3125,10 +3125,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public int getDeviceCountOfTypeByStatus(int tenantId, String deviceType, String deviceStatus) throws DeviceManagementException { + public int getDeviceCountOfTypeByStatus(String deviceType, String deviceStatus) throws DeviceManagementException { try { DeviceManagementDAOFactory.openConnection(); - return deviceDAO.getDeviceCount(deviceType, deviceStatus, tenantId); + return deviceDAO.getDeviceCount(deviceType, deviceStatus, getTenantId()); } catch (DeviceManagementDAOException e) { String msg = "Error occurred in while retrieving device count by status for deviceType :" +deviceType + " status : " + deviceStatus; log.error(msg, e); @@ -3143,11 +3143,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getDeviceIdentifiersByStatus(int tenantId, String deviceType, String deviceStatus) throws DeviceManagementException { + public List getDeviceIdentifiersByStatus(String deviceType, String deviceStatus) throws DeviceManagementException { List deviceIds; try { DeviceManagementDAOFactory.openConnection(); - deviceIds = deviceDAO.getDeviceIdentifiers(deviceType, deviceStatus, tenantId); + deviceIds = deviceDAO.getDeviceIdentifiers(deviceType, deviceStatus, getTenantId()); } catch (DeviceManagementDAOException e) { String msg = "Error occurred in while retrieving devices by status for deviceType :" +deviceType + " status : " + deviceStatus; log.error(msg, e); @@ -3163,17 +3163,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public boolean bulkUpdateDeviceStatus(int tenantId, String deviceType, - List deviceList, String status) + public boolean bulkUpdateDeviceStatus(String deviceType, List deviceList, String status) throws DeviceManagementException { boolean success; try { DeviceManagementDAOFactory.openConnection(); - success = deviceDAO.setEnrolmentStatusInBulk(deviceType, status, tenantId, deviceList); + success = deviceDAO.setEnrolmentStatusInBulk(deviceType, status, getTenantId(), deviceList); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { - String msg = "Error occurred in while updating status of devices :" + deviceType + " status : " + deviceList - .toString(); + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred in while updating status of devices :" + deviceType + " status : " + status; log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (SQLException e) { From 253afa517a06132fb4ae419b4f77a8975ef82bfe Mon Sep 17 00:00:00 2001 From: charitha Date: Sat, 26 Oct 2019 01:42:37 +0530 Subject: [PATCH 2/2] Fix transaction handling --- .../mgt/core/service/DeviceManagementProviderServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 2669d292bb..a2f9a9769e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -3167,7 +3167,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throws DeviceManagementException { boolean success; try { - DeviceManagementDAOFactory.openConnection(); + DeviceManagementDAOFactory.beginTransaction(); success = deviceDAO.setEnrolmentStatusInBulk(deviceType, status, getTenantId(), deviceList); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { @@ -3175,7 +3175,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv String msg = "Error occurred in while updating status of devices :" + deviceType + " status : " + status; log.error(msg, e); throw new DeviceManagementException(msg, e); - } catch (SQLException e) { + } catch (TransactionManagementException e) { String msg = "Error occurred while opening a connection to the data source"; log.error(msg, e); throw new DeviceManagementException(msg, e);