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 6141311c583..b9a434721c8 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 @@ -1,15 +1,45 @@ +/* + * 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; +import java.util.Map; + /** * To be updated... */ public interface GadgetDataService { + + @SuppressWarnings("unused") + int getTotalDeviceCount(Map filters); + + @SuppressWarnings("unused") + int getActiveDeviceCount(); + + @SuppressWarnings("unused") + int getInactiveDeviceCount(); + @SuppressWarnings("unused") - int getTotalFilteredDeviceCount(String[] filters); + int getRemovedDeviceCount(); @SuppressWarnings("unused") - int getNonCompliantDeviceCount(String[] filters); + int getNonCompliantDeviceCount(); @SuppressWarnings("unused") - int getUnmonitoredDeviceCount(String[] filters); + int getUnmonitoredDeviceCount(); } 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 7ddbfdcf930..adfb4359f68 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 @@ -1,8 +1,27 @@ +/* + * 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; -public interface GadgetDataServiceDAO { +import java.util.List; +import java.util.Map; - // Total devices related analytics +public interface GadgetDataServiceDAO { /** * Method to get total filtered device count from a particular tenant. @@ -12,37 +31,52 @@ public interface GadgetDataServiceDAO { * * @return Total filtered device count. */ + int getTotalDeviceCount(Map filters) throws GadgetDataServiceDAOException; + @SuppressWarnings("unused") - int getTotalFilteredDeviceCount(String[] filters) throws GadgetDataServiceDAOException; + int getFeatureNonCompliantDeviceCount(Map filters) throws GadgetDataServiceDAOException; + + int getActiveDeviceCount() throws GadgetDataServiceDAOException; + + int getInactiveDeviceCount() throws GadgetDataServiceDAOException; - // Security-concerns related analytics + int getRemovedDeviceCount() throws GadgetDataServiceDAOException; /** * Method to get non-compliant device count. * - * @param filters List of filters to be applied in getting - * non-compliant device count. - * * @return Non-compliant device count. */ - int getNonCompliantDeviceCount(String[] filters); + @SuppressWarnings("unused") + int getNonCompliantDeviceCount() throws GadgetDataServiceDAOException; + + @SuppressWarnings("unused") + Map getNonCompliantDeviceCountsByFeatures() throws GadgetDataServiceDAOException; /** * Method to get unmonitored device count. * - * @param filters List of filters to be applied in getting - * unmonitored device count. - * * @return Unmonitored device count. */ - int getUnmonitoredDeviceCount(String[] filters); + @SuppressWarnings("unused") + int getUnmonitoredDeviceCount() throws GadgetDataServiceDAOException; - // Device Groupings related analytics + @SuppressWarnings("unused") + Map getDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException; @SuppressWarnings("unused") - int getAndroidDeviceCount(String[] filters); + Map getFeatureNonCompliantDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException; + @SuppressWarnings("unused") - int getBYODDeviceCount(String[] filters); + Map getDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException; + @SuppressWarnings("unused") - int getCOPEDeviceCount(String[] filters); + Map getFeatureNonCompliantDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException; + + @SuppressWarnings("unused") + List> getDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException; + + @SuppressWarnings("unused") + List> getFeatureNonCompliantDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/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 97be958ccd8..3ebf841f130 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 @@ -1,3 +1,21 @@ +/* + * 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; @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/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 562241b6779..bb2d44ac636 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 @@ -1,3 +1,21 @@ +/* + * 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; import org.apache.commons.logging.Log; 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 1117406e3f8..7f0cede95bd 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 @@ -1,3 +1,21 @@ +/* + * 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; import org.apache.commons.logging.Log; @@ -9,65 +27,336 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +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 getTotalFilteredDeviceCount(String[] filters) throws GadgetDataServiceDAOException { + public int getTotalDeviceCount(Map filters) throws GadgetDataServiceDAOException { + int filteringViewID = 1; + return this.getDeviceCount(filteringViewID, filters); + } + + @Override + public int getFeatureNonCompliantDeviceCount(Map filters) throws GadgetDataServiceDAOException { + int filteringViewID = 2; + return this.getDeviceCount(filteringViewID, filters); + } + + @Override + public int getActiveDeviceCount() throws GadgetDataServiceDAOException { + int filteringViewID = 1; + Map filters = new HashMap<>(); + filters.put("CONNECTIVITY_STATUS", "ACTIVE"); + return this.getDeviceCount(filteringViewID, filters); + } + + @Override + public int getInactiveDeviceCount() throws GadgetDataServiceDAOException { + int filteringViewID = 1; + Map filters = new HashMap<>(); + filters.put("CONNECTIVITY_STATUS", "INACTIVE"); + return this.getDeviceCount(filteringViewID, filters); + } + + @Override + public int getRemovedDeviceCount() throws GadgetDataServiceDAOException { + int filteringViewID = 1; + Map filters = new HashMap<>(); + filters.put("CONNECTIVITY_STATUS", "REMOVED"); + return this.getDeviceCount(filteringViewID, filters); + } + + @Override + public int getNonCompliantDeviceCount() throws GadgetDataServiceDAOException { + int filteringViewID = 1; + Map filters = new HashMap<>(); + filters.put("IS_COMPLIANT", 0); + return this.getDeviceCount(filteringViewID, filters); + } + + @Override + public Map getNonCompliantDeviceCountsByFeatures() throws GadgetDataServiceDAOException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - int totalFilteredDeviceCount = 0; + Map filteredNonCompliantDeviceCountsByFeatures = new HashMap<>(); try { con = this.getConnection(); - String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES WHERE TENANT_ID = ?"; - if (filters.length > 0) { - - } + String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 " + + "WHERE TENANT_ID = ? GROUP BY FEATURE_CODE"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); + // executing query rs = stmt.executeQuery(); - + // fetching query results while (rs.next()) { - totalFilteredDeviceCount = rs.getInt("DEVICE_COUNT"); + filteredNonCompliantDeviceCountsByFeatures. + put(rs.getString("FEATURE_CODE"), rs.getInt("DEVICE_COUNT")); } } catch (SQLException e) { - throw new GadgetDataServiceDAOException("Error occurred while fetching the registered device types", e); + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } - return totalFilteredDeviceCount; + return filteredNonCompliantDeviceCountsByFeatures; + } + + @Override + public int getUnmonitoredDeviceCount() throws GadgetDataServiceDAOException { + int filteringViewID = 1; + Map filters = new HashMap<>(); + filters.put("POLICY_ID", -1); + return this.getDeviceCount(filteringViewID, filters); + } + + @Override + public Map getDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException { + int filteringViewID = 1; + return this.getDeviceCountsByPlatforms(filteringViewID, filters); } @Override - public int getNonCompliantDeviceCount(String[] filters) { - return 50; + public Map getFeatureNonCompliantDeviceCountsByPlatforms(Map filters) throws GadgetDataServiceDAOException { + int filteringViewID = 2; + return this.getDeviceCountsByPlatforms(filteringViewID, filters); } @Override - public int getUnmonitoredDeviceCount(String[] filters) { - return 60; + public Map getDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException { + int filteringViewID = 1; + return this.getDeviceCountsByOwnershipTypes(filteringViewID, filters); } @Override - public int getAndroidDeviceCount(String[] filters) { - return 0; + public Map getFeatureNonCompliantDeviceCountsByOwnershipTypes(Map filters) throws GadgetDataServiceDAOException { + int filteringViewID = 2; + return this.getDeviceCountsByOwnershipTypes(filteringViewID, filters); } @Override - public int getBYODDeviceCount(String[] filters) { - return 0; + public List> getDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException { + int filteringViewID = 1; + return this.getDevicesWithDetails(filteringViewID, filters); } @Override - public int getCOPEDeviceCount(String[] filters) { - return 0; + public List> getFeatureNonCompliantDevicesWithDetails(Map filters) throws GadgetDataServiceDAOException { + int filteringViewID = 2; + return this.getDevicesWithDetails(filteringViewID, filters); + } + + private int getDeviceCount(int filteringViewID, Map filters) throws GadgetDataServiceDAOException { + Connection con; + PreparedStatement stmt = null; + ResultSet rs = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + int filteredDeviceCount = 0; + try { + con = this.getConnection(); + String sql; + if (filteringViewID == 1) { + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + } else { + // if filteringViewID == 2 + sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?"; + } + // appending filters to support advanced filtering options + // [1] appending filter columns + if (filters != null && filters.size() > 0) { + for (String column : filters.keySet()) { + sql = sql + " AND " + column + " = ?"; + } + } + stmt = con.prepareStatement(sql); + // [2] appending filter column values + stmt.setInt(1, tenantId); + if (filters != null && filters.values().size() > 0) { + int i = 2; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; + } + } + // executing query + rs = stmt.executeQuery(); + // fetching query results + while (rs.next()) { + filteredDeviceCount = rs.getInt("DEVICE_COUNT"); + } + } catch (SQLException e) { + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return filteredDeviceCount; + } + + private Map getDeviceCountsByPlatforms(int filteringViewID, Map filters) throws GadgetDataServiceDAOException { + Connection con; + PreparedStatement stmt = null; + ResultSet rs = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + Map filteredDeviceCountsByPlatforms = new HashMap<>(); + try { + con = this.getConnection(); + String sql, advancedSqlFiltering = ""; + // appending filters if exist, to support advanced filtering options + // [1] appending filter columns + if (filters.size() > 0) { + for (String column : filters.keySet()) { + advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; + } + } + if (filteringViewID == 1) { + sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + + advancedSqlFiltering + "GROUP BY PLATFORM"; + } else { + // if filteringViewID == 2 + sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + + advancedSqlFiltering + "GROUP BY PLATFORM"; + } + stmt = con.prepareStatement(sql); + // [2] appending filter column values + stmt.setInt(1, tenantId); + int i = 2; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; + } + // executing query + rs = stmt.executeQuery(); + // fetching query results + while (rs.next()) { + filteredDeviceCountsByPlatforms.put(rs.getString("PLATFORM"), rs.getInt("DEVICE_COUNT")); + } + } catch (SQLException e) { + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return filteredDeviceCountsByPlatforms; + } + + private Map getDeviceCountsByOwnershipTypes(int filteringViewID, Map filters) throws GadgetDataServiceDAOException { + Connection con; + PreparedStatement stmt = null; + ResultSet rs = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + Map filteredDeviceCountsByOwnershipTypes = new HashMap<>(); + try { + con = this.getConnection(); + String sql, advancedSqlFiltering = ""; + // appending filters if exist, to support advanced filtering options + // [1] appending filter columns + if (filters.size() > 0) { + for (String column : filters.keySet()) { + advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; + } + } + if (filteringViewID == 1) { + sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + + advancedSqlFiltering + "GROUP BY OWNERSHIP"; + } else { + // if filteringViewID == 2 + sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + + advancedSqlFiltering + "GROUP BY OWNERSHIP"; + } + stmt = con.prepareStatement(sql); + // [2] appending filter column values + stmt.setInt(1, tenantId); + int i = 2; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; + } + // executing query + rs = stmt.executeQuery(); + // fetching query results + while (rs.next()) { + filteredDeviceCountsByOwnershipTypes.put(rs.getString("PLATFORM"), rs.getInt("DEVICE_COUNT")); + } + } catch (SQLException e) { + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return filteredDeviceCountsByOwnershipTypes; + } + + private List> getDevicesWithDetails(int filteringViewID, Map filters) throws GadgetDataServiceDAOException { + Connection con; + PreparedStatement stmt = null; + ResultSet rs = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + Map filteredDeviceWithDetails = new HashMap<>(); + List> filteredDevicesWithDetails = new ArrayList<>(); + try { + con = this.getConnection(); + String sql; + if (filteringViewID == 1) { + sql = "SELECT DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?"; + } else { + // if filteringViewID == 2 + sql = "SELECT DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?"; + } + // appending filters to support advanced filtering options + // [1] appending filter columns + if (filters.size() > 0) { + for (String column : filters.keySet()) { + sql = sql + " AND " + column + " = ?"; + } + } + stmt = con.prepareStatement(sql); + // [2] appending filter column values + stmt.setInt(1, tenantId); + int i = 2; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; + } + // executing query + rs = stmt.executeQuery(); + // fetching query results + while (rs.next()) { + filteredDeviceWithDetails.put("Device-ID", rs.getInt("DEVICE_ID")); + filteredDeviceWithDetails.put("Platform", rs.getString("PLATFORM")); + filteredDeviceWithDetails.put("Ownership", rs.getString("OWNERSHIP")); + filteredDeviceWithDetails.put("Connectivity-Details", rs.getString("CONNECTIVITY_STATUS")); + filteredDevicesWithDetails.add(filteredDeviceWithDetails); + } + } catch (SQLException e) { + throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return filteredDevicesWithDetails; } private Connection getConnection() throws SQLException { return GadgetDataServiceDAOFactory.getConnection(); } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/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 088b636b701..193afc92da6 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 @@ -1,3 +1,21 @@ +/* + * 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.internal; import org.apache.commons.logging.Log; 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 fdbf03d2aac..ab91e9a81a4 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 @@ -1,33 +1,134 @@ +/* + * 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.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.dao.GadgetDataServiceDAOException; import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOFactory; +import java.sql.SQLException; +import java.util.Map; + /** * To be updated... */ class GadgetDataServiceImpl implements GadgetDataService { + + @SuppressWarnings("unused") + private static final Log log = LogFactory.getLog(GadgetDataServiceImpl.class); + @Override - public int getTotalFilteredDeviceCount(String[] filters) { - // default - int totalFilteredDeviceCount = -1; + public int getTotalDeviceCount(Map filters) { + int totalDeviceCount; try { - totalFilteredDeviceCount = GadgetDataServiceDAOFactory. - getGadgetDataServiceDAO().getTotalFilteredDeviceCount(filters); - } catch (GadgetDataServiceDAOException e) { - return totalFilteredDeviceCount; + GadgetDataServiceDAOFactory.openConnection(); + totalDeviceCount = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getTotalDeviceCount(filters); + } catch (GadgetDataServiceDAOException | SQLException e) { + totalDeviceCount = -1; + return totalDeviceCount; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); } - return totalFilteredDeviceCount; + return totalDeviceCount; } @Override - public int getNonCompliantDeviceCount(String[] filters) { - return GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getNonCompliantDeviceCount(filters); + public int getActiveDeviceCount() { + int activeDeviceCount; + try { + GadgetDataServiceDAOFactory.openConnection(); + activeDeviceCount = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getActiveDeviceCount(); + } catch (GadgetDataServiceDAOException | SQLException e) { + activeDeviceCount = -1; + return activeDeviceCount; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return activeDeviceCount; } @Override - public int getUnmonitoredDeviceCount(String[] filters) { - return GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getUnmonitoredDeviceCount(filters); + public int getInactiveDeviceCount() { + int inactiveDeviceCount; + try { + GadgetDataServiceDAOFactory.openConnection(); + inactiveDeviceCount = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getInactiveDeviceCount(); + } catch (GadgetDataServiceDAOException | SQLException e) { + inactiveDeviceCount = -1; + return inactiveDeviceCount; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return inactiveDeviceCount; } + + @Override + public int getRemovedDeviceCount() { + int removedDeviceCount; + try { + GadgetDataServiceDAOFactory.openConnection(); + removedDeviceCount = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getRemovedDeviceCount(); + } catch (GadgetDataServiceDAOException | SQLException e) { + removedDeviceCount = -1; + return removedDeviceCount; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return removedDeviceCount; + } + + @Override + public int getNonCompliantDeviceCount() { + int nonCompliantDeviceCount; + try { + GadgetDataServiceDAOFactory.openConnection(); + nonCompliantDeviceCount = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getNonCompliantDeviceCount(); + } catch (GadgetDataServiceDAOException | SQLException e) { + nonCompliantDeviceCount = -1; + return nonCompliantDeviceCount; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return nonCompliantDeviceCount; + } + + @Override + public int getUnmonitoredDeviceCount() { + int unmonitoredDeviceCount; + try { + GadgetDataServiceDAOFactory.openConnection(); + unmonitoredDeviceCount = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getUnmonitoredDeviceCount(); + } catch (GadgetDataServiceDAOException | SQLException e) { + unmonitoredDeviceCount = -1; + return unmonitoredDeviceCount; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return unmonitoredDeviceCount; + } + } 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 38b251c24b7..24b9b322f53 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 @@ -485,7 +485,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ON UPDATE NO ACTION ); -CREATE VIEW DEVICES_VIEW AS +CREATE VIEW DEVICES_VIEW_1 AS SELECT DEVICE_INFO.DEVICE_ID, DEVICE_INFO.PLATFORM,