in dashboard-web-app/src/main/java/org/inventory/hub/controller/ProductsInventoryController.java [89:160]
public ResponseEntity<?> getProducts() {
try {
// return new ResponseEntity<List<ProductsInventory>>(ProductsInventoryRepository.findAll(), HttpStatus.OK);
final Flux<ProductsInventory> productsInventoriesFlux = productsInventoryRepository.findAll();
final Iterable<ProductsInventory> productsInventories = productsInventoriesFlux.toIterable();
final ResponseEntity<Iterable<ProductsInventory>> productInventories =
new ResponseEntity<Iterable<ProductsInventory>>(productsInventories, HttpStatus.OK);
System.out.println("======= /api/products ===== ");
System.out.println(productInventories.toString());
final Iterable <ProductsInventory> iterable = (Iterable<ProductsInventory>) productInventories.getBody();
final Iterator <ProductsInventory> it = makeCollection(iterable).iterator();
Product product;
final Map<String, Product> products = new TreeMap<String, Product>();
final Map<String, Integer> locations = new HashMap<>();
ProductsInventory productInventory = null;
while (it.hasNext()) {
productInventory = it.next();
product = products.get(productInventory.getProductName());
if (product == null) {
product = new Product();
product.setName(productInventory.getProductName());
product.setDescription(productInventory.getDescription());
products.put(productInventory.getProductName(), product);
}
final String currentLocation = productInventory.getLocation();
Integer totalItems = locations.get(currentLocation);
if (totalItems == null) {
totalItems = Integer.valueOf(productInventory.getTotalCount());
locations.put(currentLocation, totalItems);
} else {
locations.put(currentLocation, totalItems + Integer.valueOf(productInventory.getTotalCount()));
}
System.out.println("== inventory === " + productInventory);
product.getCountByLocation()
.put(productInventory.getLocation(),
Integer.valueOf(productInventory.getTotalCount()));
}
for (final Map.Entry<String, Integer> locationEntry : locations.entrySet()) {
System.out.format("== inventory total per location === %s: %d\n",
locationEntry.getKey(), locationEntry.getValue());
for (final Map.Entry<String, Product> productEntry : products.entrySet()) {
final Map<String, Integer> productByLocationMap = productEntry.getValue().getCountByLocation();
final Integer currentCount = productByLocationMap.get(locationEntry.getKey());
if (currentCount == null) {
productByLocationMap.put(locationEntry.getKey(), 0);
}
}
}
System.out.println("====== success");
return new ResponseEntity<Map<String, Product>> (products, HttpStatus.OK);
} catch (Exception e) {
System.out.println("======== EXCEPTION =======");
e.printStackTrace();
return new ResponseEntity<String>("Nothing found", HttpStatus.NOT_FOUND);
}
}