public CompletableFuture<List<DeviceCommand>> find(CommandSearchRequest request) { return find(request.getDeviceIds(), request.getNames(), request.getTimestampStart(), request.getTimestampEnd(), request.getStatus(), request.getSortField(), request.getSortOrder(), request.getTake(), request.getSkip()); }
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()); }
@Override public Response handle(Request request) { CommandSearchRequest searchRequest = (CommandSearchRequest) request.getBody(); CommandSearchResponse payload = searchRequest.getId() != null && !StringUtils.isEmpty(searchRequest.getDeviceId()) ? searchSingleCommandByDeviceAndId(searchRequest.getId(), searchRequest.getDeviceId(), searchRequest.isReturnUpdated()) : searchMultipleCommands(searchRequest); return Response.newBuilder() .withBody(payload) .buildSuccess(); }
private CommandSearchResponse searchMultipleCommands(CommandSearchRequest searchRequest) { final CommandSearchResponse commandSearchResponse = new CommandSearchResponse(); final Collection<DeviceCommand> commands = hazelcastService.find( searchRequest.getDeviceIds(), searchRequest.getNames(), getTotal(searchRequest.getSkip(), searchRequest.getTake()), searchRequest.getTimestampStart(), searchRequest.getTimestampEnd(), searchRequest.isReturnUpdated(), searchRequest.getStatus(), DeviceCommand.class); final Comparator<DeviceCommand> comparator = buildDeviceCommandComparator(searchRequest.getSortField()); String sortOrder = searchRequest.getSortOrder(); final Boolean reverse = sortOrder == null ? null : "desc".equalsIgnoreCase(sortOrder); final List<DeviceCommand> sortedDeviceCommands = orderAndLimit(new ArrayList<>(commands), comparator, reverse, searchRequest.getSkip(), searchRequest.getTake()); commandSearchResponse.setCommands(new ArrayList<>(sortedDeviceCommands)); return commandSearchResponse; }