@ -22,8 +22,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory ;
import org.apache.commons.logging.LogFactory ;
import org.homeautomation.doormanager.plugin.constants.DoorManagerConstants ;
import org.homeautomation.doormanager.plugin.constants.DoorManagerConstants ;
import org.homeautomation.doormanager.plugin.exception.DoorManagerDeviceMgtPluginException ;
import org.homeautomation.doormanager.plugin.exception.DoorManagerDeviceMgtPluginException ;
import org.homeautomation.doormanager.plugin.impl.dao.DoorManagerDAO ;
import org.homeautomation.doormanager.plugin.impl.dao.DoorLockSafe ;
import org.homeautomation.doormanager.plugin.impl.dao.DoorLockSafe ;
import org.homeautomation.doormanager.plugin.impl.dao.DoorManagerDAO ;
import org.homeautomation.doormanager.plugin.impl.dao.util.DoorManagerUtils ;
import org.homeautomation.doormanager.plugin.impl.dao.util.DoorManagerUtils ;
import org.wso2.carbon.device.mgt.common.Device ;
import org.wso2.carbon.device.mgt.common.Device ;
@ -32,9 +32,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
/ * *
/ * *
* Device Dao for automatic door locker Devices .
* Device Dao for automatic door locker Devices .
@ -42,434 +40,435 @@ import java.util.Map;
public class DoorManagerDAOImpl {
public class DoorManagerDAOImpl {
private static final Log log = LogFactory . getLog ( DoorManagerDAOImpl . class ) ;
private static final Log log = LogFactory . getLog ( DoorManagerDAOImpl . class ) ;
public Device getDevice ( String deviceId ) throws DoorManagerDeviceMgtPluginException {
public Device getDevice ( String deviceId ) throws DoorManagerDeviceMgtPluginException {
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
Device automaticDoorLockerDevice = null ;
Device automaticDoorLockerDevice = null ;
ResultSet resultSet = null ;
ResultSet resultSet = null ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery =
String selectDBQuery =
"SELECT doormanager_DEVICE_ID, DEVICE_NAME" +
"SELECT doormanager_DEVICE_ID, DEVICE_NAME" +
" FROM doormanager_DEVICE WHERE doormanager_DEVICE_ID = ?" ;
" FROM doormanager_DEVICE WHERE doormanager_DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , deviceId ) ;
stmt . setString ( 1 , deviceId ) ;
resultSet = stmt . executeQuery ( ) ;
resultSet = stmt . executeQuery ( ) ;
if ( resultSet . next ( ) ) {
if ( resultSet . next ( ) ) {
automaticDoorLockerDevice = new Device ( ) ;
automaticDoorLockerDevice = new Device ( ) ;
automaticDoorLockerDevice . setName ( resultSet . getString (
automaticDoorLockerDevice . setName ( resultSet . getString (
DoorManagerConstants . DEVICE_PLUGIN_DEVICE_NAME ) ) ;
DoorManagerConstants . DEVICE_PLUGIN_DEVICE_NAME ) ) ;
List < Device . Property > properties = new ArrayList < > ( ) ;
List < Device . Property > properties = new ArrayList < > ( ) ;
automaticDoorLockerDevice . setProperties ( properties ) ;
automaticDoorLockerDevice . setProperties ( properties ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Locker Manager service " + deviceId + " data has been fetched from" +
log . debug ( "Locker Manager service " + deviceId + " data has been fetched from" +
"Locker Manager database." ) ;
"Locker Manager database." ) ;
}
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while fetching Locker Manager device : '" + deviceId + "'" ;
String msg = "Error occurred while fetching Locker Manager device : '" + deviceId + "'" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerDAO . closeConnection ( ) ;
DoorManagerDAO . closeConnection ( ) ;
}
}
return automaticDoorLockerDevice ;
return automaticDoorLockerDevice ;
}
}
public boolean addDevice ( Device automaticDoorLOcker ) throws DoorManagerDeviceMgtPluginException {
public boolean addDevice ( Device automaticDoorLOcker ) throws DoorManagerDeviceMgtPluginException {
boolean status = false ;
boolean status = false ;
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String createDBQuery =
String createDBQuery =
"INSERT INTO doormanager_DEVICE(doormanager_DEVICE_ID, DEVICE_NAME, ACCESS_TOKEN, REFRESH_TOKEN) VALUES (?, ?, ?, ?)" ;
"INSERT INTO doormanager_DEVICE(doormanager_DEVICE_ID, DEVICE_NAME, ACCESS_TOKEN, REFRESH_TOKEN) VALUES (?, ?, ?, ?)" ;
stmt = conn . prepareStatement ( createDBQuery ) ;
stmt = conn . prepareStatement ( createDBQuery ) ;
stmt . setString ( 1 , automaticDoorLOcker . getDeviceIdentifier ( ) ) ;
stmt . setString ( 1 , automaticDoorLOcker . getDeviceIdentifier ( ) ) ;
stmt . setString ( 2 , automaticDoorLOcker . getName ( ) ) ;
stmt . setString ( 2 , automaticDoorLOcker . getName ( ) ) ;
stmt . setString ( 3 , DoorManagerUtils . getDeviceProperty (
stmt . setString ( 3 , DoorManagerUtils . getDeviceProperty (
automaticDoorLOcker . getProperties ( ) ,
automaticDoorLOcker . getProperties ( ) ,
DoorManagerConstants . DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN ) ) ;
DoorManagerConstants . DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN ) ) ;
stmt . setString ( 4 , DoorManagerUtils . getDeviceProperty (
stmt . setString ( 4 , DoorManagerUtils . getDeviceProperty (
automaticDoorLOcker . getProperties ( ) ,
automaticDoorLOcker . getProperties ( ) ,
DoorManagerConstants . DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN ) ) ;
DoorManagerConstants . DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN ) ) ;
int rows = stmt . executeUpdate ( ) ;
int rows = stmt . executeUpdate ( ) ;
if ( rows > 0 ) {
if ( rows > 0 ) {
status = true ;
status = true ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Locker Manager device " + automaticDoorLOcker . getDeviceIdentifier ( ) +
log . debug ( "Locker Manager device " + automaticDoorLOcker . getDeviceIdentifier ( ) +
" data has been added to the Locker Manager database." ) ;
" data has been added to the Locker Manager database." ) ;
}
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while adding the Locker Manager device '" +
String msg = "Error occurred while adding the Locker Manager device '" +
automaticDoorLOcker . getDeviceIdentifier ( ) + "' to the Locker Manager db." ;
automaticDoorLOcker . getDeviceIdentifier ( ) + "' to the Locker Manager db." ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
return status ;
return status ;
}
}
public boolean registerDoorLockSafe ( DoorLockSafe automaticDoorLOcker ) throws DoorManagerDeviceMgtPluginException {
public boolean registerDoorLockSafe ( DoorLockSafe automaticDoorLOcker ) throws DoorManagerDeviceMgtPluginException {
boolean status = false ;
boolean status = false ;
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String createDBQuery =
String createDBQuery =
"INSERT INTO REGISTERED_DOORLOCK_SAFE(doormanager_DEVICE_ID, SERIAL_NUMBER, UID_of_USER, POLICY, " +
"INSERT INTO REGISTERED_DOORLOCK_SAFE(doormanager_DEVICE_ID, SERIAL_NUMBER, UID_of_USER, POLICY, " +
"EMAIL_ADDRESS, ACCESS_TOKEN, REFRESH_TOKEN) VALUES (?, ?, ?, ?, ?, ?, ?)" ;
"EMAIL_ADDRESS, ACCESS_TOKEN, REFRESH_TOKEN) VALUES (?, ?, ?, ?, ?, ?, ?)" ;
stmt = conn . prepareStatement ( createDBQuery ) ;
stmt = conn . prepareStatement ( createDBQuery ) ;
stmt . setString ( 1 , automaticDoorLOcker . getDeviceId ( ) ) ;
stmt . setString ( 1 , automaticDoorLOcker . getDeviceId ( ) ) ;
stmt . setString ( 2 , automaticDoorLOcker . getSerialNumber ( ) ) ;
stmt . setString ( 2 , automaticDoorLOcker . getSerialNumber ( ) ) ;
stmt . setString ( 3 , automaticDoorLOcker . getUIDofUser ( ) ) ;
stmt . setString ( 3 , automaticDoorLOcker . getUIDofUser ( ) ) ;
stmt . setString ( 4 , automaticDoorLOcker . getPolicy ( ) ) ;
stmt . setString ( 4 , automaticDoorLOcker . getPolicy ( ) ) ;
stmt . setString ( 5 , automaticDoorLOcker . getEmailAddress ( ) ) ;
stmt . setString ( 5 , automaticDoorLOcker . getEmailAddress ( ) ) ;
stmt . setString ( 6 , automaticDoorLOcker . getAccessToken ( ) ) ;
stmt . setString ( 6 , automaticDoorLOcker . getAccessToken ( ) ) ;
stmt . setString ( 7 , automaticDoorLOcker . getRefreshToken ( ) ) ;
stmt . setString ( 7 , automaticDoorLOcker . getRefreshToken ( ) ) ;
int rows = stmt . executeUpdate ( ) ;
int rows = stmt . executeUpdate ( ) ;
if ( rows > 0 ) {
if ( rows > 0 ) {
status = true ;
status = true ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Locker Manager device " + automaticDoorLOcker . getOwner ( ) +
log . debug ( "Locker Manager device " + automaticDoorLOcker . getOwner ( ) +
" data has been added to the Locker Manager database." ) ;
" data has been added to the Locker Manager database." ) ;
}
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while adding the Locker Manager device '" +
String msg = "Error occurred while adding the Locker Manager device '" +
automaticDoorLOcker . getOwner ( ) + "' to the Locker Manager db." ;
automaticDoorLOcker . getOwner ( ) + "' to the Locker Manager db." ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
return status ;
return status ;
}
}
public boolean isDoorLockSafeRegistered ( String serialNumber , String deviceId ) throws DoorManagerDeviceMgtPluginException {
public boolean isDoorLockSafeRegistered ( String serialNumber , String deviceId ) throws DoorManagerDeviceMgtPluginException {
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
ResultSet resultSet = null ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery =
String selectDBQuery =
"SELECT SERIAL_NUMBER FROM REGISTERED_DOORLOCK_SAFE WHERE SERIAL_NUMBER = ? AND doormanager_DEVICE_ID = ?" ;
"SELECT SERIAL_NUMBER FROM REGISTERED_DOORLOCK_SAFE WHERE SERIAL_NUMBER = ? AND doormanager_DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , serialNumber ) ;
stmt . setString ( 1 , serialNumber ) ;
stmt . setString ( 2 , deviceId ) ;
stmt . setString ( 2 , deviceId ) ;
resultSet = stmt . executeQuery ( ) ;
resultSet = stmt . executeQuery ( ) ;
if ( resultSet . next ( ) ) {
if ( resultSet . next ( ) ) {
String result = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_DEVICE_SERIAL_NUMBER ) ;
String result = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_DEVICE_SERIAL_NUMBER ) ;
log . warn ( result ) ;
log . warn ( result ) ;
return true ;
return true ;
} else {
} else {
return false ;
return false ;
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while fetching all Automatic Door Locker device data'" ;
String msg = "Error occurred while fetching all Automatic Door Locker device data'" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerDAO . closeConnection ( ) ;
DoorManagerDAO . closeConnection ( ) ;
}
}
}
}
public boolean isUserAllowed ( String serialNumber , String UIDofUser , String deviceId ) throws DoorManagerDeviceMgtPluginException {
public boolean isUserAllowed ( String serialNumber , String UIDofUser , String deviceId ) throws DoorManagerDeviceMgtPluginException {
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
ResultSet resultSet = null ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery =
String selectDBQuery =
"SELECT UID_OF_USER FROM REGISTERED_DOORLOCK_SAFE WHERE SERIAL_NUMBER = ? AND DOORMANAGER_DEVICE_ID = ?" ;
"SELECT UID_OF_USER FROM REGISTERED_DOORLOCK_SAFE WHERE SERIAL_NUMBER = ? AND DOORMANAGER_DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , serialNumber ) ;
stmt . setString ( 1 , serialNumber ) ;
stmt . setString ( 2 , deviceId ) ;
stmt . setString ( 2 , deviceId ) ;
resultSet = stmt . executeQuery ( ) ;
resultSet = stmt . executeQuery ( ) ;
String result ;
String result ;
if ( resultSet . next ( ) ) {
if ( resultSet . next ( ) ) {
result = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_DEVICE_UID_OF_USER ) ;
result = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_DEVICE_UID_OF_USER ) ;
log . warn ( result ) ;
log . warn ( result ) ;
return true ;
return true ;
} else {
} else {
selectDBQuery =
selectDBQuery =
"SELECT UID_OF_USER FROM SHARED_DOORLOCK_SAFE WHERE SERIAL_NUMBER = ? AND DOORMANAGER_DEVICE_ID = ? AND UID_OF_USER = ?" ;
"SELECT UID_OF_USER FROM SHARED_DOORLOCK_SAFE WHERE SERIAL_NUMBER = ? AND DOORMANAGER_DEVICE_ID = ? AND UID_OF_USER = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , serialNumber ) ;
stmt . setString ( 1 , serialNumber ) ;
stmt . setString ( 2 , deviceId ) ;
stmt . setString ( 2 , deviceId ) ;
stmt . setString ( 3 , UIDofUser ) ;
stmt . setString ( 3 , UIDofUser ) ;
resultSet = stmt . executeQuery ( ) ;
resultSet = stmt . executeQuery ( ) ;
if ( resultSet . next ( ) ) {
if ( resultSet . next ( ) ) {
result = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_DEVICE_UID_OF_USER ) ;
result = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_DEVICE_UID_OF_USER ) ;
log . warn ( result ) ;
log . warn ( result ) ;
return true ;
return true ;
}
}
return false ;
return false ;
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while validating: whether user is registered or not" ;
String msg = "Error occurred while validating: whether user is registered or not" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerDAO . closeConnection ( ) ;
DoorManagerDAO . closeConnection ( ) ;
}
}
}
}
public boolean shareDoorLockSafe ( DoorLockSafe automaticDoorLOcker ) throws DoorManagerDeviceMgtPluginException {
public boolean shareDoorLockSafe ( DoorLockSafe automaticDoorLOcker ) throws DoorManagerDeviceMgtPluginException {
boolean status = false ;
boolean status = false ;
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String createDBQuery =
String createDBQuery =
"INSERT INTO SHARED_DOORLOCK_SAFE(doormanager_DEVICE_ID, SERIAL_NUMBER, UID_of_USER, POLICY) VALUES (?, ?, ?, ?)" ;
"INSERT INTO SHARED_DOORLOCK_SAFE(doormanager_DEVICE_ID, SERIAL_NUMBER, UID_of_USER, POLICY) VALUES (?, ?, ?, ?)" ;
stmt = conn . prepareStatement ( createDBQuery ) ;
stmt = conn . prepareStatement ( createDBQuery ) ;
stmt . setString ( 1 , automaticDoorLOcker . getDeviceId ( ) ) ;
stmt . setString ( 1 , automaticDoorLOcker . getDeviceId ( ) ) ;
stmt . setString ( 2 , automaticDoorLOcker . getSerialNumber ( ) ) ;
stmt . setString ( 2 , automaticDoorLOcker . getSerialNumber ( ) ) ;
stmt . setString ( 3 , automaticDoorLOcker . getUIDofUser ( ) ) ;
stmt . setString ( 3 , automaticDoorLOcker . getUIDofUser ( ) ) ;
stmt . setString ( 4 , automaticDoorLOcker . getPolicy ( ) ) ;
stmt . setString ( 4 , automaticDoorLOcker . getPolicy ( ) ) ;
int rows = stmt . executeUpdate ( ) ;
int rows = stmt . executeUpdate ( ) ;
if ( rows > 0 ) {
if ( rows > 0 ) {
status = true ;
status = true ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Locker Manager device " + automaticDoorLOcker . getOwner ( ) +
log . debug ( "Locker Manager device " + automaticDoorLOcker . getOwner ( ) +
" data has been added to the Locker Manager database." ) ;
" data has been added to the Locker Manager database." ) ;
}
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while adding the Locker Manager device '" +
String msg = "Error occurred while adding the Locker Manager device '" +
automaticDoorLOcker . getOwner ( ) + "' to the Locker Manager db." ;
automaticDoorLOcker . getOwner ( ) + "' to the Locker Manager db." ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
return status ;
return status ;
}
}
public boolean checkCardDoorAssociation ( String cardNum , String deviceID ) throws DoorManagerDeviceMgtPluginException {
Connection conn ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
try {
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery = "SELECT * FROM REGISTERED_DOORLOCK_SAFE WHERE UID_of_USER = ? AND doormanager_DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , cardNum ) ;
stmt . setString ( 2 , deviceID ) ;
resultSet = stmt . executeQuery ( ) ;
String result ;
if ( resultSet . next ( ) ) {
return true ;
} else {
return false ;
}
} catch ( SQLException e ) {
String msg = "No associations were found between lock : " + deviceID + " and card : " + cardNum ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
public boolean checkCardDoorAssociation ( String cardNum , String deviceID ) throws DoorManagerDeviceMgtPluginException {
Connection conn ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
try {
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery = "SELECT * FROM REGISTERED_DOORLOCK_SAFE WHERE UID_of_USER = ? AND doormanager_DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , cardNum ) ;
stmt . setString ( 2 , deviceID ) ;
resultSet = stmt . executeQuery ( ) ;
String result ;
if ( resultSet . next ( ) ) {
return true ;
} else {
return false ;
}
} catch ( SQLException e ) {
String msg = "No associations were found between lock : " + deviceID + " and card : " + cardNum ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
public String getUserEmailAddress ( String cardNum ) throws DoorManagerDeviceMgtPluginException {
Connection conn ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
String email ;
try {
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery = "SELECT EMAIL_ADDRESS FROM REGISTERED_DOORLOCK_SAFE WHERE UID_of_USER = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , cardNum ) ;
resultSet = stmt . executeQuery ( ) ;
if ( resultSet . next ( ) ) {
email = resultSet . getString ( "EMAIL_ADDRESS" ) ;
log . warn ( email ) ;
return email ;
}
return null ;
} catch ( SQLException e ) {
String msg = "No email found for the and card : " + cardNum ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
public String getUserEmailAddress ( String cardNum ) throws DoorManagerDeviceMgtPluginException {
public List < String > getUserCredentials ( String deviceId , String UIDofUser ) throws DoorManagerDeviceMgtPluginException {
Connection conn ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
String email ;
try {
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery = "SELECT EMAIL_ADDRESS FROM REGISTERED_DOORLOCK_SAFE WHERE UID_of_USER = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , cardNum ) ;
resultSet = stmt . executeQuery ( ) ;
if ( resultSet . next ( ) ) {
email = resultSet . getString ( "EMAIL_ADDRESS" ) ;
log . warn ( email ) ;
return email ;
}
return null ;
} catch ( SQLException e ) {
String msg = "No email found for the and card : " + cardNum ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
public List < String > getUserCredentials ( String deviceId , String UIDofUser ) throws DoorManagerDeviceMgtPluginException {
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
ResultSet resultSet = null ;
List < String > userCredentials = new ArrayList < > ( ) ;
List < String > userCredentials = new ArrayList < > ( ) ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery =
String selectDBQuery =
"SELECT ACCESS_TOKEN, REFRESH_TOKEN FROM REGISTERED_DOORLOCK_SAFE WHERE DOORMANAGER_DEVICE_ID = ? AND UID_OF_USER = ?" ;
"SELECT ACCESS_TOKEN, REFRESH_TOKEN FROM REGISTERED_DOORLOCK_SAFE WHERE DOORMANAGER_DEVICE_ID = ? AND UID_OF_USER = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , deviceId ) ;
stmt . setString ( 1 , deviceId ) ;
stmt . setString ( 2 , UIDofUser ) ;
stmt . setString ( 2 , UIDofUser ) ;
resultSet = stmt . executeQuery ( ) ;
resultSet = stmt . executeQuery ( ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Get user credentials from Automatic Door Locker database." ) ;
log . debug ( "Get user credentials from Automatic Door Locker database." ) ;
}
}
log . warn ( "getting user credentials" ) ;
log . warn ( "getting user credentials" ) ;
if ( resultSet . next ( ) ) {
if ( resultSet . next ( ) ) {
String accessToken = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN ) ;
String accessToken = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN ) ;
String refreshToken = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN ) ;
String refreshToken = resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN ) ;
log . warn ( accessToken ) ;
log . warn ( accessToken ) ;
userCredentials . add ( accessToken ) ;
userCredentials . add ( accessToken ) ;
userCredentials . add ( refreshToken ) ;
userCredentials . add ( refreshToken ) ;
}
}
return userCredentials ;
return userCredentials ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while getting user credentials" ;
String msg = "Error occurred while getting user credentials" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerDAO . closeConnection ( ) ;
DoorManagerDAO . closeConnection ( ) ;
}
}
}
}
public List < String > getRegisteredDoorLocks ( String deviceId ) throws DoorManagerDeviceMgtPluginException {
public List < String > getRegisteredDoorLocks ( String deviceId ) throws DoorManagerDeviceMgtPluginException {
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
ResultSet resultSet = null ;
List < String > doorLockSafes = new ArrayList < > ( ) ;
List < String > doorLockSafes = new ArrayList < > ( ) ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery =
String selectDBQuery =
"SELECT SERIAL_NUMBER FROM REGISTERED_DOORLOCK_SAFE WHERE doormanager_DEVICE_ID = ?" ;
"SELECT SERIAL_NUMBER FROM REGISTERED_DOORLOCK_SAFE WHERE doormanager_DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt . setString ( 1 , deviceId ) ;
stmt . setString ( 1 , deviceId ) ;
resultSet = stmt . executeQuery ( ) ;
resultSet = stmt . executeQuery ( ) ;
while ( resultSet . next ( ) ) {
while ( resultSet . next ( ) ) {
doorLockSafes . add ( resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_DEVICE_SERIAL_NUMBER ) ) ;
doorLockSafes . add ( resultSet . getString ( DoorManagerConstants . DEVICE_PLUGIN_DEVICE_SERIAL_NUMBER ) ) ;
}
}
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "All Locker Manager device details have fetched from Automatic Door Locker database." ) ;
log . debug ( "All Locker Manager device details have fetched from Automatic Door Locker database." ) ;
}
}
return doorLockSafes ;
return doorLockSafes ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while fetching all Automatic Door Locker device data'" ;
String msg = "Error occurred while fetching all Automatic Door Locker device data'" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerDAO . closeConnection ( ) ;
DoorManagerDAO . closeConnection ( ) ;
}
}
}
}
public boolean updateDevice ( Device automaticDoorLocker ) throws DoorManagerDeviceMgtPluginException {
public boolean updateDevice ( Device automaticDoorLocker ) throws DoorManagerDeviceMgtPluginException {
boolean status = false ;
boolean status = false ;
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String updateDBQuery =
String updateDBQuery =
"UPDATE doormanager_DEVICE SET DEVICE_NAME = ? WHERE doormanager_DEVICE_ID = ?" ;
"UPDATE doormanager_DEVICE SET DEVICE_NAME = ? WHERE doormanager_DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( updateDBQuery ) ;
stmt = conn . prepareStatement ( updateDBQuery ) ;
if ( automaticDoorLocker . getProperties ( ) = = null ) {
if ( automaticDoorLocker . getProperties ( ) = = null ) {
automaticDoorLocker . setProperties ( new ArrayList < Device . Property > ( ) ) ;
automaticDoorLocker . setProperties ( new ArrayList < Device . Property > ( ) ) ;
}
}
stmt . setString ( 1 , automaticDoorLocker . getName ( ) ) ;
stmt . setString ( 1 , automaticDoorLocker . getName ( ) ) ;
stmt . setString ( 2 , automaticDoorLocker . getDeviceIdentifier ( ) ) ;
stmt . setString ( 2 , automaticDoorLocker . getDeviceIdentifier ( ) ) ;
int rows = stmt . executeUpdate ( ) ;
int rows = stmt . executeUpdate ( ) ;
if ( rows > 0 ) {
if ( rows > 0 ) {
status = true ;
status = true ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Locker Manager device " + automaticDoorLocker . getDeviceIdentifier ( ) +
log . debug ( "Locker Manager device " + automaticDoorLocker . getDeviceIdentifier ( ) +
" data has been modified." ) ;
" data has been modified." ) ;
}
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while modifying the Locker Manager device '" +
String msg = "Error occurred while modifying the Locker Manager device '" +
automaticDoorLocker . getDeviceIdentifier ( ) + "' data." ;
automaticDoorLocker . getDeviceIdentifier ( ) + "' data." ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
return status ;
return status ;
}
}
public boolean deleteDevice ( String deviceId ) throws DoorManagerDeviceMgtPluginException {
public boolean deleteDevice ( String deviceId ) throws DoorManagerDeviceMgtPluginException {
boolean status = false ;
boolean status = false ;
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String deleteDBQuery =
String deleteDBQuery =
"DELETE FROM doormanager_DEVICE WHERE doormanager_DEVICE_ID = ?" ;
"DELETE FROM doormanager_DEVICE WHERE doormanager_DEVICE_ID = ?" ;
stmt = conn . prepareStatement ( deleteDBQuery ) ;
stmt = conn . prepareStatement ( deleteDBQuery ) ;
stmt . setString ( 1 , deviceId ) ;
stmt . setString ( 1 , deviceId ) ;
int rows = stmt . executeUpdate ( ) ;
int rows = stmt . executeUpdate ( ) ;
if ( rows > 0 ) {
if ( rows > 0 ) {
status = true ;
status = true ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Automatic Door Locker device " + deviceId + " data has deleted" +
log . debug ( "Automatic Door Locker device " + deviceId + " data has deleted" +
" from the Automatic Door Locker database." ) ;
" from the Automatic Door Locker database." ) ;
}
}
}
}
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while deleting Automatic Door Locker device " + deviceId ;
String msg = "Error occurred while deleting Automatic Door Locker device " + deviceId ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , null ) ;
DoorManagerUtils . cleanupResources ( stmt , null ) ;
}
}
return status ;
return status ;
}
}
public List < Device > getAllDevices ( ) throws DoorManagerDeviceMgtPluginException {
public List < Device > getAllDevices ( ) throws DoorManagerDeviceMgtPluginException {
Connection conn = null ;
Connection conn = null ;
PreparedStatement stmt = null ;
PreparedStatement stmt = null ;
ResultSet resultSet = null ;
ResultSet resultSet = null ;
Device connectedCupDevice ;
Device connectedCupDevice ;
List < Device > iotDevices = new ArrayList < > ( ) ;
List < Device > iotDevices = new ArrayList < > ( ) ;
try {
try {
conn = DoorManagerDAO . getConnection ( ) ;
conn = DoorManagerDAO . getConnection ( ) ;
String selectDBQuery =
String selectDBQuery =
"SELECT doormanager_DEVICE_ID, DEVICE_NAME FROM doormanager_DEVICE" ;
"SELECT doormanager_DEVICE_ID, DEVICE_NAME FROM doormanager_DEVICE" ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
stmt = conn . prepareStatement ( selectDBQuery ) ;
resultSet = stmt . executeQuery ( ) ;
resultSet = stmt . executeQuery ( ) ;
while ( resultSet . next ( ) ) {
while ( resultSet . next ( ) ) {
connectedCupDevice = new Device ( ) ;
connectedCupDevice = new Device ( ) ;
connectedCupDevice . setDeviceIdentifier ( resultSet . getString (
connectedCupDevice . setDeviceIdentifier ( resultSet . getString (
DoorManagerConstants . DEVICE_PLUGIN_DEVICE_ID ) ) ;
DoorManagerConstants . DEVICE_PLUGIN_DEVICE_ID ) ) ;
connectedCupDevice . setName ( resultSet . getString (
connectedCupDevice . setName ( resultSet . getString (
DoorManagerConstants . DEVICE_PLUGIN_DEVICE_NAME ) ) ;
DoorManagerConstants . DEVICE_PLUGIN_DEVICE_NAME ) ) ;
}
}
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "All Locker Manager device details have fetched from Automatic Door Locker database." ) ;
log . debug ( "All Locker Manager device details have fetched from Automatic Door Locker database." ) ;
}
}
return iotDevices ;
return iotDevices ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
String msg = "Error occurred while fetching all Automatic Door Locker device data'" ;
String msg = "Error occurred while fetching all Automatic Door Locker device data'" ;
log . error ( msg , e ) ;
log . error ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
throw new DoorManagerDeviceMgtPluginException ( msg , e ) ;
} finally {
} finally {
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerUtils . cleanupResources ( stmt , resultSet ) ;
DoorManagerDAO . closeConnection ( ) ;
DoorManagerDAO . closeConnection ( ) ;
}
}
}
}
}
}