public static org.wso2.carbon.device.mgt.common.DeviceType map(DeviceType deviceType) { org.wso2.carbon.device.mgt.common.DeviceType rv = new org.wso2.carbon.device.mgt.common.DeviceType(); rv.setId(deviceType.getId()); rv.setName(deviceType.getName()); rv.setDeviceTypeMetaDefinition(map(deviceType.getDeviceTypeMetaDefinition())); return rv; }
Statement.RETURN_GENERATED_KEYS ); stmt.setString(1, deviceType.getName()); String deviceMeta = null; if (deviceType.getDeviceTypeMetaDefinition() != null) { Gson gson = new Gson(); deviceMeta = gson.toJson(deviceType.getDeviceTypeMetaDefinition()); deviceTypeId = rs.getInt(1); deviceType.setId(deviceTypeId); return deviceType; } catch (SQLException e) { throw new DeviceManagementDAOException( "Error occurred while registering the device type '" + deviceType.getName() + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null);
public static DeviceType map(org.wso2.carbon.device.mgt.common.DeviceType deviceType) { DeviceType rv = new DeviceType(); rv.setId(deviceType.getId()); rv.setName(deviceType.getName()); rv.setDeviceTypeMetaDefinition(map(deviceType.getDeviceTypeMetaDefinition())); return rv; }
@Override public DeviceType updateDeviceType(DeviceType deviceType) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; try { conn = DeviceManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("UPDATE DM_DEVICE_TYPE SET DEVICE_TYPE_META = ? WHERE NAME = ?"); String deviceMeta = null; if (deviceType.getDeviceTypeMetaDefinition() != null) { Gson gson = new Gson(); deviceMeta = gson.toJson(deviceType.getDeviceTypeMetaDefinition()); } stmt.setString(1, deviceMeta); stmt.setString(2, deviceType.getName()); stmt.executeUpdate(); return deviceType; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating device type'" + deviceType.getName() + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } }
@Override public DeviceType updateDeviceType(DeviceType type, String name) throws DeviceManagementException { LOGGER.debug("Update device type"); if (name == null || !name.equals(type.getName())) { throw new DeviceManagementException("Invalid device type name: " + type.getName(), 404); } List<DeviceType> types = new ArrayList<>(); types.add(type); return updateDeviceTypes(types).get(0); }
@Override public List<DeviceType> updateDeviceTypes(List<DeviceType> types) throws DeviceManagementException { LOGGER.debug("Update device types"); try { DeviceManagementDAOFactory.beginTransaction(); List<DeviceType> updatedTypes = new ArrayList<>(); for (DeviceType type : types) { Optional<DeviceType> existingDeviceType = deviceTypeDAO.getDeviceType(type.getName()); if (existingDeviceType.isPresent()) { updatedTypes.add(deviceTypeDAO.updateDeviceType(type)); } else { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Device type: " + type.getName() + " does not exists.", 404); } } DeviceManagementDAOFactory.commitTransaction(); return updatedTypes; } catch (DeviceManagementDAOException e) { String msg = "Error occurred while obtaining the device types"; LOGGER.error(msg, e); DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while opening a connection to the data source"; LOGGER.error(msg, e); throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } }
rs = stmt.executeQuery(); if (rs.next()) { deviceType = new DeviceType(); deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); deviceType.setName(type); String devicetypeMeta = rs.getString("DEVICE_TYPE_META"); if (devicetypeMeta != null && devicetypeMeta.length() > 0) { Gson gson = new Gson(); deviceType.setDeviceTypeMetaDefinition(gson.fromJson(devicetypeMeta , DeviceTypeMetaDefinition.class));
@Override public List<DeviceType> addDeviceTypes(List<DeviceType> types) throws DeviceManagementException { LOGGER.debug("Add device type"); try { DeviceManagementDAOFactory.beginTransaction(); List<DeviceType> addedDeviceTypes = new ArrayList<>(); for (DeviceType type : types) { Optional<DeviceType> existingDeviceType = deviceTypeDAO.getDeviceType(type.getName()); if (!existingDeviceType.isPresent()) { addedDeviceTypes.add(deviceTypeDAO.addDeviceType(type)); } else { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Device type: " + type.getName() + " already exists.", 400); } } DeviceManagementDAOFactory.commitTransaction(); return addedDeviceTypes; } catch (DeviceManagementDAOException e) { String msg = "Error occurred while obtaining the device types"; LOGGER.error(msg, e); DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while opening a connection to the data source"; LOGGER.error(msg, e); throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } }
DeviceType deviceType = new DeviceType(); deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); deviceType.setName(rs.getString("DEVICE_TYPE")); String devicetypeMeta = rs.getString("DEVICE_TYPE_META"); if (devicetypeMeta != null && devicetypeMeta.length() > 0) { Gson gson = new Gson(); deviceType.setDeviceTypeMetaDefinition(gson.fromJson(devicetypeMeta, DeviceTypeMetaDefinition.class) );
DeviceType deviceType = null; while (rs.next()) { deviceType = new DeviceType(); deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); deviceType.setName(rs.getString("DEVICE_TYPE")); String devicetypeMeta = rs.getString("DEVICE_TYPE_META"); if (devicetypeMeta != null && devicetypeMeta.length() > 0) { Gson gson = new Gson(); deviceType.setDeviceTypeMetaDefinition(gson.fromJson(devicetypeMeta , DeviceTypeMetaDefinition.class));