Add operation log of a subscription

Purpose:
	https://roadmap.entgra.net/issues/11166
Co-authored-by: Nipuni Kavindya <nipuni@entgra.io>
Co-committed-by: Nipuni Kavindya <nipuni@entgra.io>
appm_improvement
Nipuni Kavindya 5 months ago committed by Lasantha Dharmakeerthi
parent 0aea6a9fa1
commit 829e1ed8a4

@ -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<OperationResponseDTO> operationResponses;
public int getDeviceId() {
return deviceId;
@ -112,4 +115,12 @@ public class DeviceOperationDTO {
public void setOperationProperties(Object operationProperties) {
this.operationProperties = operationProperties;
}
public List<OperationResponseDTO> getOperationResponses() {
return operationResponses;
}
public void setOperationResponses(List<OperationResponseDTO> operationResponses) {
this.operationResponses = operationResponses;
}
}

@ -291,13 +291,14 @@ public interface SubscriptionManager {
/**
* This method is responsible for retrieving device subscription details related to the given UUID.
*
* @param deviceId the deviceId of the device that need to get operation details.
* @param uuid the UUID of the application release.
* @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<DeviceOperationDTO> getDeviceSubscriptionsOperationsByUUID(String uuid, int offset, int limit)
List<DeviceOperationDTO> getSubscriptionOperationsByUUIDAndDeviceID(int deviceId, String uuid, int offset, int limit)
throws ApplicationManagementException;
/**

@ -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<DeviceOperationDTO> getDeviceSubscriptionsOperationsByAppReleaseID(int appReleaseId, int tenantId, int offset, int limit)
List<DeviceOperationDTO> getSubscriptionOperationsByAppReleaseIDAndDeviceID(int appReleaseId, int deviceId, int tenantId, int offset, int limit)
throws ApplicationManagementDAOException;
/**

@ -1856,10 +1856,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
@Override
public List<DeviceOperationDTO> getDeviceSubscriptionsOperationsByAppReleaseID(
int appReleaseId, int tenantId, int offset, int limit) throws ApplicationManagementDAOException {
public List<DeviceOperationDTO> 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);
}

@ -2582,7 +2582,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public List<DeviceOperationDTO> getDeviceSubscriptionsOperationsByUUID(String uuid, int offset, int limit)
public List<DeviceOperationDTO> getSubscriptionOperationsByUUIDAndDeviceID(int deviceId, String uuid, int offset, int limit)
throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
if (uuid == null || uuid.isEmpty()) {
@ -2601,7 +2601,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
List<DeviceOperationDTO> 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) {
@ -2610,6 +2610,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
deviceSubscription.setOperationCode(operationDetails.getOperationCode());
deviceSubscription.setOperationDetails(operationDetails.getOperationDetails());
deviceSubscription.setOperationProperties(operationDetails.getOperationProperties());
deviceSubscription.setOperationResponses(operationDetails.getOperationResponses());
}
}
}

@ -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<OperationResponseDTO> 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<OperationResponseDTO> getOperationResponses() {
return operationResponses;
}
public void setOperationResponses(List<OperationResponseDTO> operationResponses) {
this.operationResponses = operationResponses;
}
}

@ -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;
}
}

@ -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,36 +2784,58 @@ 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)) {
try {
Connection conn = OperationManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, operationId);
stmt.setInt(2, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
List<OperationResponseDTO> 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);
}
}
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: "
+ operationId, e);
String msg = "Error occurred while retrieving operation details for operation ID: " + operationId;
log.error(msg, e);
throw new OperationManagementDAOException(msg, e);
}
return operationDetails;

Loading…
Cancel
Save