From 10ea514be27943398e002f122761ad65d36b241a Mon Sep 17 00:00:00 2001 From: nipuni Date: Sat, 29 Jun 2024 00:06:55 +0530 Subject: [PATCH] Add operation log of a subscription --- .../mgt/common/dto/DeviceOperationDTO.java | 13 +++- .../common/services/SubscriptionManager.java | 3 +- .../mgt/core/dao/SubscriptionDAO.java | 3 +- .../GenericSubscriptionDAOImpl.java | 24 ++++--- .../core/impl/SubscriptionManagerImpl.java | 5 +- .../device/mgt/core/dto/OperationDTO.java | 66 +++---------------- .../mgt/core/dto/OperationResponseDTO.java | 43 ++++++++++++ .../mgt/dao/impl/GenericOperationDAOImpl.java | 50 +++++++++----- 8 files changed, 121 insertions(+), 86 deletions(-) create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/OperationResponseDTO.java diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceOperationDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceOperationDTO.java index bca7e8b002..75699431a2 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceOperationDTO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceOperationDTO.java @@ -18,7 +18,10 @@ package io.entgra.device.mgt.core.application.mgt.common.dto; +import io.entgra.device.mgt.core.device.mgt.core.dto.OperationResponseDTO; + import java.sql.Timestamp; +import java.util.List; public class DeviceOperationDTO { private int deviceId; @@ -31,7 +34,7 @@ public class DeviceOperationDTO { private String operationCode; private Object operationDetails; private Object operationProperties; - + private List operationResponses; public int getDeviceId() { return deviceId; @@ -112,4 +115,12 @@ public class DeviceOperationDTO { public void setOperationProperties(Object operationProperties) { this.operationProperties = operationProperties; } + + public List getOperationResponses() { + return operationResponses; + } + + public void setOperationResponses(List operationResponses) { + this.operationResponses = operationResponses; + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java index 500c324cd0..6c537c05d0 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java @@ -292,12 +292,13 @@ public interface SubscriptionManager { * This method is responsible for retrieving device subscription details related to the given UUID. * * @param uuid the UUID of the application release. + * @param deviceId the deviceId of the device that need to get operation details. * @param offset the offset for the data set * @param limit the limit for the data set * @return {@link DeviceOperationDTO} which contains the details of device subscriptions. * @throws SubscriptionManagementException if there is an error while fetching the details. */ - List getDeviceSubscriptionsOperationsByUUID(String uuid, int offset, int limit) + List getSubscriptionOperationsByUUIDAndDeviceID(String uuid, int deviceId, int offset, int limit) throws ApplicationManagementException; /** diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index cb32453082..88465a4533 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -376,13 +376,14 @@ public interface SubscriptionDAO { * This method is used to get the details of device subscriptions related to a UUID. * * @param appReleaseId the appReleaseId of the application release. + * @param deviceId the deviceId of the device that need to get operation details. * @param tenantId id of the current tenant. * @param offset the offset for the data set * @param limit the limit for the data set * @return {@link DeviceOperationDTO} which contains the details of device subscriptions. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ - List getDeviceSubscriptionsOperationsByAppReleaseID(int appReleaseId, int tenantId, int offset, int limit) + List getSubscriptionOperationsByAppReleaseIDAndDeviceID(int appReleaseId, int deviceId, int tenantId, int offset, int limit) throws ApplicationManagementDAOException; /** diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index ad10bde91d..22d86db3c3 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1856,10 +1856,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getDeviceSubscriptionsOperationsByAppReleaseID( - int appReleaseId, int tenantId, int offset, int limit) throws ApplicationManagementDAOException { + public List getSubscriptionOperationsByAppReleaseIDAndDeviceID( + int appReleaseId, int deviceId, int tenantId, int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get device subscriptions related to the given AppReleaseID."); + log.debug("Request received in DAO Layer to get device subscriptions related to the given AppReleaseID and DeviceID."); } try { Connection conn = this.getDBConnection(); @@ -1872,14 +1872,18 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc " ads.SUBSCRIBED_TIMESTAMP AS ACTION_TRIGGERED_AT, " + " ads.AP_APP_RELEASE_ID " + "FROM AP_APP_SUB_OP_MAPPING aasom " + - "JOIN AP_DEVICE_SUBSCRIPTION ads ON aasom.AP_DEVICE_SUBSCRIPTION_ID = ads.ID " + - "WHERE ads.AP_APP_RELEASE_ID = ? AND ads.TENANT_ID = ? " + + "JOIN AP_DEVICE_SUBSCRIPTION ads " + + "ON aasom.AP_DEVICE_SUBSCRIPTION_ID = ads.ID " + + "WHERE ads.AP_APP_RELEASE_ID = ? " + + "AND ads.DM_DEVICE_ID = ? " + + "AND ads.TENANT_ID = ? " + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, appReleaseId); - ps.setInt(2, tenantId); - ps.setInt(3, limit); - ps.setInt(4, offset); + ps.setInt(2, deviceId); + ps.setInt(3, tenantId); + ps.setInt(4, limit); + ps.setInt(5, offset); try (ResultSet rs = ps.executeQuery()) { DeviceOperationDTO deviceSubscription; while (rs.next()) { @@ -1897,11 +1901,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } return deviceSubscriptions; } catch (DBConnectionException e) { - String msg = "Error occurred while obtaining the DB connection to get device subscriptions for the given UUID."; + String msg = "Error occurred while obtaining the DB connection to get device subscriptions for the given AppReleaseID and DeviceID."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - String msg = "SQL Error occurred while getting device subscriptions for the given UUID."; + String msg = "SQL Error occurred while getting device subscriptions for the given AppReleaseID and DeviceID."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 48128d02d7..a1f81acd7a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -2577,7 +2577,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public List getDeviceSubscriptionsOperationsByUUID(String uuid, int offset, int limit) + public List getSubscriptionOperationsByUUIDAndDeviceID(String uuid, int deviceId, int offset, int limit) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (uuid == null || uuid.isEmpty()) { @@ -2596,7 +2596,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); List deviceSubscriptions = - subscriptionDAO.getDeviceSubscriptionsOperationsByAppReleaseID(appReleaseId, tenantId, offset, limit); + subscriptionDAO.getSubscriptionOperationsByAppReleaseIDAndDeviceID(appReleaseId, deviceId, tenantId, offset, limit); for (DeviceOperationDTO deviceSubscription : deviceSubscriptions) { Integer operationId = deviceSubscription.getOperationId(); if (operationId != null) { @@ -2605,6 +2605,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { deviceSubscription.setOperationCode(operationDetails.getOperationCode()); deviceSubscription.setOperationDetails(operationDetails.getOperationDetails()); deviceSubscription.setOperationProperties(operationDetails.getOperationProperties()); + deviceSubscription.setOperationResponses(operationDetails.getOperationResponses()); } } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/OperationDTO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/OperationDTO.java index b6ac94fc3c..05530e231b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/OperationDTO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/OperationDTO.java @@ -19,47 +19,17 @@ package io.entgra.device.mgt.core.device.mgt.core.dto; import org.json.JSONObject; - -import java.sql.Timestamp; +import java.util.List; public class OperationDTO { - private int deviceId; - private String uuid; - private String status; private int operationId; - private String actionTriggeredFrom; - private Timestamp actionTriggeredAt; - private int appReleaseId; private String operationCode; private JSONObject operationDetails; private JSONObject operationProperties; + private List operationResponses; // Getters and Setters - public int getDeviceId() { - return deviceId; - } - - public void setDeviceId(int deviceId) { - this.deviceId = deviceId; - } - - public String getUuid() { - return uuid; - } - - public void setUuid(String uuid) { - this.uuid = uuid; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - public int getOperationId() { return operationId; } @@ -68,30 +38,6 @@ public class OperationDTO { this.operationId = operationId; } - public String getActionTriggeredFrom() { - return actionTriggeredFrom; - } - - public void setActionTriggeredFrom(String actionTriggeredFrom) { - this.actionTriggeredFrom = actionTriggeredFrom; - } - - public Timestamp getActionTriggeredAt() { - return actionTriggeredAt; - } - - public void setActionTriggeredAt(Timestamp actionTriggeredAt) { - this.actionTriggeredAt = actionTriggeredAt; - } - - public int getAppReleaseId() { - return appReleaseId; - } - - public void setAppReleaseId(int appReleaseId) { - this.appReleaseId = appReleaseId; - } - public String getOperationCode() { return operationCode; } @@ -115,4 +61,12 @@ public class OperationDTO { public void setOperationProperties(JSONObject operationProperties) { this.operationProperties = operationProperties; } + + public List getOperationResponses() { + return operationResponses; + } + + public void setOperationResponses(List operationResponses) { + this.operationResponses = operationResponses; + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/OperationResponseDTO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/OperationResponseDTO.java new file mode 100644 index 0000000000..03ec090760 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/OperationResponseDTO.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 - 2024, 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 io.entgra.device.mgt.core.device.mgt.core.dto; + +import java.sql.Timestamp; + +public class OperationResponseDTO { + private String operationResponse; + private Timestamp responseTimeStamp; + + public String getOperationResponse() { + return operationResponse; + } + + public void setOperationResponse(String operationResponse) { + this.operationResponse = operationResponse; + } + + public Timestamp getResponseTimeStamp() { + return responseTimeStamp; + } + + public void setResponseTimeStamp(Timestamp responseTimeStamp) { + this.responseTimeStamp = responseTimeStamp; + } +} + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index b220cb8b77..0387dc9e5f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -28,6 +28,7 @@ import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.OperationRespon import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil; import io.entgra.device.mgt.core.device.mgt.core.dto.OperationDTO; +import io.entgra.device.mgt.core.device.mgt.core.dto.OperationResponseDTO; import io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.Operation; import io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.OperationResponseMeta; import io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.ProfileOperation; @@ -2783,9 +2784,17 @@ public class GenericOperationDAOImpl implements OperationDAO { throws OperationManagementDAOException { OperationDTO operationDetails = new OperationDTO(); - String sql = "SELECT ID, OPERATION_CODE, OPERATION_DETAILS, OPERATION_PROPERTIES " + - "FROM DM_OPERATION " + - "WHERE ID = ? AND TENANT_ID = ?"; + String sql = "SELECT o.ID, " + + "o.OPERATION_CODE, " + + "o.OPERATION_DETAILS, " + + "o.OPERATION_PROPERTIES, " + + "r.OPERATION_RESPONSE, " + + "r.RECEIVED_TIMESTAMP " + + "FROM DM_OPERATION o " + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE r " + + "ON o.ID = r.OPERATION_ID " + + "WHERE o.ID = ? " + + "AND o.TENANT_ID = ?"; try (Connection conn = OperationManagementDAOFactory.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { @@ -2793,22 +2802,33 @@ public class GenericOperationDAOImpl implements OperationDAO { stmt.setInt(2, tenantId); try (ResultSet rs = stmt.executeQuery()) { - if (rs.next()) { - operationDetails.setOperationId(rs.getInt("ID")); - operationDetails.setOperationCode(rs.getString("OPERATION_CODE")); - - Blob detailsBlob = rs.getBlob("OPERATION_DETAILS"); - if (detailsBlob != null) { - JSONObject operationDetailsJson = OperationDAOUtil.convertBlobToJsonObject(detailsBlob); - operationDetails.setOperationDetails(operationDetailsJson); + List responses = new ArrayList<>(); + while (rs.next()) { + if (operationDetails.getOperationId() == 0) { + operationDetails.setOperationId(rs.getInt("ID")); + operationDetails.setOperationCode(rs.getString("OPERATION_CODE")); + Blob detailsBlob = rs.getBlob("OPERATION_DETAILS"); + if (detailsBlob != null) { + JSONObject operationDetailsJson = OperationDAOUtil.convertBlobToJsonObject(detailsBlob); + operationDetails.setOperationDetails(operationDetailsJson); + } + Blob propertiesBlob = rs.getBlob("OPERATION_PROPERTIES"); + if (propertiesBlob != null) { + JSONObject operationPropertiesJson = OperationDAOUtil.convertBlobToJsonObject(propertiesBlob); + operationDetails.setOperationProperties(operationPropertiesJson); + } } - Blob propertiesBlob = rs.getBlob("OPERATION_PROPERTIES"); - if (propertiesBlob != null) { - JSONObject operationPropertiesJson = OperationDAOUtil.convertBlobToJsonObject(propertiesBlob); - operationDetails.setOperationProperties(operationPropertiesJson); + String response = rs.getString("OPERATION_RESPONSE"); + Timestamp responseTimestamp = rs.getTimestamp("RECEIVED_TIMESTAMP"); + if (response != null && responseTimestamp != null) { + OperationResponseDTO operationResponse = new OperationResponseDTO(); + operationResponse.setOperationResponse(response); + operationResponse.setResponseTimeStamp(responseTimestamp); + responses.add(operationResponse); } } + operationDetails.setOperationResponses(responses); } } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while retrieving operation details for operation ID: "