From ce8d4b53a29ff70a7d41709316dae261834a2484 Mon Sep 17 00:00:00 2001 From: dilanua Date: Fri, 8 Apr 2016 15:10:01 +0530 Subject: [PATCH 1/7] Adding temporary development code bits of dashboard analytics feature --- components/device-mgt/pom.xml | 2 +- .../core/util/PolicyManagementConstants.java | 17 +++++++---- components/policy-mgt/pom.xml | 2 -- .../src/main/resources/dbscripts/cdm/h2.sql | 28 +++++++++++++++++++ features/device-mgt/pom.xml | 2 +- pom.xml | 5 ++++ 6 files changed, 46 insertions(+), 10 deletions(-) diff --git a/components/device-mgt/pom.xml b/components/device-mgt/pom.xml index 4938424351..3bc5b7eb6f 100644 --- a/components/device-mgt/pom.xml +++ b/components/device-mgt/pom.xml @@ -28,7 +28,6 @@ 4.0.0 - org.wso2.carbon.devicemgt device-mgt pom WSO2 Carbon - Device Management Component @@ -39,6 +38,7 @@ org.wso2.carbon.device.mgt.common org.wso2.carbon.device.mgt.extensions org.wso2.carbon.device.mgt.ui + org.wso2.carbon.device.mgt.analytics.dashboard org.wso2.carbon.device.mgt.analytics.data.publisher org.wso2.carbon.device.mgt.etc diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index 13a2df3442..bcaac2b283 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -22,29 +22,34 @@ public final class PolicyManagementConstants { public static final String DEVICE_CONFIG_XML_NAME = "cdm-config.xml"; public static final String ANY = "ANY"; - public static final String POLICY_BUNDLE = "POLICY_BUNDLE"; + // public static final String POLICY_BUNDLE = "POLICY_BUNDLE"; public static final String TENANT_ID = "TENANT_ID"; - public static final String MONITOR = "MONITOR"; + // public static final String MONITOR = "MONITOR"; public static final String ENFORCE = "ENFORCE"; public static final String WARN = "WARN"; public static final String BLOCK = "BLOCK"; - public static final String MONITORING_TASK_TYPE = "MONITORING_TASK"; public static final String MONITORING_TASK_NAME = "MONITORING"; public static final String MONITORING_TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.task.MonitoringTask"; - public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; - public static final String DM_CACHE = "DM_CACHE"; + // public static final String DM_CACHE = "DM_CACHE"; public static final String DM_CACHE_LIST = "DM_CACHE_LIST"; - public static final String DELEGATION_TASK_TYPE = "DELEGATION__TASK"; public static final String DELEGATION_TASK_NAME = "DELEGATION"; public static final String DELEGATION_TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask"; + /** + Caller would reference the constants using PolicyManagementConstants.DEVICE_CONFIG_XML_NAME, + and so on. Any caller should be prevented from constructing objects of + this class, thus declaring this private constructor. + */ + private PolicyManagementConstants() { + throw new AssertionError(); + } } diff --git a/components/policy-mgt/pom.xml b/components/policy-mgt/pom.xml index d15bb08504..4d1c8edb69 100644 --- a/components/policy-mgt/pom.xml +++ b/components/policy-mgt/pom.xml @@ -28,7 +28,6 @@ 4.0.0 - org.wso2.carbon.devicemgt policy-mgt 1.1.0-SNAPSHOT pom @@ -43,7 +42,6 @@ org.wso2.carbon.complex.policy.decision.point - 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 67cc4d9e0a..38b251c24b 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 @@ -484,3 +484,31 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ON DELETE NO ACTION ON UPDATE NO ACTION ); + +CREATE VIEW DEVICES_VIEW AS +SELECT +DEVICE_INFO.DEVICE_ID, +DEVICE_INFO.PLATFORM, +DEVICE_INFO.OWNERSHIP, +DEVICE_INFO.CONNECTIVITY_STATUS, +IFNULL(DEVICE_WITH_POLICY_INFO.POLICY_ID, -1) AS POLICY_ID, +IFNULL(DEVICE_WITH_POLICY_INFO.IS_COMPLIANT, -1) AS IS_COMPLIANT, +DEVICE_INFO.TENANT_ID +FROM +(SELECT +DM_DEVICE.ID AS DEVICE_ID, +DM_DEVICE_TYPE.NAME AS PLATFORM, +DM_ENROLMENT.OWNERSHIP AS OWNERSHIP, +DM_ENROLMENT.STATUS AS CONNECTIVITY_STATUS, +DM_DEVICE.TENANT_ID AS TENANT_ID +FROM DM_DEVICE, DM_DEVICE_TYPE, DM_ENROLMENT +WHERE DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID) DEVICE_INFO +LEFT JOIN +(SELECT +DEVICE_ID, +POLICY_ID, +STATUS AS IS_COMPLIANT +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; diff --git a/features/device-mgt/pom.xml b/features/device-mgt/pom.xml index 8896bc9aa0..a30a6bb68e 100644 --- a/features/device-mgt/pom.xml +++ b/features/device-mgt/pom.xml @@ -27,7 +27,6 @@ 4.0.0 - org.wso2.carbon.devicemgt device-mgt-feature pom WSO2 Carbon - Device Management Feature @@ -39,6 +38,7 @@ org.wso2.carbon.device.mgt.feature org.wso2.carbon.device.mgt.extensions.feature org.wso2.carbon.device.mgt.analytics.data.publisher.feature + org.wso2.carbon.device.mgt.analytics.dashboard.feature \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3711d3cfe9..65f8dd5e8b 100644 --- a/pom.xml +++ b/pom.xml @@ -269,6 +269,11 @@ org.wso2.carbon.device.mgt.analytics.data.publisher ${carbon.device.mgt.version} + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.analytics.dashboard + ${carbon.device.mgt.version} + org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.server.feature From 274aa0ecd32e5a502855cc7587f60e3621ca548e Mon Sep 17 00:00:00 2001 From: dilanua Date: Fri, 22 Apr 2016 10:48:42 +0530 Subject: [PATCH 2/7] Adding temporary development code bits for cdmf-product dashboard feature --- .../dashboard/GadgetDataService.java | 36 +- .../dashboard/dao/GadgetDataServiceDAO.java | 66 +++- .../dao/GadgetDataServiceDAOException.java | 18 + .../dao/GadgetDataServiceDAOFactory.java | 18 + .../dao/GadgetDataServiceDAOImpl.java | 329 ++++++++++++++++-- .../internal/GadgetDataServiceComponent.java | 18 + .../internal/GadgetDataServiceImpl.java | 125 ++++++- .../src/main/resources/dbscripts/cdm/h2.sql | 2 +- 8 files changed, 560 insertions(+), 52 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 6141311c58..b9a434721c 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 7ddbfdcf93..adfb4359f6 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 97be958ccd..3ebf841f13 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 562241b677..bb2d44ac63 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 1117406e3f..7f0cede95b 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 088b636b70..193afc92da 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 fdbf03d2aa..ab91e9a81a 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 38b251c24b..24b9b322f5 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, From b99d10e075035581668b7c606d07ecd88d07be2f Mon Sep 17 00:00:00 2001 From: ayyoob Date: Fri, 22 Apr 2016 20:57:22 +0530 Subject: [PATCH 3/7] few changes invoker util to support file downloading --- .../jaggeryapps/devicemgt/api/device-api.jag | 125 ++++-------------- .../app/modules/backend-service-invoker.js | 15 ++- .../devicemgt/app/modules/constants.js | 1 + 3 files changed, 39 insertions(+), 102 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag index 1f7d62b2aa..710655f143 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag @@ -28,7 +28,6 @@ var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); var userModule = require("/app/modules/user.js").userModule; var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker; -var CarbonUtils = Packages.org.wso2.carbon.utils.CarbonUtils; var user = session.get(constants.USER_SESSION_KEY); var result; @@ -38,107 +37,37 @@ if (!user) { response.sendRedirect("/devicemgt/login?#login-required"); exit(); } else { - if (uriMatcher.match("/{context}/api/devices/sketch/download/{downloadId}")) { - downloadId = uriMatcher.elements().downloadId; - //Just download the already created zip archive - var sketchFolder = "repository/resources/sketches"; - var archivesPath = "file://" + CarbonUtils.getCarbonHome() + "/" + sketchFolder + "/archives/" + - downloadId + ".zip"; - var zipFile = new File(archivesPath); - response.addHeader('Content-type', "application/zip, application/octet-stream"); - response.addHeader('Cache-Control', 'public,max-age=12960000'); - response.addHeader("Content-Disposition", "attachment; filename=\"" + downloadId + ".zip\""); - - try { - zipFile.open('r'); - var stream = zipFile.getStream(); - print(stream); - } catch (err) { - - } finally { - if (zipFile != null) { - zipFile.close(); - } - } - - } else if (uriMatcher.match("/{context}/api/devices/sketch/download")) { - //Create a new zip archive and register user calling endpoint - - /* This should match with $CARBON_HOME/repository/resources/sketches/{sketchType} */ - sketchType = request.getParameter("sketchType"); - /* This should be registered device type of the CDMF(Connected Device Management Framework) */ - deviceType = request.getParameter("deviceType"); - deviceName = request.getParameter("deviceName"); - - if (!sketchType) { - log.error("Sketch Type is empty!"); - // HTTP status code 400 refers to - Bad request. - result = 400; + if (uriMatcher.match("/{context}/api/devices/sketch/download")) { + // works as a proxy to pass the relavant query string to back end api. + var queryString = request.getQueryString(); + if (!queryString) { + queryString = ""; } else { - /** - URL: {serverURL}/{deviceType}/{downloadAgentUri}?owner={username}&deviceName={deviceName} - {serverURL} - devicemgt/app/conf/config.json - {deviceType} - from the request - {downloadAgentUri} - device_type_specific_unit/private/conf/device-type.json - {username} - from request - {deviceName} - from request - **/ - - var sketchDownloadEndPoint = devicemgtProps["httpsURL"] + "/" + deviceType + "/devices/download"; - deviceTypeConfig = utility.getDeviceTypeConfig(deviceType); - - if (deviceTypeConfig && deviceTypeConfig.deviceType.downloadAgentUri) { - sketchDownloadEndPoint = deviceTypeConfig.deviceType.downloadAgentUri; - } - var tokenPair = session.get(constants.ACCESS_TOKEN_PAIR_IDENTIFIER); - if (tokenPair) { - response.addHeader(constants.AUTHORIZATION_HEADER, constants.BEARER_PREFIX + tokenPair.accessToken); - response.sendRedirect(sketchDownloadEndPoint + "?sketchType=" + sketchType + "&deviceName=" - + deviceName); - } else { - response.sendRedirect(devicemgtProps["httpsURL"] + "/devicemgt/login"); - exit(); - } + queryString = "?" + queryString; } - - } else if (uriMatcher.match("/{context}/api/devices/sketch/generate_link")) { - - var contents = request.getContent(); - sketchType = contents.sketchType; - deviceType = contents.deviceType; - deviceName = contents.deviceName; - generateLink = contents.generateLink; - - if (!sketchType) { - log.error("Sketch Type is empty!"); - // HTTP status code 400 refers to - Bad request. - result = 400; + var deviceType = request.getParameter("deviceType"); // need a better solution here + deviceTypeConfig = utility.getDeviceTypeConfig(deviceType); + if (deviceTypeConfig && deviceTypeConfig.deviceType.downloadAgentUri) { + sketchDownloadEndPoint = devicemgtProps["httpsURL"] + "/" + deviceTypeConfig.deviceType.downloadAgentUri; + serviceInvokers.HttpClient.get(sketchDownloadEndPoint + queryString, function (responsePayload, responseHeaders) { + for (var i = 0; i < responseHeaders.length; i++) { + var header = responseHeaders[i] + var headerName = String(header.getName()); + var headerValue = String(header.getValue()); + response.addHeader(headerName, headerValue); + } + var streamObject = new Stream(responsePayload); + print(streamObject); + }, function (responsePayload) { + log.error(responsePayload) + var response = {}; + response["status"] = "error"; + return response; + } + ); } else { - - /** - URL: {serverURL}/{deviceType}/{downloadAgentUri}?owner={username}&deviceName={deviceName} - {serverURL} - devicemgt/app/conf/config.json - {deviceType} - from the request - {downloadAgentUri} - device_type_specific_unit/private/conf/device-type.json - {username} - from request - {deviceName} - from request - **/ - - deviceManagerService = devicemgtProps["httpsURL"] + "/" + deviceType + "_mgt" + "/manager"; - sketchGenerateLinkEndPoint = deviceManagerService + "/device/" + sketchType + "/generate_link"; - var deviceTypeConfig = utility.getDeviceTypeConfig(deviceType); - //replace download endpoint - if (deviceTypeConfig && deviceTypeConfig.deviceType.downloadAgentLinkGenUri) { - sketchGenerateLinkEndPoint = devicemgtProps["httpsURL"] + "/" + deviceType + "_mgt" + - "/" + deviceTypeConfig.deviceType.downloadAgentLinkGenUri; - } - - var fileId = get(sketchGenerateLinkEndPoint + "?owner=" + user.username + "&deviceName=" + - deviceName, null, "text"); - result = "curl -k " + devicemgtProps["httpsURL"] + constants.WEB_APP_CONTEXT + - "/api/devices/sketch/download/" + fileId.data; + result = 400; } - } else if (uriMatcher.match("/{context}/api/devices/all")) { result = deviceModule.getOwnDevices(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js index 6e7540b3f0..1e3f0bd69a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js @@ -162,21 +162,28 @@ var backendServiceInvoker = function () { } } - var stringRequestEntity = new StringRequestEntity(stringify(payload)); - httpMethodObject.setRequestEntity(stringRequestEntity); + if (payload) { + var stringRequestEntity = new StringRequestEntity(stringify(payload)); + httpMethodObject.setRequestEntity(stringRequestEntity); + } var client = new HttpClient(); try { client.executeMethod(httpMethodObject); var status = httpMethodObject.getStatusCode(); if (status == 200) { - return successCallback(httpMethodObject.getResponseBody()); + var responseContentTypeHeader = httpMethodObject.getResponseHeader(constants.CONTENT_TYPE_IDENTIFIER); + if (responseContentTypeHeader && responseContentTypeHeader.getValue() == constants.APPLICATION_ZIP) { + return successCallback(httpMethodObject.getResponseBodyAsStream(), httpMethodObject.getResponseHeaders()); + } else { + return successCallback(httpMethodObject.getResponseBody()); + } } else { return errorCallback(httpMethodObject.getResponseBody()); } } catch (e) { return errorCallback(response); } finally { - method.releaseConnection(); + httpMethodObject.releaseConnection(); } }; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/constants.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/constants.js index 9e2d1469fb..8d043b5d0d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/constants.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/constants.js @@ -62,6 +62,7 @@ var ACCESS_TOKEN_PAIR_IDENTIFIER = "accessTokenPair"; var ENCODED_CLIENT_KEYS_IDENTIFIER = "encodedClientKey"; var CONTENT_TYPE_IDENTIFIER = "Content-Type"; var APPLICATION_JSON = "application/json"; +var APPLICATION_ZIP = "application/zip"; var ACCEPT_IDENTIFIER = "Accept"; var AUTHORIZATION_HEADER= "Authorization"; var BEARER_PREFIX = "Bearer "; From 1c0807a5138e897c82d7c64128f655dcf4e23224 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Sat, 23 Apr 2016 00:07:12 +0530 Subject: [PATCH 4/7] added userstore null check for permission scope validator --- .../validators/PermissionBasedScopeValidator.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java index cb8d59fb5c..04fc9996cd 100644 --- a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java +++ b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java @@ -85,9 +85,14 @@ public class PermissionBasedScopeValidator extends OAuth2ScopeValidator { int tenantId = OAuthExtUtils.getTenantId(authzUser.getTenantDomain()); UserRealm userRealm = OAuthExtensionsDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); if (userRealm != null && userRealm.getAuthorizationManager() != null) { - status = userRealm.getAuthorizationManager() - .isUserAuthorized(userStore +"/"+ username, permission.getPath(), - PermissionMethod.UI_EXECUTE); + if (userStore != null) { + status = userRealm.getAuthorizationManager() + .isUserAuthorized(userStore + "/" + username, permission.getPath(), + PermissionMethod.UI_EXECUTE); + } else { + status = userRealm.getAuthorizationManager() + .isUserAuthorized(username, permission.getPath(), PermissionMethod.UI_EXECUTE); + } } } } catch (PermissionManagementException e) { From 5e1ad224806db6829dd616ed3c37cf8889c0bd05 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Sat, 23 Apr 2016 20:30:14 +0530 Subject: [PATCH 5/7] few changes on download api --- .../jaggeryapps/devicemgt/api/device-api.jag | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag index 710655f143..d609d950e8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag @@ -50,6 +50,7 @@ if (!user) { if (deviceTypeConfig && deviceTypeConfig.deviceType.downloadAgentUri) { sketchDownloadEndPoint = devicemgtProps["httpsURL"] + "/" + deviceTypeConfig.deviceType.downloadAgentUri; serviceInvokers.HttpClient.get(sketchDownloadEndPoint + queryString, function (responsePayload, responseHeaders) { + if (responseHeaders) { for (var i = 0; i < responseHeaders.length; i++) { var header = responseHeaders[i] var headerName = String(header.getName()); @@ -58,12 +59,15 @@ if (!user) { } var streamObject = new Stream(responsePayload); print(streamObject); - }, function (responsePayload) { - log.error(responsePayload) - var response = {}; - response["status"] = "error"; - return response; + } else { + return responsePayload; } + }, function (responsePayload) { + log.error(responsePayload) + var response = {}; + response["status"] = "error"; + return response; + } ); } else { result = 400; From c3bc22519adf587aec5459827df784f2378decf1 Mon Sep 17 00:00:00 2001 From: dilanua Date: Sat, 23 Apr 2016 22:51:58 +0530 Subject: [PATCH 6/7] Adding temporary development code bits for dashboard analytics feature --- .../analytics/dashboard/GadgetDataService.java | 4 ++++ .../dashboard/dao/GadgetDataServiceDAOImpl.java | 2 +- .../dashboard/internal/GadgetDataServiceImpl.java | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) 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 b9a434721c..14956fd05d 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 @@ -42,4 +42,8 @@ public interface GadgetDataService { @SuppressWarnings("unused") int getUnmonitoredDeviceCount(); + + @SuppressWarnings("unused") + Map getNonCompliantDeviceCountsByFeatures(); + } 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 7f0cede95b..6e311e6917 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 @@ -89,7 +89,7 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { Map filteredNonCompliantDeviceCountsByFeatures = new HashMap<>(); try { con = this.getConnection(); - String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 " + + String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " + "WHERE TENANT_ID = ? GROUP BY FEATURE_CODE"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); 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 ab91e9a81a..5a87209688 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 @@ -131,4 +131,19 @@ class GadgetDataServiceImpl implements GadgetDataService { return unmonitoredDeviceCount; } + @Override + public Map getNonCompliantDeviceCountsByFeatures() { + Map nonCompliantDeviceCountsByFeatures = null; + try { + GadgetDataServiceDAOFactory.openConnection(); + nonCompliantDeviceCountsByFeatures = + GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getNonCompliantDeviceCountsByFeatures(); + } catch (GadgetDataServiceDAOException | SQLException e) { + return null; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return nonCompliantDeviceCountsByFeatures; + } + } From 7027a3553ad751157c8ab67d5cdeebefc9fe8de4 Mon Sep 17 00:00:00 2001 From: dilanua Date: Sun, 24 Apr 2016 08:24:43 +0530 Subject: [PATCH 7/7] Adding temporary development code bits for dashboard analytics feature --- .../dashboard/GadgetDataService.java | 6 ++ .../dao/GadgetDataServiceDAOImpl.java | 82 ++++++++++--------- .../internal/GadgetDataServiceImpl.java | 30 +++++++ 3 files changed, 80 insertions(+), 38 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 14956fd05d..88fc682605 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java @@ -46,4 +46,10 @@ public interface GadgetDataService { @SuppressWarnings("unused") Map getNonCompliantDeviceCountsByFeatures(); + @SuppressWarnings("unused") + Map getDeviceCountsByPlatforms(Map filters); + + @SuppressWarnings("unused") + Map getDeviceCountsByOwnershipTypes(Map filters); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java index 6e311e6917..8a842c3bdd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAOImpl.java @@ -175,7 +175,7 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { } } stmt = con.prepareStatement(sql); - // [2] appending filter column values + // [2] appending filter column values, if exist stmt.setInt(1, tenantId); if (filters != null && filters.values().size() > 0) { int i = 2; @@ -212,8 +212,8 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { con = this.getConnection(); String sql, advancedSqlFiltering = ""; // appending filters if exist, to support advanced filtering options - // [1] appending filter columns - if (filters.size() > 0) { + // [1] appending filter columns, if exist + if (filters != null && filters.size() > 0) { for (String column : filters.keySet()) { advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } @@ -227,16 +227,18 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { advancedSqlFiltering + "GROUP BY PLATFORM"; } stmt = con.prepareStatement(sql); - // [2] appending filter column values + // [2] appending filter column values, if exist stmt.setInt(1, tenantId); - int i = 2; - for (Object value : filters.values()) { - if (value instanceof Integer) { - stmt.setInt(i, (Integer) value); - } else if (value instanceof String) { - stmt.setString(i, (String) value); + if (filters != null && filters.values().size() > 0) { + int i = 2; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; } - i++; } // executing query rs = stmt.executeQuery(); @@ -262,37 +264,39 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { con = this.getConnection(); String sql, advancedSqlFiltering = ""; // appending filters if exist, to support advanced filtering options - // [1] appending filter columns - if (filters.size() > 0) { + // [1] appending filter columns, if exist + if (filters != null && filters.size() > 0) { for (String column : filters.keySet()) { advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } if (filteringViewID == 1) { - sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + + sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering + "GROUP BY OWNERSHIP"; } else { // if filteringViewID == 2 - sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + + sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " + advancedSqlFiltering + "GROUP BY OWNERSHIP"; } stmt = con.prepareStatement(sql); - // [2] appending filter column values + // [2] appending filter column values, if exist stmt.setInt(1, tenantId); - int i = 2; - for (Object value : filters.values()) { - if (value instanceof Integer) { - stmt.setInt(i, (Integer) value); - } else if (value instanceof String) { - stmt.setString(i, (String) value); + if (filters != null && filters.values().size() > 0) { + int i = 2; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; } - i++; } // executing query rs = stmt.executeQuery(); // fetching query results while (rs.next()) { - filteredDeviceCountsByOwnershipTypes.put(rs.getString("PLATFORM"), rs.getInt("DEVICE_COUNT")); + filteredDeviceCountsByOwnershipTypes.put(rs.getString("OWNERSHIP"), rs.getInt("DEVICE_COUNT")); } } catch (SQLException e) { throw new GadgetDataServiceDAOException("Error occurred while executing a selection query to the database", e); @@ -319,32 +323,34 @@ class GadgetDataServiceDAOImpl implements GadgetDataServiceDAO { sql = "SELECT DEVICE_ID, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?"; } // appending filters to support advanced filtering options - // [1] appending filter columns - if (filters.size() > 0) { + // [1] appending filter columns, if exist + if (filters != null && filters.size() > 0) { for (String column : filters.keySet()) { sql = sql + " AND " + column + " = ?"; } } stmt = con.prepareStatement(sql); - // [2] appending filter column values + // [2] appending filter column values, if exist stmt.setInt(1, tenantId); - int i = 2; - for (Object value : filters.values()) { - if (value instanceof Integer) { - stmt.setInt(i, (Integer) value); - } else if (value instanceof String) { - stmt.setString(i, (String) value); + if (filters != null && filters.values().size() > 0) { + int i = 2; + for (Object value : filters.values()) { + if (value instanceof Integer) { + stmt.setInt(i, (Integer) value); + } else if (value instanceof String) { + stmt.setString(i, (String) value); + } + i++; } - i++; } // executing query rs = stmt.executeQuery(); // fetching query results while (rs.next()) { - filteredDeviceWithDetails.put("Device-ID", rs.getInt("DEVICE_ID")); - filteredDeviceWithDetails.put("Platform", rs.getString("PLATFORM")); - filteredDeviceWithDetails.put("Ownership", rs.getString("OWNERSHIP")); - filteredDeviceWithDetails.put("Connectivity-Details", rs.getString("CONNECTIVITY_STATUS")); + filteredDeviceWithDetails.put("device-id", rs.getInt("DEVICE_ID")); + filteredDeviceWithDetails.put("platform", rs.getString("PLATFORM")); + filteredDeviceWithDetails.put("ownership", rs.getString("OWNERSHIP")); + filteredDeviceWithDetails.put("connectivity-details", rs.getString("CONNECTIVITY_STATUS")); filteredDevicesWithDetails.add(filteredDeviceWithDetails); } } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java index 5a87209688..6c52968183 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/internal/GadgetDataServiceImpl.java @@ -146,4 +146,34 @@ class GadgetDataServiceImpl implements GadgetDataService { return nonCompliantDeviceCountsByFeatures; } + @Override + public Map getDeviceCountsByPlatforms(Map filters) { + Map deviceCountsByPlatforms = null; + try { + GadgetDataServiceDAOFactory.openConnection(); + deviceCountsByPlatforms = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getDeviceCountsByPlatforms(filters); + } catch (GadgetDataServiceDAOException | SQLException e) { + return null; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return deviceCountsByPlatforms; + } + + @Override + public Map getDeviceCountsByOwnershipTypes(Map filters) { + Map deviceCountsByOwnershipTypes = null; + try { + GadgetDataServiceDAOFactory.openConnection(); + deviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory. + getGadgetDataServiceDAO().getDeviceCountsByOwnershipTypes(filters); + } catch (GadgetDataServiceDAOException | SQLException e) { + return null; + } finally { + GadgetDataServiceDAOFactory.closeConnection(); + } + return deviceCountsByOwnershipTypes; + } + }