Search in sources :

Example 1 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeDAOImpl method addDevice.

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

Example 2 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeDAOImpl method deleteDevice.

public boolean deleteDevice(String deviceId) throws DeviceMgtPluginException {
    boolean status = false;
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = DeviceTypeDAO.getConnection();
        String deleteDBQuery = "DELETE FROM sampledevice_DEVICE WHERE sampledevice_DEVICE_ID = ?";
        stmt = conn.prepareStatement(deleteDBQuery);
        stmt.setString(1, deviceId);
        int rows = stmt.executeUpdate();
        if (rows > 0) {
            status = true;
            if (log.isDebugEnabled()) {
                log.debug("sampledevice device " + deviceId + " data has deleted" + " from the sampledevice database.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while deleting sampledevice device " + deviceId;
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, null);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 3 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeDAOImpl method getAllDevices.

public List<Device> getAllDevices() throws DeviceMgtPluginException {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet resultSet = null;
    Device device;
    List<Device> iotDevices = new ArrayList<>();
    try {
        conn = DeviceTypeDAO.getConnection();
        String selectDBQuery = "SELECT sampledevice_DEVICE_ID, DEVICE_NAME " + "FROM sampledevice_DEVICE";
        stmt = conn.prepareStatement(selectDBQuery);
        resultSet = stmt.executeQuery();
        while (resultSet.next()) {
            device = new Device();
            device.setDeviceIdentifier(resultSet.getString(DeviceTypeConstants.DEVICE_PLUGIN_DEVICE_ID));
            device.setName(resultSet.getString(DeviceTypeConstants.DEVICE_PLUGIN_DEVICE_NAME));
            List<Device.Property> propertyList = new ArrayList<>();
            device.setProperties(propertyList);
        }
        if (log.isDebugEnabled()) {
            log.debug("All sampledevice device details have fetched from sampledevice database.");
        }
        return iotDevices;
    } catch (SQLException e) {
        String msg = "Error occurred while fetching all sampledevice device data'";
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, resultSet);
        DeviceTypeDAO.closeConnection();
    }
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Device(org.wso2.carbon.device.mgt.common.Device) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 4 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeDAOImpl method updateDevice.

public boolean updateDevice(Device device) throws DeviceMgtPluginException {
    boolean status = false;
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = DeviceTypeDAO.getConnection();
        String updateDBQuery = "UPDATE sampledevice_DEVICE SET  DEVICE_NAME = ? WHERE sampledevice_DEVICE_ID = ?";
        stmt = conn.prepareStatement(updateDBQuery);
        if (device.getProperties() == null) {
            device.setProperties(new ArrayList<Device.Property>());
        }
        stmt.setString(1, device.getName());
        stmt.setString(2, device.getDeviceIdentifier());
        int rows = stmt.executeUpdate();
        if (rows > 0) {
            status = true;
            if (log.isDebugEnabled()) {
                log.debug("sampledevice device " + device.getDeviceIdentifier() + " data has been" + " modified.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while modifying the sampledevice device '" + device.getDeviceIdentifier() + "' data.";
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, null);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 5 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeManager method updateDeviceInfo.

@Override
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("updating the details of sampledevice device : " + deviceIdentifier);
        }
        DeviceTypeDAO.beginTransaction();
        status = deviceTypeDAO.getDeviceTypeDAO().updateDevice(device);
        DeviceTypeDAO.commitTransaction();
    } catch (DeviceMgtPluginException e) {
        try {
            DeviceTypeDAO.rollbackTransaction();
        } catch (DeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while updating the sampledevice device : " + deviceIdentifier;
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Aggregations

DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)12 SQLException (java.sql.SQLException)7 Connection (java.sql.Connection)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 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1 BundleContext (org.osgi.framework.BundleContext)1 DeviceTypeManagerService (org.wso2.iot.sampledevice.plugin.impl.DeviceTypeManagerService)1