Search in sources :

Example 1 with ConnectedCupDeviceMgtPluginException

use of org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException in project product-iots by wso2.

the class ConnectedCupDAO method addDevice.

public boolean addDevice(Device connectedCupDevice) throws ConnectedCupDeviceMgtPluginException {
    boolean status = false;
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = ConnectedCupDAOUtil.getConnection();
        String createDBQuery = "INSERT INTO CONNECTED_CUP_DEVICE(CONNECTED_CUP_DEVICE_ID, DEVICE_NAME ) VALUES (?, ?)";
        stmt = conn.prepareStatement(createDBQuery);
        stmt.setString(1, connectedCupDevice.getDeviceIdentifier());
        stmt.setString(2, connectedCupDevice.getName());
        int rows = stmt.executeUpdate();
        if (rows > 0) {
            status = true;
            if (log.isDebugEnabled()) {
                log.debug("Connected Cup device " + connectedCupDevice.getDeviceIdentifier() + " data has been added to the Connected Cup database.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while adding the Connected Cup device '" + connectedCupDevice.getDeviceIdentifier() + "' to the Connected Cup db.";
        log.error(msg, e);
        throw new ConnectedCupDeviceMgtPluginException(msg, e);
    } finally {
        ConnectedCupUtils.cleanupResources(stmt, null);
    }
    return status;
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 2 with ConnectedCupDeviceMgtPluginException

use of org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException in project product-iots by wso2.

the class ConnectedCupDAO method getDevice.

public Device getDevice(String deviceId) throws ConnectedCupDeviceMgtPluginException {
    Connection conn = null;
    PreparedStatement stmt = null;
    Device connectedCupDevice = null;
    ResultSet resultSet = null;
    try {
        conn = ConnectedCupDAOUtil.getConnection();
        String selectDBQuery = "SELECT CONNECTED_CUP_DEVICE_ID, DEVICE_NAME FROM CONNECTED_CUP_DEVICE WHERE CONNECTED_CUP_DEVICE_ID = ?";
        stmt = conn.prepareStatement(selectDBQuery);
        stmt.setString(1, deviceId);
        resultSet = stmt.executeQuery();
        if (resultSet.next()) {
            connectedCupDevice = new Device();
            connectedCupDevice.setName(resultSet.getString(ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_NAME));
            if (log.isDebugEnabled()) {
                log.debug("Connected Cup service " + deviceId + " data has been fetched from" + "Connected Cup database.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while fetching Connected Cup device : '" + deviceId + "'";
        log.error(msg, e);
        throw new ConnectedCupDeviceMgtPluginException(msg, e);
    } finally {
        ConnectedCupUtils.cleanupResources(stmt, resultSet);
        ConnectedCupDAOUtil.closeConnection();
    }
    return connectedCupDevice;
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) SQLException(java.sql.SQLException) Device(org.wso2.carbon.device.mgt.common.Device) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with ConnectedCupDeviceMgtPluginException

use of org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException in project product-iots by wso2.

the class ConnectedCupManager method modifyEnrollment.

@Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Modifying the Connected Cup device enrollment data");
        }
        ConnectedCupDAOUtil.beginTransaction();
        status = CONNECTED_CUP_DAO_UTIL.getConnectedCupDeviceDAO().updateDevice(device);
        ConnectedCupDAOUtil.commitTransaction();
    } catch (ConnectedCupDeviceMgtPluginException e) {
        try {
            ConnectedCupDAOUtil.rollbackTransaction();
        } catch (ConnectedCupDeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the update device transaction :" + device.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while updating the enrollment of the Connected Cup device : " + device.getDeviceIdentifier();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 4 with ConnectedCupDeviceMgtPluginException

use of org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException in project product-iots by wso2.

the class ConnectedCupManager method disenrollDevice.

@Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Dis-enrolling Connected Cup device : " + deviceId);
        }
        ConnectedCupDAOUtil.beginTransaction();
        status = CONNECTED_CUP_DAO_UTIL.getConnectedCupDeviceDAO().deleteDevice(deviceId.getId());
        ConnectedCupDAOUtil.commitTransaction();
    } catch (ConnectedCupDeviceMgtPluginException e) {
        try {
            ConnectedCupDAOUtil.rollbackTransaction();
        } catch (ConnectedCupDeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while removing the Connected Cup device : " + deviceId.getId();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 5 with ConnectedCupDeviceMgtPluginException

use of org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException in project product-iots by wso2.

the class ConnectedCupDAOUtil method beginTransaction.

public static void beginTransaction() throws ConnectedCupDeviceMgtPluginException {
    try {
        Connection conn = dataSource.getConnection();
        conn.setAutoCommit(false);
        currentConnection.set(conn);
    } catch (SQLException e) {
        throw new ConnectedCupDeviceMgtPluginException("Error occurred while retrieving datasource connection", e);
    }
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Aggregations

ConnectedCupDeviceMgtPluginException (org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException)10 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 PreparedStatement (java.sql.PreparedStatement)5 DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)4 ResultSet (java.sql.ResultSet)2 Device (org.wso2.carbon.device.mgt.common.Device)2 ArrayList (java.util.ArrayList)1