How to get list of objects using Mono ResponseEntity objects, as it returns single object

374 Views Asked by At
@RequestMapping("/api/v1")
public class RemoteActionHistoryController {

    public Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>> excaliburData(@PathVariable(name = "deviceId", required = true) 
String deviceId) {
        return service.getRemoteActionHistory(deviceId);
    }
}

public class RemoteActionHistoryServiceImpl {

    @Autowired
    RemoteActionHistoryRepository arrowFlightRepository;
    
    @Override
    public Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>> getRemoteActionHistory(String deviceId) throws Exception {
        return (Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>>) arrowFlightRepository.getRemoteActionDetailsByDeviceId(deviceId).collectList().flatMap(mapper);
    }
    
    Function<List<RemoteActionHistory>, Mono<ResponseEntityDTO<RemoteActionHistoryDTO>>> mapper = remoteActions -> {
        var remoteActionDTO = new RemoteActionHistoryDTO();
        if (CollectionUtils.isEmpty(remoteActions)) {
            return Mono.justOrEmpty(new ResponseEntityDTO<>(RemoteActionHistoryConstants.CODE_RET_000, null, remoteActionDTO));
        }
        System.out.println("Printing remoteActions"+remoteActions);
        for (RemoteActionHistory excaliburData : remoteActions) {
            remoteActionDTO.setStartDateTime(excaliburData.getStartDateTime());
            remoteActionDTO.setEndDateTime(excaliburData.getEndDateTime());
            remoteActionDTO.setRemoteActionType(excaliburData.getCommand());
            remoteActionDTO.setTaskStatus(excaliburData.getStatus());
        }
        return Mono.justOrEmpty(new ResponseEntityDTO<>(RemoteActionHistoryConstants.CODE_RET_000, null, remoteActionDTO));
    };
}

public class RemoteActionHistoryServiceImpl {

    @Autowired
    RemoteActionHistoryRepository arrowFlightRepository;

    @Override
    public Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>> getRemoteActionHistory(String deviceId) throws Exception {

        return (Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>>) arrowFlightRepository.getRemoteActionDetailsByDeviceId(deviceId).collectList().flatMap(mapper);
    }

    Function<List<RemoteActionHistory>, Mono<ResponseEntityDTO<RemoteActionHistoryDTO>>> mapper = remoteActions -> {
        var remoteActionDTO = new RemoteActionHistoryDTO();
        if (CollectionUtils.isEmpty(remoteActions)) {
            return Mono.justOrEmpty(new ResponseEntityDTO<>(RemoteActionHistoryConstants.CODE_RET_000, null, remoteActionDTO));
        }
        System.out.println("Printing remoteActions"+remoteActions);
        for (RemoteActionHistory excaliburData : remoteActions) {
            remoteActionDTO.setStartDateTime(excaliburData.getStartDateTime());
            remoteActionDTO.setEndDateTime(excaliburData.getEndDateTime());
            remoteActionDTO.setRemoteActionType(excaliburData.getCommand());
            remoteActionDTO.setTaskStatus(excaliburData.getStatus());

        }
        return Mono.justOrEmpty(new ResponseEntityDTO<>(RemoteActionHistoryConstants.CODE_RET_000, null, remoteActionDTO));
    };
}

public class RemoteActionHistoryRepositoryImpl {

    @Override
    public Flux<RemoteActionHistory> getRemoteActionDetailsByDeviceId(String deviceId) throws Exception {
        return Flux.fromIterable(getExcaliburData(String.format(query, tableName, deviceId), true));
    }

    private List<RemoteActionHistory> getExcaliburData(String query, boolean retry) throws Exception {
        List<RemoteActionHistory> remoteActionData = new ArrayList<>();
        try (final FlightStream flightStream = adhocFlightClientConfig.createAdhocFlightClient().runQuery(query);) {    
            while (flightStream.next()) {

                if (!flightStream.hasRoot()) {
                    break;
                }
                VectorSchemaRoot vectorSchemaRoot = flightStream.getRoot();

                TimeStampMilliVector createdDateVector = (TimeStampMilliVector) vectorSchemaRoot
                        .getVector(RemoteActionHistoryConstants.CREATED_DATE_TIME);
                TimeStampMilliVector modifiedDateVector = (TimeStampMilliVector) vectorSchemaRoot
                        .getVector(RemoteActionHistoryConstants.MODIFIED_DATE_TIME);

                RemoteActionHistory remoteAction = null;
                for (int i = 0; i < vectorSchemaRoot.getRowCount(); i++) {
                    remoteAction = new RemoteActionHistory();
                    remoteAction.setStartDateTime(createdDateVector.isNull(i)? null : new Timestamp(createdDateVector.get(i)));
                    remoteAction.setEndDateTime(modifiedDateVector.isNull(i)? null : new Timestamp(modifiedDateVector.get(i)));

                    remoteActionData.add(remoteAction);
                }

        } catch (Exception ex) {
            log.error(String.format("Exception string : %s with retryEnabled is %s", ex.toString(),retry));
            if (retry && StringUtils.containsIgnoreCase(ex.toString(), RemoteActionHistoryConstants.UNAUTHENTICATED_ERROR)) {
                adhocFlightClientConfig.removeToken();
                getExcaliburData(query, false);
            }
            throw ex;
        }
        return remoteActionData;
    }
}

I need to fetch the records like this

Currently getting single record

Getting error on Mono code

I have attached the result which I'm currently getting single record. when i debugged the code in the object i have multiple records but it shows only one record in postman

0

There are 0 best solutions below