From c828a5264bb37636330c0b19ea478be7301ad39d Mon Sep 17 00:00:00 2001 From: dilanua Date: Mon, 2 May 2016 15:15:45 +0530 Subject: [PATCH] Adding temporary development code bits for dashboard analytics feature --- .../dashboard/GadgetDataService.java | 37 ++-- .../dao/GadgetDataServiceDAOException.java | 9 +- .../dao/GadgetDataServiceDAOFactory.java | 32 +-- .../dao/GadgetDataServiceDAOImpl.java | 60 +++-- .../internal/GadgetDataServiceComponent.java | 20 +- .../internal/GadgetDataServiceImpl.java | 208 ++++++++++++------ 6 files changed, 228 insertions(+), 138 deletions(-) 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 d34bd4c2e5a..7c5b2038e66 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 @@ -30,58 +30,61 @@ import java.util.Map; public interface GadgetDataService { @SuppressWarnings("unused") - int getTotalDeviceCount(); + int getTotalDeviceCount() throws GadgetDataServiceException; @SuppressWarnings("unused") - int getActiveDeviceCount(); + int getActiveDeviceCount() throws GadgetDataServiceException; @SuppressWarnings("unused") - int getInactiveDeviceCount(); + int getInactiveDeviceCount() throws GadgetDataServiceException; @SuppressWarnings("unused") - int getRemovedDeviceCount(); + int getRemovedDeviceCount() throws GadgetDataServiceException; @SuppressWarnings("unused") - int getNonCompliantDeviceCount(); + int getNonCompliantDeviceCount() throws GadgetDataServiceException; @SuppressWarnings("unused") - int getUnmonitoredDeviceCount(); + int getUnmonitoredDeviceCount() throws GadgetDataServiceException; @SuppressWarnings("unused") - PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest); + PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest) + throws GadgetDataServiceException; @SuppressWarnings("unused") - int getDeviceCount(Map filters); + int getDeviceCount(Map filters) throws GadgetDataServiceException; @SuppressWarnings("unused") - int getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, Map filters); + int getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, + Map filters) throws GadgetDataServiceException; @SuppressWarnings("unused") - Map getDeviceCountsByPlatforms(Map filters); + Map getDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceException; @SuppressWarnings("unused") Map getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode, - Map filters); + Map filters) throws GadgetDataServiceException; @SuppressWarnings("unused") - Map getDeviceCountsByOwnershipTypes(Map filters); + Map getDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceException; @SuppressWarnings("unused") Map getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, - Map filters); + Map filters) throws GadgetDataServiceException; @SuppressWarnings("unused") - PaginationResult getDevicesWithDetails(Map filters, PaginationRequest paginationRequest); + PaginationResult getDevicesWithDetails(Map filters, + PaginationRequest paginationRequest) throws GadgetDataServiceException; @SuppressWarnings("unused") PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - Map filters, PaginationRequest paginationRequest); + Map filters, PaginationRequest paginationRequest) throws GadgetDataServiceException; @SuppressWarnings("unused") - List> getDevicesWithDetails(Map filters); + List> getDevicesWithDetails(Map filters) throws GadgetDataServiceException; @SuppressWarnings("unused") List> getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - Map filters); + Map filters) throws GadgetDataServiceException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOException.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOException.java index 3ebf841f130..adb1917c1eb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOException.java @@ -18,11 +18,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao; -@SuppressWarnings("unused") /** - * Custom exception class for data access related exceptions. + * Custom exception class for GadgetDataServiceDAO layer. */ public class GadgetDataServiceDAOException extends Exception { + private String errorMessage; private static final long serialVersionUID = 2021891706072918864L; @@ -43,6 +43,7 @@ public class GadgetDataServiceDAOException extends Exception { * @param errorMessage Specific error message. * @param cause Cause of this exception. */ + @SuppressWarnings("unused") public GadgetDataServiceDAOException(String errorMessage, Throwable cause) { super(errorMessage, cause); setErrorMessage(errorMessage); @@ -53,6 +54,7 @@ public class GadgetDataServiceDAOException extends Exception { * * @param errorMessage Specific error message. */ + @SuppressWarnings("unused") public GadgetDataServiceDAOException(String errorMessage) { super(errorMessage); setErrorMessage(errorMessage); @@ -63,10 +65,12 @@ public class GadgetDataServiceDAOException extends Exception { * * @param cause Cause of this exception. */ + @SuppressWarnings("unused") public GadgetDataServiceDAOException(Throwable cause) { super(cause); } + @SuppressWarnings("unused") public String getErrorMessage() { return errorMessage; } @@ -74,4 +78,5 @@ public class GadgetDataServiceDAOException extends Exception { 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/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 bb2d44ac636..75555690cce 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 @@ -33,6 +33,7 @@ import java.util.List; @SuppressWarnings("unused") public class GadgetDataServiceDAOFactory { + private static final Log log = LogFactory.getLog(GadgetDataServiceDAOFactory.class); private static DataSource dataSource; private static String databaseEngine; @@ -47,7 +48,7 @@ public class GadgetDataServiceDAOFactory { try { databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); } catch (SQLException e) { - log.error("Error occurred while retrieving config.datasource connection", e); + log.error("Error occurred while retrieving config.datasource connection.", e); } } @@ -56,7 +57,7 @@ public class GadgetDataServiceDAOFactory { try { databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); } catch (SQLException e) { - log.error("Error occurred while retrieving config.datasource connection", e); + log.error("Error occurred while retrieving config.datasource connection.", e); } } @@ -64,8 +65,8 @@ public class GadgetDataServiceDAOFactory { Connection conn = currentConnection.get(); if (conn != null) { throw new IllegalTransactionStateException("A transaction is already active within the context of " + - "this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " + - "transaction is already active is a sign of improper transaction handling"); + "this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " + + "transaction is already active is a sign of improper transaction handling."); } conn = dataSource.getConnection(); currentConnection.set(conn); @@ -75,8 +76,8 @@ public class GadgetDataServiceDAOFactory { Connection conn = currentConnection.get(); 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"); + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods."); } return conn; } @@ -85,36 +86,35 @@ public class GadgetDataServiceDAOFactory { Connection conn = currentConnection.get(); 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"); + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods."); } try { conn.close(); } catch (SQLException e) { - log.warn("Error occurred while close the connection"); + log.warn("Error occurred while close the connection."); } currentConnection.remove(); } /** - * Resolve data source from the data source definition + * Resolve data source from the data source definition. * - * @param config data source configuration - * @return data source resolved from the data source definition + * @param config data source configuration. + * @return data source resolved from the data source definition. */ private static DataSource resolveDataSource(DataSourceConfig config) { DataSource dataSource = null; if (config == null) { throw new RuntimeException( - "Device Management Repository data source configuration " + "is null and " + - "thus, is not initialized"); + "Device Management Repository data source configuration " + "is null and " + + "thus, is not initialized."); } JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); if (jndiConfig != null) { if (log.isDebugEnabled()) { - log.debug("Initializing Device Management Repository data source using the JNDI " + - "Lookup Definition"); + log.debug("Initializing Device Management Repository data source using the JNDI Lookup Definition."); } List jndiPropertyList = jndiConfig.getJndiProperties(); if (jndiPropertyList != null) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java index e8f2e19a941..e4c4286ba1b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java @@ -18,8 +18,6 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -35,8 +33,6 @@ import java.util.List; import java.util.Map; class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { - @SuppressWarnings("unused") - private static final Log log = LogFactory.getLog(GadgetDataServiceDAOImpl.class); @Override public int getTotalDeviceCount() throws GadgetDataServiceDAOException { @@ -72,7 +68,15 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { } @Override - public PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest) throws GadgetDataServiceDAOException { + public int getUnmonitoredDeviceCount() throws GadgetDataServiceDAOException { + Map filters = new HashMap<>(); + filters.put("POLICY_ID", -1); + return this.getDeviceCount(filters); + } + + @Override + public PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest) + throws GadgetDataServiceDAOException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; @@ -82,7 +86,7 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { 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 ?, ?"; + "WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC LIMIT ?, ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, paginationRequest.getStartIndex()); @@ -112,7 +116,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT"); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting non compliant device counts by features.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -122,13 +127,6 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { return paginationResult; } - @Override - public int getUnmonitoredDeviceCount() throws GadgetDataServiceDAOException { - Map filters = new HashMap<>(); - filters.put("POLICY_ID", -1); - return this.getDeviceCount(filters); - } - public int getDeviceCount(Map filters) throws GadgetDataServiceDAOException { Connection con; PreparedStatement stmt = null; @@ -166,7 +164,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { filteredDeviceCount = rs.getInt("DEVICE_COUNT"); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a filtered device count.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -211,7 +210,9 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { filteredDeviceCount = rs.getInt("DEVICE_COUNT"); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a filtered device count, " + + "non compliant by a particular feature.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -257,7 +258,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { filteredDeviceCountsByPlatforms.put(rs.getString("PLATFORM"), rs.getInt("DEVICE_COUNT")); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a filtered set of device counts by platforms.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -304,7 +306,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { filteredDeviceCountsByPlatforms.put(rs.getString("PLATFORM"), rs.getInt("DEVICE_COUNT")); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a set of feature non-compliant device counts by platforms.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -350,7 +353,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { filteredDeviceCountsByOwnershipTypes.put(rs.getString("OWNERSHIP"), rs.getInt("DEVICE_COUNT")); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a filtered set of device counts by ownership types.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -398,7 +402,9 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { filteredDeviceCountsByOwnershipTypes.put(rs.getString("OWNERSHIP"), rs.getInt("DEVICE_COUNT")); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a filtered set of feature " + + "non-compliant device counts by ownership types.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -469,7 +475,9 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a filtered set of devices " + + "with details when pagination is enabled.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -546,7 +554,9 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { totalRecordsCount = rs.getInt("DEVICE_COUNT"); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a filtered set of feature non-compliant devices " + + "with details when pagination is enabled.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -600,7 +610,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { filteredDevicesWithDetails.add(filteredDeviceWithDetails); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting a filtered set of devices with details.", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -654,7 +665,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { filteredDevicesWithDetails.add(filteredDeviceWithDetails); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + throw new GadgetDataServiceDAOException("Error occurred @ GadgetDataServiceDAO layer while trying to " + + "execute relevant SQL queries for getting filtered set of feature non-compliant devices with details.", 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/internal/GadgetDataServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceComponent.java index 193afc92da6..a506665f6ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceComponent.java @@ -39,6 +39,7 @@ import org.wso2.carbon.ndatasource.core.DataSourceService; * unbind="unsetDataSourceService" */ public class GadgetDataServiceComponent { + private static final Log log = LogFactory.getLog(GadgetDataServiceComponent.class); protected void activate(ComponentContext componentContext) { @@ -56,10 +57,10 @@ public class GadgetDataServiceComponent { componentContext.getBundleContext(). registerService(GadgetDataService.class.getName(), new GadgetDataServiceImpl(), null); if (log.isDebugEnabled()) { - log.debug("Device Management Dashboard Analytics Bundle has been started successfully"); + log.debug("Device Management Dashboard Analytics Bundle has been started successfully."); } } catch (Throwable e) { - log.error("Error occurred while initializing the bundle", e); + log.error("Error occurred while initializing the bundle.", e); } } @@ -70,11 +71,18 @@ public class GadgetDataServiceComponent { //do nothing } - public void setDataSourceService(DataSourceService dataSourceService){ - + public void setDataSourceService(DataSourceService dataSourceService) { + if (log.isDebugEnabled()) { + log.debug("Binding org.wso2.carbon.ndatasource.core.DataSourceService..."); + } + //do nothing } - public void unsetDataSourceService(DataSourceService dataSourceService){ - + public void unsetDataSourceService(DataSourceService dataSourceService) { + if (log.isDebugEnabled()) { + log.debug("Unbinding org.wso2.carbon.ndatasource.core.DataSourceService..."); + } + //do nothing } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java index 2f31b84f403..b948d63aaaa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java @@ -18,9 +18,8 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.internal; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.analytics.dashboard.GadgetDataService; +import org.wso2.carbon.device.mgt.analytics.dashboard.GadgetDataServiceException; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOException; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOFactory; import org.wso2.carbon.device.mgt.common.PaginationRequest; @@ -35,18 +34,18 @@ import java.util.Map; */ class GadgetDataServiceImpl implements GadgetDataService { - @SuppressWarnings("unused") - private static final Log log = LogFactory.getLog(GadgetDataServiceImpl.class); - @Override - public int getTotalDeviceCount() { + public int getTotalDeviceCount() throws GadgetDataServiceException { int totalDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); totalDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getTotalDeviceCount(); - } catch (GadgetDataServiceDAOException | SQLException e) { - totalDeviceCount = -1; - return totalDeviceCount; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in calling DAO function for total device count.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -54,14 +53,17 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public int getActiveDeviceCount() { + public int getActiveDeviceCount() throws GadgetDataServiceException { int activeDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); activeDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getActiveDeviceCount(); - } catch (GadgetDataServiceDAOException | SQLException e) { - activeDeviceCount = -1; - return activeDeviceCount; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in calling DAO function for active device count.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -69,14 +71,17 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public int getInactiveDeviceCount() { + public int getInactiveDeviceCount() throws GadgetDataServiceException { int inactiveDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); inactiveDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getInactiveDeviceCount(); - } catch (GadgetDataServiceDAOException | SQLException e) { - inactiveDeviceCount = -1; - return inactiveDeviceCount; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in calling DAO function for inactive device count.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -84,14 +89,17 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public int getRemovedDeviceCount() { + public int getRemovedDeviceCount() throws GadgetDataServiceException { int removedDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); removedDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getRemovedDeviceCount(); - } catch (GadgetDataServiceDAOException | SQLException e) { - removedDeviceCount = -1; - return removedDeviceCount; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in calling DAO function for removed device count.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -99,14 +107,17 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public int getNonCompliantDeviceCount() { + public int getNonCompliantDeviceCount() throws GadgetDataServiceException { int nonCompliantDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); nonCompliantDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getNonCompliantDeviceCount(); - } catch (GadgetDataServiceDAOException | SQLException e) { - nonCompliantDeviceCount = -1; - return nonCompliantDeviceCount; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in calling DAO function for non-compliant device count.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -114,14 +125,17 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public int getUnmonitoredDeviceCount() { + public int getUnmonitoredDeviceCount() throws GadgetDataServiceException { int unmonitoredDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); unmonitoredDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getUnmonitoredDeviceCount(); - } catch (GadgetDataServiceDAOException | SQLException e) { - unmonitoredDeviceCount = -1; - return unmonitoredDeviceCount; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in calling DAO function for unmonitored device count.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -129,14 +143,19 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest) { - PaginationResult paginationResult = null; + public PaginationResult getNonCompliantDeviceCountsByFeatures(PaginationRequest paginationRequest) + throws GadgetDataServiceException { + PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getNonCompliantDeviceCountsByFeatures(paginationRequest); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for non-compliant device counts by features.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -144,14 +163,17 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public int getDeviceCount(Map filters) { + public int getDeviceCount(Map filters) throws GadgetDataServiceException { int deviceCount; try { GadgetDataServiceDAOFactory.openConnection(); deviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getDeviceCount(filters); - } catch (GadgetDataServiceDAOException | SQLException e) { - deviceCount = -1; - return deviceCount; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in calling DAO function for getting a filtered device count.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -159,15 +181,19 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public int getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, Map filters) { + public int getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, Map filters) + throws GadgetDataServiceException { int featureNonCompliantDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCount = GadgetDataServiceDAOFactory. getGadgetDataServiceDAO().getFeatureNonCompliantDeviceCount(nonCompliantFeatureCode, filters); - } catch (GadgetDataServiceDAOException | SQLException e) { - featureNonCompliantDeviceCount = -1; - return featureNonCompliantDeviceCount; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting a filtered device count, non compliant by a particular feature.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -175,14 +201,19 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public Map getDeviceCountsByPlatforms(Map filters) { - Map deviceCountsByPlatforms = null; + public Map getDeviceCountsByPlatforms(Map filters) + throws GadgetDataServiceException { + Map deviceCountsByPlatforms; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByPlatforms = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getDeviceCountsByPlatforms(filters); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting filtered device counts by platforms.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -191,14 +222,18 @@ class GadgetDataServiceImpl implements GadgetDataService { @Override public Map getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode, - Map filters) { - Map featureNonCompliantDeviceCountsByPlatforms = null; + Map filters) throws GadgetDataServiceException { + Map featureNonCompliantDeviceCountsByPlatforms; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCountsByPlatforms = GadgetDataServiceDAOFactory. getGadgetDataServiceDAO().getFeatureNonCompliantDeviceCountsByPlatforms(nonCompliantFeatureCode, filters); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting filtered device counts by platforms, non compliant by a particular feature.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -206,14 +241,19 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public Map getDeviceCountsByOwnershipTypes(Map filters) { - Map deviceCountsByOwnershipTypes = null; + public Map getDeviceCountsByOwnershipTypes(Map filters) + throws GadgetDataServiceException { + Map deviceCountsByOwnershipTypes; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getDeviceCountsByOwnershipTypes(filters); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting filtered device counts by ownership types.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -222,15 +262,19 @@ class GadgetDataServiceImpl implements GadgetDataService { @Override public Map getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode, - Map filters) { - Map featureNonCompliantDeviceCountsByOwnershipTypes = null; + Map filters) throws GadgetDataServiceException { + Map featureNonCompliantDeviceCountsByOwnershipTypes; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getFeatureNonCompliantDeviceCountsByOwnershipTypes(nonCompliantFeatureCode, filters); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection.", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting filtered device counts by ownership types, non compliant by a particular feature.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -238,14 +282,19 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public PaginationResult getDevicesWithDetails(Map filters, PaginationRequest paginationRequest) { - PaginationResult paginationResult = null; + public PaginationResult getDevicesWithDetails(Map filters, + PaginationRequest paginationRequest) throws GadgetDataServiceException { + PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getDevicesWithDetails(filters, paginationRequest); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting filtered devices with details when pagination is enabled.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -254,14 +303,18 @@ class GadgetDataServiceImpl implements GadgetDataService { @Override public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - Map filters, PaginationRequest paginationRequest) { - PaginationResult paginationResult = null; + Map filters, PaginationRequest paginationRequest) throws GadgetDataServiceException { + PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, filters, paginationRequest); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting filtered devices with details, non compliant by feature when pagination is enabled.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -269,13 +322,18 @@ class GadgetDataServiceImpl implements GadgetDataService { } @Override - public List> getDevicesWithDetails(Map filters) { - List> devicesWithDetails = null; + public List> getDevicesWithDetails(Map filters) + throws GadgetDataServiceException { + List> devicesWithDetails; try { GadgetDataServiceDAOFactory.openConnection(); devicesWithDetails = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getDevicesWithDetails(filters); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting filtered devices with details.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); } @@ -284,14 +342,18 @@ class GadgetDataServiceImpl implements GadgetDataService { @Override public List> getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode, - Map filters) { - List> featureNonCompliantDevicesWithDetails = null; + Map filters) throws GadgetDataServiceException { + List> featureNonCompliantDevicesWithDetails; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDevicesWithDetails = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, filters); - } catch (GadgetDataServiceDAOException | SQLException e) { - return null; + } catch (SQLException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer " + + "in opening database connection", e); + } catch (GadgetDataServiceDAOException e) { + throw new GadgetDataServiceException("Error occurred @ GadgetDataService layer in calling DAO function " + + "for getting filtered devices with details, non compliant by feature.", e); } finally { GadgetDataServiceDAOFactory.closeConnection(); }