public CompletableFuture<List<DeviceNotification>> find(NotificationSearchRequest request) { return find(request.getDeviceIds(), request.getNames(), request.getTimestampStart(), request.getTimestampEnd(), request.getSortField(), request.getSortOrder(), request.getTake(), request.getSkip()); }
public static NotificationSearchRequest createNotificationSearchRequest(JsonObject request) { NotificationSearchRequest notificationSearchRequest = new GsonBuilder() .registerTypeAdapter(Date.class, new TimestampAdapter()) .excludeFieldsWithModifiers(Modifier.PROTECTED) .create() .fromJson(request, NotificationSearchRequest.class); notificationSearchRequest.setTake(Optional.ofNullable(notificationSearchRequest.getTake()).orElse(DEFAULT_TAKE)); notificationSearchRequest.setSkip(Optional.ofNullable(notificationSearchRequest.getSkip()).orElse(DEFAULT_SKIP)); if (CollectionUtils.isEmpty(notificationSearchRequest.getDeviceIds())) { Optional.ofNullable(request.get(DEVICE_ID)).map(JsonElement::getAsString).ifPresent(deviceId -> { notificationSearchRequest.setDeviceIds(Collections.singleton(deviceId)); }); } if (CollectionUtils.isEmpty(notificationSearchRequest.getNames())) { Optional.ofNullable(request.get(NOTIFICATION)).map(JsonElement::getAsString).ifPresent(notification -> { notificationSearchRequest.setNames(Collections.singleton(notification)); }); } return notificationSearchRequest; }
@SuppressWarnings("unchecked") public CompletableFuture<List<DeviceNotification>> find(Set<String> deviceIds, Set<String> names, Date timestampSt, Date timestampEnd, String sortField, String sortOrder, Integer take, Integer skip) { NotificationSearchRequest searchRequest = new NotificationSearchRequest(); searchRequest.setDeviceIds(deviceIds); searchRequest.setNames(names); searchRequest.setTimestampStart(timestampSt); searchRequest.setTimestampEnd(timestampEnd); searchRequest.setSortField(sortField); searchRequest.setSortOrder(sortOrder); searchRequest.setTake(take); searchRequest.setSkip(skip); CompletableFuture<Response> future = new CompletableFuture<>(); rpcClient.call(Request.newBuilder() .withBody(searchRequest) .withPartitionKey(searchRequest.getDeviceId()) .build(), new ResponseConsumer(future)); // CompletableFuture<Response> => CompletableFuture<List<DeviceNotification>> return future.thenApply(v -> v.getBody().cast(NotificationSearchResponse.class).getNotifications()); }
public CompletableFuture<Optional<DeviceNotification>> findOne(Long id, String deviceId) { NotificationSearchRequest searchRequest = new NotificationSearchRequest(); searchRequest.setId(id); searchRequest.setDeviceIds(Collections.singleton(deviceId)); CompletableFuture<Response> future = new CompletableFuture<>(); rpcClient.call(Request.newBuilder() .withBody(searchRequest) .withPartitionKey(deviceId) .build(), new ResponseConsumer(future)); return future.thenApply(r -> ((NotificationSearchResponse) r.getBody()).getNotifications().stream().findFirst()); }
@Override public Response handle(Request request) { NotificationSearchRequest searchRequest = (NotificationSearchRequest) request.getBody(); NotificationSearchResponse payload = searchRequest.getId() != null && !StringUtils.isEmpty(searchRequest.getDeviceId()) ? searchSingleNotificationByDeviceAndId(searchRequest.getId(), searchRequest.getDeviceId()) : searchMultipleNotifications(searchRequest); return Response.newBuilder() .withBody(payload) .buildSuccess(); }
@HiveWebsocketAuth @PreAuthorize("isAuthenticated() and hasPermission(null, 'GET_DEVICE_NOTIFICATION')") public void processNotificationList(JsonObject request, WebSocketSession session) { NotificationSearchRequest notificationSearchRequest = createNotificationSearchRequest(request); String deviceId = notificationSearchRequest.getDeviceId(); if (deviceId == null) { logger.error("notification/list proceed with error. Device ID should be provided."); throw new HiveException(Messages.DEVICE_ID_REQUIRED, SC_BAD_REQUEST); } logger.debug("Device notification query requested for device {}", deviceId); DeviceVO byIdWithPermissionsCheck = deviceService.findById(deviceId); if (byIdWithPermissionsCheck == null) { logger.error("notification/get proceed with error. No Device with Device ID = {} found.", deviceId); throw new HiveException(String.format(Messages.DEVICE_NOT_FOUND, deviceId), SC_NOT_FOUND); } WebSocketResponse response = new WebSocketResponse(); notificationService.find(notificationSearchRequest) .thenAccept(sortedDeviceNotifications -> { response.addValue(NOTIFICATIONS, sortedDeviceNotifications, NOTIFICATION_TO_CLIENT); clientHandler.sendMessage(request, response, session); }); }
private NotificationSearchResponse searchMultipleNotifications(NotificationSearchRequest searchRequest) { //TODO [rafa] has response is quite bad, instead we should separate command and reply into two separate collections. final Collection<DeviceNotification> notifications = hazelcastService.find( searchRequest.getDeviceIds(), searchRequest.getNames(), getTotal(searchRequest.getSkip(), searchRequest.getTake()), searchRequest.getTimestampStart(), searchRequest.getTimestampEnd(), false, null, DeviceNotification.class); final Comparator<DeviceNotification> comparator = buildDeviceNotificationComparator(searchRequest.getSortField()); String sortOrder = searchRequest.getSortOrder(); final Boolean reverse = sortOrder == null ? null : "desc".equalsIgnoreCase(sortOrder); final List<DeviceNotification> sortedDeviceNotifications = orderAndLimit(new ArrayList<>(notifications), comparator, reverse, searchRequest.getSkip(), searchRequest.getTake()); return new NotificationSearchResponse(new ArrayList<>(sortedDeviceNotifications)); }