Fixed conflicts

revert-70aa11f8
mharindu 8 years ago
commit bca1b4b059

@ -18,11 +18,13 @@
package org.wso2.carbon.device.mgt.analytics.dashboard;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.*;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.DataAccessLayerException;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import java.sql.SQLException;
import java.util.List;
/**
@ -31,56 +33,57 @@ import java.util.List;
public interface GadgetDataService {
@SuppressWarnings("unused")
DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) throws InvalidParameterValueException, SQLException;
DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet)
throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException;
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException;
DeviceCountByGroupEntry getTotalDeviceCount() throws DataAccessLayerException;
@SuppressWarnings("unused")
List<DeviceCountByGroupEntry> getDeviceCountsByConnectivityStatuses() throws SQLException;
List<DeviceCountByGroupEntry> getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException;
@SuppressWarnings("unused")
List<DeviceCountByGroupEntry> getDeviceCountsByPotentialVulnerabilities() throws SQLException;
List<DeviceCountByGroupEntry> getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException;
@SuppressWarnings("unused")
PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
List<DeviceCountByGroupEntry> getDeviceCountsByPlatforms(FilterSet filterSet)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
List<DeviceCountByGroupEntry> getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException;
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
List<DeviceCountByGroupEntry> getDeviceCountsByOwnershipTypes(FilterSet filterSet)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
List<DeviceCountByGroupEntry> getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException;
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
List<DetailedDeviceEntry> getDevicesWithDetails(FilterSet filterSet)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
@SuppressWarnings("unused")
List<DetailedDeviceEntry> getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException;
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException;
}

@ -22,6 +22,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.DataAccessLayerException;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
@ -37,7 +38,7 @@ import java.util.Map;
public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceDAO {
@Override
public DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException {
public DeviceCountByGroupEntry getTotalDeviceCount() throws DataAccessLayerException {
int totalDeviceCount;
try {
totalDeviceCount = this.getFilteredDeviceCount(null);
@ -55,7 +56,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
@Override
public DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
int filteredDeviceCount = this.getFilteredDeviceCount(filterSet);
@ -67,7 +68,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
return deviceCountByGroupEntry;
}
private int getFilteredDeviceCount(FilterSet filterSet) throws InvalidParameterValueException, SQLException {
private int getFilteredDeviceCount(FilterSet filterSet)
throws InvalidParameterValueException, DataAccessLayerException {
Map<String, Object> filters = this.extractDatabaseFiltersFromBean(filterSet);
@ -78,7 +80,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
int filteredDeviceCount = 0;
try {
con = this.getConnection();
String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?";
String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.DatabaseView.
DEVICES_VIEW_1 + " WHERE TENANT_ID = ?";
// appending filters to support advanced filtering options
// [1] appending filter columns
if (filters != null && filters.size() > 0) {
@ -106,6 +109,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
while (rs.next()) {
filteredDeviceCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -114,7 +120,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
@Override
public DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException {
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException {
if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) {
throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty.");
@ -129,8 +135,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
int filteredDeviceCount = 0;
try {
con = this.getConnection();
String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " +
"WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
String sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.DatabaseView.
DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
// appending filters to support advanced filtering options
// [1] appending filter columns
if (filters != null && filters.size() > 0) {
@ -159,6 +165,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
while (rs.next()) {
filteredDeviceCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -172,7 +181,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
}
@Override
public List<DeviceCountByGroupEntry> getDeviceCountsByConnectivityStatuses() throws SQLException {
public List<DeviceCountByGroupEntry> getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException {
Connection con;
PreparedStatement stmt = null;
ResultSet rs = null;
@ -180,8 +189,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
List<DeviceCountByGroupEntry> deviceCountsByConnectivityStatuses = new ArrayList<>();
try {
con = this.getConnection();
String sql = "SELECT CONNECTIVITY_STATUS, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 " +
"WHERE TENANT_ID = ? GROUP BY CONNECTIVITY_STATUS";
String sql = "SELECT CONNECTIVITY_STATUS, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " +
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 +
" WHERE TENANT_ID = ? GROUP BY CONNECTIVITY_STATUS";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -196,6 +206,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
deviceCountByConnectivityStatus.setDeviceCount(rs.getInt("DEVICE_COUNT"));
deviceCountsByConnectivityStatuses.add(deviceCountByConnectivityStatus);
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -203,7 +216,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
}
@Override
public List<DeviceCountByGroupEntry> getDeviceCountsByPotentialVulnerabilities() throws SQLException {
public List<DeviceCountByGroupEntry> getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException {
// getting non-compliant device count
DeviceCountByGroupEntry nonCompliantDeviceCount = new DeviceCountByGroupEntry();
nonCompliantDeviceCount.setGroup(GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT);
@ -223,7 +236,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
return deviceCountsByPotentialVulnerabilities;
}
private int getNonCompliantDeviceCount() throws SQLException {
private int getNonCompliantDeviceCount() throws DataAccessLayerException {
FilterSet filterSet = new FilterSet();
filterSet.setPotentialVulnerability(GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT);
try {
@ -233,7 +246,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
}
}
private int getUnmonitoredDeviceCount() throws SQLException {
private int getUnmonitoredDeviceCount() throws DataAccessLayerException {
FilterSet filterSet = new FilterSet();
filterSet.setPotentialVulnerability(GadgetDataServiceDAOConstants.PotentialVulnerability.UNMONITORED);
try {
@ -245,7 +258,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
@Override
public List<DeviceCountByGroupEntry> getDeviceCountsByPlatforms(FilterSet filterSet)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
Map<String, Object> filters = this.extractDatabaseFiltersFromBean(filterSet);
@ -264,8 +277,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? ";
}
}
sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " +
advancedSqlFiltering + "GROUP BY PLATFORM";
sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ? " + advancedSqlFiltering + "GROUP BY PLATFORM";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -291,6 +304,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
filteredDeviceCountByPlatform.setDeviceCount(rs.getInt("DEVICE_COUNT"));
filteredDeviceCountsByPlatforms.add(filteredDeviceCountByPlatform);
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -300,7 +316,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
@Override
public List<DeviceCountByGroupEntry>
getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException {
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException {
if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) {
throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty.");
@ -323,8 +339,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? ";
}
}
sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " +
"AND FEATURE_CODE = ? " + advancedSqlFiltering + "GROUP BY PLATFORM";
sql = "SELECT PLATFORM, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " +
advancedSqlFiltering + "GROUP BY PLATFORM";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -351,6 +368,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
filteredDeviceCountByPlatform.setDeviceCount(rs.getInt("DEVICE_COUNT"));
filteredDeviceCountsByPlatforms.add(filteredDeviceCountByPlatform);
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -359,7 +379,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
@Override
public List<DeviceCountByGroupEntry> getDeviceCountsByOwnershipTypes(FilterSet filterSet)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
Map<String, Object> filters = this.extractDatabaseFiltersFromBean(filterSet);
@ -378,8 +398,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? ";
}
}
sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " +
advancedSqlFiltering + "GROUP BY OWNERSHIP";
sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ? " +
advancedSqlFiltering + "GROUP BY OWNERSHIP";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -405,6 +426,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
filteredDeviceCountByOwnershipType.setDeviceCount(rs.getInt("DEVICE_COUNT"));
filteredDeviceCountsByOwnershipTypes.add(filteredDeviceCountByOwnershipType);
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -413,8 +437,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
@Override
public List<DeviceCountByGroupEntry>
getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException {
getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException {
if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) {
throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty.");
@ -437,8 +461,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? ";
}
}
sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? " +
"AND FEATURE_CODE = ? " + advancedSqlFiltering + "GROUP BY OWNERSHIP";
sql = "SELECT OWNERSHIP, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " +
advancedSqlFiltering + "GROUP BY OWNERSHIP";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -465,6 +490,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
filteredDeviceCountByOwnershipType.setDeviceCount(rs.getInt("DEVICE_COUNT"));
filteredDeviceCountsByOwnershipTypes.add(filteredDeviceCountByOwnershipType);
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -473,7 +501,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
@Override
public List<DetailedDeviceEntry> getDevicesWithDetails(FilterSet filterSet)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
Map<String, Object> filters = this.extractDatabaseFiltersFromBean(filterSet);
@ -486,7 +514,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
con = this.getConnection();
String sql;
sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " +
"DEVICES_VIEW_1 WHERE TENANT_ID = ?";
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?";
// appending filters to support advanced filtering options
// [1] appending filter columns, if exist
if (filters != null && filters.size() > 0) {
@ -521,6 +549,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
filteredDeviceWithDetails.setConnectivityStatus(rs.getString("CONNECTIVITY_STATUS"));
filteredDevicesWithDetails.add(filteredDeviceWithDetails);
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -529,7 +560,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
@Override
public List<DetailedDeviceEntry> getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException {
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException {
if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) {
throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty.");
@ -546,7 +577,8 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
con = this.getConnection();
String sql;
sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " +
"DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 +
" WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
// appending filters to support advanced filtering options
// [1] appending filter columns, if exist
if (filters != null && filters.size() > 0) {
@ -582,6 +614,9 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
filteredDeviceWithDetails.setConnectivityStatus(rs.getString("CONNECTIVITY_STATUS"));
filteredDevicesWithDetails.add(filteredDeviceWithDetails);
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -604,7 +639,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD
String potentialVulnerability = filterSet.getPotentialVulnerability();
if (potentialVulnerability != null) {
if (GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT.equals(potentialVulnerability) ||
GadgetDataServiceDAOConstants.PotentialVulnerability.UNMONITORED.equals(potentialVulnerability)) {
GadgetDataServiceDAOConstants.PotentialVulnerability.UNMONITORED.equals(potentialVulnerability)) {
if (GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT.equals(potentialVulnerability)) {
filters.put("IS_COMPLIANT", 0);
} else {

@ -18,56 +18,67 @@
package org.wso2.carbon.device.mgt.analytics.dashboard.dao;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.*;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.DataAccessLayerException;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import java.sql.SQLException;
import java.util.List;
public interface GadgetDataServiceDAO {
DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) throws InvalidParameterValueException, SQLException;
/**
* This method is used to get device count based on a defined filter set.
* @param filterSet An abstract representation of possible filtering options.
* if this value is simply "null" or no values are set for the defined filtering options,
* this method would return total device count in the system
* wrapped with in the defined return format.
* @return An object of type DeviceCountByGroupEntry.
* @throws InvalidParameterValueException This can occur if and only if potentialVulnerability value of filterSet
* is set with some value other than "NON_COMPLIANT" or "UNMONITORED".
* @throws DataAccessLayerException This can occur due to errors connecting to database,
* executing SQL query and retrieving data.
*/
DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet)
throws InvalidParameterValueException, DataAccessLayerException;
DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException;
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException;
/**
* Method to get total device count from a particular tenant.
*
* @return Total device count.
*/
DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException;
DeviceCountByGroupEntry getTotalDeviceCount() throws DataAccessLayerException;
List<DeviceCountByGroupEntry> getDeviceCountsByConnectivityStatuses() throws SQLException;
List<DeviceCountByGroupEntry> getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException;
List<DeviceCountByGroupEntry> getDeviceCountsByPotentialVulnerabilities() throws SQLException;
List<DeviceCountByGroupEntry> getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException;
PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
List<DeviceCountByGroupEntry> getDeviceCountsByPlatforms(FilterSet filterSet)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
List<DeviceCountByGroupEntry> getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException;
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException;
List<DeviceCountByGroupEntry> getDeviceCountsByOwnershipTypes(FilterSet filterSet)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
List<DeviceCountByGroupEntry> getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException;
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException;
PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet, int startIndex, int resultCount) throws InvalidParameterValueException, SQLException;
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, DataAccessLayerException;
List<DetailedDeviceEntry> getDevicesWithDetails(FilterSet filterSet)
throws InvalidParameterValueException, SQLException;
throws InvalidParameterValueException, DataAccessLayerException;
List<DetailedDeviceEntry> getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException;
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException;
}

