@Transactional public DeviceNotification deviceSave(String deviceId, DeviceUpdate deviceUpdate) { logger.debug("Device save executed for device update: id {}", deviceId); Long networkId = deviceUpdate.getNetworkId().isPresent() ? deviceUpdate.getNetworkId().get() : null; //TODO [requires a lot of details] DeviceVO existingDevice = deviceDao.findById(deviceId); if (existingDevice == null) { DeviceVO device = deviceUpdate.convertTo(deviceId); device.setNetworkId(networkId); deviceDao.persist(device); return ServerResponsesFactory.createNotificationForDevice(device, SpecialNotifications.DEVICE_ADD); } else { if (deviceUpdate.getData().isPresent()){ existingDevice.setData(deviceUpdate.getData().get()); } if (deviceUpdate.getNetworkId().isPresent()){ existingDevice.setNetworkId(networkId); } if (deviceUpdate.getBlocked().isPresent()){ existingDevice.setBlocked(deviceUpdate.getBlocked().get()); } deviceDao.merge(existingDevice); return ServerResponsesFactory.createNotificationForDevice(existingDevice, SpecialNotifications.DEVICE_UPDATE); } }
public DeviceVO convertTo(String deviceId) { DeviceVO device = new DeviceVO(); if (deviceId != null){ device.setDeviceId(deviceId); } if (this.data != null){ device.setData(this.data); } if (this.name != null){ device.setName(this.name); } if (this.networkId != null){ device.setNetworkId(this.networkId); } if (this.deviceTypeId != null){ device.setDeviceTypeId(this.deviceTypeId); } if (this.blocked != null){ device.setBlocked(this.blocked); } return device; } }
existingDevice.setData(deviceUpdate.getData().orElse(null));
public static DeviceVO convertToVo(Device dc) { DeviceVO vo = null; if (dc != null) { vo = new DeviceVO(); vo.setId(dc.getId()); vo.setDeviceId(dc.getDeviceId()); vo.setName(dc.getName()); vo.setData(dc.getData()); vo.setBlocked(dc.getBlocked()); NetworkVO networkVO = Network.convertNetwork(dc.getNetwork()); vo.setNetworkId(networkVO.getId()); DeviceTypeVO deviceTypeVO = DeviceType.convertDeviceType(dc.getDeviceType()); vo.setDeviceTypeId(deviceTypeVO.getId()); } return vo; }