@Transactional(readOnly = true) public DeviceVO findById(String deviceId) { DeviceVO device = deviceDao.findById(deviceId); if (device == null) { logger.error("Device with ID {} not found", deviceId); throw new HiveException(String.format(Messages.DEVICE_NOT_FOUND, deviceId), NOT_FOUND.getStatusCode()); } return device; }
private List<DeviceVO> getDeviceList(List<String> deviceIds, HivePrincipal principal) { return deviceDao.getDeviceList(deviceIds, principal); } }
@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); } }
@Transactional public boolean deleteDevice(@NotNull String deviceId) { DeviceVO deviceVO = deviceDao.findById(deviceId); if (deviceVO == null) { logger.error("Device with ID {} not found", deviceId); return false; } DeviceDeleteRequest deviceDeleteRequest = new DeviceDeleteRequest(deviceVO); Request request = Request.newBuilder() .withBody(deviceDeleteRequest) .build(); CompletableFuture<String> future = new CompletableFuture<>(); Consumer<Response> responseConsumer = response -> { Action resAction = response.getBody().getAction(); if (resAction.equals(Action.DEVICE_DELETE_RESPONSE)) { future.complete(response.getBody().getAction().name()); } else { logger.warn("Unknown action received from backend {}", resAction); } }; rpcClient.call(request, responseConsumer); return deviceDao.deleteById(deviceId) != 0; }
@Override public Response handle(Request request) { final CountDeviceRequest req = (CountDeviceRequest) request.getBody(); final long count = deviceDao.count(req.getName(), req.getNamePattern(), req.getNetworkId(), req.getNetworkName(), req.getPrincipal()); final CountResponse countResponse = new CountResponse(count); return Response.newBuilder() .withBody(countResponse) .buildSuccess(); } }
@Override public Response handle(Request request) { final ListDeviceRequest req = (ListDeviceRequest) request.getBody(); final List<DeviceVO> devices = deviceDao.list(req.getName(), req.getNamePattern(), req.getNetworkId(), req.getNetworkName(), req.getSortField(), req.isSortOrderAsc(), req.getTake(), req.getSkip(), req.getPrincipal()); return Response.newBuilder() .withBody(new ListDeviceResponse(devices)) .buildSuccess(); } }
DeviceVO existingDevice = deviceDao.findById(deviceId); if (existingDevice == null) { DeviceVO device = deviceUpdate.convertTo(deviceId); device.setBlocked(false); deviceDao.persist(device); return ServerResponsesFactory.createNotificationForDevice(device, SpecialNotifications.DEVICE_ADD); } else { existingDevice.setBlocked(deviceUpdate.getBlocked().get()); deviceDao.merge(existingDevice); return ServerResponsesFactory.createNotificationForDevice(existingDevice, SpecialNotifications.DEVICE_UPDATE);