@ -20,6 +20,17 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao;
public final class GadgetDataServiceDAOConstants {
public static class DatabaseView {
public static final String DEVICES_VIEW_1 = "DEVICES_WITH_POLICY_COMPLIANCE_STATUS";
public static final String DEVICES_VIEW_2 = "DEVICES_WITH_NON_COMPLIANT_FEATURES";
private DatabaseView() {
throw new AssertionError();
}
}
public static class PotentialVulnerability {
// These constants do not hold actual database values

@ -90,7 +90,11 @@ public class GadgetDataServiceDAOFactory {
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
"transaction is already active is a sign of improper transaction handling.");
}
conn = dataSource.getConnection();
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
}
currentConnection.set(conn);
}
@ -109,7 +113,7 @@ public class GadgetDataServiceDAOFactory {
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods.");
"'beginTransaction'/'openConnection' methods.");
}
try {
conn.close();
@ -130,7 +134,7 @@ public class GadgetDataServiceDAOFactory {
DataSource dataSource = null;
if (config == null) {
throw new RuntimeException(
"Device Management Repository data source configuration " + "is null and " +
"Device Management Repository data source configuration is null and " +
"thus, is not initialized.");
}
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();

@ -0,0 +1,80 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception;
/**
* Custom exception class for communicating data access layer issues
* relevant to Gadget Data Service DAO layer.
* (In this particular instance, SQL exceptions related to database access).
*/
public class DataAccessLayerException extends Exception {
private String errorMessage;
private static final long serialVersionUID = 2021891706072918864L;
/**
* Constructs a new exception with the specific error message and nested exception.
* @param errorMessage specific error message.
* @param nestedException Nested exception.
*/
@SuppressWarnings("unused")
public DataAccessLayerException(String errorMessage, Exception nestedException) {
super(errorMessage, nestedException);
setErrorMessage(errorMessage);
}
/**
* Constructs a new exception with the specific error message and cause.
* @param errorMessage Specific error message.
* @param cause Cause of this exception.
*/
@SuppressWarnings("unused")
public DataAccessLayerException(String errorMessage, Throwable cause) {
super(errorMessage, cause);
setErrorMessage(errorMessage);
}
/**
* Constructs a new exception with the specific error message.
* @param errorMessage Specific error message.
*/
public DataAccessLayerException(String errorMessage) {
super(errorMessage);
setErrorMessage(errorMessage);
}
/**
* Constructs a new exception with the specific error message and cause.
* @param cause Cause of this exception.
*/
@SuppressWarnings("unused")
public DataAccessLayerException(Throwable cause) {
super(cause);
}
@SuppressWarnings("unused")
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}

@ -29,7 +29,6 @@ public class InvalidParameterValueException extends Exception {
/**
* Constructs a new exception with the specific error message and nested exception.
*
* @param errorMessage specific error message.
* @param nestedException Nested exception.
*/
@ -41,7 +40,6 @@ public class InvalidParameterValueException extends Exception {
/**
* Constructs a new exception with the specific error message and cause.
*
* @param errorMessage Specific error message.
* @param cause Cause of this exception.
*/
@ -53,7 +51,6 @@ public class InvalidParameterValueException extends Exception {
/**
* Constructs a new exception with the specific error message.
*
* @param errorMessage Specific error message.
*/
public InvalidParameterValueException(String errorMessage) {
@ -63,7 +60,6 @@ public class InvalidParameterValueException extends Exception {
/**
* Constructs a new exception with the specific error message and cause.
*
* @param cause Cause of this exception.
*/
@SuppressWarnings("unused")

@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.AbstractGadgetDataServiceDAO;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOConstants;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.DataAccessLayerException;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
@ -39,7 +41,7 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
@Override
public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
if (startIndex < 0) {
throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that.");
@ -57,8 +59,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
int totalRecordsCount = 0;
try {
con = this.getConnection();
String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " +
"WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC LIMIT ?, ?";
String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? GROUP BY FEATURE_CODE " +
"ORDER BY DEVICE_COUNT DESC LIMIT ?, ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, startIndex);
@ -76,8 +79,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
filteredNonCompliantDeviceCountsByFeatures.add(filteredNonCompliantDeviceCountByFeature);
}
// fetching total records count
sql = "SELECT COUNT(FEATURE_CODE) AS NON_COMPLIANT_FEATURE_COUNT FROM " +
"(SELECT DISTINCT FEATURE_CODE FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE";
sql = "SELECT COUNT(FEATURE_CODE) AS NON_COMPLIANT_FEATURE_COUNT FROM (SELECT DISTINCT FEATURE_CODE FROM " +
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ?) " +
"NON_COMPLIANT_FEATURE_CODE";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -88,6 +92,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
while (rs.next()) {
totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -99,7 +106,7 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
@Override
public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
if (startIndex < 0) {
throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that.");
@ -128,7 +135,8 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
}
}
sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " +
"DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC LIMIT ?, ?";
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ? " +
advancedSqlFiltering + "ORDER BY DEVICE_ID ASC LIMIT ?, ?";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -163,7 +171,8 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
}
// fetching total records count
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?";
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -174,6 +183,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
while (rs.next()) {
totalRecordsCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -185,8 +197,8 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
@Override
public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, DataAccessLayerException {
if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) {
throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty.");
@ -219,8 +231,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
}
}
sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " +
"DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering +
"ORDER BY DEVICE_ID ASC LIMIT ?, ?";
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 +
" WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering +
"ORDER BY DEVICE_ID ASC LIMIT ?, ?";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -256,8 +269,8 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
}
// fetching total records count
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " +
"WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -269,6 +282,9 @@ public class GenericGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDA
while (rs.next()) {
totalRecordsCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}

@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.AbstractGadgetDataServiceDAO;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOConstants;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.DataAccessLayerException;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
@ -39,7 +41,7 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
@Override
public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
if (startIndex < 0) {
throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that.");
@ -57,8 +59,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
int totalRecordsCount = 0;
try {
con = this.getConnection();
String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " +
"WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC " +
String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC " +
"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -78,7 +80,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
// fetching total records count
sql = "SELECT COUNT(FEATURE_CODE) AS NON_COMPLIANT_FEATURE_COUNT FROM " +
"(SELECT DISTINCT FEATURE_CODE FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE";
"(SELECT DISTINCT FEATURE_CODE FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 +
" WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -89,6 +92,9 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
while (rs.next()) {
totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -100,7 +106,7 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
@Override
public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
if (startIndex < 0) {
throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that.");
@ -129,8 +135,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
}
sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " +
"DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC " +
"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ? " +
advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -165,7 +171,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
// fetching total records count
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?";
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -176,6 +183,9 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
while (rs.next()) {
totalRecordsCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -187,8 +197,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
@Override
public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, DataAccessLayerException {
if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) {
throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty.");
@ -221,8 +231,9 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
}
sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " +
"DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering +
"ORDER BY DEVICE_ID ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " +
advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -258,8 +269,8 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
// fetching total records count
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " +
"WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -271,6 +282,9 @@ public class MSSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
while (rs.next()) {
totalRecordsCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}

@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.AbstractGadgetDataServiceDAO;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOConstants;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.DataAccessLayerException;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
@ -39,7 +41,7 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
@Override
public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
if (startIndex < 0) {
throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that.");
@ -58,8 +60,10 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
try {
con = this.getConnection();
String sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT FEATURE_CODE, COUNT(DEVICE_ID) " +
"AS DEVICE_COUNT FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY " +
"DEVICE_COUNT DESC) rs) WHERE offset >= ? AND ROWNUM <= ?";
"AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 +
" WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC) rs) " +
"WHERE offset >= ? AND ROWNUM <= ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, startIndex);
@ -78,7 +82,8 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
// fetching total records count
sql = "SELECT COUNT(FEATURE_CODE) AS NON_COMPLIANT_FEATURE_COUNT FROM " +
"(SELECT DISTINCT FEATURE_CODE FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE";
"(SELECT DISTINCT FEATURE_CODE FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 +
" WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -89,6 +94,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
while (rs.next()) {
totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -100,7 +108,7 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
@Override
public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
if (startIndex < 0) {
throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that.");
@ -129,8 +137,10 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
}
sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, " +
"OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering +
"ORDER BY DEVICE_ID ASC) rs) WHERE offset >= ? AND ROWNUM <= ?";
"OWNERSHIP, CONNECTIVITY_STATUS FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 +
" WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC) rs) " +
"WHERE offset >= ? AND ROWNUM <= ?";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -165,7 +175,8 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
// fetching total records count
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?";
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -176,6 +187,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
while (rs.next()) {
totalRecordsCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -187,8 +201,8 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
@Override
public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, DataAccessLayerException {
if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) {
throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty.");
@ -221,8 +235,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
}
sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, " +
"OWNERSHIP, CONNECTIVITY_STATUS FROM DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ? " +
advancedSqlFiltering + "ORDER BY DEVICE_ID ASC) rs) WHERE offset >= ? AND ROWNUM <= ?";
"OWNERSHIP, CONNECTIVITY_STATUS FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 +
" WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering +
"ORDER BY DEVICE_ID ASC) rs) WHERE offset >= ? AND ROWNUM <= ?";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -258,8 +273,8 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
}
// fetching total records count
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " +
"WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -271,6 +286,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO
while (rs.next()) {
totalRecordsCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}

@ -20,9 +20,11 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.dao.impl;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.AbstractGadgetDataServiceDAO;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOConstants;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.DataAccessLayerException;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
@ -39,7 +41,7 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
@Override
public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
if (startIndex < 0) {
throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that.");
@ -57,8 +59,10 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
int totalRecordsCount = 0;
try {
con = this.getConnection();
String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " +
"WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC OFFSET ? LIMIT ?";
String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? GROUP BY FEATURE_CODE " +
"ORDER BY DEVICE_COUNT DESC OFFSET ? LIMIT ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, startIndex);
@ -77,7 +81,8 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
}
// fetching total records count
sql = "SELECT COUNT(FEATURE_CODE) AS NON_COMPLIANT_FEATURE_COUNT FROM " +
"(SELECT DISTINCT FEATURE_CODE FROM DEVICES_VIEW_2 WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE";
"(SELECT DISTINCT FEATURE_CODE FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 +
" WHERE TENANT_ID = ?) NON_COMPLIANT_FEATURE_CODE";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -88,6 +93,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
while (rs.next()) {
totalRecordsCount = rs.getInt("NON_COMPLIANT_FEATURE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -99,7 +107,7 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
@Override
public PaginationResult getDevicesWithDetails(FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
if (startIndex < 0) {
throw new InvalidParameterValueException("Start index should be equal to 0 or greater than that.");
@ -128,7 +136,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
}
}
sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " +
"DEVICES_VIEW_1 WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? LIMIT ?";
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ? " +
advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? LIMIT ?";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -163,7 +173,8 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
}
// fetching total records count
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_1 WHERE TENANT_ID = ?";
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -174,6 +185,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
while (rs.next()) {
totalRecordsCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -185,8 +199,8 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
@Override
public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, DataAccessLayerException {
if (nonCompliantFeatureCode == null || nonCompliantFeatureCode.isEmpty()) {
throw new InvalidParameterValueException("Non-compliant feature code should not be either null or empty.");
@ -219,8 +233,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
}
}
sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " +
"DEVICES_VIEW_2 WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering +
"ORDER BY DEVICE_ID ASC OFFSET ? LIMIT ?";
GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " +
advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? LIMIT ?";
stmt = con.prepareStatement(sql);
// [2] appending filter column values, if exist
stmt.setInt(1, tenantId);
@ -256,8 +271,8 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
}
// fetching total records count
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM DEVICES_VIEW_2 " +
"WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
sql = "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.
DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -269,6 +284,9 @@ public class PostgreSQLGadgetDataServiceDAOImpl extends AbstractGadgetDataServic
while (rs.next()) {
totalRecordsCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DataAccessLayerException("Error in either getting database connection, " +
"running SQL query or fetching results.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}

@ -20,7 +20,10 @@ package org.wso2.carbon.device.mgt.analytics.dashboard.impl;
import org.wso2.carbon.device.mgt.analytics.dashboard.GadgetDataService;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.GadgetDataServiceDAOFactory;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.*;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DetailedDeviceEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.DeviceCountByGroupEntry;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.bean.FilterSet;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.DataAccessLayerException;
import org.wso2.carbon.device.mgt.analytics.dashboard.dao.exception.InvalidParameterValueException;
import org.wso2.carbon.device.mgt.common.PaginationResult;
@ -33,11 +36,14 @@ import java.util.List;
public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet) throws InvalidParameterValueException, SQLException {
public DeviceCountByGroupEntry getDeviceCount(FilterSet filterSet)
throws InvalidParameterValueException, DataAccessLayerException {
DeviceCountByGroupEntry filteredDeviceCount;
try {
GadgetDataServiceDAOFactory.openConnection();
filteredDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getDeviceCount(filterSet);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -45,13 +51,15 @@ public class GadgetDataServiceImpl implements GadgetDataService {
}
@Override
public DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode, FilterSet filterSet)
throws InvalidParameterValueException, SQLException {
public DeviceCountByGroupEntry getFeatureNonCompliantDeviceCount(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException {
DeviceCountByGroupEntry featureNonCompliantDeviceCount;
try {
GadgetDataServiceDAOFactory.openConnection();
featureNonCompliantDeviceCount = GadgetDataServiceDAOFactory.
getGadgetDataServiceDAO().getFeatureNonCompliantDeviceCount(nonCompliantFeatureCode, filterSet);
getGadgetDataServiceDAO().getFeatureNonCompliantDeviceCount(nonCompliantFeatureCode, filterSet);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -59,11 +67,13 @@ public class GadgetDataServiceImpl implements GadgetDataService {
}
@Override
public DeviceCountByGroupEntry getTotalDeviceCount() throws SQLException {
public DeviceCountByGroupEntry getTotalDeviceCount() throws DataAccessLayerException {
DeviceCountByGroupEntry totalDeviceCount;
try {
GadgetDataServiceDAOFactory.openConnection();
totalDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getTotalDeviceCount();
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -71,12 +81,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
}
@Override
public List<DeviceCountByGroupEntry> getDeviceCountsByConnectivityStatuses() throws SQLException {
public List<DeviceCountByGroupEntry> getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException {
List<DeviceCountByGroupEntry> deviceCountsByConnectivityStatuses;
try {
GadgetDataServiceDAOFactory.openConnection();
deviceCountsByConnectivityStatuses = GadgetDataServiceDAOFactory.
getGadgetDataServiceDAO().getDeviceCountsByConnectivityStatuses();
getGadgetDataServiceDAO().getDeviceCountsByConnectivityStatuses();
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -84,12 +96,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
}
@Override
public List<DeviceCountByGroupEntry> getDeviceCountsByPotentialVulnerabilities() throws SQLException {
public List<DeviceCountByGroupEntry> getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException {
List<DeviceCountByGroupEntry> deviceCountsByPotentialVulnerabilities;
try {
GadgetDataServiceDAOFactory.openConnection();
deviceCountsByPotentialVulnerabilities = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getDeviceCountsByPotentialVulnerabilities();
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -98,12 +112,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount)
throws SQLException, InvalidParameterValueException {
throws InvalidParameterValueException, DataAccessLayerException {
PaginationResult paginationResult;
try {
GadgetDataServiceDAOFactory.openConnection();
paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getNonCompliantDeviceCountsByFeatures(startIndex, resultCount);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -112,12 +128,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public List<DeviceCountByGroupEntry> getDeviceCountsByPlatforms(FilterSet filterSet)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
List<DeviceCountByGroupEntry> deviceCountsByPlatforms;
try {
GadgetDataServiceDAOFactory.openConnection();
deviceCountsByPlatforms = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getDeviceCountsByPlatforms(filterSet);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -126,12 +144,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public List<DeviceCountByGroupEntry> getFeatureNonCompliantDeviceCountsByPlatforms(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException {
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException {
List<DeviceCountByGroupEntry> featureNonCompliantDeviceCountsByPlatforms;
try {
GadgetDataServiceDAOFactory.openConnection();
featureNonCompliantDeviceCountsByPlatforms = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getFeatureNonCompliantDeviceCountsByPlatforms(nonCompliantFeatureCode, filterSet);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -140,12 +160,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public List<DeviceCountByGroupEntry> getDeviceCountsByOwnershipTypes(FilterSet filterSet)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
List<DeviceCountByGroupEntry> deviceCountsByOwnershipTypes;
try {
GadgetDataServiceDAOFactory.openConnection();
deviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getDeviceCountsByOwnershipTypes(filterSet);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -155,12 +177,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public List<DeviceCountByGroupEntry>
getFeatureNonCompliantDeviceCountsByOwnershipTypes(String nonCompliantFeatureCode,
FilterSet filterSet) throws SQLException, InvalidParameterValueException {
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException {
List<DeviceCountByGroupEntry> featureNonCompliantDeviceCountsByOwnershipTypes;
try {
GadgetDataServiceDAOFactory.openConnection();
featureNonCompliantDeviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getFeatureNonCompliantDeviceCountsByOwnershipTypes(nonCompliantFeatureCode, filterSet);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -169,12 +193,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public PaginationResult getDevicesWithDetails(FilterSet filterSet,
int startIndex, int resultCount) throws InvalidParameterValueException, SQLException {
int startIndex, int resultCount) throws InvalidParameterValueException, DataAccessLayerException {
PaginationResult paginationResult;
try {
GadgetDataServiceDAOFactory.openConnection();
paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getDevicesWithDetails(filterSet, startIndex, resultCount);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -183,13 +209,15 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public PaginationResult getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, SQLException {
FilterSet filterSet, int startIndex, int resultCount)
throws InvalidParameterValueException, DataAccessLayerException {
PaginationResult paginationResult;
try {
GadgetDataServiceDAOFactory.openConnection();
paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, filterSet, startIndex, resultCount);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -198,12 +226,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public List<DetailedDeviceEntry> getDevicesWithDetails(FilterSet filterSet)
throws InvalidParameterValueException, SQLException {
throws InvalidParameterValueException, DataAccessLayerException {
List<DetailedDeviceEntry> devicesWithDetails;
try {
GadgetDataServiceDAOFactory.openConnection();
devicesWithDetails = GadgetDataServiceDAOFactory.
getGadgetDataServiceDAO().getDevicesWithDetails(filterSet);
getGadgetDataServiceDAO().getDevicesWithDetails(filterSet);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}
@ -212,12 +242,14 @@ public class GadgetDataServiceImpl implements GadgetDataService {
@Override
public List<DetailedDeviceEntry> getFeatureNonCompliantDevicesWithDetails(String nonCompliantFeatureCode,
FilterSet filterSet) throws InvalidParameterValueException, SQLException {
FilterSet filterSet) throws InvalidParameterValueException, DataAccessLayerException {
List<DetailedDeviceEntry> featureNonCompliantDevicesWithDetails;
try {
GadgetDataServiceDAOFactory.openConnection();
featureNonCompliantDevicesWithDetails = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().
getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, filterSet);
} catch (SQLException e) {
throw new DataAccessLayerException("Error in opening database connection.", e);
} finally {
GadgetDataServiceDAOFactory.closeConnection();
}

@ -20,14 +20,17 @@ package org.wso2.carbon.device.mgt.jaxrs.api;
import io.swagger.annotations.*;
import org.wso2.carbon.apimgt.annotations.api.*;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
* Device information related operations.
@ -60,6 +63,26 @@ public interface DeviceInformation {
@ApiParam(name = "id", value = "Provide the device identifier", required = true)
@PathParam("id") String id);
@POST
@Path("{list}")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Get devices information from the supplied device identifies",
notes = "This will return device information such as CPU usage, memory usage etc for supplied device " +
"identifiers.",
response = DeviceInfo.class,
responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = ""),
@ApiResponse(code = 400, message = ""),
@ApiResponse(code = 400, message = ""),
@ApiResponse(code = 500, message = "Internal Server Error")
})
Response getDevicesInfo(@ApiParam(name = "deviceIdentifiers", value = "List of device identifiers",
required = true) List<DeviceIdentifier> deviceIdentifiers);
@GET
@Path("location/{type}/{id}")
@ApiOperation(

@ -30,9 +30,11 @@ import org.wso2.carbon.device.mgt.jaxrs.api.DeviceInformation;
import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import java.util.List;
@SuppressWarnings("NonJaxWsWebServices")
public class DeviceInformationImpl implements DeviceInformation {
@ -59,6 +61,23 @@ public class DeviceInformationImpl implements DeviceInformation {
}
@POST
@Path("list")
public Response getDevicesInfo(List<DeviceIdentifier> deviceIdentifiers) {
DeviceInformationManager informationManager;
List<DeviceInfo> deviceInfos;
try {
informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService();
deviceInfos = informationManager.getDevicesInfo(deviceIdentifiers);
} catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the device information.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
return Response.status(Response.Status.OK).entity(deviceInfos).build();
}
@GET
@Path("location/{type}/{id}")
public Response getDeviceLocation(@PathParam("type") String type, @PathParam("id") String id) {

@ -876,6 +876,13 @@
<method>GET</method>
</Permission>
<Permission>
<name>Device Information</name>
<path>/device-mgt/admin/information/list</path>
<url>/information/list</url>
<method>POST</method>
</Permission>
<Permission>
<name>Device Search</name>
<path>/device-mgt/admin/search</path>

@ -23,6 +23,8 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
import java.util.List;
/**
* This class will manage the storing of device details related generic information such as cpu/memory utilization, battery level,
* plugged in to a power source or operation on battery.
@ -47,6 +49,14 @@ public interface DeviceInformationManager {
*/
DeviceInfo getDeviceInfo(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException;
/**
* This method will return device information for the supplied devices list.
* @param deviceIdentifiers
* @return List of device info objects
* @throws DeviceDetailsMgtException
*/
List<DeviceInfo> getDevicesInfo(List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException;
/**
* This method will manage storing the device location as latitude, longitude, address, zip, country etc..
* @param deviceLocation - Device location object.

@ -35,6 +35,10 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDA
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DeviceInformationManagerImpl implements DeviceInformationManager {
@ -97,6 +101,43 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
}
}
@Override
public List<DeviceInfo> getDevicesInfo(List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException {
List<DeviceInfo> deviceInfos = new ArrayList<>();
Map<String, DeviceIdentifier> identifierMap = new HashMap<>();
for (DeviceIdentifier identifier : deviceIdentifiers) {
identifierMap.put(identifier.getId(), identifier);
}
try {
List<Integer> deviceIds = new ArrayList<>();
List<Device> devices = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider().getAllDevices();
for (Device device : devices) {
if (identifierMap.containsKey(device.getDeviceIdentifier()) &&
device.getType().equals(identifierMap.get(device.getDeviceIdentifier()))) {
deviceIds.add(device.getId());
}
}
DeviceManagementDAOFactory.openConnection();
for(Integer id : deviceIds) {
DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(id);
deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(id));
deviceInfos.add(deviceInfo);
}
} catch (SQLException e) {
throw new DeviceDetailsMgtException("SQL error occurred while retrieving devices from database.", e);
} catch (DeviceManagementException e) {
throw new DeviceDetailsMgtException("Exception occurred while retrieving the devices.", e);
} catch (DeviceDetailsMgtDAOException e) {
throw new DeviceDetailsMgtException("Exception occurred while retrieving devices details.", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return deviceInfos;
}
@Override
public void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtException {

@ -513,7 +513,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY (
-- DASHBOARD RELATED VIEWS --
CREATE VIEW DEVICES_VIEW_1 AS
CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS
SELECT
DEVICE_INFO.DEVICE_ID,
DEVICE_INFO.DEVICE_IDENTIFICATION,
@ -542,7 +542,7 @@ FROM DM_POLICY_COMPLIANCE_STATUS) DEVICE_WITH_POLICY_INFO
ON DEVICE_INFO.DEVICE_ID = DEVICE_WITH_POLICY_INFO.DEVICE_ID
ORDER BY DEVICE_INFO.DEVICE_ID;
CREATE VIEW DEVICES_VIEW_2 AS
CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS
SELECT
DM_DEVICE.ID AS DEVICE_ID,
DM_DEVICE.DEVICE_IDENTIFICATION,

@ -505,7 +505,7 @@ CREATE INDEX FK_DM_DEVICE_DETAILS_DEVICE_idx ON DM_DEVICE_DETAIL (DEVICE_ID ASC)
-- DASHBOARD RELATED VIEWS --
CREATE VIEW DEVICES_VIEW_1 AS
CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS
SELECT TOP 100 PERCENT
DEVICE_INFO.DEVICE_ID,
DEVICE_INFO.DEVICE_IDENTIFICATION,
@ -537,7 +537,7 @@ ON DEVICE_INFO.DEVICE_ID = DEVICE_WITH_POLICY_INFO.DEVICE_ID
ORDER BY DEVICE_INFO.DEVICE_ID;
GO
CREATE VIEW DEVICES_VIEW_2 AS
CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS
SELECT TOP 100 PERCENT
DM_DEVICE.ID AS DEVICE_ID,
DM_DEVICE.DEVICE_IDENTIFICATION,

@ -543,7 +543,7 @@ POLICY_ID,
STATUS AS IS_COMPLIANT
FROM DM_POLICY_COMPLIANCE_STATUS;
CREATE VIEW DEVICES_VIEW_1 AS
CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS
SELECT
DEVICE_INFO_VIEW.DEVICE_ID,
DEVICE_INFO_VIEW.DEVICE_IDENTIFICATION,
@ -560,7 +560,7 @@ DEVICE_WITH_POLICY_INFO_VIEW
ON DEVICE_INFO_VIEW.DEVICE_ID = DEVICE_WITH_POLICY_INFO_VIEW.DEVICE_ID
ORDER BY DEVICE_INFO_VIEW.DEVICE_ID;
CREATE VIEW DEVICES_VIEW_2 AS
CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS
SELECT
DM_DEVICE.ID AS DEVICE_ID,
DM_DEVICE.DEVICE_IDENTIFICATION,

@ -851,7 +851,7 @@ WHEN (NEW.ID IS NULL)
-- DASHBOARD RELATED VIEWS --
CREATE VIEW DEVICES_VIEW_1 AS
CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS
SELECT
DEVICE_INFO.DEVICE_ID,
DEVICE_INFO.DEVICE_IDENTIFICATION,
@ -880,7 +880,7 @@ FROM DM_POLICY_COMPLIANCE_STATUS) DEVICE_WITH_POLICY_INFO
ON DEVICE_INFO.DEVICE_ID = DEVICE_WITH_POLICY_INFO.DEVICE_ID
ORDER BY DEVICE_INFO.DEVICE_ID;
CREATE VIEW DEVICES_VIEW_2 AS
CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS
SELECT
DM_DEVICE.ID AS DEVICE_ID,
DM_DEVICE.DEVICE_IDENTIFICATION,

@ -432,7 +432,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL (
-- DASHBOARD RELATED VIEWS --
CREATE VIEW DEVICES_VIEW_1 AS
CREATE VIEW DEVICES_WITH_POLICY_COMPLIANCE_STATUS AS
SELECT
DEVICE_INFO.DEVICE_ID,
DEVICE_INFO.DEVICE_IDENTIFICATION,
@ -461,7 +461,7 @@ FROM DM_POLICY_COMPLIANCE_STATUS) DEVICE_WITH_POLICY_INFO
ON DEVICE_INFO.DEVICE_ID = DEVICE_WITH_POLICY_INFO.DEVICE_ID
ORDER BY DEVICE_INFO.DEVICE_ID;
CREATE VIEW DEVICES_VIEW_2 AS
CREATE VIEW DEVICES_WITH_NON_COMPLIANT_FEATURES AS
SELECT
DM_DEVICE.ID AS DEVICE_ID,
DM_DEVICE.DEVICE_IDENTIFICATION,

Loading…
Cancel
Save