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