Skip to content

Instantly share code, notes, and snippets.

@3gcodes
Created March 25, 2024 20:20
Show Gist options
  • Save 3gcodes/be84428b64f4989a684255bc84b8c81b to your computer and use it in GitHub Desktop.
Save 3gcodes/be84428b64f4989a684255bc84b8c81b to your computer and use it in GitHub Desktop.
private List<VehicleDescription> filterVehicleDescriptionsByIsInDMS(List<VehicleDescription> descriptions, String organizationId) {
try (var executor = ApplicationUtility.createExecutor()) {
if ((descriptions == null) || descriptions.isEmpty() || StringUtils.isBlank(organizationId)) {
return new ArrayList<>();
}
final var threadContext = SkAIThreadContext.export();
var vins = descriptions.stream() //
.filter(d -> d.getVinDescription() != null) //
.filter(d -> StringUtils.isNotBlank(d.getVinDescription().getVin())) //
.map(d -> d.getVinDescription().getVin()) //
.distinct() //
.collect(Collectors.toCollection(ArrayList::new)) //
;
AtomicReference<Map<String, Boolean>> vinMap = new AtomicReference<>(new HashMap<>());
vins.stream().forEach(vin -> {
vinMap.get().put(vin, false);
});
final List<CompletableFuture<Boolean>> allFutures = new ArrayList<>();
allFutures.add(CompletableFuture.supplyAsync(() -> {
SkAIThreadContext.init(threadContext);
final var response = v1DMSRepairOrderRepository.findAllByOrganizationIdInAndVinInWithinTimeRange( //
Arrays.asList(organizationId) //
, vins //
, CommonUtility.getUTCZonedDateTime().minusYears(3) //
, CommonUtility.getUTCZonedDateTime().plusMonths(3) //
, 0 //
, 1 //
);
response.getList().stream() //
.filter(r -> r.getVehicle() != null) //
.filter(r -> StringUtils.isNotBlank(r.getVehicle().getVin())) //
.map(r -> r.getVehicle().getVin()) //
.distinct() //
.forEach(vin -> {
vinMap.get().remove(vin);
vinMap.get().put(vin, true);
}) //
;
return true;
}, executor));
allFutures.add(CompletableFuture.supplyAsync(() -> {
SkAIThreadContext.init(threadContext);
final var response = v1DMSServiceAppointmentsRepository.findAllByOrganizationIdInAndVinInWithinTimeRange( //
Arrays.asList(organizationId) //
, vins //
, CommonUtility.getUTCZonedDateTime().minusYears(3) //
, CommonUtility.getUTCZonedDateTime().plusMonths(3) //
, 0 //
, 1 //
);
response.getList().stream() //
.filter(r -> r.getVehicle() != null) //
.filter(r -> StringUtils.isNotBlank(r.getVehicle().getVin())) //
.map(r -> r.getVehicle().getVin()) //
.distinct() //
.forEach(vin -> {
vinMap.get().remove(vin);
vinMap.get().put(vin, true);
}) //
;
return true;
}, executor));
CompletableFuture.allOf(allFutures.toArray(new CompletableFuture[allFutures.size()])).join();
return descriptions.stream() //
.filter(d -> d.getVinDescription() != null) //
.filter(d -> StringUtils.isNotBlank(d.getVinDescription().getVin())) //
.filter(d -> vinMap.get().get(d.getVinDescription().getVin())) //
.collect(Collectors.toCollection(ArrayList::new)) //
;
} catch (Exception ex) {
return new ArrayList<>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment