Commit e10591ed authored by Maxym Borodenko's avatar Maxym Borodenko

WIP: handling list of inventories

parent 3043df8d
......@@ -18,7 +18,9 @@ package org.gringlobal.component.repository;
import java.util.Date;
import java.util.Iterator;
import java.util.Objects;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
......@@ -61,8 +63,16 @@ public class InventoryAspect {
public Object aroundInventorySave(final ProceedingJoinPoint joinPoint, final Iterable<Inventory> inventories) throws Throwable {
LOG.warn("Many inventories are being saved!!! {}", inventories);
var newInventories = Lists.newArrayList(inventories).stream().filter(i -> i.getId() == null).collect(Collectors.toList());
var existingInventories = Lists.newArrayList(inventories).stream().filter(i -> i.getId() != null).collect(Collectors.toList());
existingInventories.forEach(this::handleUpdatingOfExistingInventory);
// Complete saving
return joinPoint.proceed();
Object proceed = joinPoint.proceed();
newInventories.forEach(this::handleCreatingNewInventory);
return proceed;
}
@Around(value = "(execution(* org.gringlobal.persistence.InventoryRepository.save(..)) || execution(* org.gringlobal.persistence.InventoryRepository.saveAndFlush(..)) || execution(* org.gringlobal.persistence.InventoryRepository.save(..))) && args(inventory)")
......@@ -75,25 +85,9 @@ public class InventoryAspect {
// Complete saving entity
joinPoint.proceed();
// Register two PENDING inventory actions
addInventoryAction(inventory, CommunityCodeValues.INVENTORY_ACTION_QUANTITYSET);
addInventoryAction(inventory, CommunityCodeValues.INVENTORY_ACTION_LOCATIONSET);
handleCreatingNewInventory(inventory);
} else {
// Detach the new version from the session to be able to load the old version with previous property values
entityManager.detach(inventory);
// Load old version to compare with a new version
Inventory loaded = inventoryService.get(inventory.getId());
if (!Objects.equals(loaded.getQuantityOnHand(), inventory.getQuantityOnHand()) || !Objects.equals(loaded.getQuantityOnHandUnitCode(), inventory.getQuantityOnHandUnitCode())) {
// Quantity is updated - close the QUANTITYSET action
closeInventoryAction(loaded, CommunityCodeValues.INVENTORY_ACTION_QUANTITYSET);
}
if (!Objects.equals(loaded.getStorageLocationPart1(), inventory.getStorageLocationPart1()) || !Objects.equals(loaded.getStorageLocationPart2(), inventory.getStorageLocationPart2())
|| !Objects.equals(loaded.getStorageLocationPart3(), inventory.getStorageLocationPart3()) || !Objects.equals(loaded.getStorageLocationPart4(), inventory.getStorageLocationPart4())) {
// Storage location is updated - close the LOCATIONSET action
closeInventoryAction(loaded, CommunityCodeValues.INVENTORY_ACTION_LOCATIONSET);
}
handleUpdatingOfExistingInventory(inventory);
// Complete saving entity
return joinPoint.proceed();
......@@ -102,6 +96,30 @@ public class InventoryAspect {
return inventory;
}
private void handleCreatingNewInventory(Inventory newInventory) {
// Register two PENDING inventory actions
addInventoryAction(newInventory, CommunityCodeValues.INVENTORY_ACTION_QUANTITYSET);
addInventoryAction(newInventory, CommunityCodeValues.INVENTORY_ACTION_LOCATIONSET);
}
private void handleUpdatingOfExistingInventory(Inventory inventory) {
// Detach the new version from the session to be able to load the old version with previous property values
entityManager.detach(inventory);
// Load old version to compare with a new version
Inventory loaded = inventoryService.get(inventory.getId());
if (!Objects.equals(loaded.getQuantityOnHand(), inventory.getQuantityOnHand()) || !Objects.equals(loaded.getQuantityOnHandUnitCode(), inventory.getQuantityOnHandUnitCode())) {
// Quantity is updated - close the QUANTITYSET action
closeInventoryAction(loaded, CommunityCodeValues.INVENTORY_ACTION_QUANTITYSET);
}
if (!Objects.equals(loaded.getStorageLocationPart1(), inventory.getStorageLocationPart1()) || !Objects.equals(loaded.getStorageLocationPart2(), inventory.getStorageLocationPart2())
|| !Objects.equals(loaded.getStorageLocationPart3(), inventory.getStorageLocationPart3()) || !Objects.equals(loaded.getStorageLocationPart4(), inventory.getStorageLocationPart4())) {
// Storage location is updated - close the LOCATIONSET action
closeInventoryAction(loaded, CommunityCodeValues.INVENTORY_ACTION_LOCATIONSET);
}
}
private void addInventoryAction(Inventory inventory, CommunityCodeValues.CodeValueDef actionCodeValue) {
InventoryAction quantityAction = new InventoryAction();
quantityAction.setActionNameCode(actionCodeValue.value);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment