From fc0fa92826f9011717b25eaf69aba546cd7cd3ba Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Thu, 19 Nov 2020 20:21:57 +0530 Subject: [PATCH] Fix incorrect device operation count returning issue --- .../operation/mgt/OperationManagerImpl.java | 2 +- .../core/operation/mgt/dao/OperationDAO.java | 4 +- .../mgt/dao/impl/GenericOperationDAOImpl.java | 118 +++++++++++++++--- 3 files changed, 104 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index f7e5908498..3a9bef08e6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -627,7 +627,7 @@ public class OperationManagerImpl implements OperationManager { operations.add(operation); } paginationResult = new PaginationResult(); - int count = operationDAO.getOperationCountForDevice(enrolmentId); + int count = operationDAO.getOperationCountForDevice(enrolmentId, request); paginationResult.setData(operations); paginationResult.setRecordsTotal(count); paginationResult.setRecordsFiltered(count); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index 0b9d953ed2..58015003d6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -44,7 +44,7 @@ public interface OperationDAO { List getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException; - int getOperationCountForDevice(int enrolmentId) throws OperationManagementDAOException; + int getOperationCountForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException; List getOperationsForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException; @@ -99,4 +99,4 @@ public interface OperationDAO { Map> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus, int limit) throws OperationManagementDAOException; -} \ 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/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 11d61fc6c4..59d37100a0 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 @@ -52,6 +52,8 @@ import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.Date; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * This class holds the generic implementation of OperationDAO which can be used to support ANSI db syntax. @@ -1479,27 +1481,109 @@ public class GenericOperationDAOImpl implements OperationDAO { } @Override - public int getOperationCountForDevice(int enrolmentId) throws OperationManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int operationCount = 0; + public int getOperationCountForDevice(int enrolmentId, PaginationRequest request) + throws OperationManagementDAOException { + String createdTo = null; + String createdFrom = null; + DateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + if (request.getOperationLogFilters().getCreatedDayFrom() != null) { + createdFrom = simple.format(request.getOperationLogFilters().getCreatedDayFrom()); + } + if (request.getOperationLogFilters().getCreatedDayTo() != null) { + createdTo = simple.format(request.getOperationLogFilters().getCreatedDayTo()); + } + + Long updatedFrom = request.getOperationLogFilters().getUpdatedDayFrom(); + Long updatedTo = request.getOperationLogFilters().getUpdatedDayTo(); + List operationCodes = request.getOperationLogFilters().getOperationCode(); + List status = request.getOperationLogFilters().getStatus(); + boolean isCreatedDayProvided = false; + boolean isUpdatedDayProvided = false; + boolean isOperationCodeProvided = false; + boolean isStatusProvided = false; + + String sql = "SELECT " + + "COUNT(o.ID) AS OPERATION_COUNT " + + "FROM " + + "DM_OPERATION o " + + "INNER JOIN " + + "(SELECT dm.OPERATION_ID, " + + "dm.ID, " + + "dm.STATUS, " + + "dm.UPDATED_TIMESTAMP " + + "FROM " + + "DM_ENROLMENT_OP_MAPPING dm " + + "WHERE " + + "dm.ENROLMENT_ID = ?"; + + if (updatedFrom != null && updatedFrom != 0 && updatedTo != null && updatedTo != 0) { + sql += " AND dm.UPDATED_TIMESTAMP BETWEEN ? AND ?"; + isUpdatedDayProvided = true; + } + sql += ") om ON o.ID = om.OPERATION_ID "; + if (createdFrom != null && !createdFrom.isEmpty() && createdTo != null && !createdTo.isEmpty()) { + sql += " WHERE o.CREATED_TIMESTAMP BETWEEN ? AND ?"; + isCreatedDayProvided = true; + } + if (status != null && !status.isEmpty()) { + if (isCreatedDayProvided) { + sql += " AND (om.STATUS = ? "; + } else { + sql += " WHERE (om.STATUS = ? "; + } + sql = IntStream.range(0, status.size() - 1).mapToObj(i -> " OR om.STATUS = ?") + .collect(Collectors.joining("", sql, "")); + sql += ")"; + isStatusProvided = true; + } + if (operationCodes != null && !operationCodes.isEmpty()) { + if (isCreatedDayProvided || isStatusProvided) { + sql += " AND (o.OPERATION_CODE = ? "; + } else { + sql += " WHERE (o.OPERATION_CODE = ? "; + } + sql = IntStream.range(0, operationCodes.size() - 1).mapToObj(i -> " OR o.OPERATION_CODE = ?") + .collect(Collectors.joining("", sql, "")); + sql += ")"; + isOperationCodeProvided = true; + } try { - conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(ID) AS OPERATION_COUNT FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrolmentId); - rs = stmt.executeQuery(); - if (rs.next()) { - operationCount = rs.getInt("OPERATION_COUNT"); + Connection conn = OperationManagementDAOFactory.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + stmt.setInt(paramIndex++, enrolmentId); + if (isUpdatedDayProvided) { + stmt.setLong(paramIndex++, updatedFrom); + stmt.setLong(paramIndex++, updatedTo); + } + if (isCreatedDayProvided) { + stmt.setString(paramIndex++, createdFrom); + stmt.setString(paramIndex++, createdTo); + } + if (isStatusProvided) { + for (String s : status) { + stmt.setString(paramIndex++, s); + } + } + if (isOperationCodeProvided) { + for (String s : operationCodes) { + stmt.setString(paramIndex++, s); + } + } + + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt("OPERATION_COUNT"); + } + } } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the operations count for enrolment: " - + enrolmentId, e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "SQL error occurred while retrieving the operation count of the device" + enrolmentId + + " for search query"; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } - return operationCount; + return 0; } @Override