Add and update dao layer methods related to hierarchical changes

(cherry picked from commit e22d9fd8fc)
4.x.x
Saad Sahibjan 4 years ago committed by Pahansith
parent 5bc39dd7dc
commit 3e310d4dbf

@ -14,6 +14,23 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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; package org.wso2.carbon.device.mgt.core.dao;
@ -74,6 +91,15 @@ public interface GroupDAO {
*/ */
void deleteAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException; void deleteAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException;
/**
* Remove properties of device groups.
*
* @param groupIds to be deleted.
* @param tenantId of the group.
* @throws GroupManagementDAOException on error during deletion of group properties of groups
*/
void deleteAllGroupsProperties(List<Integer> groupIds, int tenantId) throws GroupManagementDAOException;
/** /**
* Retrives all properties stored against a group. * Retrives all properties stored against a group.
* *
@ -95,6 +121,15 @@ public interface GroupDAO {
void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId) void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId)
throws GroupManagementDAOException; throws GroupManagementDAOException;
/**
* Update existing Device Groups.
*
* @param deviceGroups groups to update.
* @param tenantId of the group.
* @throws GroupManagementDAOException on error during updating of groups
*/
void updateGroups(List<DeviceGroup> deviceGroups, int tenantId) throws GroupManagementDAOException;
/** /**
* Delete an existing Device Group. * Delete an existing Device Group.
* *
@ -104,6 +139,24 @@ public interface GroupDAO {
*/ */
void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException; void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException;
/**
* Delete mappings of Device Groups.
*
* @param groupIds of Device Groups.
* @param tenantId of the group.
* @throws GroupManagementDAOException on error during deletion of mappings of groups
*/
void deleteGroupsMapping(List<Integer> groupIds, int tenantId) throws GroupManagementDAOException;
/**
* Delete existing Device Groups.
*
* @param groupIds of Device Groups.
* @param tenantId of the group.
* @throws GroupManagementDAOException on error during deletion of groups
*/
void deleteGroups(List<Integer> groupIds, int tenantId) throws GroupManagementDAOException;
/** /**
* Get device group by id. * Get device group by id.
* *
@ -114,6 +167,25 @@ public interface GroupDAO {
*/ */
DeviceGroup getGroup(int groupId, int tenantId) throws GroupManagementDAOException; DeviceGroup getGroup(int groupId, int tenantId) throws GroupManagementDAOException;
/**
* Get children groups by parent path.
*
* @param parentPath of parent group.
* @param tenantId of the group.
* @return {@link List<DeviceGroup>} list of children device groups
* @throws GroupManagementDAOException on error during retrieval of children groups
*/
List<DeviceGroup> getChildrenGroups(String parentPath, int tenantId) throws GroupManagementDAOException;
/**
* Get root groups.
*
* @param tenantId of the group.
* @return {@link List<DeviceGroup>} list of root device groups
* @throws GroupManagementDAOException on error during retrieval of root groups
*/
List<DeviceGroup> getRootGroups(int tenantId) throws GroupManagementDAOException;
/** /**
* Get the groups of device with device id provided * Get the groups of device with device id provided
* @param deviceId * @param deviceId
@ -306,10 +378,11 @@ public interface GroupDAO {
* *
* @param roles of the group. * @param roles of the group.
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @param parentPath of the group.
* @return count of device groups. * @return count of device groups.
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
int getGroupsCount(String[] roles, int tenantId) throws GroupManagementDAOException; int getGroupsCount(String[] roles, int tenantId, String parentPath) throws GroupManagementDAOException;
/** /**
* Get all device groups which owned by user. * Get all device groups which owned by user.
@ -336,10 +409,11 @@ public interface GroupDAO {
* *
* @param username of the owner. * @param username of the owner.
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @param parentPath of the group.
* @return count of device groups. * @return count of device groups.
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
int getOwnGroupsCount(String username, int tenantId) throws GroupManagementDAOException; int getOwnGroupsCount(String username, int tenantId, String parentPath) throws GroupManagementDAOException;
/** /**
* Get device Ids of devices which are assigned to groups. * Get device Ids of devices which are assigned to groups.

@ -14,18 +14,34 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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; package org.wso2.carbon.device.mgt.core.dao.impl;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.StringUtils;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.GroupDAO; 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.GroupManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory;
@ -49,6 +65,127 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
private static final Log log = LogFactory.getLog(AbstractGroupDAOImpl.class); private static final Log log = LogFactory.getLog(AbstractGroupDAOImpl.class);
@Override
public List<DeviceGroup> getGroups(GroupPaginationRequest request, int tenantId)
throws GroupManagementDAOException {
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE TENANT_ID = ?";
if (StringUtils.isNotBlank(request.getGroupName())) {
sql += " AND UPPER(GROUP_NAME) LIKE ?";
}
if (StringUtils.isNotBlank(request.getOwner())) {
sql += " AND UPPER(OWNER) LIKE ?";
}
if (StringUtils.isNotBlank(request.getStatus())) {
sql += " AND STATUS = ?";
}
if (StringUtils.isNotBlank(request.getParentPath())) {
sql += " AND PARENT_PATH LIKE ?";
}
if (request.getRowCount() != 0) {
sql += " LIMIT ? OFFSET ?";
}
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
stmt.setInt(paramIndex++, tenantId);
if (StringUtils.isNotBlank(request.getGroupName())) {
stmt.setString(paramIndex++, request.getGroupName() + "%");
}
if (StringUtils.isNotBlank(request.getOwner())) {
stmt.setString(paramIndex++, request.getOwner() + "%");
}
if (StringUtils.isNotBlank(request.getStatus())) {
stmt.setString(paramIndex++, request.getStatus().toUpperCase());
}
if (StringUtils.isNotBlank(request.getParentPath())) {
stmt.setString(paramIndex++, request.getParentPath());
}
if (request.getRowCount() != 0) {
stmt.setInt(paramIndex++, request.getRowCount());
stmt.setInt(paramIndex, request.getStartIndex());
}
List<DeviceGroup> deviceGroupList = new ArrayList<>();
try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet));
}
}
return deviceGroupList;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving groups in tenant: " + tenantId;
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
}
@Override
public List<DeviceGroup> getGroups(GroupPaginationRequest request, List<Integer> deviceGroupIds,
int tenantId) throws GroupManagementDAOException {
int deviceGroupIdsCount = deviceGroupIds.size();
if (deviceGroupIdsCount == 0) {
return new ArrayList<>();
}
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP WHERE TENANT_ID = ?";
if (StringUtils.isNotBlank(request.getGroupName())) {
sql += " AND GROUP_NAME LIKE ?";
}
if (StringUtils.isNotBlank(request.getOwner())) {
sql += " AND OWNER LIKE ?";
}
if (StringUtils.isNotBlank(request.getParentPath())) {
sql += " AND PARENT_PATH LIKE ?";
}
sql += " AND ID IN (";
for (int i = 0; i < deviceGroupIdsCount; i++) {
sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?";
}
sql += ")";
if (request.getRowCount() != 0) {
sql += " LIMIT ? OFFSET ?";
}
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
stmt.setInt(paramIndex++, tenantId);
if (StringUtils.isNotBlank(request.getGroupName())) {
stmt.setString(paramIndex++, request.getGroupName() + "%");
}
if (StringUtils.isNotBlank(request.getOwner())) {
stmt.setString(paramIndex++, request.getOwner() + "%");
}
if (StringUtils.isNotBlank(request.getParentPath())) {
stmt.setString(paramIndex++, request.getParentPath());
}
for (Integer deviceGroupId : deviceGroupIds) {
stmt.setInt(paramIndex++, deviceGroupId);
}
if (request.getRowCount() != 0) {
stmt.setInt(paramIndex++, request.getRowCount());
stmt.setInt(paramIndex, request.getStartIndex());
}
List<DeviceGroup> deviceGroupList = new ArrayList<>();
try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet));
}
}
return deviceGroupList;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving groups of groups IDs " + deviceGroupIds.toString()
+ " in tenant: " + tenantId;
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
}
@Override @Override
public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException { public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -59,9 +196,11 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql; String sql;
if (deviceGroup.getStatus() == null || deviceGroup.getStatus().isEmpty()) { if (deviceGroup.getStatus() == null || deviceGroup.getStatus().isEmpty()) {
sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) VALUES (?, ?, ?, ?)"; sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH) "
+ "VALUES (?, ?, ?, ?, ?)";
} else { } else {
sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, STATUS) VALUES (?, ?, ?, ?, ?)"; sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, STATUS) "
+ "VALUES (?, ?, ?, ?, ?, ?)";
hasStatus = true; hasStatus = true;
} }
stmt = conn.prepareStatement(sql, new String[]{"ID"}); stmt = conn.prepareStatement(sql, new String[]{"ID"});
@ -69,8 +208,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
stmt.setString(2, deviceGroup.getName()); stmt.setString(2, deviceGroup.getName());
stmt.setString(3, deviceGroup.getOwner()); stmt.setString(3, deviceGroup.getOwner());
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
stmt.setString(5, deviceGroup.getParentPath());
if (hasStatus) { if (hasStatus) {
stmt.setString(5, deviceGroup.getStatus()); stmt.setString(6, deviceGroup.getStatus());
} }
stmt.executeUpdate(); stmt.executeUpdate();
@ -151,10 +291,12 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = String sql =
"UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ? WHERE ID = ? AND TENANT_ID = ?"; "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, PARENT_PATH = ? WHERE ID = ? "
+ "AND TENANT_ID = ?";
if (deviceGroup.getStatus() != null && !deviceGroup.getStatus().isEmpty()) { if (deviceGroup.getStatus() != null && !deviceGroup.getStatus().isEmpty()) {
sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, STATUS = ? WHERE ID = ? AND TENANT_ID = ?"; sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, PARENT_PATH = ?, STATUS = ? "
+ "WHERE ID = ? AND TENANT_ID = ?";
hasStatus = true; hasStatus = true;
} }
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -162,11 +304,12 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
stmt.setString(paramIndex++, deviceGroup.getDescription()); stmt.setString(paramIndex++, deviceGroup.getDescription());
stmt.setString(paramIndex++, deviceGroup.getName()); stmt.setString(paramIndex++, deviceGroup.getName());
stmt.setString(paramIndex++, deviceGroup.getOwner()); stmt.setString(paramIndex++, deviceGroup.getOwner());
stmt.setString(paramIndex++, deviceGroup.getParentPath());
if (hasStatus) { if (hasStatus) {
stmt.setString(paramIndex++, deviceGroup.getStatus()); stmt.setString(paramIndex++, deviceGroup.getStatus());
} }
stmt.setInt(paramIndex++, groupId); stmt.setInt(paramIndex++, groupId);
stmt.setInt(paramIndex++, tenantId); stmt.setInt(paramIndex, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" + throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" +
@ -176,6 +319,32 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
} }
@Override
public void updateGroups(List<DeviceGroup> deviceGroups, int tenantId) throws GroupManagementDAOException {
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, STATUS = ?, "
+ "PARENT_PATH = ? WHERE ID = ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)){
for (DeviceGroup deviceGroup : deviceGroups) {
stmt.setString(1, deviceGroup.getDescription());
stmt.setString(2, deviceGroup.getName());
stmt.setString(3, deviceGroup.getOwner());
stmt.setString(4, deviceGroup.getStatus());
stmt.setString(5, deviceGroup.getParentPath());
stmt.setInt(6, deviceGroup.getGroupId());
stmt.setInt(7, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
}
} catch (SQLException e) {
String msg = "Error occurred while updating groups as batch";
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
}
@Override @Override
public void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException { public void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException {
Connection conn; Connection conn;
@ -217,6 +386,64 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
} }
@Override
public void deleteGroupsMapping(List<Integer> groupIds, int tenantId) throws GroupManagementDAOException {
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "DELETE FROM DM_ROLE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (int groupId : groupIds) {
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
}
sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (int groupId : groupIds) {
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
}
sql = "DELETE FROM DM_DEVICE_GROUP_POLICY WHERE DEVICE_GROUP_ID = ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (int groupId : groupIds) {
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
}
} catch (SQLException e) {
String msg = "Error occurred while removing mappings of groups as batches";
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
}
@Override
public void deleteGroups(List<Integer> groupIds, int tenantId) throws GroupManagementDAOException {
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "DELETE FROM DM_GROUP WHERE ID = ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (int groupId : groupIds) {
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
stmt.addBatch();
}
stmt.executeBatch();
}
} catch (SQLException e) {
String msg = "Error occurred while deleting groups as batches";
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
}
public void deleteAllGroupProperties(int groupId, int tenantId) public void deleteAllGroupProperties(int groupId, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -235,6 +462,25 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
} }
public void deleteAllGroupsProperties(List<Integer> groupIds, int tenantId) throws GroupManagementDAOException {
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "DELETE FROM GROUP_PROPERTIES WHERE GROUP_ID = ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (Integer groupId : groupIds) {
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
stmt.addBatch();
}
stmt.executeUpdate();
}
} catch (SQLException e) {
String msg = "Error occurred while deleting properties of groups as batches";
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
}
public Map<String, String> getAllGroupProperties(int groupId, int tenantId) public Map<String, String> getAllGroupProperties(int groupId, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -266,7 +512,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE ID = ? AND TENANT_ID = ?"; String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP WHERE ID = ? "
+ "AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, groupId); stmt.setInt(1, groupId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -284,6 +531,56 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
} }
@Override
public List<DeviceGroup> getChildrenGroups(String parentPath, int tenantId) throws GroupManagementDAOException {
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE PARENT_PATH LIKE ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, parentPath + "%");
stmt.setInt(2, tenantId);
List<DeviceGroup> deviceGroupList = new ArrayList<>();
try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet));
}
}
return deviceGroupList;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving children group having parent path '" + parentPath
+ "' in tenant: " + tenantId;
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
}
@Override
public List<DeviceGroup> getRootGroups(int tenantId) throws GroupManagementDAOException {
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE PARENT_PATH LIKE ? AND TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, "/");
stmt.setInt(2, tenantId);
List<DeviceGroup> deviceGroupList = new ArrayList<>();
try (ResultSet resultSet = stmt.executeQuery()) {
deviceGroupList = new ArrayList<>();
while (resultSet.next()) {
deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet));
}
}
return deviceGroupList;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving root groups in tenant: " + tenantId;
log.error(msg);
throw new GroupManagementDAOException(msg, e);
}
}
@Override @Override
public List<DeviceGroup> getGroups(int deviceId, int tenantId) throws GroupManagementDAOException { public List<DeviceGroup> getGroups(int deviceId, int tenantId) throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -291,7 +588,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
List<DeviceGroup> deviceGroupBuilders = new ArrayList<>(); List<DeviceGroup> deviceGroupBuilders = new ArrayList<>();
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.OWNER, G.STATUS FROM DM_GROUP G " + String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.OWNER, G.STATUS, G.PARENT_PATH FROM DM_GROUP G " +
"INNER JOIN DM_DEVICE_GROUP_MAP GM ON G.ID = GM.GROUP_ID " + "INNER JOIN DM_DEVICE_GROUP_MAP GM ON G.ID = GM.GROUP_ID " +
"WHERE GM.DEVICE_ID = ? AND GM.TENANT_ID = ?"; "WHERE GM.DEVICE_ID = ? AND GM.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -316,7 +613,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
List<DeviceGroup> deviceGroupList = null; List<DeviceGroup> deviceGroupList = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
@ -389,6 +687,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
sql += " AND STATUS = ?"; sql += " AND STATUS = ?";
hasStatus = true; hasStatus = true;
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
sql += " AND PARENT_PATH = ?";
}
int paramIndex = 1; int paramIndex = 1;
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -397,10 +698,13 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
stmt.setString(paramIndex++, groupName + "%"); stmt.setString(paramIndex++, groupName + "%");
} }
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex, owner + "%"); stmt.setString(paramIndex++, owner + "%");
} }
if (hasStatus) { if (hasStatus) {
stmt.setString(paramIndex, request.getStatus()); stmt.setString(paramIndex++, request.getStatus());
}
if (StringUtils.isNotBlank(request.getParentPath())) {
stmt.setString(paramIndex, request.getParentPath());
} }
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
@ -422,7 +726,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = String sql =
"SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?"; "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE GROUP_NAME = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, groupName); stmt.setString(1, groupName);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -592,7 +897,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
List<DeviceGroup> deviceGroupList = null; List<DeviceGroup> deviceGroupList = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP g, " + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP g, " +
"(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN ("; "(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN (";
int index = 0; int index = 0;
@ -658,7 +963,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
@Override @Override
public int getGroupsCount(String[] roles, int tenantId) throws GroupManagementDAOException { public int getGroupsCount(String[] roles, int tenantId, String parentPath) throws GroupManagementDAOException {
int rolesCount = roles.length; int rolesCount = roles.length;
if (rolesCount == 0) { if (rolesCount == 0) {
return 0; return 0;
@ -672,14 +977,20 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
for (int i = 0; i < rolesCount; i++) { for (int i = 0; i < rolesCount; i++) {
sql += (rolesCount - 1 != i) ? "?," : "?"; sql += (rolesCount - 1 != i) ? "?," : "?";
} }
sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID"; sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? ";
if (StringUtils.isNotBlank(parentPath)) {
sql += " AND g.PARENT_PATH = ? ";
}
sql += "GROUP BY g.ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
int index = 0; int index = 0;
while (index++ < rolesCount) { while (index++ < rolesCount) {
stmt.setString(index, roles[index - 1]); stmt.setString(index, roles[index - 1]);
} }
stmt.setInt(index, tenantId); stmt.setInt(index++, tenantId);
if (StringUtils.isNotBlank(parentPath)) {
stmt.setString(index, parentPath);
}
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
return resultSet.getInt("GROUP_COUNT"); return resultSet.getInt("GROUP_COUNT");
@ -700,7 +1011,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
List<DeviceGroup> deviceGroupList = null; List<DeviceGroup> deviceGroupList = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE OWNER = ? AND TENANT_ID = ?"; String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, username); stmt.setString(1, username);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -744,15 +1056,21 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
@Override @Override
public int getOwnGroupsCount(String username, int tenantId) throws GroupManagementDAOException { public int getOwnGroupsCount(String username, int tenantId, String parentPath) throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP WHERE OWNER = ? AND TENANT_ID = ?"; String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP WHERE OWNER = ? AND TENANT_ID = ?";
if (StringUtils.isNotBlank(parentPath)) {
sql += " AND PARENT_PATH = ?";
}
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, username); stmt.setString(1, username);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
if (StringUtils.isNotBlank(parentPath)) {
stmt.setString(3, parentPath);
}
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
return resultSet.getInt("GROUP_COUNT"); return resultSet.getInt("GROUP_COUNT");

@ -14,18 +14,32 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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; 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.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.GroupManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; 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.impl.AbstractGroupDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; 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.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -38,134 +52,6 @@ import java.util.List;
* This class represents implementation of GroupDAO * This class represents implementation of GroupDAO
*/ */
public class GenericGroupDAOImpl extends AbstractGroupDAOImpl { public class GenericGroupDAOImpl extends AbstractGroupDAOImpl {
@Override
public List<DeviceGroup> getGroups(GroupPaginationRequest request, int tenantId)
throws GroupManagementDAOException {
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<DeviceGroup> deviceGroupList = null;
String groupName = request.getGroupName();
boolean hasGroupName = false;
String owner = request.getOwner();
String status = request.getStatus();
boolean hasOwner = false;
boolean hasStatus = false;
boolean hasLimit = request.getRowCount() != 0;
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?";
if (groupName != null && !groupName.isEmpty()) {
sql += " AND UPPER(GROUP_NAME) LIKE ?";
hasGroupName = true;
}
if (owner != null && !owner.isEmpty()) {
sql += " AND UPPER(OWNER) LIKE ?";
hasOwner = true;
}
if (status != null && !status.isEmpty()) {
sql += " AND STATUS = ?";
hasStatus = 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 (hasStatus) {
stmt.setString(paramIndex++, status.toUpperCase());
}
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<DeviceGroup> getGroups(GroupPaginationRequest request, List<Integer> deviceGroupIds,
int tenantId) throws GroupManagementDAOException {
int deviceGroupIdsCount = deviceGroupIds.size();
if (deviceGroupIdsCount == 0) {
return new ArrayList<>();
}
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<DeviceGroup> 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, STATUS 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 @Override
public List<Device> getDevices(int groupId, int startIndex, int rowCount, int tenantId) public List<Device> getDevices(int groupId, int startIndex, int rowCount, int tenantId)

@ -14,10 +14,28 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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; package org.wso2.carbon.device.mgt.core.dao.impl.group;
import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
@ -56,7 +74,8 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE TENANT_ID = ?";
if (groupName != null && !groupName.isEmpty()) { if (groupName != null && !groupName.isEmpty()) {
sql += " AND GROUP_NAME LIKE ?"; sql += " AND GROUP_NAME LIKE ?";
hasGroupName = true; hasGroupName = true;
@ -69,6 +88,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
sql += " AND STATUS = ?"; sql += " AND STATUS = ?";
hasStatus = true; hasStatus = true;
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
sql += " AND PARENT_PATH LIKE ?";
}
if (hasLimit) { if (hasLimit) {
sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} }
@ -85,6 +107,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
if (hasStatus) { if (hasStatus) {
stmt.setString(paramIndex++, status.toUpperCase()); stmt.setString(paramIndex++, status.toUpperCase());
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
stmt.setString(paramIndex++, request.getParentPath());
}
if (hasLimit) { if (hasLimit) {
stmt.setInt(paramIndex++, request.getStartIndex()); stmt.setInt(paramIndex++, request.getStartIndex());
stmt.setInt(paramIndex, request.getRowCount()); stmt.setInt(paramIndex, request.getRowCount());
@ -121,7 +146,8 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE TENANT_ID = ?";
if (groupName != null && !groupName.isEmpty()) { if (groupName != null && !groupName.isEmpty()) {
sql += " AND GROUP_NAME LIKE ?"; sql += " AND GROUP_NAME LIKE ?";
hasGroupName = true; hasGroupName = true;
@ -130,6 +156,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
sql += " AND OWNER LIKE ?"; sql += " AND OWNER LIKE ?";
hasOwner = true; hasOwner = true;
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
sql += " AND PARENT_PATH LIKE ?";
}
sql += " AND ID IN ("; sql += " AND ID IN (";
for (int i = 0; i < deviceGroupIdsCount; i++) { for (int i = 0; i < deviceGroupIdsCount; i++) {
sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?";
@ -148,6 +177,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex++, owner + "%"); stmt.setString(paramIndex++, owner + "%");
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
stmt.setString(paramIndex++, request.getParentPath());
}
for (Integer deviceGroupId : deviceGroupIds) { for (Integer deviceGroupId : deviceGroupIds) {
stmt.setInt(paramIndex++, deviceGroupId); stmt.setInt(paramIndex++, deviceGroupId);
} }

@ -14,13 +14,29 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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; package org.wso2.carbon.device.mgt.core.dao.impl.group;
import org.apache.solr.common.StringUtils; import org.apache.solr.common.StringUtils;
import org.wso2.carbon.device.mgt.common.Device; 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.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; 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.GroupManagementDAOFactory;
@ -50,10 +66,10 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql; String sql;
if(StringUtils.isEmpty(deviceGroup.getStatus())) { if(StringUtils.isEmpty(deviceGroup.getStatus())) {
sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) " + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH) " +
"VALUES (?, ?, ?, ?) RETURNING ID"; "VALUES (?, ?, ?, ?) RETURNING ID";
} else { } else {
sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, STATUS) " + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, STATUS) " +
"VALUES (?, ?, ?, ?, ?) RETURNING ID"; "VALUES (?, ?, ?, ?, ?) RETURNING ID";
hasStatus = true; hasStatus = true;
} }
@ -62,8 +78,9 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl {
stmt.setString(2, deviceGroup.getName()); stmt.setString(2, deviceGroup.getName());
stmt.setString(3, deviceGroup.getOwner()); stmt.setString(3, deviceGroup.getOwner());
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
stmt.setString(5, deviceGroup.getParentPath());
if(hasStatus) { if(hasStatus) {
stmt.setString(5, deviceGroup.getStatus()); stmt.setString(6, deviceGroup.getStatus());
} }
stmt.execute(); stmt.execute();
rs = stmt.getGeneratedKeys(); rs = stmt.getGeneratedKeys();
@ -79,135 +96,6 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl {
} }
} }
@Override
public List<DeviceGroup> getGroups(GroupPaginationRequest request, int tenantId)
throws GroupManagementDAOException {
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<DeviceGroup> deviceGroupList = null;
String groupName = request.getGroupName();
boolean hasGroupName = false;
String owner = request.getOwner();
String status = request.getStatus();
boolean hasOwner = false;
boolean hasStatus = false;
boolean hasLimit = request.getRowCount() != 0;
try {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS 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 (status != null && !status.isEmpty()) {
sql += " AND STATUS = ?";
hasStatus = 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 (hasStatus) {
stmt.setString(paramIndex++, status.toUpperCase());
}
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<DeviceGroup> getGroups(GroupPaginationRequest request, List<Integer> deviceGroupIds,
int tenantId) throws GroupManagementDAOException {
int deviceGroupIdsCount = deviceGroupIds.size();
if (deviceGroupIdsCount == 0) {
return new ArrayList<>();
}
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<DeviceGroup> 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, STATUS 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 @Override
public List<Device> getDevices(int groupId, int startIndex, int rowCount, int tenantId) public List<Device> getDevices(int groupId, int startIndex, int rowCount, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {

@ -14,10 +14,28 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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; package org.wso2.carbon.device.mgt.core.dao.impl.group;
import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
@ -56,7 +74,8 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE TENANT_ID = ?";
if (groupName != null && !groupName.isEmpty()) { if (groupName != null && !groupName.isEmpty()) {
sql += " AND GROUP_NAME LIKE ?"; sql += " AND GROUP_NAME LIKE ?";
hasGroupName = true; hasGroupName = true;
@ -69,6 +88,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
sql += " AND STATUS = ?"; sql += " AND STATUS = ?";
hasStatus = true; hasStatus = true;
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
sql += " AND PARENT_PATH LIKE ?";
}
if (hasLimit) { if (hasLimit) {
sql += " ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; sql += " ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} }
@ -85,6 +107,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
if (hasStatus) { if (hasStatus) {
stmt.setString(paramIndex++, status.toUpperCase()); stmt.setString(paramIndex++, status.toUpperCase());
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
stmt.setString(paramIndex++, request.getParentPath());
}
if (hasLimit) { if (hasLimit) {
stmt.setInt(paramIndex++, request.getStartIndex()); stmt.setInt(paramIndex++, request.getStartIndex());
stmt.setInt(paramIndex, request.getRowCount()); stmt.setInt(paramIndex, request.getRowCount());
@ -121,7 +146,8 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE TENANT_ID = ?";
if (groupName != null && !groupName.isEmpty()) { if (groupName != null && !groupName.isEmpty()) {
sql += " AND GROUP_NAME LIKE ?"; sql += " AND GROUP_NAME LIKE ?";
hasGroupName = true; hasGroupName = true;
@ -130,6 +156,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
sql += " AND OWNER LIKE ?"; sql += " AND OWNER LIKE ?";
hasOwner = true; hasOwner = true;
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
sql += " AND PARENT_PATH LIKE ?";
}
sql += " AND ID IN ("; sql += " AND ID IN (";
for (int i = 0; i < deviceGroupIdsCount; i++) { for (int i = 0; i < deviceGroupIdsCount; i++) {
sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?";
@ -148,6 +177,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex++, owner + "%"); stmt.setString(paramIndex++, owner + "%");
} }
if (StringUtils.isNotBlank(request.getParentPath())) {
stmt.setString(paramIndex++, request.getParentPath());
}
for (Integer deviceGroupId : deviceGroupIds) { for (Integer deviceGroupId : deviceGroupIds) {
stmt.setInt(paramIndex++, deviceGroupId); stmt.setInt(paramIndex++, deviceGroupId);
} }

@ -14,6 +14,22 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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.util; package org.wso2.carbon.device.mgt.core.dao.util;
@ -86,6 +102,7 @@ public final class GroupManagementDAOUtil {
group.setName(resultSet.getString("GROUP_NAME")); group.setName(resultSet.getString("GROUP_NAME"));
group.setOwner(resultSet.getString("OWNER")); group.setOwner(resultSet.getString("OWNER"));
group.setStatus(resultSet.getString("STATUS")); group.setStatus(resultSet.getString("STATUS"));
group.setParentPath(resultSet.getString("PARENT_PATH"));
return group; return group;
} }

Loading…
Cancel
Save