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 c17dcfb2636..bfaa2d38ff6 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,11 +18,13 @@ package org.wso2.carbon.device.mgt.analytics.dashboard; -import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.*; +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.DataAccessLayerException; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException; import org.wso2.carbon.device.mgt.common.PaginationResult; -import java.sql.SQLException; import java.util.List; /** @@ -31,56 +33,57 @@ import java.util.List; public interface GadgetDataService { @SuppressWarnings("unused") - DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) throws InvalidParameterValueException, SQLException; + DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) + throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException; + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") - DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException; + DeviceCountByGroupEntry getTotalDeviceCount() throws DataAccessLayerException; @SuppressWarnings("unused") - List getDeviceCountsByConnectivityStatuses() throws SQLException; + List getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException; @SuppressWarnings("unused") - List getDeviceCountsByPotentialVulnerabilities() throws SQLException; + List getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException; @SuppressWarnings("unused") PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") List getDeviceCountsByPlatforms(FilterSet filterSet) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") List getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException; + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") List getDeviceCountsByOwnershipTypes(FilterSet filterSet) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") List getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException; + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") List getDevicesWithDetails(FilterSet filterSet) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; @SuppressWarnings("unused") List getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException; + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.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 index c36877d9501..c5e2b8398a1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.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 @@ -22,6 +22,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; 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.DataAccessLayerException; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; @@ -37,7 +38,7 @@ import java.util.Map; public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceDAO { @Override - public DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException { + public DeviceCountByGroupEntry getTotalDeviceCount() throws DataAccessLayerException { int totalDeviceCount; try { totalDeviceCount = this.getFilteredDeviceCount(null); @@ -55,7 +56,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { int filteredDeviceCount = this.getFilteredDeviceCount(filterSet); @@ -67,7 +68,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD return deviceCountByGroupEntry; } - private int getFilteredDeviceCount(FilterSet filterSet) throws InvalidParameterValueException, SQLException { + private int getFilteredDeviceCount(FilterSet filterSet) + throws InvalidParameterValueException, DataAccessLayerException { Map filters = this.extractDatabaseFiltersFromBean(filterSet); @@ -78,7 +80,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD int filteredDeviceCount = 0; try { con = this.getConnection(); - String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.DatabaseView. + DEVICES_VIEW_1 + " WHERE TENANT_ID = ?"; // appending filters to support advanced filtering options // [1] appending filter columns if (filters != null && filters.size() > 0) { @@ -106,6 +109,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD while (rs.next()) { filteredDeviceCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -114,7 +120,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException { + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException { if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) { throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); @@ -129,8 +135,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD int filteredDeviceCount = 0; try { con = this.getConnection(); - String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " + - "WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; + String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.DatabaseView. + 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) { @@ -159,6 +165,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD while (rs.next()) { filteredDeviceCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -172,7 +181,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD } @Override - public List getDeviceCountsByConnectivityStatuses() throws SQLException { + public List getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; @@ -180,8 +189,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD List deviceCountsByConnectivityStatuses = new ArrayList<>(); try { con = this.getConnection(); - String sql = "SELECT CONNECTIVITY_STATUS, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 " + - "WHERE TENANT_ID = ? GROUP BY CONNECTIVITY_STATUS"; + String sql = "SELECT CONNECTIVITY_STATUS, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + + " WHERE TENANT_ID = ? GROUP BY CONNECTIVITY_STATUS"; stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -196,6 +206,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD deviceCountByConnectivityStatus.setDeviceCount(rs.getInt("DEVICE_COUNT")); deviceCountsByConnectivityStatuses.add(deviceCountByConnectivityStatus); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -203,7 +216,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD } @Override - public List getDeviceCountsByPotentialVulnerabilities() throws SQLException { + public List getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException { // getting non-compliant device count DeviceCountByGroupEntry nonCompliantDeviceCount = new DeviceCountByGroupEntry(); nonCompliantDeviceCount.setGroup(GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT); @@ -223,7 +236,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD return deviceCountsByPotentialVulnerabilities; } - private int getNonCompliantDeviceCount() throws SQLException { + private int getNonCompliantDeviceCount() throws DataAccessLayerException { FilterSet filterSet = new FilterSet(); filterSet.setPotentialVulnerability(GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT); try { @@ -233,7 +246,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD } } - private int getUnmonitoredDeviceCount() throws SQLException { + private int getUnmonitoredDeviceCount() throws DataAccessLayerException { FilterSet filterSet = new FilterSet(); filterSet.setPotentialVulnerability(GadgetDataServiceDAOConstants.PotentialVulnerability.UNMONITORED); try { @@ -245,7 +258,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List getDeviceCountsByPlatforms(FilterSet filterSet) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { Map filters = this.extractDatabaseFiltersFromBean(filterSet); @@ -264,8 +277,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } - sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + - advancedSqlFiltering + "GROUP BY PLATFORM"; + sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.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); @@ -291,6 +304,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD filteredDeviceCountByPlatform.setDeviceCount(rs.getInt("DEVICE_COUNT")); filteredDeviceCountsByPlatforms.add(filteredDeviceCountByPlatform); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -300,7 +316,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException { + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException { if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) { throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); @@ -323,8 +339,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } - sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + - "AND FEATURE_CODE = ? " + advancedSqlFiltering + "GROUP BY PLATFORM"; + sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.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); @@ -351,6 +368,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD filteredDeviceCountByPlatform.setDeviceCount(rs.getInt("DEVICE_COUNT")); filteredDeviceCountsByPlatforms.add(filteredDeviceCountByPlatform); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -359,7 +379,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List getDeviceCountsByOwnershipTypes(FilterSet filterSet) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { Map filters = this.extractDatabaseFiltersFromBean(filterSet); @@ -378,8 +398,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } - sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + - advancedSqlFiltering + "GROUP BY OWNERSHIP"; + sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.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); @@ -405,6 +426,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD filteredDeviceCountByOwnershipType.setDeviceCount(rs.getInt("DEVICE_COUNT")); filteredDeviceCountsByOwnershipTypes.add(filteredDeviceCountByOwnershipType); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -413,8 +437,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List - getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException { + getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException { if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) { throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); @@ -437,8 +461,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD 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"; + sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.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); @@ -465,6 +490,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD filteredDeviceCountByOwnershipType.setDeviceCount(rs.getInt("DEVICE_COUNT")); filteredDeviceCountsByOwnershipTypes.add(filteredDeviceCountByOwnershipType); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -473,7 +501,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List getDevicesWithDetails(FilterSet filterSet) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { Map filters = this.extractDatabaseFiltersFromBean(filterSet); @@ -486,7 +514,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD con = this.getConnection(); String sql; sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + - "DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + GadgetDataServiceDAOConstants.DatabaseView.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) { @@ -521,6 +549,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD filteredDeviceWithDetails.setConnectivityStatus(rs.getString("CONNECTIVITY_STATUS")); filteredDevicesWithDetails.add(filteredDeviceWithDetails); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -529,7 +560,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException { + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException { if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) { throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); @@ -546,7 +577,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD con = this.getConnection(); String sql; sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + - "DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; + GadgetDataServiceDAOConstants.DatabaseView.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) { @@ -582,6 +614,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD filteredDeviceWithDetails.setConnectivityStatus(rs.getString("CONNECTIVITY_STATUS")); filteredDevicesWithDetails.add(filteredDeviceWithDetails); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -604,7 +639,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD String potentialVulnerability = filterSet.getPotentialVulnerability(); if (potentialVulnerability != null) { if (GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT.equals(potentialVulnerability) || - GadgetDataServiceDAOConstants.PotentialVulnerability.UNMONITORED.equals(potentialVulnerability)) { + GadgetDataServiceDAOConstants.PotentialVulnerability.UNMONITORED.equals(potentialVulnerability)) { if (GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT.equals(potentialVulnerability)) { filters.put("IS_COMPLIANT", 0); } else { 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 d95250f1bf2..77fb489f50d 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,56 +18,67 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao; -import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.*; +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.DataAccessLayerException; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException; import org.wso2.carbon.device.mgt.common.PaginationResult; -import java.sql.SQLException; import java.util.List; public interface GadgetDataServiceDAO { - DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) throws InvalidParameterValueException, SQLException; + /** + * This method is used to get device count based on a defined filter set. + * @param filterSet An abstract representation of possible filtering options. + * if this value is simply "null" or no values are set for the defined filtering options, + * this method would return total device count in the system + * wrapped with in the defined return format. + * @return An object of type DeviceCountByGroupEntry. + * @throws InvalidParameterValueException This can occur if and only if potentialVulnerability value of filterSet + * is set with some value other than "NON_COMPLIANT" or "UNMONITORED". + * @throws DataAccessLayerException This can occur due to errors connecting to database, + * executing SQL query and retrieving data. + */ + DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) + throws InvalidParameterValueException, DataAccessLayerException; DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException; + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException; - /** - * Method to get total device count from a particular tenant. - * - * @return Total device count. - */ - DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException; + DeviceCountByGroupEntry getTotalDeviceCount() throws DataAccessLayerException; - List getDeviceCountsByConnectivityStatuses() throws SQLException; + List getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException; - List getDeviceCountsByPotentialVulnerabilities() throws SQLException; + List getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException; PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; List getDeviceCountsByPlatforms(FilterSet filterSet) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; List getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException; + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException; List getDeviceCountsByOwnershipTypes(FilterSet filterSet) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; List getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException; + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException; PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet, int startIndex, int resultCount) throws InvalidParameterValueException, SQLException; + FilterSet filterSet, int startIndex, int resultCount) + throws InvalidParameterValueException, DataAccessLayerException; List getDevicesWithDetails(FilterSet filterSet) - throws InvalidParameterValueException, SQLException; + throws InvalidParameterValueException, DataAccessLayerException; List getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException; + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/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 index 82ca1b95944..927d4b7ad18 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/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 @@ -20,6 +20,17 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao; public final class GadgetDataServiceDAOConstants { + public static class DatabaseView { + + public static final String DEVICES_VIEW_1 = "DEVICES_WITH_POLICY_COMPLIANCE_STATUS"; + public static final String DEVICES_VIEW_2 = "DEVICES_WITH_NON_COMPLIANT_FEATURES"; + + private DatabaseView() { + throw new AssertionError(); + } + + } + public static class PotentialVulnerability { // These constants do not hold actual database values 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 43999fc46ab..7848c789054 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 @@ -90,7 +90,11 @@ public class GadgetDataServiceDAOFactory { "this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " + "transaction is already active is a sign of improper transaction handling."); } - conn = dataSource.getConnection(); + try { + conn = dataSource.getConnection(); + } catch (SQLException e) { + + } currentConnection.set(conn); } @@ -109,7 +113,7 @@ public class GadgetDataServiceDAOFactory { if (conn == null) { throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + "This might have ideally been caused by not properly initiating the transaction via " + - "'beginTransaction'/'openConnection' methods."); + "'beginTransaction'/'openConnection' methods."); } try { conn.close(); @@ -130,7 +134,7 @@ public class GadgetDataServiceDAOFactory { DataSource dataSource = null; if (config == null) { throw new RuntimeException( - "Device Management Repository data source configuration " + "is null and " + + "Device Management Repository data source configuration is null and " + "thus, is not initialized."); } JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/exception/DataAccessLayerException.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/exception/DataAccessLayerException.java new file mode 100644 index 00000000000..ff03f413d42 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/exception/DataAccessLayerException.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.analytics.dashboard.dao.exception; + +/** + * Custom exception class for communicating data access layer issues + * relevant to Gadget Data Service DAO layer. + * (In this particular instance, SQL exceptions related to database access). + */ +public class DataAccessLayerException extends Exception { + + private String errorMessage; + private static final long serialVersionUID = 2021891706072918864L; + + /** + * Constructs a new exception with the specific error message and nested exception. + * @param errorMessage specific error message. + * @param nestedException Nested exception. + */ + @SuppressWarnings("unused") + public DataAccessLayerException(String errorMessage, Exception nestedException) { + super(errorMessage, nestedException); + setErrorMessage(errorMessage); + } + + /** + * Constructs a new exception with the specific error message and cause. + * @param errorMessage Specific error message. + * @param cause Cause of this exception. + */ + @SuppressWarnings("unused") + public DataAccessLayerException(String errorMessage, Throwable cause) { + super(errorMessage, cause); + setErrorMessage(errorMessage); + } + + /** + * Constructs a new exception with the specific error message. + * @param errorMessage Specific error message. + */ + public DataAccessLayerException(String errorMessage) { + super(errorMessage); + setErrorMessage(errorMessage); + } + + /** + * Constructs a new exception with the specific error message and cause. + * @param cause Cause of this exception. + */ + @SuppressWarnings("unused") + public DataAccessLayerException(Throwable cause) { + super(cause); + } + + @SuppressWarnings("unused") + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/exception/InvalidParameterValueException.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/exception/InvalidParameterValueException.java index c36bf90e7cc..a6ae0189f59 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/exception/InvalidParameterValueException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/exception/InvalidParameterValueException.java @@ -29,7 +29,6 @@ public class InvalidParameterValueException extends Exception { /** * Constructs a new exception with the specific error message and nested exception. - * * @param errorMessage specific error message. * @param nestedException Nested exception. */ @@ -41,7 +40,6 @@ public class InvalidParameterValueException extends Exception { /** * Constructs a new exception with the specific error message and cause. - * * @param errorMessage Specific error message. * @param cause Cause of this exception. */ @@ -53,7 +51,6 @@ public class InvalidParameterValueException extends Exception { /** * Constructs a new exception with the specific error message. - * * @param errorMessage Specific error message. */ public InvalidParameterValueException(String errorMessage) { @@ -63,7 +60,6 @@ public class InvalidParameterValueException extends Exception { /** * Constructs a new exception with the specific error message and cause. - * * @param cause Cause of this exception. */ @SuppressWarnings("unused") 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/GenericGadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/GenericGadgetDataServiceDAOImpl.java index 9c2f6931864..1885dfd79f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/GenericGadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/GenericGadgetDataServiceDAOImpl.java @@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.AbstractGadgetDataServiceDAO; +import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOConstants; 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.DataAccessLayerException; 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; @@ -39,7 +41,7 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA @Override public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { if (startIndex < 0) { throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); @@ -57,8 +59,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA 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 ?, ?"; + String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.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); @@ -76,8 +79,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA 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"; + sql = "SELECT COUNT(FEATURE_CODE) AS NON_COMPLIANT_FEATURE_COUNT FROM (SELECT DISTINCT FEATURE_CODE FROM " + + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ?) " + + "NON_COMPLIANT_FEATURE_CODE"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -88,6 +92,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA while (rs.next()) { totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -99,7 +106,7 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA @Override public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { if (startIndex < 0) { throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); @@ -128,7 +135,8 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA } } sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + - "DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC LIMIT ?, ?"; + GadgetDataServiceDAOConstants.DatabaseView.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); @@ -163,7 +171,8 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA } // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -174,6 +183,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA while (rs.next()) { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -185,8 +197,8 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA @Override public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + FilterSet filterSet, int startIndex, int resultCount) + throws InvalidParameterValueException, DataAccessLayerException { if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) { throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); @@ -219,8 +231,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA } } sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + - "DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering + - "ORDER BY DEVICE_ID ASC LIMIT ?, ?"; + GadgetDataServiceDAOConstants.DatabaseView.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); @@ -256,8 +269,8 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA } // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " + - "WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -269,6 +282,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA while (rs.next()) { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } 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/MSSQLGadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/MSSQLGadgetDataServiceDAOImpl.java index 74f0cc4ee6a..90163baddad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/MSSQLGadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/MSSQLGadgetDataServiceDAOImpl.java @@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.AbstractGadgetDataServiceDAO; +import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOConstants; 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.DataAccessLayerException; 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; @@ -39,7 +41,7 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO @Override public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { if (startIndex < 0) { throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); @@ -57,8 +59,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO 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 " + + String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC " + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -78,7 +80,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } // 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"; + "(SELECT DISTINCT FEATURE_CODE FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + + " WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -89,6 +92,9 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO while (rs.next()) { totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -100,7 +106,7 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO @Override public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { if (startIndex < 0) { throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); @@ -129,8 +135,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } } sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + - "DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC " + - "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ? " + + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -165,7 +171,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -176,6 +183,9 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO while (rs.next()) { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -187,8 +197,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO @Override public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + FilterSet filterSet, int startIndex, int resultCount) + throws InvalidParameterValueException, DataAccessLayerException { if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) { throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); @@ -221,8 +231,9 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } } sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + - "DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering + - "ORDER BY DEVICE_ID ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -258,8 +269,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " + - "WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -271,6 +282,9 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO while (rs.next()) { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } 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/OracleGadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/OracleGadgetDataServiceDAOImpl.java index a4284938d2b..53c48cab567 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/OracleGadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/OracleGadgetDataServiceDAOImpl.java @@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.AbstractGadgetDataServiceDAO; +import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOConstants; 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.DataAccessLayerException; 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; @@ -39,7 +41,7 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO @Override public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { if (startIndex < 0) { throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); @@ -58,8 +60,10 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO try { con = this.getConnection(); String sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (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) rs) WHERE offset >= ? AND ROWNUM <= ?"; + "AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + + " WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC) rs) " + + "WHERE offset >= ? AND ROWNUM <= ?"; + stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, startIndex); @@ -78,7 +82,8 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } // 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"; + "(SELECT DISTINCT FEATURE_CODE FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + + " WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -89,6 +94,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO while (rs.next()) { totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -100,7 +108,7 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO @Override public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { if (startIndex < 0) { throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); @@ -129,8 +137,10 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } } sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, " + - "OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering + - "ORDER BY DEVICE_ID ASC) rs) WHERE offset >= ? AND ROWNUM <= ?"; + "OWNERSHIP, CONNECTIVITY_STATUS FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + + " WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC) rs) " + + "WHERE offset >= ? AND ROWNUM <= ?"; + stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -165,7 +175,8 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -176,6 +187,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO while (rs.next()) { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -187,8 +201,8 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO @Override public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + FilterSet filterSet, int startIndex, int resultCount) + throws InvalidParameterValueException, DataAccessLayerException { if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) { throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); @@ -221,8 +235,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } } sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, " + - "OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + - advancedSqlFiltering + "ORDER BY DEVICE_ID ASC) rs) WHERE offset >= ? AND ROWNUM <= ?"; + "OWNERSHIP, CONNECTIVITY_STATUS FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + + " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering + + "ORDER BY DEVICE_ID ASC) rs) WHERE offset >= ? AND ROWNUM <= ?"; stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -258,8 +273,8 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO } // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " + - "WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -271,6 +286,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO while (rs.next()) { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } 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/PostgreSQLGadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/PostgreSQLGadgetDataServiceDAOImpl.java index e8c8570a0de..81c2d777648 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/PostgreSQLGadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/PostgreSQLGadgetDataServiceDAOImpl.java @@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.AbstractGadgetDataServiceDAO; +import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOConstants; 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.DataAccessLayerException; 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; @@ -39,7 +41,7 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic @Override public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { if (startIndex < 0) { throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); @@ -57,8 +59,10 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic 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 OFFSET ? LIMIT ?"; + String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? GROUP BY FEATURE_CODE " + + "ORDER BY DEVICE_COUNT DESC OFFSET ? LIMIT ?"; + stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, startIndex); @@ -77,7 +81,8 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic } // 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"; + "(SELECT DISTINCT FEATURE_CODE FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + + " WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -88,6 +93,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic while (rs.next()) { totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -99,7 +107,7 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic @Override public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { if (startIndex < 0) { throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that."); @@ -128,7 +136,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic } } sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + - "DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? LIMIT ?"; + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ? " + + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? LIMIT ?"; + stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -163,7 +173,8 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic } // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -174,6 +185,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic while (rs.next()) { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -185,8 +199,8 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic @Override public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + FilterSet filterSet, int startIndex, int resultCount) + throws InvalidParameterValueException, DataAccessLayerException { if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) { throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty."); @@ -219,8 +233,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic } } sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + - "DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering + - "ORDER BY DEVICE_ID ASC OFFSET ? LIMIT ?"; + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? LIMIT ?"; + stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -256,8 +271,8 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic } // fetching total records count - sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " + - "WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -269,6 +284,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic while (rs.next()) { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } + } catch (SQLException e) { + throw new DataAccessLayerException("Error in either getting database connection, " + + "running SQL query or fetching results.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/impl/GadgetDataServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/impl/GadgetDataServiceImpl.java index 7c3304bb9e0..ac87e46fec7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/impl/GadgetDataServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/impl/GadgetDataServiceImpl.java @@ -20,7 +20,10 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.impl; import org.wso2.carbon.device.mgt.analytics.dashboard.GadgetDataService; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOFactory; -import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.*; +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.DataAccessLayerException; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -33,11 +36,14 @@ import java.util.List; public class GadgetDataServiceImpl implements GadgetDataService { @Override - public DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) throws InvalidParameterValueException, SQLException { + public DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) + throws InvalidParameterValueException, DataAccessLayerException { DeviceCountByGroupEntry filteredDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); filteredDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getDeviceCount(filterSet); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -45,13 +51,15 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, FilterSet filterSet) - throws InvalidParameterValueException, SQLException { + public DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException { DeviceCountByGroupEntry featureNonCompliantDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCount = GadgetDataServiceDAOFactory. - getGadgetDataServiceDAO().getFeatureNonCompliantDeviceCount(nonCompliantFeatureCode, filterSet); + getGadgetDataServiceDAO().getFeatureNonCompliantDeviceCount(nonCompliantFeatureCode, filterSet); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -59,11 +67,13 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException { + public DeviceCountByGroupEntry getTotalDeviceCount() throws DataAccessLayerException { DeviceCountByGroupEntry totalDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); totalDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getTotalDeviceCount(); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -71,12 +81,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public List getDeviceCountsByConnectivityStatuses() throws SQLException { + public List getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException { List deviceCountsByConnectivityStatuses; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByConnectivityStatuses = GadgetDataServiceDAOFactory. - getGadgetDataServiceDAO().getDeviceCountsByConnectivityStatuses(); + getGadgetDataServiceDAO().getDeviceCountsByConnectivityStatuses(); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -84,12 +96,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public List getDeviceCountsByPotentialVulnerabilities() throws SQLException { + public List getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException { List deviceCountsByPotentialVulnerabilities; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByPotentialVulnerabilities = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getDeviceCountsByPotentialVulnerabilities(); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -98,12 +112,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) - throws SQLException, InvalidParameterValueException { + throws InvalidParameterValueException, DataAccessLayerException { PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getNonCompliantDeviceCountsByFeatures(startIndex, resultCount); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -112,12 +128,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List getDeviceCountsByPlatforms(FilterSet filterSet) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { List deviceCountsByPlatforms; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByPlatforms = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getDeviceCountsByPlatforms(filterSet); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -126,12 +144,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException { + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException { List featureNonCompliantDeviceCountsByPlatforms; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCountsByPlatforms = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getFeatureNonCompliantDeviceCountsByPlatforms(nonCompliantFeatureCode, filterSet); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -140,12 +160,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List getDeviceCountsByOwnershipTypes(FilterSet filterSet) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { List deviceCountsByOwnershipTypes; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getDeviceCountsByOwnershipTypes(filterSet); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -155,12 +177,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, - FilterSet filterSet) throws SQLException, InvalidParameterValueException { + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException { List featureNonCompliantDeviceCountsByOwnershipTypes; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getFeatureNonCompliantDeviceCountsByOwnershipTypes(nonCompliantFeatureCode, filterSet); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -169,12 +193,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public PaginationResult getDevicesWithDetails(FilterSet filterSet, - int startIndex, int resultCount) throws InvalidParameterValueException, SQLException { + int startIndex, int resultCount) throws InvalidParameterValueException, DataAccessLayerException { PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getDevicesWithDetails(filterSet, startIndex, resultCount); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -183,13 +209,15 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet, int startIndex, int resultCount) - throws InvalidParameterValueException, SQLException { + FilterSet filterSet, int startIndex, int resultCount) + throws InvalidParameterValueException, DataAccessLayerException { PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, filterSet, startIndex, resultCount); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -198,12 +226,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List getDevicesWithDetails(FilterSet filterSet) - throws InvalidParameterValueException, SQLException { + throws InvalidParameterValueException, DataAccessLayerException { List devicesWithDetails; try { GadgetDataServiceDAOFactory.openConnection(); devicesWithDetails = GadgetDataServiceDAOFactory. - getGadgetDataServiceDAO().getDevicesWithDetails(filterSet); + getGadgetDataServiceDAO().getDevicesWithDetails(filterSet); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -212,12 +242,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - FilterSet filterSet) throws InvalidParameterValueException, SQLException { + FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException { List featureNonCompliantDevicesWithDetails; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDevicesWithDetails = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, filterSet); + } catch (SQLException e) { + throw new DataAccessLayerException("Error in opening database connection.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java index 7246aaaefa0..5c439e48429 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java @@ -20,14 +20,17 @@ package org.wso2.carbon.device.mgt.jaxrs.api; import io.swagger.annotations.*; import org.wso2.carbon.apimgt.annotations.api.*; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.List; /** * Device information related operations. @@ -60,6 +63,26 @@ public interface DeviceInformation { @ApiParam(name = "id", value = "Provide the device identifier", required = true) @PathParam("id") String id); + + @POST + @Path("{list}") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Get devices information from the supplied device identifies", + notes = "This will return device information such as CPU usage, memory usage etc for supplied device " + + "identifiers.", + response = DeviceInfo.class, + responseContainer = "List") + @ApiResponses(value = { + @ApiResponse(code = 200, message = ""), + @ApiResponse(code = 400, message = ""), + @ApiResponse(code = 400, message = ""), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + Response getDevicesInfo(@ApiParam(name = "deviceIdentifiers", value = "List of device identifiers", + required = true) List deviceIdentifiers); + @GET @Path("location/{type}/{id}") @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java index 17b59364f6b..a59c81be542 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java @@ -30,9 +30,11 @@ import org.wso2.carbon.device.mgt.jaxrs.api.DeviceInformation; import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; +import java.util.List; @SuppressWarnings("NonJaxWsWebServices") public class DeviceInformationImpl implements DeviceInformation { @@ -59,6 +61,23 @@ public class DeviceInformationImpl implements DeviceInformation { } + @POST + @Path("list") + public Response getDevicesInfo(List deviceIdentifiers) { + DeviceInformationManager informationManager; + List deviceInfos; + try { + informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); + deviceInfos = informationManager.getDevicesInfo(deviceIdentifiers); + } catch (DeviceDetailsMgtException e) { + String msg = "Error occurred while getting the device information."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(deviceInfos).build(); + } + + @GET @Path("location/{type}/{id}") public Response getDeviceLocation(@PathParam("type") String type, @PathParam("id") String id) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml index 4fd0ac4386e..b06dc5fb4f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -876,6 +876,13 @@ GET + + Device Information + /device-mgt/admin/information/list + /information/list + POST + + Device Search /device-mgt/admin/search diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java index e30652a870d..5b62a0f8dd2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java @@ -23,6 +23,8 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import java.util.List; + /** * This class will manage the storing of device details related generic information such as cpu/memory utilization, battery level, * plugged in to a power source or operation on battery. @@ -47,6 +49,14 @@ public interface DeviceInformationManager { */ DeviceInfo getDeviceInfo(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException; + /** + * This method will return device information for the supplied devices list. + * @param deviceIdentifiers + * @return List of device info objects + * @throws DeviceDetailsMgtException + */ + List getDevicesInfo(List deviceIdentifiers) throws DeviceDetailsMgtException; + /** * This method will manage storing the device location as latitude, longitude, address, zip, country etc.. * @param deviceLocation - Device location object. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index fb7e163767f..b2266d35cd8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -35,6 +35,10 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDA import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class DeviceInformationManagerImpl implements DeviceInformationManager { @@ -97,6 +101,43 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } + @Override + public List getDevicesInfo(List deviceIdentifiers) throws DeviceDetailsMgtException { + + List deviceInfos = new ArrayList<>(); + + Map identifierMap = new HashMap<>(); + for (DeviceIdentifier identifier : deviceIdentifiers) { + identifierMap.put(identifier.getId(), identifier); + } + try { + List deviceIds = new ArrayList<>(); + List devices = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider().getAllDevices(); + for (Device device : devices) { + if (identifierMap.containsKey(device.getDeviceIdentifier()) && + device.getType().equals(identifierMap.get(device.getDeviceIdentifier()))) { + deviceIds.add(device.getId()); + } + } + DeviceManagementDAOFactory.openConnection(); + for(Integer id : deviceIds) { + DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(id); + deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(id)); + deviceInfos.add(deviceInfo); + } + } catch (SQLException e) { + throw new DeviceDetailsMgtException("SQL error occurred while retrieving devices from database.", e); + } catch (DeviceManagementException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving the devices.", e); + } catch (DeviceDetailsMgtDAOException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving devices details.", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return deviceInfos; + } + @Override public void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtException { 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 a767ae6bfb5..22fb0b9d1a3 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 @@ -513,7 +513,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( -- DASHBOARD RELATED VIEWS -- -CREATE VIEW DEVICES_VIEW_1 AS +CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS SELECT DEVICE_INFO.DEVICE_ID, DEVICE_INFO.DEVICE_IDENTIFICATION, @@ -542,7 +542,7 @@ 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 +CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS SELECT DM_DEVICE.ID AS DEVICE_ID, DM_DEVICE.DEVICE_IDENTIFICATION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index 5c7e5e97993..18036ae943d 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -505,7 +505,7 @@ CREATE INDEX FK_DM_DEVICE_DETAILS_DEVICE_idx ON DM_DEVICE_DETAIL (DEVICE_ID ASC) -- DASHBOARD RELATED VIEWS -- -CREATE VIEW DEVICES_VIEW_1 AS +CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS SELECT TOP 100 PERCENT DEVICE_INFO.DEVICE_ID, DEVICE_INFO.DEVICE_IDENTIFICATION, @@ -537,7 +537,7 @@ ON DEVICE_INFO.DEVICE_ID = DEVICE_WITH_POLICY_INFO.DEVICE_ID ORDER BY DEVICE_INFO.DEVICE_ID; GO -CREATE VIEW DEVICES_VIEW_2 AS +CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS SELECT TOP 100 PERCENT DM_DEVICE.ID AS DEVICE_ID, DM_DEVICE.DEVICE_IDENTIFICATION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql index b7f06c57aa7..f092a99a0d8 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -543,7 +543,7 @@ POLICY_ID, STATUS AS IS_COMPLIANT FROM DM_POLICY_COMPLIANCE_STATUS; -CREATE VIEW DEVICES_VIEW_1 AS +CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS SELECT DEVICE_INFO_VIEW.DEVICE_ID, DEVICE_INFO_VIEW.DEVICE_IDENTIFICATION, @@ -560,7 +560,7 @@ DEVICE_WITH_POLICY_INFO_VIEW ON DEVICE_INFO_VIEW.DEVICE_ID = DEVICE_WITH_POLICY_INFO_VIEW.DEVICE_ID ORDER BY DEVICE_INFO_VIEW.DEVICE_ID; -CREATE VIEW DEVICES_VIEW_2 AS +CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS SELECT DM_DEVICE.ID AS DEVICE_ID, DM_DEVICE.DEVICE_IDENTIFICATION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index fa18093ba9c..2cb94d5ed4b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -851,7 +851,7 @@ WHEN (NEW.ID IS NULL) -- DASHBOARD RELATED VIEWS -- -CREATE VIEW DEVICES_VIEW_1 AS +CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS SELECT DEVICE_INFO.DEVICE_ID, DEVICE_INFO.DEVICE_IDENTIFICATION, @@ -880,7 +880,7 @@ 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 +CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS SELECT DM_DEVICE.ID AS DEVICE_ID, DM_DEVICE.DEVICE_IDENTIFICATION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql index de4a8ca0165..957e0f0149c 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -432,7 +432,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( -- DASHBOARD RELATED VIEWS -- -CREATE VIEW DEVICES_VIEW_1 AS +CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS SELECT DEVICE_INFO.DEVICE_ID, DEVICE_INFO.DEVICE_IDENTIFICATION, @@ -461,7 +461,7 @@ 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 +CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS SELECT DM_DEVICE.ID AS DEVICE_ID, DM_DEVICE.DEVICE_IDENTIFICATION,