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;
}
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;
}
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();
}
}
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;
}
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;
}
Aggregations