diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java index 88fc6826055..7be30840776 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java @@ -18,6 +18,9 @@ package org.wso2.carbon.device.mgt.analytics.dashboard; +import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.PaginationResult; + import java.util.Map; /** @@ -26,7 +29,7 @@ import java.util.Map; public interface GadgetDataService { @SuppressWarnings("unused") - int getTotalDeviceCount(Map filters); + int getTotalDeviceCount(); @SuppressWarnings("unused") int getActiveDeviceCount(); @@ -44,7 +47,10 @@ public interface GadgetDataService { int getUnmonitoredDeviceCount(); @SuppressWarnings("unused") - Map getNonCompliantDeviceCountsByFeatures(); + PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest); + + @SuppressWarnings("unused") + int getDeviceCount(Map filters); @SuppressWarnings("unused") Map getDeviceCountsByPlatforms(Map filters); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAO.java index adfb4359f68..b7c68090678 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAO.java @@ -18,6 +18,9 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao; +import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.PaginationResult; + import java.util.List; import java.util.Map; @@ -26,15 +29,9 @@ public interface GadgetDataServiceDAO { /** * Method to get total filtered device count from a particular tenant. * - * @param filters List of filters to be applied in getting - * total filtered device count. - * * @return Total filtered device count. */ - int getTotalDeviceCount(Map filters) throws GadgetDataServiceDAOException; - - @SuppressWarnings("unused") - int getFeatureNonCompliantDeviceCount(Map filters) throws GadgetDataServiceDAOException; + int getTotalDeviceCount() throws GadgetDataServiceDAOException; int getActiveDeviceCount() throws GadgetDataServiceDAOException; @@ -50,9 +47,6 @@ public interface GadgetDataServiceDAO { @SuppressWarnings("unused") int getNonCompliantDeviceCount() throws GadgetDataServiceDAOException; - @SuppressWarnings("unused") - Map getNonCompliantDeviceCountsByFeatures() throws GadgetDataServiceDAOException; - /** * Method to get unmonitored device count. * @@ -61,22 +55,36 @@ public interface GadgetDataServiceDAO { @SuppressWarnings("unused") int getUnmonitoredDeviceCount() throws GadgetDataServiceDAOException; + @SuppressWarnings("unused") + PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest) throws GadgetDataServiceDAOException; + + @SuppressWarnings("unused") + int getDeviceCount(Map filters) throws GadgetDataServiceDAOException; + + @SuppressWarnings("unused") + int getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, + Map filters) throws GadgetDataServiceDAOException; + @SuppressWarnings("unused") Map getDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException; @SuppressWarnings("unused") - Map getFeatureNonCompliantDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException; + Map getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode, + Map filters) throws GadgetDataServiceDAOException; @SuppressWarnings("unused") - Map getDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException; + Map getDeviceCountsByOwnershipTypes(Map filters) + throws GadgetDataServiceDAOException; @SuppressWarnings("unused") - Map getFeatureNonCompliantDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException; + Map getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, + Map filters) throws GadgetDataServiceDAOException; @SuppressWarnings("unused") List> getDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException; @SuppressWarnings("unused") - List> getFeatureNonCompliantDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException; + List> getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, + Map filters) throws GadgetDataServiceDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java index 8a842c3bddf..a76c8d9c506 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java @@ -21,6 +21,8 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao; 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.PaginationRequest; +import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import java.sql.Connection; @@ -37,122 +39,141 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { private static final Log log = LogFactory.getLog(GadgetDataServiceDAOImpl.class); @Override - public int getTotalDeviceCount(Map filters) throws GadgetDataServiceDAOException { - int filteringViewID = 1; - return this.getDeviceCount(filteringViewID, filters); - } - - @Override - public int getFeatureNonCompliantDeviceCount(Map filters) throws GadgetDataServiceDAOException { - int filteringViewID = 2; - return this.getDeviceCount(filteringViewID, filters); + public int getTotalDeviceCount() throws GadgetDataServiceDAOException { + return this.getDeviceCount(null); } @Override public int getActiveDeviceCount() throws GadgetDataServiceDAOException { - int filteringViewID = 1; Map filters = new HashMap<>(); filters.put("CONNECTIVITY_STATUS", "ACTIVE"); - return this.getDeviceCount(filteringViewID, filters); + return this.getDeviceCount(filters); } @Override public int getInactiveDeviceCount() throws GadgetDataServiceDAOException { - int filteringViewID = 1; Map filters = new HashMap<>(); filters.put("CONNECTIVITY_STATUS", "INACTIVE"); - return this.getDeviceCount(filteringViewID, filters); + return this.getDeviceCount(filters); } @Override public int getRemovedDeviceCount() throws GadgetDataServiceDAOException { - int filteringViewID = 1; Map filters = new HashMap<>(); filters.put("CONNECTIVITY_STATUS", "REMOVED"); - return this.getDeviceCount(filteringViewID, filters); + return this.getDeviceCount(filters); } @Override public int getNonCompliantDeviceCount() throws GadgetDataServiceDAOException { - int filteringViewID = 1; Map filters = new HashMap<>(); filters.put("IS_COMPLIANT", 0); - return this.getDeviceCount(filteringViewID, filters); + return this.getDeviceCount(filters); } @Override - public Map getNonCompliantDeviceCountsByFeatures() throws GadgetDataServiceDAOException { + public PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest) throws GadgetDataServiceDAOException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - Map filteredNonCompliantDeviceCountsByFeatures = new HashMap<>(); + List> filteredNonCompliantDeviceCountsByFeatures = new ArrayList<>(); + int totalRecordsCount = 0; try { con = this.getConnection(); String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " + - "WHERE TENANT_ID = ? GROUP BY FEATURE_CODE"; + "WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC LIMIT ?, ?"; + stmt = con.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setInt(2, paginationRequest.getStartIndex()); + stmt.setInt(3, paginationRequest.getRowCount()); + + // executing query + rs = stmt.executeQuery(); + // fetching query results + Map filteredNonCompliantDeviceCountByFeature; + while (rs.next()) { + filteredNonCompliantDeviceCountByFeature = new HashMap<>(); + filteredNonCompliantDeviceCountByFeature.put("FEATURE_CODE", rs.getString("FEATURE_CODE")); + filteredNonCompliantDeviceCountByFeature.put("DEVICE_COUNT", rs.getInt("DEVICE_COUNT")); + filteredNonCompliantDeviceCountsByFeatures.add(filteredNonCompliantDeviceCountByFeature); + } + // fetching total records count + sql = "SELECT COUNT(FEATURE_CODE) AS NON_COMPLIANT_FEATURE_COUNT FROM " + + "(SELECT DISTINCT FEATURE_CODE FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?)"; + stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); + // executing query rs = stmt.executeQuery(); // fetching query results while (rs.next()) { - filteredNonCompliantDeviceCountsByFeatures. - put(rs.getString("FEATURE_CODE"), rs.getInt("DEVICE_COUNT")); + totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT"); } } catch (SQLException e) { throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } - return filteredNonCompliantDeviceCountsByFeatures; + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(filteredNonCompliantDeviceCountsByFeatures); + paginationResult.setRecordsTotal(totalRecordsCount); + return paginationResult; } @Override public int getUnmonitoredDeviceCount() throws GadgetDataServiceDAOException { - int filteringViewID = 1; Map filters = new HashMap<>(); filters.put("POLICY_ID", -1); - return this.getDeviceCount(filteringViewID, filters); - } - - @Override - public Map getDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException { - int filteringViewID = 1; - return this.getDeviceCountsByPlatforms(filteringViewID, filters); - } - - @Override - public Map getFeatureNonCompliantDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException { - int filteringViewID = 2; - return this.getDeviceCountsByPlatforms(filteringViewID, filters); - } - - @Override - public Map getDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException { - int filteringViewID = 1; - return this.getDeviceCountsByOwnershipTypes(filteringViewID, filters); - } - - @Override - public Map getFeatureNonCompliantDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException { - int filteringViewID = 2; - return this.getDeviceCountsByOwnershipTypes(filteringViewID, filters); + return this.getDeviceCount(filters); } - @Override - public List> getDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException { - int filteringViewID = 1; - return this.getDevicesWithDetails(filteringViewID, filters); - } - - @Override - public List> getFeatureNonCompliantDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException { - int filteringViewID = 2; - return this.getDevicesWithDetails(filteringViewID, filters); + public int getDeviceCount(Map filters) throws GadgetDataServiceDAOException { + Connection con; + PreparedStatement stmt = null; + ResultSet rs = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + int filteredDeviceCount = 0; + try { + con = this.getConnection(); + String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + // appending filters to support advanced filtering options + // [1] appending filter columns + if (filters != null && filters.size() > 0) { + for (String column : filters.keySet()) { + sql = sql + " AND " + column + " = ?"; + } + } + stmt = con.prepareStatement(sql); + // [2] appending filter column values, if exist + stmt.setInt(1, tenantId); + if (filters != null && filters.values().size() > 0) { + int i = 2; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; + } + } + // executing query + rs = stmt.executeQuery(); + // fetching query results + while (rs.next()) { + filteredDeviceCount = rs.getInt("DEVICE_COUNT"); + } + } catch (SQLException e) { + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return filteredDeviceCount; } - private int getDeviceCount(int filteringViewID, Map filters) throws GadgetDataServiceDAOException { + public int getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, Map filters) throws GadgetDataServiceDAOException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; @@ -160,13 +181,7 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { int filteredDeviceCount = 0; try { con = this.getConnection(); - String sql; - if (filteringViewID == 1) { - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; - } else { - // if filteringViewID == 2 - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?"; - } + String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; // appending filters to support advanced filtering options // [1] appending filter columns if (filters != null && filters.size() > 0) { @@ -177,8 +192,9 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); + stmt.setString(2, nonCompliantFeatureCode); if (filters != null && filters.values().size() > 0) { - int i = 2; + int i = 3; for (Object value : filters.values()) { if (value instanceof Integer) { stmt.setInt(i, (Integer) value); @@ -202,7 +218,7 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { return filteredDeviceCount; } - private Map getDeviceCountsByPlatforms(int filteringViewID, Map filters) throws GadgetDataServiceDAOException { + public Map getDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; @@ -218,14 +234,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } - if (filteringViewID == 1) { - sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + - advancedSqlFiltering + "GROUP BY PLATFORM"; - } else { - // if filteringViewID == 2 - sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + - advancedSqlFiltering + "GROUP BY PLATFORM"; - } + sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + + advancedSqlFiltering + "GROUP BY PLATFORM"; stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -254,12 +264,12 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { return filteredDeviceCountsByPlatforms; } - private Map getDeviceCountsByOwnershipTypes(int filteringViewID, Map filters) throws GadgetDataServiceDAOException { + public Map getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode, Map filters) throws GadgetDataServiceDAOException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - Map filteredDeviceCountsByOwnershipTypes = new HashMap<>(); + Map filteredDeviceCountsByPlatforms = new HashMap<>(); try { con = this.getConnection(); String sql, advancedSqlFiltering = ""; @@ -270,14 +280,55 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } - if (filteringViewID == 1) { - sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + - advancedSqlFiltering + "GROUP BY OWNERSHIP"; - } else { - // if filteringViewID == 2 - sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + - advancedSqlFiltering + "GROUP BY OWNERSHIP"; + sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + + "AND FEATURE_CODE = ? " + advancedSqlFiltering + "GROUP BY PLATFORM"; + stmt = con.prepareStatement(sql); + // [2] appending filter column values, if exist + stmt.setInt(1, tenantId); + stmt.setString(2, nonCompliantFeatureCode); + if (filters != null && filters.values().size() > 0) { + int i = 3; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; + } + } + // executing query + rs = stmt.executeQuery(); + // fetching query results + while (rs.next()) { + filteredDeviceCountsByPlatforms.put(rs.getString("PLATFORM"), rs.getInt("DEVICE_COUNT")); + } + } catch (SQLException e) { + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return filteredDeviceCountsByPlatforms; + } + + public Map getDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException { + Connection con; + PreparedStatement stmt = null; + ResultSet rs = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + Map filteredDeviceCountsByOwnershipTypes = new HashMap<>(); + try { + con = this.getConnection(); + String sql, advancedSqlFiltering = ""; + // appending filters if exist, to support advanced filtering options + // [1] appending filter columns, if exist + if (filters != null && filters.size() > 0) { + for (String column : filters.keySet()) { + advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; + } } + sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + + advancedSqlFiltering + "GROUP BY OWNERSHIP"; stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -306,7 +357,54 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { return filteredDeviceCountsByOwnershipTypes; } - private List> getDevicesWithDetails(int filteringViewID, Map filters) throws GadgetDataServiceDAOException { + public Map getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, Map filters) throws GadgetDataServiceDAOException { + Connection con; + PreparedStatement stmt = null; + ResultSet rs = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + Map filteredDeviceCountsByOwnershipTypes = new HashMap<>(); + try { + con = this.getConnection(); + String sql, advancedSqlFiltering = ""; + // appending filters if exist, to support advanced filtering options + // [1] appending filter columns, if exist + if (filters != null && filters.size() > 0) { + for (String column : filters.keySet()) { + advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; + } + } + sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + + "AND FEATURE_CODE = ? " + advancedSqlFiltering + "GROUP BY OWNERSHIP"; + stmt = con.prepareStatement(sql); + // [2] appending filter column values, if exist + stmt.setInt(1, tenantId); + stmt.setString(2, nonCompliantFeatureCode); + if (filters != null && filters.values().size() > 0) { + int i = 3; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; + } + } + // executing query + rs = stmt.executeQuery(); + // fetching query results + while (rs.next()) { + filteredDeviceCountsByOwnershipTypes.put(rs.getString("OWNERSHIP"), rs.getInt("DEVICE_COUNT")); + } + } catch (SQLException e) { + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return filteredDeviceCountsByOwnershipTypes; + } + + public List> getDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; @@ -316,12 +414,7 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { try { con = this.getConnection(); String sql; - if (filteringViewID == 1) { - sql = "SELECT DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; - } else { - // if filteringViewID == 2 - sql = "SELECT DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?"; - } + sql = "SELECT DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; // appending filters to support advanced filtering options // [1] appending filter columns, if exist if (filters != null && filters.size() > 0) { @@ -361,6 +454,58 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { return filteredDevicesWithDetails; } + public List> getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, Map filters) throws GadgetDataServiceDAOException { + Connection con; + PreparedStatement stmt = null; + ResultSet rs = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + Map filteredDeviceWithDetails = new HashMap<>(); + List> filteredDevicesWithDetails = new ArrayList<>(); + try { + con = this.getConnection(); + String sql; + sql = "SELECT DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_2 " + + "WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; + // appending filters to support advanced filtering options + // [1] appending filter columns, if exist + if (filters != null && filters.size() > 0) { + for (String column : filters.keySet()) { + sql = sql + " AND " + column + " = ?"; + } + } + stmt = con.prepareStatement(sql); + // [2] appending filter column values, if exist + stmt.setInt(1, tenantId); + stmt.setString(2, nonCompliantFeatureCode); + if (filters != null && filters.values().size() > 0) { + int i = 3; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; + } + } + // executing query + rs = stmt.executeQuery(); + // fetching query results + while (rs.next()) { + filteredDeviceWithDetails.put("device-id", rs.getInt("DEVICE_ID")); + filteredDeviceWithDetails.put("platform", rs.getString("PLATFORM")); + filteredDeviceWithDetails.put("ownership", rs.getString("OWNERSHIP")); + filteredDeviceWithDetails.put("connectivity-details", rs.getString("CONNECTIVITY_STATUS")); + filteredDevicesWithDetails.add(filteredDeviceWithDetails); + } + } catch (SQLException e) { + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return filteredDevicesWithDetails; + } + private Connection getConnection() throws SQLException { return GadgetDataServiceDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java index 6c529681833..eb7ed4847d4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java @@ -23,6 +23,8 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.analytics.dashboard.GadgetDataService; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOException; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOFactory; +import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.PaginationResult; import java.sql.SQLException; import java.util.Map; @@ -36,12 +38,12 @@ class GadgetDataServiceImpl implements GadgetDataService { private static final Log log = LogFactory.getLog(GadgetDataServiceImpl.class); @Override - public int getTotalDeviceCount(Map filters) { + public int getTotalDeviceCount() { int totalDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); totalDeviceCount = GadgetDataServiceDAOFactory. - getGadgetDataServiceDAO().getTotalDeviceCount(filters); + getGadgetDataServiceDAO().getTotalDeviceCount(); } catch (GadgetDataServiceDAOException | SQLException e) { totalDeviceCount = -1; return totalDeviceCount; @@ -132,18 +134,34 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public Map getNonCompliantDeviceCountsByFeatures() { - Map nonCompliantDeviceCountsByFeatures = null; + public PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest) { + PaginationResult paginationResult = null; try { GadgetDataServiceDAOFactory.openConnection(); - nonCompliantDeviceCountsByFeatures = - GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getNonCompliantDeviceCountsByFeatures(); + paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). + getNonCompliantDeviceCountsByFeatures(paginationRequest); } catch (GadgetDataServiceDAOException | SQLException e) { return null; } finally { GadgetDataServiceDAOFactory.closeConnection(); } - return nonCompliantDeviceCountsByFeatures; + return paginationResult; + } + + @Override + public int getDeviceCount(Map filters) { + int deviceCount; + try { + GadgetDataServiceDAOFactory.openConnection(); + deviceCount = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getDeviceCount(filters); + } catch (GadgetDataServiceDAOException | SQLException e) { + deviceCount = -1; + return deviceCount; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return deviceCount; } @Override diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 24b9b322f53..d324b3882f2 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -498,9 +498,9 @@ FROM (SELECT DM_DEVICE.ID AS DEVICE_ID, DM_DEVICE_TYPE.NAME AS PLATFORM, -DM_ENROLMENT.OWNERSHIP AS OWNERSHIP, +DM_ENROLMENT.OWNERSHIP, DM_ENROLMENT.STATUS AS CONNECTIVITY_STATUS, -DM_DEVICE.TENANT_ID AS TENANT_ID +DM_DEVICE.TENANT_ID FROM DM_DEVICE, DM_DEVICE_TYPE, DM_ENROLMENT WHERE DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID) DEVICE_INFO LEFT JOIN @@ -508,7 +508,30 @@ LEFT JOIN DEVICE_ID, POLICY_ID, STATUS AS IS_COMPLIANT -FROM -DM_POLICY_COMPLIANCE_STATUS) DEVICE_WITH_POLICY_INFO +FROM DM_POLICY_COMPLIANCE_STATUS) DEVICE_WITH_POLICY_INFO ON DEVICE_INFO.DEVICE_ID = DEVICE_WITH_POLICY_INFO.DEVICE_ID ORDER BY DEVICE_INFO.DEVICE_ID; + +CREATE VIEW DEVICES_VIEW_2 AS +SELECT +DM_DEVICE.ID AS DEVICE_ID, +DM_DEVICE_DETAIL.DEVICE_MODEL, +DM_DEVICE_DETAIL.VENDOR, +DM_DEVICE_DETAIL.OS_VERSION, +DM_ENROLMENT.OWNERSHIP, +DM_ENROLMENT.OWNER, +DM_ENROLMENT.STATUS AS CONNECTIVITY_STATUS, +DM_POLICY_COMPLIANCE_STATUS.POLICY_ID, +DM_DEVICE_TYPE.NAME AS PLATFORM, +DM_POLICY_COMPLIANCE_FEATURES.FEATURE_CODE, +DM_POLICY_COMPLIANCE_FEATURES.STATUS AS IS_COMPLAINT, +DM_DEVICE.TENANT_ID +FROM +DM_POLICY_COMPLIANCE_FEATURES, DM_POLICY_COMPLIANCE_STATUS, DM_ENROLMENT, DM_DEVICE, DM_DEVICE_TYPE, DM_DEVICE_DETAIL +WHERE +DM_POLICY_COMPLIANCE_FEATURES.COMPLIANCE_STATUS_ID = DM_POLICY_COMPLIANCE_STATUS.ID AND +DM_POLICY_COMPLIANCE_STATUS.ENROLMENT_ID = DM_ENROLMENT.ID AND +DM_POLICY_COMPLIANCE_STATUS.DEVICE_ID = DM_DEVICE.ID AND +DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND +DM_DEVICE.ID = DM_DEVICE_DETAIL.DEVICE_ID +ORDER BY TENANT_ID, DEVICE_ID;