From 367a87697a158ba04f8e8802190261f0c8620e5c Mon Sep 17 00:00:00 2001 From: charitha Date: Wed, 15 Dec 2021 14:32:54 +0530 Subject: [PATCH] Allow retrieving activities for multiple device ids --- .../api/ActivityInfoProviderService.java | 3 +- .../impl/ActivityProviderServiceImpl.java | 6 +- .../mgt/common/ActivityPaginationRequest.java | 12 ++- .../mgt/dao/impl/GenericOperationDAOImpl.java | 102 ++++++++++-------- 4 files changed, 72 insertions(+), 51 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java index 75af90eed69..251d6fc0a52 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java @@ -48,6 +48,7 @@ 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; /** * Activity related REST-API implementation. @@ -460,7 +461,7 @@ public interface ActivityInfoProviderService { name = "deviceId", value = "Device Id to filter" ) - @QueryParam("deviceId") String deviceId, + @QueryParam("deviceId") List deviceIds, @ApiParam( name = "type", value = "Operation type to filter" diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java index 8b74afc8c48..9c253c4fa6e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java @@ -225,7 +225,7 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService @QueryParam("initiatedBy") String initiatedBy, @QueryParam("operationCode") String operationCode, @QueryParam("deviceType") String deviceType, - @QueryParam("deviceId") String deviceId, + @QueryParam("deviceId") List deviceIds, @QueryParam("type") String type, @QueryParam("status") String status, @HeaderParam("If-Modified-Since") String ifModifiedSince, @@ -297,8 +297,8 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService if (deviceType != null && !deviceType.isEmpty()) { activityPaginationRequest.setDeviceType(deviceType); } - if (deviceId != null && !deviceId.isEmpty()) { - activityPaginationRequest.setDeviceId(deviceId); + if (deviceIds != null && !deviceIds.isEmpty()) { + activityPaginationRequest.setDeviceIds(deviceIds); } if (type != null && !type.isEmpty()) { activityPaginationRequest.setType(Operation.Type.valueOf(type.toUpperCase())); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java index 08cc89093a3..b45d6c5083b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java @@ -20,6 +20,8 @@ package org.wso2.carbon.device.mgt.common; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import java.util.List; + /** * This class holds required parameters for a querying a paginated activity response. */ @@ -28,7 +30,7 @@ public class ActivityPaginationRequest { private int offset; private int limit; private String deviceType; - private String deviceId; + private List deviceIds; private String operationCode; private String initiatedBy; private long since; @@ -66,12 +68,12 @@ public class ActivityPaginationRequest { this.deviceType = deviceType; } - public String getDeviceId() { - return deviceId; + public List getDeviceIds() { + return deviceIds; } - public void setDeviceId(String deviceId) { - this.deviceId = deviceId; + public void setDeviceIds(List deviceIds) { + this.deviceIds = deviceIds; } public String getOperationCode() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index de92618c466..7a1f1083a1f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -1729,7 +1729,7 @@ public class GenericOperationDAOImpl implements OperationDAO { boolean isTimeDurationFilteringProvided = false; Connection conn = OperationManagementDAOFactory.getConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + + StringBuilder sql = new StringBuilder("SELECT " + " eom.ENROLMENT_ID," + " eom.CREATED_TIMESTAMP," + " eom.UPDATED_TIMESTAMP," + @@ -1750,72 +1750,82 @@ public class GenericOperationDAOImpl implements OperationDAO { "LEFT JOIN " + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + "INNER JOIN " + - " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ? "; + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ? "); if (activityPaginationRequest.getDeviceType() != null) { - sql += "AND DEVICE_TYPE = ? "; + sql.append("AND DEVICE_TYPE = ? "); } - if (activityPaginationRequest.getDeviceId() != null) { - sql += "AND DEVICE_IDENTIFICATION = ? "; + if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) { + sql.append("AND DEVICE_IDENTIFICATION IN ("); + for (int i = 0; i < activityPaginationRequest.getDeviceIds().size() - 1; i++) { + sql.append("?, "); + } + sql.append("?) "); } if (activityPaginationRequest.getOperationCode() != null) { - sql += "AND OPERATION_CODE = ? "; + sql.append("AND OPERATION_CODE = ? "); } if (activityPaginationRequest.getInitiatedBy() != null) { - sql += "AND INITIATED_BY = ? "; + sql.append("AND INITIATED_BY = ? "); } if (activityPaginationRequest.getSince() != 0) { - sql += "AND UPDATED_TIMESTAMP > ? "; + sql.append("AND UPDATED_TIMESTAMP > ? "); } if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { isTimeDurationFilteringProvided = true; - sql += "AND CREATED_TIMESTAMP BETWEEN ? AND ? "; + sql.append("AND CREATED_TIMESTAMP BETWEEN ? AND ? "); } if (activityPaginationRequest.getType() != null) { - sql += "AND TYPE = ? "; + sql.append("AND TYPE = ? "); } if (activityPaginationRequest.getStatus() != null) { - sql += "AND STATUS = ? "; + sql.append("AND STATUS = ? "); } - sql += "ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + - "ON eom_ordered.OPERATION_ID = eom.OPERATION_ID WHERE eom.TENANT_ID = ? "; + sql.append("ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + "ON eom_ordered.OPERATION_ID = eom.OPERATION_ID WHERE eom.TENANT_ID = ? "); if (activityPaginationRequest.getDeviceType() != null) { - sql += "AND eom.DEVICE_TYPE = ? "; + sql.append("AND eom.DEVICE_TYPE = ? "); } - if (activityPaginationRequest.getDeviceId() != null) { - sql += "AND eom.DEVICE_IDENTIFICATION = ? "; + if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) { + sql.append("AND eom.DEVICE_IDENTIFICATION IN ("); + for (int i = 0; i < activityPaginationRequest.getDeviceIds().size() - 1; i++) { + sql.append("?, "); + } + sql.append("?) "); } if (activityPaginationRequest.getOperationCode() != null) { - sql += "AND eom.OPERATION_CODE = ? "; + sql.append("AND eom.OPERATION_CODE = ? "); } if (activityPaginationRequest.getInitiatedBy() != null) { - sql += "AND eom.INITIATED_BY = ? "; + sql.append("AND eom.INITIATED_BY = ? "); } if (activityPaginationRequest.getSince() != 0) { - sql += "AND eom.UPDATED_TIMESTAMP > ? "; + sql.append("AND eom.UPDATED_TIMESTAMP > ? "); } if (isTimeDurationFilteringProvided) { - sql += "AND eom.CREATED_TIMESTAMP BETWEEN ? AND ? "; + sql.append("AND eom.CREATED_TIMESTAMP BETWEEN ? AND ? "); } if (activityPaginationRequest.getType() != null) { - sql += "AND eom.TYPE = ? "; + sql.append("AND eom.TYPE = ? "); } if (activityPaginationRequest.getStatus() != null) { - sql += "AND eom.STATUS = ? "; + sql.append("AND eom.STATUS = ? "); } - sql += "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + sql.append("ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"); int index = 1; - try (PreparedStatement stmt = conn.prepareStatement(sql)) { + try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) { stmt.setInt(index++, tenantId); if (activityPaginationRequest.getDeviceType() != null) { stmt.setString(index++, activityPaginationRequest.getDeviceType()); } - if (activityPaginationRequest.getDeviceId() != null) { - stmt.setString(index++, activityPaginationRequest.getDeviceId()); + if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) { + for (String deviceId : activityPaginationRequest.getDeviceIds()) { + stmt.setString(index++, deviceId); + } } if (activityPaginationRequest.getOperationCode() != null) { stmt.setString(index++, activityPaginationRequest.getOperationCode()); @@ -1844,8 +1854,10 @@ public class GenericOperationDAOImpl implements OperationDAO { if (activityPaginationRequest.getDeviceType() != null) { stmt.setString(index++, activityPaginationRequest.getDeviceType()); } - if (activityPaginationRequest.getDeviceId() != null) { - stmt.setString(index++, activityPaginationRequest.getDeviceId()); + if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) { + for (String deviceId : activityPaginationRequest.getDeviceIds()) { + stmt.setString(index++, deviceId); + } } if (activityPaginationRequest.getOperationCode() != null) { stmt.setString(index++, activityPaginationRequest.getOperationCode()); @@ -1891,43 +1903,49 @@ public class GenericOperationDAOImpl implements OperationDAO { boolean isTimeDurationFilteringProvided = false; Connection conn = OperationManagementDAOFactory.getConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT count(DISTINCT OPERATION_ID) AS ACTIVITY_COUNT FROM DM_ENROLMENT_OP_MAPPING " + - "WHERE TENANT_ID = ? "; + StringBuilder sql = new StringBuilder("SELECT count(DISTINCT OPERATION_ID) AS ACTIVITY_COUNT " + + "FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ? "); if (activityPaginationRequest.getDeviceType() != null) { - sql += "AND DEVICE_TYPE = ? "; + sql.append("AND DEVICE_TYPE = ? "); } - if (activityPaginationRequest.getDeviceId() != null) { - sql += "AND DEVICE_IDENTIFICATION = ? "; + if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) { + sql.append("AND DEVICE_IDENTIFICATION IN ("); + for (int i = 0; i < activityPaginationRequest.getDeviceIds().size() - 1; i++) { + sql.append("?, "); + } + sql.append("?) "); } if (activityPaginationRequest.getOperationCode() != null) { - sql += "AND OPERATION_CODE = ? "; + sql.append("AND OPERATION_CODE = ? "); } if (activityPaginationRequest.getInitiatedBy() != null) { - sql += "AND INITIATED_BY = ? "; + sql.append("AND INITIATED_BY = ? "); } if (activityPaginationRequest.getSince() != 0) { - sql += "AND UPDATED_TIMESTAMP > ? "; + sql.append("AND UPDATED_TIMESTAMP > ? "); } if (activityPaginationRequest.getType() != null) { - sql += "AND TYPE = ? "; + sql.append("AND TYPE = ? "); } if (activityPaginationRequest.getStatus() != null) { - sql += "AND STATUS = ? "; + sql.append("AND STATUS = ? "); } if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { isTimeDurationFilteringProvided = true; - sql += "AND CREATED_TIMESTAMP BETWEEN ? AND ? "; + sql.append("AND CREATED_TIMESTAMP BETWEEN ? AND ? "); } int index = 1; - try (PreparedStatement stmt = conn.prepareStatement(sql)) { + try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) { stmt.setInt(index++, tenantId); if (activityPaginationRequest.getDeviceType() != null) { stmt.setString(index++, activityPaginationRequest.getDeviceType()); } - if (activityPaginationRequest.getDeviceId() != null) { - stmt.setString(index++, activityPaginationRequest.getDeviceId()); + if (activityPaginationRequest.getDeviceIds() != null && !activityPaginationRequest.getDeviceIds().isEmpty()) { + for (String deviceId : activityPaginationRequest.getDeviceIds()) { + stmt.setString(index++, deviceId); + } } if (activityPaginationRequest.getOperationCode() != null) { stmt.setString(index++, activityPaginationRequest.getOperationCode());