@ -56,6 +56,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.sql.SQLException ;
import java.sql.Timestamp ;
import java.sql.Timestamp ;
import java.sql.Statement ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Collection ;
import java.util.Collection ;
import java.util.Date ;
import java.util.Date ;
@ -1560,9 +1561,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
ps . setInt ( index + + , tenantId ) ;
ps . setInt ( index + + , tenantId ) ;
ps . setInt ( index , tenantId ) ;
ps . setInt ( index , tenantId ) ;
try ( ResultSet rs = ps . executeQuery ( ) ) {
try ( ResultSet rs = ps . executeQuery ( ) ) {
if ( rs . next ( ) ) {
while ( rs . next ( ) ) {
Device device = DeviceManagementDAOUtil . loadDevice ( rs ) ;
devices . add ( DeviceManagementDAOUtil . loadDevice ( rs ) ) ;
devices . add ( device ) ;
}
}
}
}
}
}
@ -1573,368 +1573,393 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
}
}
public void deleteDevice ( DeviceIdentifier deviceIdentifier , int tenantId ) throws DeviceManagementDAOException {
@Override
String deviceIdentifierId = deviceIdentifier . getId ( ) ;
public void deleteDevices ( List < String > deviceIdentifiers , List < Integer > deviceIds , List < Integer > enrollmentIds )
String deviceType = deviceIdentifier . getType ( ) ;
throws DeviceManagementDAOException {
Connection conn ;
Connection conn ;
try {
try {
conn = this . getConnection ( ) ;
conn = this . getConnection ( ) ;
int deviceId = getDeviceId ( conn , deviceIdentifier , tenantId ) ;
if ( enrollmentIds . isEmpty ( ) ) {
if ( deviceId = = - 1 ) {
String msg = "Enrollments not found for the devices: " + deviceIdentifiers ;
String msg = "Device " + deviceIdentifierId + " of type " + deviceType + " is not found" ;
log . error ( msg ) ;
log . error ( msg ) ;
throw new DeviceManagementDAOException ( msg ) ;
throw new DeviceManagementDAOException ( msg ) ;
} else {
} else {
List < Integer > enrollmentIds = getEnrollmentIds ( conn , deviceId , tenantId ) ;
removeDeviceDetail ( conn , deviceIds ) ;
if ( enrollmentIds = = null | | enrollmentIds . isEmpty ( ) ) {
String msg = "Enrollments not found for the device " + deviceIdentifierId + " of type "
+ deviceType ;
log . error ( msg ) ;
throw new DeviceManagementDAOException ( msg ) ;
} else {
removeDeviceDetail ( conn , deviceId ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device detail data of device " + deviceIdentifierId
log . debug ( "Successfully removed device detail data of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
removeDeviceLocation ( conn , deviceId ) ;
removeDeviceLocation ( conn , deviceIds ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device location data of device " + deviceIdentifierId
log . debug ( "Successfully removed device location data of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
removeDeviceInfo ( conn , deviceId ) ;
removeDeviceInfo ( conn , deviceId s ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device info data of device " + deviceIdentifierId
log . debug ( "Successfully removed device info data of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
removeDeviceNotification ( conn , deviceId ) ;
removeDeviceNotification ( conn , deviceId s ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device notification data of device " + deviceIdentifierId
log . debug ( "Successfully removed device notification data of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
removeDeviceApplicationMapping ( conn , deviceId ) ;
removeDeviceApplicationMapping ( conn , deviceId s ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device application mapping data of device "
log . debug ( "Successfully removed device application mapping data of device s: "
+ deviceIdentifierId + " of type " + deviceType ) ;
+ deviceIdentifiers ) ;
}
}
removeDevicePolicyApplied ( conn , deviceId ) ;
removeDevicePolicyApplied ( conn , deviceId s ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device applied policy data of device " + deviceIdentifierId
log . debug ( "Successfully removed device applied policy data of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
removeDevicePolicy ( conn , deviceId ) ;
removeDevicePolicy ( conn , deviceId s ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device policy data of device " + deviceIdentifierId
log . debug ( "Successfully removed device policy data of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Starting to remove " + enrollmentIds . size ( ) + " enrollment data of device "
log . debug ( "Starting to remove " + enrollmentIds . size ( ) + " enrollment data of devices with " +
+ deviceIdentifierId + " of type " + deviceType ) ;
"identifiers: " + deviceIdentifiers ) ;
}
}
for ( Integer enrollmentId : enrollmentIds ) {
removeEnrollmentDeviceDetail ( conn , enrollmentIds ) ;
removeEnrollmentDeviceDetail ( conn , enrollmentId ) ;
removeEnrollmentDeviceLocation ( conn , enrollmentIds ) ;
removeEnrollmentDeviceLocation ( conn , enrollmentId ) ;
removeEnrollmentDeviceInfo ( conn , enrollmentIds ) ;
removeEnrollmentDeviceInfo ( conn , enrollmentId ) ;
removeEnrollmentDeviceApplicationMapping ( conn , enrollmentIds ) ;
removeEnrollmentDeviceApplicationMapping ( conn , enrollmentId ) ;
removeDeviceOperationResponse ( conn , enrollmentIds ) ;
removeDeviceOperationResponse ( conn , enrollmentId ) ;
removeEnrollmentOperationMapping ( conn , enrollmentIds ) ;
removeEnrollmentOperationMapping ( conn , enrollmentId ) ;
}
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed enrollment device details, enrollment device location, " +
log . debug ( "Successfully removed enrollment device details, enrollment device location," +
"enrollment device info, enrollment device application mapping, " +
"enrollment device info, enrollment device application mapping, " +
"enrollment device operation response, enrollment operation mapping data of device "
"enrollment device operation response, enrollment operation mapping data of " +
+ deviceIdentifierId + " of type " + deviceType ) ;
"devices with identifiers: " + deviceIdentifiers ) ;
}
}
removeDeviceEnrollment ( conn , deviceId ) ;
removeDeviceEnrollment ( conn , deviceId s ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device enrollment data of device " + deviceIdentifierId
log . debug ( "Successfully removed device enrollment data of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
removeDeviceGroupMapping ( conn , deviceId ) ;
removeDeviceGroupMapping ( conn , deviceId s ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully removed device group mapping data of device " + deviceIdentifierId
log . debug ( "Successfully removed device group mapping data of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
removeDevice ( conn , deviceId ) ;
removeDevice ( conn , deviceId s ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Successfully permanently deleted the device of device " + deviceIdentifierId
log . debug ( "Successfully permanently deleted the device of devices: " + deviceIdentifiers ) ;
+ " of type " + deviceType ) ;
}
}
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while deleting the device " + deviceIdentifierId
String msg = "Error occurred while deleting the devices: " + deviceIdentifiers ;
+ " of type " + deviceType , e ) ;
log . error ( msg , e ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private int getDeviceId ( Connection conn , DeviceIdentifier deviceIdentifier , int tenantId )
/ * * *
throws DeviceManagementDAOException {
* This method removes records of a given list of devices from the DM_DEVICE_DETAIL table
PreparedStatement stmt = null ;
* @param conn Connection object
ResultSet rs = null ;
* @param deviceIds list of device ids ( primary keys )
int deviceId = - 1 ;
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDeviceDetail ( Connection conn , List < Integer > deviceIds ) throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "SELECT ID FROM DM_DEVICE WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setString ( 1 , deviceIdentifier . getId ( ) ) ;
stmt . setInt ( 2 , tenantId ) ;
rs = stmt . executeQuery ( ) ;
if ( rs . next ( ) ) {
deviceId = rs . getInt ( "ID" ) ;
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while retrieving device id of the device" , e ) ;
String msg = "Error occurred while removing device details." ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , rs ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
return deviceId ;
}
}
private List < Integer > getEnrollmentIds ( Connection conn , int deviceId , int tenantId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of devices from the DM_DEVICE_LOCATION table
ResultSet rs = null ;
* @param conn Connection object
List < Integer > enrollmentIds = new ArrayList < > ( ) ;
* @param deviceIds list of device ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDeviceLocation ( Connection conn , List < Integer > deviceIds ) throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "SELECT ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . setInt ( 2 , tenantId ) ;
rs = stmt . executeQuery ( ) ;
while ( rs . next ( ) ) {
enrollmentIds . add ( rs . getInt ( "ID" ) ) ;
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while retrieving enrollment id of the device" , e ) ;
String msg = "Error occurred while obtaining locations of devices." ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , rs ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
return enrollmentIds ;
}
}
private void removeDeviceDetail ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of devices from the DM_DEVICE_INFO table
* @param conn Connection object
* @param deviceIds list of device ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDeviceInfo ( Connection conn , List < Integer > deviceIds ) throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device detail" , e ) ;
String msg = "Error occurred while removing device info." ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeDeviceLocation ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of devices from the DM_NOTIFICATION table
* @param conn Connection object
* @param deviceIds list of device ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDeviceNotification ( Connection conn , List < Integer > deviceIds ) throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_NOTIFICATION WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device location" , e ) ;
String msg = "Error occurred while removing device notifications." ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
private void removeDeviceInfo ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
PreparedStatement stmt = null ;
try {
String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device info" , e ) ;
} finally {
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
}
}
}
private void removeDeviceNotification ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of devices from the DM_DEVICE_APPLICATION_MAPPING table
* @param conn Connection object
* @param deviceIds list of device ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDeviceApplicationMapping ( Connection conn , List < Integer > deviceIds )
throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_NOTIFICATION WHERE DEVICE_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device notification" , e ) ;
String msg = "Error occurred while removing device application mapping" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeDeviceApplicationMapping ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of devices from the DM_DEVICE_POLICY_APPLIED table
* @param conn Connection object
* @param deviceIds list of device ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDevicePolicyApplied ( Connection conn , List < Integer > deviceIds )
throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device application mapping" , e ) ;
String msg = "Error occurred while removing policies applied on devices" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeDevicePolicyApplied ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of devices from the DM_DEVICE_POLICY table
* @param conn Connection object
* @param deviceIds list of device ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDevicePolicy ( Connection conn , List < Integer > deviceIds ) throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device policy applied" , e ) ;
String msg = "Error occurred while removing policies of devices" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeDevicePolicy ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of enrollments from the DM_DEVICE_DETAIL table
* @param conn Connection object
* @param enrollmentIds list of enrollment ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeEnrollmentDeviceDetail ( Connection conn , List < Integer > enrollmentIds )
throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE ENROLMENT_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ?" ;
executeBatchOperation ( conn , sql , enrollmentIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device policy" , e ) ;
String msg = "Error occurred while removing enrollment details of devices" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeEnrollmentDeviceDetail ( Connection conn , int enrollmentId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of enrollments from the DM_DEVICE_LOCATION table
* @param conn Connection object
* @param enrollmentIds list of enrollment ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeEnrollmentDeviceLocation ( Connection conn , List < Integer > enrollmentIds )
throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE ENROLMENT_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE ENROLMENT_ID = ?" ;
executeBatchOperation ( conn , sql , enrollmentIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , enrollmentId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing enrollment device detail" , e ) ;
String msg = "Error occurred while removing enrollment locations of devices" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeEnrollmentDeviceLocation ( Connection conn , int enrollmentId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of enrollments from the DM_DEVICE_INFO table
* @param conn Connection object
* @param enrollmentIds list of enrollment ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeEnrollmentDeviceInfo ( Connection conn , List < Integer > enrollmentIds )
throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_INFO WHERE ENROLMENT_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE ENROLMENT_ID = ?" ;
executeBatchOperation ( conn , sql , enrollmentIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , enrollmentId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing enrollment device location" , e ) ;
String msg = "Error occurred while removing enrollment info of devices" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeEnrollmentDeviceInfo ( Connection conn , int enrollmentId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of enrollments from the DM_DEVICE_APPLICATION_MAPPING table
* @param conn Connection object
* @param enrollmentIds list of enrollment ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeEnrollmentDeviceApplicationMapping ( Connection conn , List < Integer > enrollmentIds )
throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE ENROLMENT_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_INFO WHERE ENROLMENT_ID = ?" ;
executeBatchOperation ( conn , sql , enrollmentIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , enrollmentId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing enrollment device info" , e ) ;
String msg = "Error occurred while removing enrollment device application mapping" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeEnrollmentDeviceApplicationMapping ( Connection conn , int enrollmentId )
/ * * *
* This method removes records of a given list of enrollments from the DM_DEVICE_OPERATION_RESPONSE table
* @param conn Connection object
* @param enrollmentIds list of enrollment ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDeviceOperationResponse ( Connection conn , List < Integer > enrollmentIds )
throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
PreparedStatement stmt = null ;
String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE ENROLMENT_ID = ?" ;
executeBatchOperation ( conn , sql , enrollmentIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , enrollmentId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing enrollment device application " +
String msg = "Error occurred while removing device operation response" ;
"mapping" , e ) ;
log . error ( msg , e ) ;
} finally {
throw new DeviceManagementDAOException ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
}
}
}
}
private void removeDeviceOperationResponse ( Connection conn , int enrollmentId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of enrollments from the DM_ENROLMENT_OP_MAPPING table
* @param conn Connection object
* @param enrollmentIds list of enrollment ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeEnrollmentOperationMapping ( Connection conn , List < Integer > enrollmentIds )
throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?" ;
executeBatchOperation ( conn , sql , enrollmentIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , enrollmentId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device operation response" , e ) ;
String msg = "Error occurred while removing enrollment operation mapping" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeEnrollmentOperationMapping ( Connection conn , int enrollmentId )
/ * * *
* This method removes records of a given list of enrollments from the DM_ENROLMENT table
* @param conn Connection object
* @param enrollmentIds list of enrollment ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDeviceEnrollment ( Connection conn , List < Integer > enrollmentIds )
throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
PreparedStatement stmt = null ;
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?" ;
executeBatchOperation ( conn , sql , enrollmentIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , enrollmentId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing enrollment operation mapping" , e ) ;
String msg = "Error occurred while removing enrollments of devices" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeDeviceEnrollment ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of devices from the DM_DEVICE_GROUP_MAP table
* @param conn Connection object
* @param deviceIds list of device ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDeviceGroupMapping ( Connection conn , List < Integer > deviceIds ) throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device enrollment" , e ) ;
String msg = "Error occurred while removing device group mapping" ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeDeviceGroupMapping ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method removes records of a given list of devices from the DM_DEVICE table
* @param conn Connection object
* @param deviceIds list of device ids ( primary keys )
* @throws DeviceManagementDAOException if deletion fails
* /
private void removeDevice ( Connection conn , List < Integer > deviceIds ) throws DeviceManagementDAOException {
String sql = "DELETE FROM DM_DEVICE WHERE ID = ?" ;
try {
try {
String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ?" ;
executeBatchOperation ( conn , sql , deviceIds ) ;
stmt = conn . prepareStatement ( sql ) ;
stmt . setInt ( 1 , deviceId ) ;
stmt . executeUpdate ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new DeviceManagementDAOException ( "Error occurred while removing device group mapping" , e ) ;
String msg = "Error occurred while removing devices." ;
} finally {
log . error ( msg , e ) ;
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
throw new DeviceManagementDAOException ( msg , e ) ;
}
}
}
}
private void removeDevice ( Connection conn , int deviceId ) throws DeviceManagementDAOException {
/ * * *
PreparedStatement stmt = null ;
* This method executes batch operations for a given list of primary keys
try {
* where the statement only has one param of type int , following the given pattern :
String sql = "DELETE FROM DM_DEVICE WHERE ID = ?" ;
* DELETE FROM TABLE WHERE ID = ?
stmt = conn . prepareStatement ( sql ) ;
* @param sql SQL statement
stmt . setInt ( 1 , deviceId ) ;
* @param conn Connection object
stmt . executeUpdate ( ) ;
* @param identifiers list of device ids ( primary keys )
} catch ( SQLException e ) {
* @throws SQLException if deletion fails .
throw new DeviceManagementDAOException ( "Error occurred while removing device" , e ) ;
* /
} finally {
private void executeBatchOperation ( Connection conn , String sql , List < Integer > identifiers ) throws SQLException {
DeviceManagementDAOUtil . cleanupResources ( stmt , null ) ;
try ( PreparedStatement ps = conn . prepareStatement ( sql ) ) {
if ( conn . getMetaData ( ) . supportsBatchUpdates ( ) ) {
for ( int identifier : identifiers ) {
ps . setInt ( 1 , identifier ) ;
ps . addBatch ( ) ;
}
for ( int i : ps . executeBatch ( ) ) {
if ( i = = 0 | | i = = Statement . SUCCESS_NO_INFO | | i = = Statement . EXECUTE_FAILED ) {
break ;
}
}
} else {
for ( int enrollmentId : identifiers ) {
ps . setInt ( 1 , enrollmentId ) ;
if ( ps . executeUpdate ( ) = = 0 ) {
break ;
}
}
}
}
}
}
}
}
}