diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/GadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.java similarity index 71% rename from components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/GadgetDataServiceDAOImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.java index f6db11bc51..bfc8af3f62 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/GadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.java @@ -16,16 +16,13 @@ * under the License. */ -package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl; +package org.wso2.carbon.device.mgt.analytics.dashboard.dao; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAO; -import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOFactory; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException; -import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import java.sql.Connection; @@ -37,7 +34,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -public class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { +public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceDAO { @Override public DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException { @@ -246,66 +243,6 @@ public class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { } } - @Override - public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { - - if (startIndex < 0) { - throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); - } - - if (resultCount < 5) { - throw new InvalidParameterValueException("Result count should be equal to 5 or greater than that."); - } - - Connection con; - PreparedStatement stmt = null; - ResultSet rs = null; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - 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 ORDER BY DEVICE_COUNT DESC LIMIT ?, ?"; - stmt = con.prepareStatement(sql); - stmt.setInt(1, tenantId); - stmt.setInt(2, startIndex); - stmt.setInt(3, resultCount); - - // executing query - rs = stmt.executeQuery(); - // fetching query results - DeviceCountByGroupEntry filteredNonCompliantDeviceCountByFeature; - while (rs.next()) { - filteredNonCompliantDeviceCountByFeature = new DeviceCountByGroupEntry(); - filteredNonCompliantDeviceCountByFeature.setGroup(rs.getString("FEATURE_CODE")); - filteredNonCompliantDeviceCountByFeature.setDisplayNameForGroup(rs.getString("FEATURE_CODE")); - filteredNonCompliantDeviceCountByFeature.setDeviceCount(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 = ?) NON_COMPLIANT_FEATURE_CODE"; - - stmt = con.prepareStatement(sql); - stmt.setInt(1, tenantId); - - // executing query - rs = stmt.executeQuery(); - // fetching query results - while (rs.next()) { - totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT"); - } - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - PaginationResult paginationResult = new PaginationResult(); - paginationResult.setData(filteredNonCompliantDeviceCountsByFeatures); - paginationResult.setRecordsTotal(totalRecordsCount); - return paginationResult; - } - @Override public List getDeviceCountsByPlatforms(FilterSet filterSet) throws InvalidParameterValueException, SQLException { @@ -533,184 +470,6 @@ public class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { return filteredDeviceCountsByOwnershipTypes; } - @Override - public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { - - if (startIndex < 0) { - throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); - } - - if (resultCount < 5) { - throw new InvalidParameterValueException("Result count should be equal to 5 or greater than that."); - } - - Map filters = this.extractDatabaseFiltersFromBean(filterSet); - - Connection con; - PreparedStatement stmt = null; - ResultSet rs = null; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - List filteredDevicesWithDetails = new ArrayList<>(); - int totalRecordsCount = 0; - 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 DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_1 " + - "WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC LIMIT ?, ?"; - 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++; - } - stmt.setInt(i, startIndex); - stmt.setInt(++i, resultCount); - } else { - stmt.setInt(2, startIndex); - stmt.setInt(3, resultCount); - } - // executing query - rs = stmt.executeQuery(); - // fetching query results - DetailedDeviceEntry filteredDeviceWithDetails; - while (rs.next()) { - filteredDeviceWithDetails = new DetailedDeviceEntry(); - filteredDeviceWithDetails.setDeviceId(rs.getInt("DEVICE_ID")); - filteredDeviceWithDetails.setPlatform(rs.getString("PLATFORM")); - filteredDeviceWithDetails.setOwnershipType(rs.getString("OWNERSHIP")); - filteredDeviceWithDetails.setConnectivityStatus(rs.getString("CONNECTIVITY_STATUS")); - filteredDevicesWithDetails.add(filteredDeviceWithDetails); - } - - // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; - - stmt = con.prepareStatement(sql); - stmt.setInt(1, tenantId); - - // executing query - rs = stmt.executeQuery(); - // fetching query results - while (rs.next()) { - totalRecordsCount = rs.getInt("DEVICE_COUNT"); - } - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - PaginationResult paginationResult = new PaginationResult(); - paginationResult.setData(filteredDevicesWithDetails); - paginationResult.setRecordsTotal(totalRecordsCount); - return paginationResult; - } - - @Override - public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { - - if (nonCompliantFeatureCode == null || "".equals(nonCompliantFeatureCode)) { - throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); - } - - if (startIndex < 0) { - throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); - } - - if (resultCount < 5) { - throw new InvalidParameterValueException("Result count should be equal to 5 or greater than that."); - } - - Map filters = this.extractDatabaseFiltersFromBean(filterSet); - - Connection con; - PreparedStatement stmt = null; - ResultSet rs = null; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - List filteredDevicesWithDetails = new ArrayList<>(); - int totalRecordsCount = 0; - 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 DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_2 " + - "WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC LIMIT ?, ?"; - 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++; - } - stmt.setInt(i, startIndex); - stmt.setInt(++i, resultCount); - } else { - stmt.setInt(3, startIndex); - stmt.setInt(4, resultCount); - } - // executing query - rs = stmt.executeQuery(); - // fetching query results - DetailedDeviceEntry filteredDeviceWithDetails; - while (rs.next()) { - filteredDeviceWithDetails = new DetailedDeviceEntry(); - filteredDeviceWithDetails.setDeviceId(rs.getInt("DEVICE_ID")); - filteredDeviceWithDetails.setPlatform(rs.getString("PLATFORM")); - filteredDeviceWithDetails.setOwnershipType(rs.getString("OWNERSHIP")); - filteredDeviceWithDetails.setConnectivityStatus(rs.getString("CONNECTIVITY_STATUS")); - filteredDevicesWithDetails.add(filteredDeviceWithDetails); - } - - // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " + - "WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; - - stmt = con.prepareStatement(sql); - stmt.setInt(1, tenantId); - stmt.setString(2, nonCompliantFeatureCode); - - // executing query - rs = stmt.executeQuery(); - // fetching query results - while (rs.next()) { - totalRecordsCount = rs.getInt("DEVICE_COUNT"); - } - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - PaginationResult paginationResult = new PaginationResult(); - paginationResult.setData(filteredDevicesWithDetails); - paginationResult.setRecordsTotal(totalRecordsCount); - return paginationResult; - } - @Override public List getDevicesWithDetails(FilterSet filterSet) throws InvalidParameterValueException, SQLException { @@ -825,7 +584,7 @@ public class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { return filteredDevicesWithDetails; } - private Map extractDatabaseFiltersFromBean(FilterSet filterSet) + protected Map extractDatabaseFiltersFromBean(FilterSet filterSet) throws InvalidParameterValueException { if (filterSet == null) { return null; @@ -868,7 +627,7 @@ public class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { return filters; } - private Connection getConnection() throws SQLException { + protected 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/dao/impl/GadgetDataServiceDAOConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOConstants.java similarity index 87% rename from components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/GadgetDataServiceDAOConstants.java rename to components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOConstants.java index 9caad2f3a8..82ca1b9594 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/GadgetDataServiceDAOConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOConstants.java @@ -16,14 +16,14 @@ * under the License. */ -package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl; +package org.wso2.carbon.device.mgt.analytics.dashboard.dao; public final class GadgetDataServiceDAOConstants { public static class PotentialVulnerability { // These constants do not hold actual database values - // These are just logical values defined and used @ Gadget Data Service DAO Implementation layer + // These are just abstract values defined and used @ Gadget Data Service DAO Implementation layer public static final String NON_COMPLIANT = "NON_COMPLIANT"; public static final String UNMONITORED = "UNMONITORED"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOFactory.java index c5a6097f3f..5ee595424f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOFactory.java @@ -20,8 +20,12 @@ 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.device.mgt.analytics.dashboard.dao.impl.GadgetDataServiceDAOImpl; +import org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl.GenericGadgetDataServiceDAOImpl; +import org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl.OracleGadgetDataServiceDAOImpl; +import org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl.PostgreSQLGadgetDataServiceDAOImpl; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; +import org.wso2.carbon.device.mgt.common.UnsupportedDatabaseEngineException; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; @@ -41,7 +45,23 @@ public class GadgetDataServiceDAOFactory { private static ThreadLocal currentConnection = new ThreadLocal<>(); public static GadgetDataServiceDAO getGadgetDataServiceDAO() { - return new GadgetDataServiceDAOImpl(); + if (databaseEngine != null) { + switch (databaseEngine) { + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: + return new GenericGadgetDataServiceDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericGadgetDataServiceDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: + // to be added + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: + return new PostgreSQLGadgetDataServiceDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: + return new OracleGadgetDataServiceDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); } public static void init(DataSourceConfig config) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index 4f98359104..9ec776d2ef 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -97,7 +97,7 @@ public class DeviceManagementDAOFactory { private static ThreadLocal currentConnection = new ThreadLocal(); public static DeviceDAO getDeviceDAO() { - if(databaseEngine != null) { + if (databaseEngine != null) { switch (databaseEngine) { case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: return new OracleDeviceDAOImpl(); @@ -112,7 +112,7 @@ public class DeviceManagementDAOFactory { throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } } - throw new RuntimeException("Database engine has not initialized properly."); + throw new IllegalStateException("Database engine has not initialized properly."); } public static DeviceTypeDAO getDeviceTypeDAO() {