public static CommandSearchRequest createCommandSearchRequest(JsonObject request) { CommandSearchRequest commandSearchRequest = new GsonBuilder() .registerTypeAdapter(Date.class, new TimestampAdapter()) .excludeFieldsWithModifiers(Modifier.PROTECTED) .create() .fromJson(request, CommandSearchRequest.class); commandSearchRequest.setTake(Optional.ofNullable(commandSearchRequest.getTake()).orElse(DEFAULT_TAKE)); commandSearchRequest.setSkip(Optional.ofNullable(commandSearchRequest.getSkip()).orElse(DEFAULT_SKIP)); if (CollectionUtils.isEmpty(commandSearchRequest.getDeviceIds())) { Optional.ofNullable(request.get(DEVICE_ID)).map(JsonElement::getAsString).ifPresent(deviceId -> { commandSearchRequest.setDeviceIds(Collections.singleton(deviceId)); }); } if (CollectionUtils.isEmpty(commandSearchRequest.getNames())) { Optional.ofNullable(request.get(COMMAND)).map(JsonElement::getAsString).ifPresent(command -> { commandSearchRequest.setNames(Collections.singleton(command)); }); } return commandSearchRequest; }
public CompletableFuture<List<DeviceCommand>> find(Collection<String> deviceIds, Collection<String> names, Date timestampSt, Date timestampEnd, String status, String sortField, String sortOrder, Integer take, Integer skip) { CommandSearchRequest searchRequest = new CommandSearchRequest(); searchRequest.setDeviceIds(Sets.newHashSet(deviceIds)); if (names != null) { searchRequest.setNames(Sets.newHashSet(names)); } searchRequest.setTimestampStart(timestampSt); searchRequest.setTimestampEnd(timestampEnd); searchRequest.setStatus(status); 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<DeviceCommand>> return future.thenApply(v -> v.getBody().cast(CommandSearchResponse.class).getCommands()); }