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; 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.sql.Timestamp;
import java.util.List;
public class DeviceOperationDTO { public class DeviceOperationDTO {
private int deviceId; private int deviceId;
@ -31,7 +34,7 @@ public class DeviceOperationDTO {
private String operationCode; private String operationCode;
private Object operationDetails; private Object operationDetails;
private Object operationProperties; private Object operationProperties;
private List<OperationResponseDTO> operationResponses;
public int getDeviceId() { public int getDeviceId() {
return deviceId; return deviceId;
@ -112,4 +115,12 @@ public class DeviceOperationDTO {
public void setOperationProperties(Object operationProperties) { public void setOperationProperties(Object operationProperties) {
this.operationProperties = 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. * 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 uuid the UUID of the application release.
* @param offset the offset for the data set * @param offset the offset for the data set
* @param limit the limit for the data set * @param limit the limit for the data set
* @return {@link DeviceOperationDTO} which contains the details of device subscriptions. * @return {@link DeviceOperationDTO} which contains the details of device subscriptions.
* @throws SubscriptionManagementException if there is an error while fetching the details. * @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; throws ApplicationManagementException;
/** /**

@ -376,13 +376,14 @@ public interface SubscriptionDAO {
* This method is used to get the details of device subscriptions related to a UUID. * This method is used to get the details of device subscriptions related to a UUID.
* *
* @param appReleaseId the appReleaseId of the application release. * @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 tenantId id of the current tenant.
* @param offset the offset for the data set * @param offset the offset for the data set
* @param limit the limit for the data set * @param limit the limit for the data set
* @return {@link DeviceOperationDTO} which contains the details of device subscriptions. * @return {@link DeviceOperationDTO} which contains the details of device subscriptions.
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. * @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; throws ApplicationManagementDAOException;
/** /**

@ -1856,10 +1856,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
@Override @Override
public List<DeviceOperationDTO> getDeviceSubscriptionsOperationsByAppReleaseID( public List<DeviceOperationDTO> getSubscriptionOperationsByAppReleaseIDAndDeviceID(
int appReleaseId, int tenantId, int offset, int limit) throws ApplicationManagementDAOException { int appReleaseId, int deviceId, int tenantId, int offset, int limit) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { 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 { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
@ -1872,14 +1872,18 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
" ads.SUBSCRIBED_TIMESTAMP AS ACTION_TRIGGERED_AT, " + " ads.SUBSCRIBED_TIMESTAMP AS ACTION_TRIGGERED_AT, " +
" ads.AP_APP_RELEASE_ID " + " ads.AP_APP_RELEASE_ID " +
"FROM AP_APP_SUB_OP_MAPPING aasom " + "FROM AP_APP_SUB_OP_MAPPING aasom " +
"JOIN AP_DEVICE_SUBSCRIPTION ads ON aasom.AP_DEVICE_SUBSCRIPTION_ID = ads.ID " + "JOIN AP_DEVICE_SUBSCRIPTION ads " +
"WHERE ads.AP_APP_RELEASE_ID = ? AND ads.TENANT_ID = ? " + "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 ?"; "LIMIT ? OFFSET ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) { try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, appReleaseId); ps.setInt(1, appReleaseId);
ps.setInt(2, tenantId); ps.setInt(2, deviceId);
ps.setInt(3, limit); ps.setInt(3, tenantId);
ps.setInt(4, offset); ps.setInt(4, limit);
ps.setInt(5, offset);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
DeviceOperationDTO deviceSubscription; DeviceOperationDTO deviceSubscription;
while (rs.next()) { while (rs.next()) {
@ -1897,11 +1901,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
return deviceSubscriptions; return deviceSubscriptions;
} catch (DBConnectionException e) { } 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); log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException 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); log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} }

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

@ -19,47 +19,17 @@
package io.entgra.device.mgt.core.device.mgt.core.dto; package io.entgra.device.mgt.core.device.mgt.core.dto;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.List;
import java.sql.Timestamp;
public class OperationDTO { public class OperationDTO {
private int deviceId;
private String uuid;
private String status;
private int operationId; private int operationId;
private String actionTriggeredFrom;
private Timestamp actionTriggeredAt;
private int appReleaseId;
private String operationCode; private String operationCode;
private JSONObject operationDetails; private JSONObject operationDetails;
private JSONObject operationProperties; private JSONObject operationProperties;
private List<OperationResponseDTO> operationResponses;
// Getters and Setters // 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() { public int getOperationId() {
return operationId; return operationId;
} }
@ -68,30 +38,6 @@ public class OperationDTO {
this.operationId = operationId; 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() { public String getOperationCode() {
return operationCode; return operationCode;
} }
@ -115,4 +61,12 @@ public class OperationDTO {
public void setOperationProperties(JSONObject operationProperties) { public void setOperationProperties(JSONObject operationProperties) {
this.operationProperties = 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.DeviceManagementConstants;
import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil; 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.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.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.OperationResponseMeta;
import io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.ProfileOperation; import io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.ProfileOperation;
@ -2783,36 +2784,58 @@ public class GenericOperationDAOImpl implements OperationDAO {
throws OperationManagementDAOException { throws OperationManagementDAOException {
OperationDTO operationDetails = new OperationDTO(); OperationDTO operationDetails = new OperationDTO();
String sql = "SELECT ID, OPERATION_CODE, OPERATION_DETAILS, OPERATION_PROPERTIES " + String sql = "SELECT o.ID, " +
"FROM DM_OPERATION " + "o.OPERATION_CODE, " +
"WHERE ID = ? AND TENANT_ID = ?"; "o.OPERATION_DETAILS, " +
"o.OPERATION_PROPERTIES, " +
try (Connection conn = OperationManagementDAOFactory.getConnection(); "r.OPERATION_RESPONSE, " +
PreparedStatement stmt = conn.prepareStatement(sql)) { "r.RECEIVED_TIMESTAMP " +
stmt.setInt(1, operationId); "FROM DM_OPERATION o " +
stmt.setInt(2, tenantId); "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE r " +
"ON o.ID = r.OPERATION_ID " +
"WHERE o.ID = ? " +
"AND o.TENANT_ID = ?";
try (ResultSet rs = stmt.executeQuery()) { try {
if (rs.next()) { Connection conn = OperationManagementDAOFactory.getConnection();
operationDetails.setOperationId(rs.getInt("ID")); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
operationDetails.setOperationCode(rs.getString("OPERATION_CODE")); stmt.setInt(1, operationId);
stmt.setInt(2, tenantId);
Blob detailsBlob = rs.getBlob("OPERATION_DETAILS"); try (ResultSet rs = stmt.executeQuery()) {
if (detailsBlob != null) { List<OperationResponseDTO> responses = new ArrayList<>();
JSONObject operationDetailsJson = OperationDAOUtil.convertBlobToJsonObject(detailsBlob); while (rs.next()) {
operationDetails.setOperationDetails(operationDetailsJson); 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"); String response = rs.getString("OPERATION_RESPONSE");
if (propertiesBlob != null) { Timestamp responseTimestamp = rs.getTimestamp("RECEIVED_TIMESTAMP");
JSONObject operationPropertiesJson = OperationDAOUtil.convertBlobToJsonObject(propertiesBlob); if (response != null && responseTimestamp != null) {
operationDetails.setOperationProperties(operationPropertiesJson); OperationResponseDTO operationResponse = new OperationResponseDTO();
operationResponse.setOperationResponse(response);
operationResponse.setResponseTimeStamp(responseTimestamp);
responses.add(operationResponse);
}
} }
operationDetails.setOperationResponses(responses);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while retrieving operation details for operation ID: " String msg = "Error occurred while retrieving operation details for operation ID: " + operationId;
+ operationId, e); log.error(msg, e);
throw new OperationManagementDAOException(msg, e);
} }
return operationDetails; return operationDetails;

Loading…
Cancel
Save