@ -22,9 +22,9 @@ 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.util.DeviceManagementDAOUtil ;
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 ;
@ -32,7 +32,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.Date ;
import java.util.List ;
/ * *
@ -47,15 +46,12 @@ public class GroupDAOImpl implements GroupDAO {
int groupId = - 1 ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, "
+ "OWNER, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?)" ;
String sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) VALUES (?, ?, ?, ?)" ;
stmt = conn . prepareStatement ( sql , new String [ ] { "ID" } ) ;
stmt . setString ( 1 , deviceGroup . getDescription ( ) ) ;
stmt . setString ( 2 , deviceGroup . getName ( ) ) ;
stmt . setLong ( 3 , new Date ( ) . getTime ( ) ) ;
stmt . setLong ( 4 , new Date ( ) . getTime ( ) ) ;
stmt . setString ( 5 , deviceGroup . getOwner ( ) ) ;
stmt . setInt ( 6 , tenantId ) ;
stmt . setString ( 3 , deviceGroup . getOwner ( ) ) ;
stmt . setInt ( 4 , tenantId ) ;
stmt . executeUpdate ( ) ;
rs = stmt . getGeneratedKeys ( ) ;
if ( rs . next ( ) ) {
@ -64,7 +60,7 @@ public class GroupDAOImpl implements GroupDAO {
return groupId ;
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while adding deviceGroup '" +
deviceGroup . getName ( ) + "'" , e ) ;
deviceGroup . getName ( ) + "'" , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , null ) ;
}
@ -76,19 +72,18 @@ public class GroupDAOImpl implements GroupDAO {
PreparedStatement stmt = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, DATE_OF_LAST_UPDATE = ?, OWNER = ? "
+ " WHERE ID = ? AND TENANT_ID = ?";
String sql =
"UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ? WHERE ID = ? AND TENANT_ID = ?";
stmt = conn . prepareStatement ( sql ) ;
stmt . setString ( 1 , deviceGroup . getDescription ( ) ) ;
stmt . setString ( 2 , deviceGroup . getName ( ) ) ;
stmt . setLong ( 3 , deviceGroup . getDateOfLastUpdate ( ) ) ;
stmt . setString ( 4 , deviceGroup . getOwner ( ) ) ;
stmt . setInt ( 5 , groupId ) ;
stmt . setInt ( 6 , tenantId ) ;
stmt . setString ( 3 , deviceGroup . getOwner ( ) ) ;
stmt . setInt ( 4 , groupId ) ;
stmt . setInt ( 5 , tenantId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while updating deviceGroup '" +
deviceGroup . getName ( ) + "'" , e ) ;
deviceGroup . getName ( ) + "'" , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , null ) ;
}
@ -100,7 +95,12 @@ public class GroupDAOImpl implements GroupDAO {
PreparedStatement stmt = null ;
try {
conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?" ;
String sql = "DELETE FROM DM_ROLE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , groupId ) ;
stmt . setInt ( 2 , tenantId ) ;
stmt . executeUpdate ( ) ;
sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , groupId ) ;
stmt . setInt ( 2 , tenantId ) ;
@ -131,8 +131,7 @@ public class GroupDAOImpl implements GroupDAO {
ResultSet resultSet = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER "
+ "FROM DM_GROUP WHERE ID = ? AND TENANT_ID = ?" ;
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE ID = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , groupId ) ;
stmt . setInt ( 2 , tenantId ) ;
@ -144,7 +143,7 @@ public class GroupDAOImpl implements GroupDAO {
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while obtaining information of Device Group '" +
groupId + "'" , e ) ;
groupId + "'" , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , resultSet ) ;
}
@ -157,15 +156,15 @@ public class GroupDAOImpl implements GroupDAO {
List < DeviceGroup > deviceGroupBuilders = new ArrayList < > ( ) ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G. DATE_OF_CREATE, G.DATE_OF_LAST_UPDATE, \n " +
" G.OWNER FROM DM_GROUP G INNER JOIN DM_DEVICE_GROUP_MAP GM ON G.ID = GM.GROUP_ID " +
String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G. OWNER FROM DM_GROUP G " +
" INNER JOIN DM_DEVICE_GROUP_MAP GM ON G.ID = GM.GROUP_ID " +
"WHERE GM.DEVICE_ID = ? AND GM.TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . setInt ( 2 , tenantId ) ;
resultSet = stmt . executeQuery ( ) ;
while ( resultSet . next ( ) ) {
deviceGroupBuilders . add ( GroupManagementDAOUtil . loadGroup ( resultSet ) ) ;
deviceGroupBuilders . add ( GroupManagementDAOUtil . loadGroup ( resultSet ) ) ;
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while obtaining information of Device Groups " , e ) ;
@ -190,8 +189,65 @@ public class GroupDAOImpl implements GroupDAO {
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER "
+ "FROM DM_GROUP WHERE TENANT_ID = ?" ;
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 < 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 FROM DM_GROUP WHERE TENANT_ID = ?" ;
if ( groupName ! = null & & ! groupName . isEmpty ( ) ) {
sql + = " AND GROUP_NAME LIKE ?" ;
hasGroupName = true ;
@ -200,6 +256,11 @@ public class GroupDAOImpl implements GroupDAO {
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 ?, ?" ;
}
@ -208,11 +269,14 @@ public class GroupDAOImpl implements GroupDAO {
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( paramIndex + + , tenantId ) ;
if ( hasGroupName ) {
stmt . setString ( paramIndex + + , groupName + "%" ) ;
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 ( ) ) ;
@ -237,8 +301,7 @@ public class GroupDAOImpl implements GroupDAO {
List < DeviceGroup > deviceGroupList = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER "
+ "FROM DM_GROUP WHERE TENANT_ID = ?" ;
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , tenantId ) ;
resultSet = stmt . executeQuery ( ) ;
@ -327,8 +390,8 @@ public class GroupDAOImpl implements GroupDAO {
ResultSet resultSet = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER " +
" FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?";
String sql =
"SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?";
stmt = conn . prepareStatement ( sql ) ;
stmt . setString ( 1 , groupName ) ;
stmt . setInt ( 2 , tenantId ) ;
@ -356,7 +419,6 @@ public class GroupDAOImpl implements GroupDAO {
stmt . setInt ( 2 , groupId ) ;
stmt . setInt ( 3 , tenantId ) ;
stmt . executeUpdate ( ) ;
stmt . getGeneratedKeys ( ) ;
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while adding device to Group." , e ) ;
} finally {
@ -376,7 +438,6 @@ public class GroupDAOImpl implements GroupDAO {
stmt . setInt ( 2 , groupId ) ;
stmt . setInt ( 3 , tenantId ) ;
stmt . executeUpdate ( ) ;
stmt . getGeneratedKeys ( ) ;
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while removing device from Group." , e ) ;
} finally {
@ -411,7 +472,8 @@ public class GroupDAOImpl implements GroupDAO {
ResultSet resultSet = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?" ;
String sql =
"SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , groupId ) ;
stmt . setInt ( 2 , tenantId ) ;
@ -428,7 +490,6 @@ public class GroupDAOImpl implements GroupDAO {
}
}
//TODO: Move this to device mgt
@Override
public List < Device > getDevices ( int groupId , int startIndex , int rowCount , int tenantId )
throws GroupManagementDAOException {
@ -439,13 +500,15 @@ public class GroupDAOImpl implements GroupDAO {
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 ? , ?" ;
"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 ) ;
@ -463,11 +526,257 @@ public class GroupDAOImpl implements GroupDAO {
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while retrieving information of all " +
"registered devices" , e ) ;
"registered devices" , e ) ;
} finally {
DeviceManagementDAOUtil . cleanupResources ( stmt , rs ) ;
}
return devices ;
}
@Override
public List < String > getRoles ( int groupId , int tenantId ) throws GroupManagementDAOException {
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
List < String > userRoles ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ROLE FROM DM_ROLE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , groupId ) ;
stmt . setInt ( 2 , tenantId ) ;
resultSet = stmt . executeQuery ( ) ;
userRoles = new ArrayList < > ( ) ;
while ( resultSet . next ( ) ) {
userRoles . add ( resultSet . getString ( "ROLE" ) ) ;
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while listing all groups in tenant: " + tenantId , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , resultSet ) ;
}
return userRoles ;
}
@Override
public void addRole ( int groupId , String role , int tenantId ) throws GroupManagementDAOException {
PreparedStatement stmt = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "INSERT INTO DM_ROLE_GROUP_MAP(GROUP_ID, ROLE, TENANT_ID) VALUES (?, ?, ?)" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , groupId ) ;
stmt . setString ( 2 , role ) ;
stmt . setInt ( 3 , tenantId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while adding new user role to Group." , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , null ) ;
}
}
@Override
public void removeRole ( int groupId , String role , int tenantId ) throws GroupManagementDAOException {
PreparedStatement stmt = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "DELETE FROM DM_ROLE_GROUP_MAP WHERE GROUP_ID = ? AND ROLE = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , groupId ) ;
stmt . setString ( 2 , role ) ;
stmt . setInt ( 3 , tenantId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while removing device from Group." , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , null ) ;
}
}
@Override
public List < DeviceGroup > getGroups ( String [ ] roles , int tenantId ) throws GroupManagementDAOException {
int rolesCount = roles . length ;
if ( rolesCount = = 0 ) {
return new ArrayList < > ( ) ;
}
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
List < DeviceGroup > deviceGroupList = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP g, " +
"(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN (" ;
int index = 0 ;
while ( index + + < rolesCount - 1 ) {
sql + = "?," ;
}
sql + = "?)) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID" ;
stmt = conn . prepareStatement ( sql ) ;
index = 0 ;
while ( index + + < rolesCount ) {
stmt . setString ( index , roles [ index - 1 ] ) ;
}
stmt . setInt ( index , tenantId ) ;
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 < Integer > getGroupIds ( String [ ] roles , int tenantId ) throws GroupManagementDAOException {
if ( roles . length = = 0 ) {
return new ArrayList < > ( ) ;
}
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
List < Integer > deviceGroupIdList = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ID FROM DM_GROUP g, (SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN (" ;
int rolesCount = roles . length ;
for ( int i = 0 ; i < rolesCount ; i + + ) {
sql + = ( rolesCount - 1 ! = i ) ? "?," : "?" ;
}
sql + = ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID" ;
stmt = conn . prepareStatement ( sql ) ;
int index = 0 ;
while ( index + + < rolesCount ) {
stmt . setString ( index , roles [ index - 1 ] ) ;
}
stmt . setInt ( index , tenantId ) ;
resultSet = stmt . executeQuery ( ) ;
deviceGroupIdList = new ArrayList < > ( ) ;
while ( resultSet . next ( ) ) {
deviceGroupIdList . add ( resultSet . getInt ( "ID" ) ) ;
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while listing all groups in tenant: " + tenantId , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , resultSet ) ;
}
return deviceGroupIdList ;
}
@Override
public int getGroupsCount ( String [ ] roles , int tenantId ) throws GroupManagementDAOException {
int rolesCount = roles . length ;
if ( rolesCount = = 0 ) {
return 0 ;
}
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP g, " +
"(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN (" ;
for ( int i = 0 ; i < rolesCount ; i + + ) {
sql + = ( rolesCount - 1 ! = i ) ? "?," : "?" ;
}
sql + = ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID" ;
stmt = conn . prepareStatement ( sql ) ;
int index = 0 ;
while ( index + + < rolesCount ) {
stmt . setString ( index , roles [ index - 1 ] ) ;
}
stmt . setInt ( index , tenantId ) ;
resultSet = stmt . executeQuery ( ) ;
if ( resultSet . next ( ) ) {
return resultSet . getInt ( "GROUP_COUNT" ) ;
} else {
return 0 ;
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while getting permitted groups count." , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , resultSet ) ;
}
}
@Override
public List < DeviceGroup > getOwnGroups ( String username , int tenantId ) throws GroupManagementDAOException {
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
List < DeviceGroup > deviceGroupList = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE OWNER = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setString ( 1 , username ) ;
stmt . setInt ( 2 , tenantId ) ;
resultSet = stmt . executeQuery ( ) ;
deviceGroupList = new ArrayList < > ( ) ;
while ( resultSet . next ( ) ) {
deviceGroupList . add ( GroupManagementDAOUtil . loadGroup ( resultSet ) ) ;
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while getting own groups of user '"
+ username + "'" , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , resultSet ) ;
}
return deviceGroupList ;
}
@Override
public List < Integer > getOwnGroupIds ( String username , int tenantId ) throws GroupManagementDAOException {
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
List < Integer > deviceGroupIdList = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT ID FROM DM_GROUP WHERE OWNER = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setString ( 1 , username ) ;
stmt . setInt ( 2 , tenantId ) ;
resultSet = stmt . executeQuery ( ) ;
deviceGroupIdList = new ArrayList < > ( ) ;
while ( resultSet . next ( ) ) {
deviceGroupIdList . add ( resultSet . getInt ( "ID" ) ) ;
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while getting own groups of user '"
+ username + "'" , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , resultSet ) ;
}
return deviceGroupIdList ;
}
@Override
public int getOwnGroupsCount ( String username , int tenantId ) throws GroupManagementDAOException {
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
try {
Connection conn = GroupManagementDAOFactory . getConnection ( ) ;
String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP WHERE OWNER = ? AND TENANT_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setString ( 1 , username ) ;
stmt . setInt ( 2 , tenantId ) ;
resultSet = stmt . executeQuery ( ) ;
if ( resultSet . next ( ) ) {
return resultSet . getInt ( "GROUP_COUNT" ) ;
} else {
return 0 ;
}
} catch ( SQLException e ) {
throw new GroupManagementDAOException ( "Error occurred while getting own groups count of user '"
+ username + "'" , e ) ;
} finally {
GroupManagementDAOUtil . cleanupResources ( stmt , resultSet ) ;
}
}
}