From 51c18d2108f3b9e51ccc684d0ecd242150de1ee4 Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Fri, 16 Jun 2017 16:46:30 +0530 Subject: [PATCH 1/3] Added pagination support for group-management for major db engines. --- .../core/dao/GroupManagementDAOFactory.java | 53 +++-- ...DAOImpl.java => AbstractGroupDAOImpl.java} | 169 +-------------- .../dao/impl/group/GenericGroupDAOImpl.java | 204 ++++++++++++++++++ .../dao/impl/group/OracleGroupDAOImpl.java | 204 ++++++++++++++++++ .../impl/group/PostgreSQLGroupDAOImpl.java | 204 ++++++++++++++++++ .../dao/impl/group/SQLServerGroupDAOImpl.java | 204 ++++++++++++++++++ 6 files changed, 855 insertions(+), 183 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/{GroupDAOImpl.java => AbstractGroupDAOImpl.java} (78%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/OracleGroupDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/SQLServerGroupDAOImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupManagementDAOFactory.java index 3626c34b197..a962f91773d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupManagementDAOFactory.java @@ -20,11 +20,16 @@ package org.wso2.carbon.device.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; import org.wso2.carbon.device.mgt.common.TransactionManagementException; +import org.wso2.carbon.device.mgt.common.UnsupportedDatabaseEngineException; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; -import org.wso2.carbon.device.mgt.core.dao.impl.GroupDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.impl.group.GenericGroupDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.impl.group.OracleGroupDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.impl.group.PostgreSQLGroupDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.impl.group.SQLServerGroupDAOImpl; import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; import javax.sql.DataSource; @@ -40,6 +45,7 @@ public class GroupManagementDAOFactory { private static final Log log = LogFactory.getLog(GroupManagementDAOFactory.class); private static DataSource dataSource; + private static String databaseEngine; private static ThreadLocal currentConnection = new ThreadLocal<>(); /** @@ -48,25 +54,40 @@ public class GroupManagementDAOFactory { * @return instance of GroupDAO implementation */ public static GroupDAO getGroupDAO() { - return new GroupDAOImpl(); + if (databaseEngine != null) { + switch (databaseEngine) { + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: + return new OracleGroupDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: + return new SQLServerGroupDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: + return new PostgreSQLGroupDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericGroupDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); } - /** - * Initialize factory with datasource configs - * - * @param config data source configuration - */ public static void init(DataSourceConfig config) { dataSource = resolveDataSource(config); + try { + databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); + } catch (SQLException e) { + log.error("Error occurred while retrieving config.datasource connection", e); + } } - /** - * Initialize factory with existing datasource - * - * @param dtSource an existing datasource - */ public static void init(DataSource dtSource) { dataSource = dtSource; + try { + databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); + } catch (SQLException e) { + log.error("Error occurred while retrieving config.datasource connection", e); + } } /** @@ -129,8 +150,8 @@ public class GroupManagementDAOFactory { Connection conn = currentConnection.get(); if (conn == null) { throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + - "This might have ideally been caused by not properly initiating the transaction via " + - "'beginTransaction'/'openConnection' methods"); + "This might have ideally been caused by not properly initiating " + + "the transaction via 'beginTransaction'/'openConnection' methods"); } try { conn.commit(); @@ -146,8 +167,8 @@ public class GroupManagementDAOFactory { Connection conn = currentConnection.get(); if (conn == null) { throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + - "This might have ideally been caused by not properly initiating the transaction via " + - "'beginTransaction'/'openConnection' methods"); + "This might have ideally been caused by not properly initiating " + + "the transaction via 'beginTransaction'/'openConnection' methods"); } try { conn.rollback(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java similarity index 78% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GroupDAOImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 7d9947a421b..dc8d767862c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -18,13 +18,11 @@ package org.wso2.carbon.device.mgt.core.dao.impl; -import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; import java.sql.Connection; @@ -37,7 +35,7 @@ import java.util.List; /** * This class represents implementation of GroupDAO */ -public class GroupDAOImpl implements GroupDAO { +public abstract class AbstractGroupDAOImpl implements GroupDAO { @Override public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException { @@ -47,7 +45,7 @@ public class GroupDAOImpl implements GroupDAO { try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) VALUES (?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[]{"ID"}); + stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setString(1, deviceGroup.getDescription()); stmt.setString(2, deviceGroup.getName()); stmt.setString(3, deviceGroup.getOwner()); @@ -179,126 +177,6 @@ public class GroupDAOImpl implements GroupDAO { return deviceGroupBuilders; } - @Override - public List getGroups(GroupPaginationRequest request, int tenantId) - throws GroupManagementDAOException { - PreparedStatement stmt = null; - ResultSet resultSet = null; - List deviceGroupList = null; - - String groupName = request.getGroupName(); - boolean hasGroupName = false; - String owner = request.getOwner(); - boolean hasOwner = false; - boolean hasLimit = request.getRowCount() != 0; - - try { - Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; - if (groupName != null && !groupName.isEmpty()) { - sql += " AND GROUP_NAME LIKE ?"; - hasGroupName = true; - } - if (owner != null && !owner.isEmpty()) { - sql += " AND OWNER LIKE ?"; - hasOwner = true; - } - if (hasLimit) { - sql += " LIMIT ?, ?"; - } - - int paramIndex = 1; - stmt = conn.prepareStatement(sql); - stmt.setInt(paramIndex++, tenantId); - if (hasGroupName) { - stmt.setString(paramIndex++, groupName + "%"); - } - if (hasOwner) { - stmt.setString(paramIndex++, owner + "%"); - } - if (hasLimit) { - stmt.setInt(paramIndex++, request.getStartIndex()); - stmt.setInt(paramIndex, request.getRowCount()); - } - resultSet = stmt.executeQuery(); - deviceGroupList = new ArrayList<>(); - while (resultSet.next()) { - deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, resultSet); - } - return deviceGroupList; - } - - @Override - public List getGroups(GroupPaginationRequest request, List deviceGroupIds, - int tenantId) throws GroupManagementDAOException { - int deviceGroupIdsCount = deviceGroupIds.size(); - if (deviceGroupIdsCount == 0) { - return new ArrayList<>(); - } - PreparedStatement stmt = null; - ResultSet resultSet = null; - List deviceGroupList = null; - - String groupName = request.getGroupName(); - boolean hasGroupName = false; - String owner = request.getOwner(); - boolean hasOwner = false; - boolean hasLimit = request.getRowCount() != 0; - - try { - Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; - if (groupName != null && !groupName.isEmpty()) { - sql += " AND GROUP_NAME LIKE ?"; - hasGroupName = true; - } - if (owner != null && !owner.isEmpty()) { - sql += " AND OWNER LIKE ?"; - hasOwner = true; - } - sql += " AND ID IN ("; - for (int i = 0; i < deviceGroupIdsCount; i++) { - sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; - } - sql += ")"; - if (hasLimit) { - sql += " LIMIT ?, ?"; - } - - int paramIndex = 1; - stmt = conn.prepareStatement(sql); - stmt.setInt(paramIndex++, tenantId); - if (hasGroupName) { - stmt.setString(paramIndex++, groupName + "%"); - } - if (hasOwner) { - stmt.setString(paramIndex++, owner + "%"); - } - for (Integer deviceGroupId : deviceGroupIds) { - stmt.setInt(paramIndex++, deviceGroupId); - } - if (hasLimit) { - stmt.setInt(paramIndex++, request.getStartIndex()); - stmt.setInt(paramIndex, request.getRowCount()); - } - resultSet = stmt.executeQuery(); - deviceGroupList = new ArrayList<>(); - while (resultSet.next()) { - deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, resultSet); - } - return deviceGroupList; - } - @Override public List getGroups(int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; @@ -495,49 +373,6 @@ public class GroupDAOImpl implements GroupDAO { } } - @Override - public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) - throws GroupManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - List devices = null; - try { - conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + - "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + - "FROM " + - "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + - " DM_DEVICE d, (" + - "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + - "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + - "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?"; - - stmt = conn.prepareStatement(sql); - stmt.setInt(1, groupId); - stmt.setInt(2, tenantId); - stmt.setInt(3, tenantId); - //noinspection JpaQueryApiInspection - stmt.setInt(4, startIndex); - //noinspection JpaQueryApiInspection - stmt.setInt(5, rowCount); - rs = stmt.executeQuery(); - devices = new ArrayList<>(); - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while retrieving information of all " + - "registered devices", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return devices; - } - @Override public List getRoles(int groupId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java new file mode 100644 index 00000000000..7ad8b9981ee --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2017, 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.core.dao.impl.group; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.impl.AbstractGroupDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This class represents implementation of GroupDAO + */ +public class GenericGroupDAOImpl extends AbstractGroupDAOImpl { + + @Override + public List getGroups(GroupPaginationRequest request, int tenantId) + throws GroupManagementDAOException { + PreparedStatement stmt = null; + ResultSet resultSet = null; + List deviceGroupList = null; + + String groupName = request.getGroupName(); + boolean hasGroupName = false; + String owner = request.getOwner(); + boolean hasOwner = false; + boolean hasLimit = request.getRowCount() != 0; + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; + if (groupName != null && !groupName.isEmpty()) { + sql += " AND GROUP_NAME LIKE ?"; + hasGroupName = true; + } + if (owner != null && !owner.isEmpty()) { + sql += " AND OWNER LIKE ?"; + hasOwner = true; + } + if (hasLimit) { + sql += " LIMIT ?, ?"; + } + + int paramIndex = 1; + stmt = conn.prepareStatement(sql); + stmt.setInt(paramIndex++, tenantId); + if (hasGroupName) { + stmt.setString(paramIndex++, groupName + "%"); + } + if (hasOwner) { + stmt.setString(paramIndex++, owner + "%"); + } + if (hasLimit) { + stmt.setInt(paramIndex++, request.getStartIndex()); + stmt.setInt(paramIndex, request.getRowCount()); + } + resultSet = stmt.executeQuery(); + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return deviceGroupList; + } + + @Override + public List getGroups(GroupPaginationRequest request, List deviceGroupIds, + int tenantId) throws GroupManagementDAOException { + int deviceGroupIdsCount = deviceGroupIds.size(); + if (deviceGroupIdsCount == 0) { + return new ArrayList<>(); + } + PreparedStatement stmt = null; + ResultSet resultSet = null; + List deviceGroupList = null; + + String groupName = request.getGroupName(); + boolean hasGroupName = false; + String owner = request.getOwner(); + boolean hasOwner = false; + boolean hasLimit = request.getRowCount() != 0; + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; + if (groupName != null && !groupName.isEmpty()) { + sql += " AND GROUP_NAME LIKE ?"; + hasGroupName = true; + } + if (owner != null && !owner.isEmpty()) { + sql += " AND OWNER LIKE ?"; + hasOwner = true; + } + sql += " AND ID IN ("; + for (int i = 0; i < deviceGroupIdsCount; i++) { + sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; + } + sql += ")"; + if (hasLimit) { + sql += " LIMIT ?, ?"; + } + + int paramIndex = 1; + stmt = conn.prepareStatement(sql); + stmt.setInt(paramIndex++, tenantId); + if (hasGroupName) { + stmt.setString(paramIndex++, groupName + "%"); + } + if (hasOwner) { + stmt.setString(paramIndex++, owner + "%"); + } + for (Integer deviceGroupId : deviceGroupIds) { + stmt.setInt(paramIndex++, deviceGroupId); + } + if (hasLimit) { + stmt.setInt(paramIndex++, request.getStartIndex()); + stmt.setInt(paramIndex, request.getRowCount()); + } + resultSet = stmt.executeQuery(); + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return deviceGroupList; + } + + @Override + public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) + throws GroupManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "FROM " + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + + " DM_DEVICE d, (" + + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + //noinspection JpaQueryApiInspection + stmt.setInt(4, startIndex); + //noinspection JpaQueryApiInspection + stmt.setInt(5, rowCount); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/OracleGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/OracleGroupDAOImpl.java new file mode 100644 index 00000000000..ecbe0013b71 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/OracleGroupDAOImpl.java @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2017, 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.core.dao.impl.group; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.impl.AbstractGroupDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This class represents implementation of GroupDAO + */ +public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { + + @Override + public List getGroups(GroupPaginationRequest request, int tenantId) + throws GroupManagementDAOException { + PreparedStatement stmt = null; + ResultSet resultSet = null; + List deviceGroupList = null; + + String groupName = request.getGroupName(); + boolean hasGroupName = false; + String owner = request.getOwner(); + boolean hasOwner = false; + boolean hasLimit = request.getRowCount() != 0; + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; + if (groupName != null && !groupName.isEmpty()) { + sql += " AND GROUP_NAME LIKE ?"; + hasGroupName = true; + } + if (owner != null && !owner.isEmpty()) { + sql += " AND OWNER LIKE ?"; + hasOwner = true; + } + if (hasLimit) { + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } + + int paramIndex = 1; + stmt = conn.prepareStatement(sql); + stmt.setInt(paramIndex++, tenantId); + if (hasGroupName) { + stmt.setString(paramIndex++, groupName + "%"); + } + if (hasOwner) { + stmt.setString(paramIndex++, owner + "%"); + } + if (hasLimit) { + stmt.setInt(paramIndex++, request.getStartIndex()); + stmt.setInt(paramIndex, request.getRowCount()); + } + resultSet = stmt.executeQuery(); + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return deviceGroupList; + } + + @Override + public List getGroups(GroupPaginationRequest request, List deviceGroupIds, + int tenantId) throws GroupManagementDAOException { + int deviceGroupIdsCount = deviceGroupIds.size(); + if (deviceGroupIdsCount == 0) { + return new ArrayList<>(); + } + PreparedStatement stmt = null; + ResultSet resultSet = null; + List deviceGroupList = null; + + String groupName = request.getGroupName(); + boolean hasGroupName = false; + String owner = request.getOwner(); + boolean hasOwner = false; + boolean hasLimit = request.getRowCount() != 0; + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; + if (groupName != null && !groupName.isEmpty()) { + sql += " AND GROUP_NAME LIKE ?"; + hasGroupName = true; + } + if (owner != null && !owner.isEmpty()) { + sql += " AND OWNER LIKE ?"; + hasOwner = true; + } + sql += " AND ID IN ("; + for (int i = 0; i < deviceGroupIdsCount; i++) { + sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; + } + sql += ")"; + if (hasLimit) { + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } + + int paramIndex = 1; + stmt = conn.prepareStatement(sql); + stmt.setInt(paramIndex++, tenantId); + if (hasGroupName) { + stmt.setString(paramIndex++, groupName + "%"); + } + if (hasOwner) { + stmt.setString(paramIndex++, owner + "%"); + } + for (Integer deviceGroupId : deviceGroupIds) { + stmt.setInt(paramIndex++, deviceGroupId); + } + if (hasLimit) { + stmt.setInt(paramIndex++, request.getStartIndex()); + stmt.setInt(paramIndex, request.getRowCount()); + } + resultSet = stmt.executeQuery(); + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return deviceGroupList; + } + + @Override + public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) + throws GroupManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "FROM " + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + + " DM_DEVICE d, (" + + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + //noinspection JpaQueryApiInspection + stmt.setInt(4, startIndex); + //noinspection JpaQueryApiInspection + stmt.setInt(5, rowCount); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java new file mode 100644 index 00000000000..c7b0c6b78b9 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2017, 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.core.dao.impl.group; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.impl.AbstractGroupDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This class represents implementation of GroupDAO + */ +public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { + + @Override + public List getGroups(GroupPaginationRequest request, int tenantId) + throws GroupManagementDAOException { + PreparedStatement stmt = null; + ResultSet resultSet = null; + List deviceGroupList = null; + + String groupName = request.getGroupName(); + boolean hasGroupName = false; + String owner = request.getOwner(); + boolean hasOwner = false; + boolean hasLimit = request.getRowCount() != 0; + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; + if (groupName != null && !groupName.isEmpty()) { + sql += " AND GROUP_NAME LIKE ?"; + hasGroupName = true; + } + if (owner != null && !owner.isEmpty()) { + sql += " AND OWNER LIKE ?"; + hasOwner = true; + } + if (hasLimit) { + sql += " LIMIT ? OFFSET ?"; + } + + int paramIndex = 1; + stmt = conn.prepareStatement(sql); + stmt.setInt(paramIndex++, tenantId); + if (hasGroupName) { + stmt.setString(paramIndex++, groupName + "%"); + } + if (hasOwner) { + stmt.setString(paramIndex++, owner + "%"); + } + if (hasLimit) { + stmt.setInt(paramIndex++, request.getRowCount()); + stmt.setInt(paramIndex, request.getStartIndex()); + } + resultSet = stmt.executeQuery(); + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return deviceGroupList; + } + + @Override + public List getGroups(GroupPaginationRequest request, List deviceGroupIds, + int tenantId) throws GroupManagementDAOException { + int deviceGroupIdsCount = deviceGroupIds.size(); + if (deviceGroupIdsCount == 0) { + return new ArrayList<>(); + } + PreparedStatement stmt = null; + ResultSet resultSet = null; + List deviceGroupList = null; + + String groupName = request.getGroupName(); + boolean hasGroupName = false; + String owner = request.getOwner(); + boolean hasOwner = false; + boolean hasLimit = request.getRowCount() != 0; + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; + if (groupName != null && !groupName.isEmpty()) { + sql += " AND GROUP_NAME LIKE ?"; + hasGroupName = true; + } + if (owner != null && !owner.isEmpty()) { + sql += " AND OWNER LIKE ?"; + hasOwner = true; + } + sql += " AND ID IN ("; + for (int i = 0; i < deviceGroupIdsCount; i++) { + sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; + } + sql += ")"; + if (hasLimit) { + sql += " LIMIT ? OFFSET ?"; + } + + int paramIndex = 1; + stmt = conn.prepareStatement(sql); + stmt.setInt(paramIndex++, tenantId); + if (hasGroupName) { + stmt.setString(paramIndex++, groupName + "%"); + } + if (hasOwner) { + stmt.setString(paramIndex++, owner + "%"); + } + for (Integer deviceGroupId : deviceGroupIds) { + stmt.setInt(paramIndex++, deviceGroupId); + } + if (hasLimit) { + stmt.setInt(paramIndex++, request.getRowCount()); + stmt.setInt(paramIndex, request.getStartIndex()); + } + resultSet = stmt.executeQuery(); + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return deviceGroupList; + } + + @Override + public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) + throws GroupManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "FROM " + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + + " DM_DEVICE d, (" + + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + //noinspection JpaQueryApiInspection + stmt.setInt(4, rowCount); + //noinspection JpaQueryApiInspection + stmt.setInt(5, startIndex); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/SQLServerGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/SQLServerGroupDAOImpl.java new file mode 100644 index 00000000000..6e483e88dd7 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/SQLServerGroupDAOImpl.java @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2017, 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.core.dao.impl.group; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.impl.AbstractGroupDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This class represents implementation of GroupDAO + */ +public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { + + @Override + public List getGroups(GroupPaginationRequest request, int tenantId) + throws GroupManagementDAOException { + PreparedStatement stmt = null; + ResultSet resultSet = null; + List deviceGroupList = null; + + String groupName = request.getGroupName(); + boolean hasGroupName = false; + String owner = request.getOwner(); + boolean hasOwner = false; + boolean hasLimit = request.getRowCount() != 0; + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; + if (groupName != null && !groupName.isEmpty()) { + sql += " AND GROUP_NAME LIKE ?"; + hasGroupName = true; + } + if (owner != null && !owner.isEmpty()) { + sql += " AND OWNER LIKE ?"; + hasOwner = true; + } + if (hasLimit) { + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } + + int paramIndex = 1; + stmt = conn.prepareStatement(sql); + stmt.setInt(paramIndex++, tenantId); + if (hasGroupName) { + stmt.setString(paramIndex++, groupName + "%"); + } + if (hasOwner) { + stmt.setString(paramIndex++, owner + "%"); + } + if (hasLimit) { + stmt.setInt(paramIndex++, request.getStartIndex()); + stmt.setInt(paramIndex, request.getRowCount()); + } + resultSet = stmt.executeQuery(); + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return deviceGroupList; + } + + @Override + public List getGroups(GroupPaginationRequest request, List deviceGroupIds, + int tenantId) throws GroupManagementDAOException { + int deviceGroupIdsCount = deviceGroupIds.size(); + if (deviceGroupIdsCount == 0) { + return new ArrayList<>(); + } + PreparedStatement stmt = null; + ResultSet resultSet = null; + List deviceGroupList = null; + + String groupName = request.getGroupName(); + boolean hasGroupName = false; + String owner = request.getOwner(); + boolean hasOwner = false; + boolean hasLimit = request.getRowCount() != 0; + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?"; + if (groupName != null && !groupName.isEmpty()) { + sql += " AND GROUP_NAME LIKE ?"; + hasGroupName = true; + } + if (owner != null && !owner.isEmpty()) { + sql += " AND OWNER LIKE ?"; + hasOwner = true; + } + sql += " AND ID IN ("; + for (int i = 0; i < deviceGroupIdsCount; i++) { + sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; + } + sql += ")"; + if (hasLimit) { + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } + + int paramIndex = 1; + stmt = conn.prepareStatement(sql); + stmt.setInt(paramIndex++, tenantId); + if (hasGroupName) { + stmt.setString(paramIndex++, groupName + "%"); + } + if (hasOwner) { + stmt.setString(paramIndex++, owner + "%"); + } + for (Integer deviceGroupId : deviceGroupIds) { + stmt.setInt(paramIndex++, deviceGroupId); + } + if (hasLimit) { + stmt.setInt(paramIndex++, request.getStartIndex()); + stmt.setInt(paramIndex, request.getRowCount()); + } + resultSet = stmt.executeQuery(); + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return deviceGroupList; + } + + @Override + public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) + throws GroupManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "FROM " + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + + " DM_DEVICE d, (" + + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + //noinspection JpaQueryApiInspection + stmt.setInt(4, startIndex); + //noinspection JpaQueryApiInspection + stmt.setInt(5, rowCount); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } +} \ No newline at end of file From 8b9420766f76f1a397ee31a18793e4dd126733c9 Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Fri, 16 Jun 2017 16:47:34 +0530 Subject: [PATCH 2/3] Fixed SQL syntax issue in DeviceStatusMonitoring Task when running on PostgreSQL. --- .../core/operation/mgt/dao/impl/OperationMappingDAOImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java index 82f2cd66e75..9d9b98f7a84 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java @@ -152,7 +152,8 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { "DM_DEVICE D ON E.DEVICE_ID = D.ID WHERE " + "OP.STATUS IN ('"+ Operation.Status.PENDING.name() + "','" + Operation.Status.REPEATED.name() + "') " + "AND OP.CREATED_TIMESTAMP BETWEEN ? AND ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() + - "','" + EnrolmentInfo.Status.UNREACHABLE.name() + "') AND D.DEVICE_TYPE_ID = ? GROUP BY ENROLMENT_ID"; + "','" + EnrolmentInfo.Status.UNREACHABLE.name() + "') AND D.DEVICE_TYPE_ID = ? GROUP BY ENROLMENT_ID," + + " D.DEVICE_IDENTIFICATION, E.STATUS, E.TENANT_ID"; stmt = conn.prepareStatement(sql); stmt.setLong(1, maxDuration); stmt.setLong(2, minDuration); From 0c041c3556233beeb1bcafffe626d360dc0b4499 Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Fri, 16 Jun 2017 16:48:17 +0530 Subject: [PATCH 3/3] Fixed issues in Postgre SQL script which resolves wso2/product-iots#494 --- .../resources/dbscripts/cdm/postgresql.sql | 78 +++---------------- 1 file changed, 12 insertions(+), 66 deletions(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 6c1fc86a915..eea332e22ee 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -1,19 +1,17 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( - ID BIGSERIAL PRIMARY KEY, - NAME VARCHAR(300) DEFAULT NULL, - PROVIDER_TENANT_ID INTEGER DEFAULT 0, - SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE, + ID BIGSERIAL PRIMARY KEY, + NAME VARCHAR(300) DEFAULT NULL, + PROVIDER_TENANT_ID INTEGER DEFAULT 0, + SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE, UNIQUE(NAME, PROVIDER_TENANT_ID) ); -CREATE INDEX IDX_DEVICE_TYPE ON DM_DEVICE_TYPE (NAME, PROVIDER_TENANT_ID); - CREATE TABLE IF NOT EXISTS DM_GROUP ( - ID BIGSERIAL NOT NULL PRIMARY KEY, - DESCRIPTION TEXT DEFAULT NULL, - NAME VARCHAR(100) DEFAULT NULL, - OWNER VARCHAR(45) DEFAULT NULL, - TENANT_ID INTEGER DEFAULT 0 + ID BIGSERIAL NOT NULL PRIMARY KEY, + GROUP_NAME VARCHAR(100) DEFAULT NULL, + DESCRIPTION TEXT DEFAULT NULL, + OWNER VARCHAR(45) DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0 ); CREATE TABLE IF NOT EXISTS DM_ROLE_GROUP_MAP ( @@ -21,11 +19,12 @@ CREATE TABLE IF NOT EXISTS DM_ROLE_GROUP_MAP ( GROUP_ID INTEGER DEFAULT NULL, ROLE VARCHAR(45) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, - PRIMARY KEY (ID), CONSTRAINT fk_DM_ROLE_GROUP_MAP_DM_GROUP2 FOREIGN KEY (GROUP_ID) REFERENCES DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +CREATE INDEX IDX_DEVICE_TYPE ON DM_DEVICE_TYPE (NAME, PROVIDER_TENANT_ID); + CREATE TABLE IF NOT EXISTS DM_DEVICE ( ID BIGSERIAL NOT NULL PRIMARY KEY, DESCRIPTION TEXT DEFAULT NULL, @@ -45,7 +44,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_MAP ( DEVICE_ID INTEGER DEFAULT NULL, GROUP_ID INTEGER DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, - PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_GROUP_MAP_DM_DEVICE2 FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_DEVICE_GROUP_MAP_DM_GROUP2 FOREIGN KEY (GROUP_ID) @@ -323,31 +321,6 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( - ID BIGSERIAL NOT NULL PRIMARY KEY, - DEVICE_GROUP_ID INT NOT NULL, - POLICY_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - CONSTRAINT FK_DM_DEVICE_GROUP_DM_POLICY - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( - ID BIGSERIAL NOT NULL PRIMARY KEY, - DEVICE_ID INTEGER NOT NULL, - OWNER VARCHAR(50) NOT NULL, - OWNERSHIP VARCHAR(45) DEFAULT NULL, - STATUS VARCHAR(50) NULL, - DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, - DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL, - TENANT_ID INT NOT NULL, - CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - CREATE TABLE IF NOT EXISTS DM_APPLICATION ( ID BIGSERIAL NOT NULL PRIMARY KEY, NAME VARCHAR(150) NOT NULL, @@ -377,31 +350,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( -- POLICY RELATED TABLES FINISHED -- --- DEVICE GROUP TABLES -- - -CREATE TABLE IF NOT EXISTS DM_GROUP ( - ID BIGSERIAL NOT NULL PRIMARY KEY, - GROUP_NAME VARCHAR(100) DEFAULT NULL, - DESCRIPTION BYTEA DEFAULT NULL, - DATE_OF_CREATE INTEGER DEFAULT NULL, - DATE_OF_LAST_UPDATE INTEGER DEFAULT NULL, - OWNER VARCHAR(45) DEFAULT NULL, - TENANT_ID INTEGER NOT NULL -); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_MAP ( - ID BIGSERIAL NOT NULL PRIMARY KEY, - DEVICE_ID INTEGER DEFAULT NULL, - GROUP_ID INTEGER DEFAULT NULL, - TENANT_ID INTEGER DEFAULT 0, - CONSTRAINT fk_DM_DEVICE_GROUP_MAP_DM_DEVICE2 FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_DM_DEVICE_GROUP_MAP_DM_GROUP2 FOREIGN KEY (GROUP_ID) - REFERENCES DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - --- END OF DEVICE GROUP TABLES -- - -- POLICY AND DEVICE GROUP MAPPING -- CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( @@ -481,7 +429,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( DEVICE_MODEL VARCHAR(45) NULL, VENDOR VARCHAR(45) NULL, OS_VERSION VARCHAR(45) NULL, - OS_BUILD_DATE VARCHAR(100) NULL, BATTERY_LEVEL DECIMAL(4) NULL, INTERNAL_TOTAL_MEMORY DECIMAL(30,3) NULL, @@ -558,5 +505,4 @@ DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND DM_DEVICE.ID = DM_DEVICE_DETAIL.DEVICE_ID ORDER BY TENANT_ID, DEVICE_ID; --- END OF DASHBOARD RELATED VIEWS -- - +-- END OF DASHBOARD RELATED VIEWS -- \ No newline at end of file