From 2ae8c126e88c001d5c6f5c7096d5f4ce50e14f14 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 15 Oct 2020 21:01:24 +0530 Subject: [PATCH 1/5] fixing issues with activity retrieval --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 429 ++++-------------- .../impl/operation/MySQLOperationDAOImpl.java | 341 +++----------- .../operation/OracleOperationDAOImpl.java | 230 ++-------- .../operation/SQLServerOperationDAOImpl.java | 223 ++------- .../mgt/dao/util/OperationDAOUtil.java | 117 ++++- 5 files changed, 375 insertions(+), 965 deletions(-) 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 37984c8709..caa2a86601 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 @@ -23,6 +23,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; @@ -375,6 +376,10 @@ public class GenericOperationDAOImpl implements OperationDAO { for (Map.Entry> deviceOpRes : deviceOpResponseMap.entrySet()) { for (ActivityStatus status : activityStatuses) { if (deviceOpRes.getKey().equalsIgnoreCase(status.getDeviceIdentifier().getId())) { + if(status.getResponses()==null){ + List responses = new ArrayList<>(); + status.setResponses(responses); + } status.getResponses().addAll(deviceOpRes.getValue()); } } @@ -942,349 +947,117 @@ public class GenericOperationDAOImpl implements OperationDAO { @Override public List getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String sql = "SELECT " + - " opr.ENROLMENT_ID, " + - " opr.CREATED_TIMESTAMP, " + - " opr.UPDATED_TIMESTAMP, " + - " opr.OPERATION_ID, " + - " opr.OPERATION_CODE, " + - " opr.OPERATION_TYPE, " + - " opr.STATUS, " + - " opr.DEVICE_ID, " + - " opr.DEVICE_IDENTIFICATION, " + - " opr.DEVICE_TYPE, " + - " ops.RECEIVED_TIMESTAMP, " + - " ops.ID AS OP_RES_ID, " + - " ops.OPERATION_RESPONSE , " + - " ops.IS_LARGE_RESPONSE , " + - " opr.INITIATED_BY " + - " FROM " + - " (SELECT " + - " opm.ID MAPPING_ID, " + - " opm.ENROLMENT_ID, " + - " opm.CREATED_TIMESTAMP, " + - " opm.UPDATED_TIMESTAMP, " + - " opm.OPERATION_ID, " + - " op.OPERATION_CODE, " + - " op.INITIATED_BY, " + - " op.TYPE AS OPERATION_TYPE, " + - " opm.STATUS, " + - " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME AS DEVICE_TYPE, " + - " de.TENANT_ID " + - " FROM" + - " DM_ENROLMENT_OP_MAPPING opm " + - " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + - " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + - " WHERE " + - " opm.UPDATED_TIMESTAMP > ? AND op.INITIATED_BY = ?" + - " AND de.TENANT_ID = ? " + - " ORDER BY opm.UPDATED_TIMESTAMP " + - " LIMIT ? OFFSET ?) opr " + - " LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + - " WHERE " + - " opr.UPDATED_TIMESTAMP > ? AND opr.INITIATED_BY = ?" + - " AND opr.TENANT_ID = ? "; - - stmt = conn.prepareStatement(sql); - - stmt.setLong(1, timestamp); - stmt.setString(2, user); - stmt.setInt(3, tenantId); - stmt.setInt(4, limit); - stmt.setInt(5, offset); - stmt.setLong(6, timestamp); - stmt.setString(7, user); - stmt.setInt(8, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - List largeResponseIDs = new ArrayList<>(); - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " opr.ID AS OP_RES_ID," + + " opr.RECEIVED_TIMESTAMP," + + " opr.OPERATION_RESPONSE," + + " opr.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING eom " + + "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 ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + " AND eom.INITIATED_BY = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + stmt.setString(5, user); + + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if (!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operation details from " + - "the database. " + e.getMessage(), e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database. "; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - - return activities; - } @Override - public List getActivitiesUpdatedAfter(long timestamp, int limit, - int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); - List largeResponseIDs = new ArrayList<>(); + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) + throws OperationManagementDAOException { try { Connection conn = OperationManagementDAOFactory.getConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + - " opr.ENROLMENT_ID, " + - " opr.CREATED_TIMESTAMP, " + - " opr.UPDATED_TIMESTAMP, " + - " opr.OPERATION_ID, " + - " opr.OPERATION_CODE, " + - " opr.OPERATION_TYPE, " + - " opr.STATUS, " + - " opr.DEVICE_ID, " + - " opr.DEVICE_IDENTIFICATION, " + - " opr.DEVICE_TYPE, " + - " ops.RECEIVED_TIMESTAMP, " + - " ops.IS_LARGE_RESPONSE, " + - " ops.ID AS OP_RES_ID, " + - " ops.OPERATION_RESPONSE " + - " FROM " + - " (SELECT " + - " opm.ID MAPPING_ID, " + - " opm.ENROLMENT_ID, " + - " opm.CREATED_TIMESTAMP, " + - " opm.UPDATED_TIMESTAMP, " + - " opm.OPERATION_ID, " + - " op.OPERATION_CODE, " + - " op.TYPE AS OPERATION_TYPE, " + - " opm.STATUS, " + - " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME AS DEVICE_TYPE, " + - " de.TENANT_ID " + - " FROM" + - " DM_ENROLMENT_OP_MAPPING opm " + - " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + - " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + - " WHERE " + - " opm.UPDATED_TIMESTAMP > ? " + - " AND de.TENANT_ID = ? " + - " ORDER BY opm.UPDATED_TIMESTAMP " + - " LIMIT ? OFFSET ?) opr " + - " LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + - " WHERE " + - " opr.UPDATED_TIMESTAMP > ? " + - " AND opr.TENANT_ID = ? "; - - stmt = conn.prepareStatement(sql); - - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setInt(3, limit); - stmt.setInt(4, offset); - stmt.setLong(5, timestamp); - stmt.setInt(6, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + String sql = "SELECT " + + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " ops.ID AS OP_RES_ID," + + " ops.RECEIVED_TIMESTAMP," + + " ops.OPERATION_RESPONSE," + + " ops.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING AS eom " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if (!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operation details from " + - "the database. " + e.getMessage(), e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg,e); + throw new OperationManagementDAOException(msg, e); } - return activities; } @Override @@ -1293,7 +1066,7 @@ public class GenericOperationDAOImpl implements OperationDAO { ResultSet rs = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING WHERE " + + String sql = "SELECT COUNT(DISTINCT(OPERATION_ID)) AS COUNT FROM DM_ENROLMENT_OP_MAPPING WHERE " + "UPDATED_TIMESTAMP > ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setLong(1, timestamp); @@ -1490,11 +1263,6 @@ public class GenericOperationDAOImpl implements OperationDAO { operation.setId(rs.getInt("ID")); operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); -// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { -// operation.setReceivedTimeStamp(""); -// } else { -// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); -// } if (rs.getLong("UPDATED_TIMESTAMP") == 0) { operation.setReceivedTimeStamp(""); } else { @@ -1692,11 +1460,6 @@ public class GenericOperationDAOImpl implements OperationDAO { operation.setId(rs.getInt("ID")); operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); -// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { -// operation.setReceivedTimeStamp(""); -// } else { -// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); -// } if (rs.getLong("UPDATED_TIMESTAMP") == 0) { operation.setReceivedTimeStamp(""); } else { 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/operation/MySQLOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java index d65f7a49d5..ec097133c5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java @@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; @@ -200,171 +201,69 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { } @Override - public List getActivitiesUpdatedAfter(long timestamp, int limit, - int offset) + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + - " eom.ENROLMENT_ID," + - " eom.CREATED_TIMESTAMP," + - " eom.UPDATED_TIMESTAMP," + - " eom.OPERATION_ID," + - " eom.OPERATION_CODE," + - " eom.INITIATED_BY," + - " eom.TYPE," + - " eom.STATUS," + - " eom.DEVICE_ID," + - " eom.DEVICE_IDENTIFICATION," + - " eom.DEVICE_TYPE," + - " opr.ID AS OP_RES_ID," + - " opr.RECEIVED_TIMESTAMP," + - " opr.OPERATION_RESPONSE," + - " opr.IS_LARGE_RESPONSE " + - "FROM " + - " DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " + - " LEFT JOIN " + - " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + - "WHERE " + - " eom.UPDATED_TIMESTAMP > ? " + - " AND eom.TENANT_ID = ? " + - "ORDER BY eom.UPDATED_TIMESTAMP " + - "LIMIT ? OFFSET ?"; - - stmt = conn.prepareStatement(sql); - - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setInt(3, limit); - stmt.setInt(4, offset); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - List largeResponseIDs = new ArrayList<>(); - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " opr.ID AS OP_RES_ID," + + " opr.RECEIVED_TIMESTAMP," + + " opr.OPERATION_RESPONSE," + + " opr.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " + + "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 ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if(!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operation details from " + - "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return activities; } @Override public List getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); - List largeResponseIDs = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + " eom.ENROLMENT_ID," + " eom.CREATED_TIMESTAMP," + @@ -383,138 +282,38 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { " opr.IS_LARGE_RESPONSE " + "FROM " + " DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " + - " LEFT JOIN " + + "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 ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + "WHERE " + " eom.UPDATED_TIMESTAMP > ? " + " AND eom.TENANT_ID = ? " + " AND eom.INITIATED_BY = ? " + - "ORDER BY eom.UPDATED_TIMESTAMP " + - "LIMIT ? OFFSET ?"; - - stmt = conn.prepareStatement(sql); - - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setString(3, user); - stmt.setInt(4, limit); - stmt.setInt(5, offset); - -// stmt.setLong(1, timestamp); -// stmt.setString(2, user); -// stmt.setInt(3, tenantId); -// stmt.setInt(4, limit); -// stmt.setInt(5, offset); -// stmt.setLong(6, timestamp); -// stmt.setString(7, user); -// stmt.setInt(8, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); - activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - activity.setInitiatedBy(rs.getString("INITIATED_BY")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp(new java.util.Date( - rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + stmt.setString(5, user); + + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if(!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operation details from " + - "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return activities; } - -} \ No newline at end of file +} 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/operation/OracleOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java index 45d656e194..37de111d8a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.java @@ -18,12 +18,13 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; -import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; @@ -32,7 +33,6 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; -import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -48,6 +48,8 @@ import java.util.Map; */ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { + private static final Log log = LogFactory.getLog(OracleOperationDAOImpl.class); + @Override public List getOperationsForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException { @@ -185,192 +187,56 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { @Override public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); - List largeResponseIDs = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - /*String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" - + "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" - + "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n" - + "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n" - + "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n" - + "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n" - + "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n" - + "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n" - + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n" - + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" - + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; - - if (timestamp == 0) { - sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - } else { - sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - } - */ - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + - " opr.ENROLMENT_ID, " + - " opr.CREATED_TIMESTAMP, " + - " opr.UPDATED_TIMESTAMP, " + - " opr.OPERATION_ID, " + - " opr.OPERATION_CODE, " + - " opr.OPERATION_TYPE, " + - " opr.STATUS, " + - " opr.DEVICE_ID, " + - " opr.DEVICE_IDENTIFICATION, " + - " opr.DEVICE_TYPE, " + - " ops.RECEIVED_TIMESTAMP, " + - " ops.ID OP_RES_ID, " + - " ops.IS_LARGE_RESPONSE, " + - " ops.OPERATION_RESPONSE " + - " FROM " + - " (SELECT " + - " opm.ID MAPPING_ID, " + - " opm.ENROLMENT_ID, " + - " opm.CREATED_TIMESTAMP, " + - " opm.UPDATED_TIMESTAMP, " + - " opm.OPERATION_ID, " + - " op.OPERATION_CODE, " + - " op.TYPE OPERATION_TYPE, " + - " opm.STATUS, " + - " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME DEVICE_TYPE, " + - " de.TENANT_ID " + - " FROM " + - " DM_ENROLMENT_OP_MAPPING opm " + - " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + - " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + - " WHERE " + - " opm.UPDATED_TIMESTAMP > ? " + - " AND de.TENANT_ID = ? " + - " ORDER BY opm.UPDATED_TIMESTAMP " + - " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) opr " + - " LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + - " WHERE " + - " opr.UPDATED_TIMESTAMP > ? " + - " AND opr.TENANT_ID = ? "; - - - stmt = conn.prepareStatement(sql); - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setInt(3, offset); - stmt.setInt(4, limit); - stmt.setLong(5, timestamp); - stmt.setInt(6, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp( - new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp( - new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp( - new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp( - new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); - - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " ops.ID AS OP_RES_ID," + + " ops.RECEIVED_TIMESTAMP," + + " ops.OPERATION_RESPONSE," + + " ops.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING AS eom " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if(!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException( - "Error occurred while getting the operation details from " + "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return activities; } -} \ No newline at end of file +} 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/operation/SQLServerOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java index 5c486bac29..a3520084f6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/SQLServerOperationDAOImpl.java @@ -41,6 +41,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; @@ -51,7 +52,6 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; -import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -67,7 +67,7 @@ import java.util.Map; */ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl { - private static final Log log = LogFactory.getLog(GenericOperationDAOImpl.class); + private static final Log log = LogFactory.getLog(SQLServerOperationDAOImpl.class); @Override public List getOperationsForDevice(int enrolmentId, PaginationRequest request) @@ -162,192 +162,59 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl { } @Override - public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - List activities = new ArrayList<>(); - List largeResponseIDs = new ArrayList<>(); + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) + throws OperationManagementDAOException { try { Connection conn = OperationManagementDAOFactory.getConnection(); - /*String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" - + "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" - + "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n" - + "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n" - + "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n" - + "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n" - + "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n" - + "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n" - + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n" - + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" - + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; - - if (timestamp == 0) { - sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - } else { - sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - }*/ - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - String sql = "SELECT " + - " opr.ENROLMENT_ID, " + - " opr.CREATED_TIMESTAMP, " + - " opr.UPDATED_TIMESTAMP, " + - " opr.OPERATION_ID, " + - " opr.OPERATION_CODE, " + - " opr.OPERATION_TYPE, " + - " opr.STATUS, " + - " opr.DEVICE_ID, " + - " opr.DEVICE_IDENTIFICATION, " + - " opr.DEVICE_TYPE, " + - " ops.RECEIVED_TIMESTAMP, " + - " ops.ID OP_RES_ID, " + - " ops.IS_LARGE_RESPONSE, " + - " ops.OPERATION_RESPONSE " + - " FROM " + - " (SELECT " + - " opm.ID MAPPING_ID, " + - " opm.ENROLMENT_ID, " + - " opm.CREATED_TIMESTAMP, " + - " opm.UPDATED_TIMESTAMP, " + - " opm.OPERATION_ID, " + - " op.OPERATION_CODE, " + - " op.TYPE OPERATION_TYPE, " + - " opm.STATUS, " + - " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME DEVICE_TYPE, " + - " de.TENANT_ID " + - " FROM" + - " DM_ENROLMENT_OP_MAPPING opm " + - " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + - " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + - " WHERE " + - " opm.UPDATED_TIMESTAMP > ? " + - " AND de.TENANT_ID = ? " + - " ORDER BY opm.UPDATED_TIMESTAMP " + - " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) opr " + - " LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + - " WHERE" + - " opr.UPDATED_TIMESTAMP > ? " + - " AND opr.TENANT_ID = ? "; - - stmt = conn.prepareStatement(sql); - stmt.setLong(1, timestamp); - stmt.setInt(2, tenantId); - stmt.setInt(3, offset); - stmt.setInt(4, limit); - stmt.setLong(5, timestamp); - stmt.setInt(6, tenantId); - - rs = stmt.executeQuery(); - - int operationId = 0; - int enrolmentId = 0; - int responseId = 0; - Activity activity = null; - ActivityStatus activityStatus = null; - while (rs.next()) { - - if (operationId != rs.getInt("OPERATION_ID")) { - activity = new Activity(); - activities.add(activity); - List statusList = new ArrayList<>(); - activityStatus = new ActivityStatus(); - - operationId = rs.getInt("OPERATION_ID"); - enrolmentId = rs.getInt("ENROLMENT_ID"); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp( - new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp( - new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - statusList.add(activityStatus); - activity.setActivityStatus(statusList); - activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); - - } - - if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { - activityStatus = new ActivityStatus(); - - activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); - activity.setCreatedTimeStamp( - new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); - activity.setCode(rs.getString("OPERATION_CODE")); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - activityStatus.setDeviceIdentifier(deviceIdentifier); - - activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); - - List operationResponses = new ArrayList<>(); - if (rs.getInt("UPDATED_TIMESTAMP") != 0) { - activityStatus.setUpdatedTimestamp( - new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); - } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); - } - } - activityStatus.setResponses(operationResponses); - activity.getActivityStatus().add(activityStatus); + " eom.ENROLMENT_ID," + + " eom.CREATED_TIMESTAMP," + + " eom.UPDATED_TIMESTAMP," + + " eom.OPERATION_ID," + + " eom.OPERATION_CODE," + + " eom.INITIATED_BY," + + " eom.TYPE," + + " eom.STATUS," + + " eom.DEVICE_ID," + + " eom.DEVICE_IDENTIFICATION," + + " eom.DEVICE_TYPE," + + " ops.ID AS OP_RES_ID," + + " ops.RECEIVED_TIMESTAMP," + + " ops.OPERATION_RESPONSE," + + " ops.IS_LARGE_RESPONSE " + + "FROM " + + " DM_ENROLMENT_OP_MAPPING AS eom " + + "INNER JOIN " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " + + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + + "LEFT JOIN " + + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " + + "WHERE " + + " eom.UPDATED_TIMESTAMP > ? " + + " AND eom.TENANT_ID = ? " + + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; - enrolmentId = rs.getInt("ENROLMENT_ID"); - } - - if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { - responseId = rs.getInt("OP_RES_ID"); - if (rs.getBoolean("IS_LARGE_RESPONSE")) { - largeResponseIDs.add(responseId); - } else { - activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); - } + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, offset); + stmt.setInt(2, limit); + stmt.setLong(3, timestamp); + stmt.setInt(4, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); + List largeResponseIDs = activityHolder.getLargeResponseIDs(); + List activities = activityHolder.getActivityList(); + if (!largeResponseIDs.isEmpty()) { + populateLargeOperationResponses(activities, largeResponseIDs); } + return activities; } } - if(!largeResponseIDs.isEmpty()) { - populateLargeOperationResponses(activities, largeResponseIDs); - } } catch (SQLException e) { - throw new OperationManagementDAOException( - "Error occurred while getting the operation details from " + "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while getting the operation details from the database."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return activities; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java index 65fe7b99f3..c58b75e745 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/util/OperationDAOUtil.java @@ -20,6 +20,11 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityMapper; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation; @@ -27,12 +32,17 @@ import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; public class OperationDAOUtil { private static final Log log = LogFactory.getLog(OperationDAOUtil.class); @@ -178,8 +188,113 @@ public class OperationDAOUtil { operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId); } - public static String getActivityId(int operationId) { return DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId; } + + public static ActivityHolder getActivityHolder(ResultSet rs) throws OperationManagementDAOException { + Map activityInfo = new TreeMap<>(); + List largeResponseIDs = new ArrayList<>(); + List activities = new ArrayList<>(); + try { + while (rs.next()) { + ActivityMapper activityMapper = activityInfo.get(rs.getInt("OPERATION_ID")); + if (activityMapper != null) { + ActivityStatus activityStatus = activityMapper.getActivityStatusMap() + .get(rs.getInt("ENROLMENT_ID")); + if (activityStatus == null) { + + activityStatus = new ActivityStatus(); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); + + activityStatus.setDeviceIdentifier(deviceIdentifier); + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp( + new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + } + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + if (rs.getBoolean("IS_LARGE_RESPONSE")) { + largeResponseIDs.add(rs.getInt("OP_RES_ID")); + } else { + List operationResponses = new ArrayList<>(); + operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); + activityStatus.setResponses(operationResponses); + } + } + activityMapper.getActivityStatusMap().put(rs.getInt("ENROLMENT_ID"), activityStatus); + } else { + if (rs.getInt("OP_RES_ID") != 0 && rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + if (rs.getBoolean("IS_LARGE_RESPONSE")) { + largeResponseIDs.add(rs.getInt("OP_RES_ID")); + } else { + activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); + } + } + } + } else { + Activity activity = new Activity(); + List statusList = new ArrayList<>(); + ActivityStatus activityStatus = new ActivityStatus(); + + activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); + activity.setCreatedTimeStamp( + new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); + activity.setCode(rs.getString("OPERATION_CODE")); + activity.setInitiatedBy(rs.getString("INITIATED_BY")); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); + activityStatus.setDeviceIdentifier(deviceIdentifier); + + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp( + new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + + } + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + if (rs.getBoolean("IS_LARGE_RESPONSE")) { + largeResponseIDs.add(rs.getInt("OP_RES_ID")); + } else { + operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); + } + } + activityStatus.setResponses(operationResponses); + statusList.add(activityStatus); + activity.setActivityStatus(statusList); + activity.setActivityId(getActivityId(rs.getInt("OPERATION_ID"))); + + ActivityMapper newActivityMapper = new ActivityMapper(); + Map activityStatusMap = new TreeMap<>(); + activityStatusMap.put(rs.getInt("ENROLMENT_ID"), activityStatus); + + newActivityMapper.setActivity(activity); + newActivityMapper.setActivityStatusMap(activityStatusMap); + activityInfo.put(rs.getInt("OPERATION_ID"), newActivityMapper); + } + } + + activityInfo.values().forEach(holder -> { + List activityStatuses = new ArrayList<>(holder.getActivityStatusMap().values()); + holder.getActivity().setActivityStatus(activityStatuses); + activities.add(holder.getActivity()); + }); + + ActivityHolder activityHolder = new ActivityHolder(); + activityHolder.setActivityList(activities); + activityHolder.setLargeResponseIDs(largeResponseIDs); + return activityHolder; + } catch (SQLException e) { + String msg = "Error occurred while getting activity data from the result set."; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); + } + } } From 22eba43aa9fa81d858063c5f9af51b2cd8796d43 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 15 Oct 2020 21:05:33 +0530 Subject: [PATCH 2/5] including missing files --- .../common/device/details/LocationBean.java | 89 +++++++++++++++++++ .../device/details/LocationEventBean.java | 69 ++++++++++++++ .../common/operation/mgt/ActivityHolder.java | 42 +++++++++ .../common/operation/mgt/ActivityMapper.java | 42 +++++++++ 4 files changed, 242 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityHolder.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityMapper.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java new file mode 100644 index 0000000000..261d387e03 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://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 org.wso2.carbon.device.mgt.common.device.details; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.List; + +@ApiModel( + value = "LocationBean", + description = "This class carries all information related IOS Device location." +) +public class LocationBean { + + @ApiModelProperty( + name = "latitude", + value = "Latitude of the IOS device Location.", + required = true + ) + private float latitude; + @ApiModelProperty( + name = "longitude", + value = "Longitude of the IOS device Location.", + required = true + ) + private float longitude; + @ApiModelProperty( + name = "operationId", + value = "Specific Id of the Location operation.", + required = true + ) + private String operationId; + + @ApiModelProperty( + name = "locationEvents", + value = "If this is a device initiated location publishing." + ) + private List locations; + + public List getLocationEvents() { + return locations; + } + + public void setLocationEvents(List locationEvents) { + this.locations = locationEvents; + } + + public String getOperationId() { + return operationId; + } + + public void setOperationId(String operationId) { + this.operationId = operationId; + } + + + public float getLatitude() { + return latitude; + } + + public void setLatitude(float latitude) { + this.latitude = latitude; + } + + public float getLongitude() { + return longitude; + } + + public void setLongitude(float longitude) { + this.longitude = longitude; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java new file mode 100644 index 0000000000..6e1ef6c976 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://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 org.wso2.carbon.device.mgt.common.device.details; + +import io.swagger.annotations.ApiModelProperty; + +public class LocationEventBean { + + @ApiModelProperty( + name = "latitude", + value = "Latitude of the IOS device Location.", + required = true + ) + private String latitude; + + @ApiModelProperty( + name = "longitude", + value = "Longitude of the IOS device Location.", + required = true + ) + private String longitude; + + @ApiModelProperty( + name = "timestamp", + value = "Device Location time.", + required = true + ) + private String timestamp; + + public String getLatitude() { + return latitude; + } + + public void setLatitude(String latitude) { + this.latitude = latitude; + } + + public String getLongitude() { + return longitude; + } + + public void setLongitude(String longitude) { + this.longitude = longitude; + } + + public String getTimestamp() { + return timestamp; + } + + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityHolder.java new file mode 100644 index 0000000000..6ab26276b0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityHolder.java @@ -0,0 +1,42 @@ +/* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.common.operation.mgt; + +import java.util.List; + +public class ActivityHolder { + + private List activityList; + private List largeResponseIDs; + + public List getActivityList() { + return activityList; + } + + public void setActivityList(List activityList) { + this.activityList = activityList; + } + + public List getLargeResponseIDs() { + return largeResponseIDs; + } + + public void setLargeResponseIDs(List largeResponseIDs) { + this.largeResponseIDs = largeResponseIDs; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityMapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityMapper.java new file mode 100644 index 0000000000..544611453e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityMapper.java @@ -0,0 +1,42 @@ +/* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.common.operation.mgt; + +import java.util.Map; + +public class ActivityMapper { + + private Activity activity; + private Map activityStatusMap; + + public Activity getActivity() { + return activity; + } + + public void setActivity(Activity activity) { + this.activity = activity; + } + + public Map getActivityStatusMap() { + return activityStatusMap; + } + + public void setActivityStatusMap(Map activityStatusMap) { + this.activityStatusMap = activityStatusMap; + } +} From 61c05143c4dd5410fcc9810edfa62f9074d76dd9 Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 23 Oct 2020 16:59:35 +0530 Subject: [PATCH 3/5] Fixing activity retrieval with intiatedBy --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 2 +- .../dao/impl/operation/MySQLOperationDAOImpl.java | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) 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 caa2a86601..caa4de580a 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 @@ -1090,7 +1090,7 @@ public class GenericOperationDAOImpl implements OperationDAO { ResultSet rs = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(*) AS COUNT " + + String sql = "SELECT COUNT(DISTINCT(OPERATION_ID)) AS COUNT " + "FROM DM_ENROLMENT_OP_MAPPING AS m " + " INNER JOIN " + " DM_OPERATION dp ON dp.ID = m.OPERATION_ID " + 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/operation/MySQLOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java index ec097133c5..532f0ca364 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java @@ -285,20 +285,20 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { "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 ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING WHERE INITIATED_BY = ? " + + " ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " + "WHERE " + " eom.UPDATED_TIMESTAMP > ? " + " AND eom.TENANT_ID = ? " + - " AND eom.INITIATED_BY = ? " + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, offset); - stmt.setInt(2, limit); - stmt.setLong(3, timestamp); - stmt.setInt(4, tenantId); - stmt.setString(5, user); + stmt.setString(1, user); + stmt.setInt(2, offset); + stmt.setInt(3, limit); + stmt.setLong(4, timestamp); + stmt.setInt(5, tenantId); try (ResultSet rs = stmt.executeQuery()) { ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs); From ad299e676c3589914f01bda780c17d3a74b0d775 Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 23 Oct 2020 17:21:23 +0530 Subject: [PATCH 4/5] removing un-necessary files --- .../common/device/details/LocationBean.java | 89 ------------------- .../device/details/LocationEventBean.java | 69 -------------- 2 files changed, 158 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java deleted file mode 100644 index 261d387e03..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationBean.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2020, Entgra (pvt) Ltd. (http://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 org.wso2.carbon.device.mgt.common.device.details; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; - -import java.util.List; - -@ApiModel( - value = "LocationBean", - description = "This class carries all information related IOS Device location." -) -public class LocationBean { - - @ApiModelProperty( - name = "latitude", - value = "Latitude of the IOS device Location.", - required = true - ) - private float latitude; - @ApiModelProperty( - name = "longitude", - value = "Longitude of the IOS device Location.", - required = true - ) - private float longitude; - @ApiModelProperty( - name = "operationId", - value = "Specific Id of the Location operation.", - required = true - ) - private String operationId; - - @ApiModelProperty( - name = "locationEvents", - value = "If this is a device initiated location publishing." - ) - private List locations; - - public List getLocationEvents() { - return locations; - } - - public void setLocationEvents(List locationEvents) { - this.locations = locationEvents; - } - - public String getOperationId() { - return operationId; - } - - public void setOperationId(String operationId) { - this.operationId = operationId; - } - - - public float getLatitude() { - return latitude; - } - - public void setLatitude(float latitude) { - this.latitude = latitude; - } - - public float getLongitude() { - return longitude; - } - - public void setLongitude(float longitude) { - this.longitude = longitude; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java deleted file mode 100644 index 6e1ef6c976..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/LocationEventBean.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2020, Entgra (pvt) Ltd. (http://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 org.wso2.carbon.device.mgt.common.device.details; - -import io.swagger.annotations.ApiModelProperty; - -public class LocationEventBean { - - @ApiModelProperty( - name = "latitude", - value = "Latitude of the IOS device Location.", - required = true - ) - private String latitude; - - @ApiModelProperty( - name = "longitude", - value = "Longitude of the IOS device Location.", - required = true - ) - private String longitude; - - @ApiModelProperty( - name = "timestamp", - value = "Device Location time.", - required = true - ) - private String timestamp; - - public String getLatitude() { - return latitude; - } - - public void setLatitude(String latitude) { - this.latitude = latitude; - } - - public String getLongitude() { - return longitude; - } - - public void setLongitude(String longitude) { - this.longitude = longitude; - } - - public String getTimestamp() { - return timestamp; - } - - public void setTimestamp(String timestamp) { - this.timestamp = timestamp; - } -} From d831ef32844ed5daba349a849e86265970f0cbe4 Mon Sep 17 00:00:00 2001 From: Ace Date: Mon, 26 Oct 2020 09:40:50 +0530 Subject: [PATCH 5/5] code improvements --- .../core/operation/mgt/dao/impl/GenericOperationDAOImpl.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 caa4de580a..7094afdcd3 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 @@ -376,9 +376,8 @@ public class GenericOperationDAOImpl implements OperationDAO { for (Map.Entry> deviceOpRes : deviceOpResponseMap.entrySet()) { for (ActivityStatus status : activityStatuses) { if (deviceOpRes.getKey().equalsIgnoreCase(status.getDeviceIdentifier().getId())) { - if(status.getResponses()==null){ - List responses = new ArrayList<>(); - status.setResponses(responses); + if (status.getResponses() == null) { + status.setResponses(new ArrayList<>()); } status.getResponses().addAll(deviceOpRes.getValue()); }