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 14956fd05d6..88fc6826055 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 @@ -46,4 +46,10 @@ public interface GadgetDataService { @SuppressWarnings("unused") Map getNonCompliantDeviceCountsByFeatures(); + @SuppressWarnings("unused") + Map getDeviceCountsByPlatforms(Map filters); + + @SuppressWarnings("unused") + Map getDeviceCountsByOwnershipTypes(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/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 6e311e6917a..8a842c3bddf 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 @@ -175,7 +175,7 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { } } stmt = con.prepareStatement(sql); - // [2] appending filter column values + // [2] appending filter column values, if exist stmt.setInt(1, tenantId); if (filters != null && filters.values().size() > 0) { int i = 2; @@ -212,8 +212,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { con = this.getConnection(); String sql, advancedSqlFiltering = ""; // appending filters if exist, to support advanced filtering options - // [1] appending filter columns - if (filters.size() > 0) { + // [1] appending filter columns, if exist + if (filters != null && filters.size() > 0) { for (String column : filters.keySet()) { advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } @@ -227,16 +227,18 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { advancedSqlFiltering + "GROUP BY PLATFORM"; } stmt = con.prepareStatement(sql); - // [2] appending filter column values + // [2] appending filter column values, if exist stmt.setInt(1, tenantId); - 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); + 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++; } - i++; } // executing query rs = stmt.executeQuery(); @@ -262,37 +264,39 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { con = this.getConnection(); String sql, advancedSqlFiltering = ""; // appending filters if exist, to support advanced filtering options - // [1] appending filter columns - if (filters.size() > 0) { + // [1] appending filter columns, if exist + if (filters != null && filters.size() > 0) { for (String column : filters.keySet()) { advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } if (filteringViewID == 1) { - sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + + 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 PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + + sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + advancedSqlFiltering + "GROUP BY OWNERSHIP"; } stmt = con.prepareStatement(sql); - // [2] appending filter column values + // [2] appending filter column values, if exist stmt.setInt(1, tenantId); - 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); + 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++; } - i++; } // executing query rs = stmt.executeQuery(); // fetching query results while (rs.next()) { - filteredDeviceCountsByOwnershipTypes.put(rs.getString("PLATFORM"), rs.getInt("DEVICE_COUNT")); + 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); @@ -319,32 +323,34 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { sql = "SELECT DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?"; } // appending filters to support advanced filtering options - // [1] appending filter columns - if (filters.size() > 0) { + // [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 + // [2] appending filter column values, if exist stmt.setInt(1, tenantId); - 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); + 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++; } - 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")); + 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) { 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 5a87209688e..6c529681833 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 @@ -146,4 +146,34 @@ class GadgetDataServiceImpl implements GadgetDataService { return nonCompliantDeviceCountsByFeatures; } + @Override + public Map getDeviceCountsByPlatforms(Map filters) { + Map deviceCountsByPlatforms = null; + try { + GadgetDataServiceDAOFactory.openConnection(); + deviceCountsByPlatforms = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getDeviceCountsByPlatforms(filters); + } catch (GadgetDataServiceDAOException | SQLException e) { + return null; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return deviceCountsByPlatforms; + } + + @Override + public Map getDeviceCountsByOwnershipTypes(Map filters) { + Map deviceCountsByOwnershipTypes = null; + try { + GadgetDataServiceDAOFactory.openConnection(); + deviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getDeviceCountsByOwnershipTypes(filters); + } catch (GadgetDataServiceDAOException | SQLException e) { + return null; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return deviceCountsByOwnershipTypes; + } + }