@Test public void testGroupDeliveriesIntoMixedIfNecessaryNoStock() throws Exception { final Map<DeliveryBucket, List<CartItem>> buckets = new HashMap<>(); final DeliveryBucket oos = context.mock(DeliveryBucket.class, "oos"); buckets.put(oos, Collections.<CartItem>emptyList()); context.checking(new Expectations() {{ allowing(oos).getSupplier(); will(returnValue("s1")); allowing(oos).getGroup(); will(returnValue(CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP)); }}); new OrderSplittingStrategyImpl(null, null, null, null).groupDeliveriesIntoMixedIfNecessary(buckets, Collections.singletonMap("s1", Boolean.TRUE)); assertNotNull(buckets); assertEquals(1, buckets.size()); }
void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); } }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); } OrderSplittingStrategyImpl(final ShopService shopService,
                                      final ProductService productService,
                                      final WarehouseService warehouseService,
                                      final InventoryResolver inventoryResolver); }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); } OrderSplittingStrategyImpl(final ShopService shopService,
                                      final ProductService productService,
                                      final WarehouseService warehouseService,
                                      final InventoryResolver inventoryResolver); @Override DeliveryBucket determineDeliveryBucket(final CartItem item,
                                                  final ShoppingCart cart); @Override Map<DeliveryBucket, List<CartItem>> determineDeliveryBuckets(final long shopId,
                                                                        final Collection<CartItem> items,
                                                                        final Map<String, Boolean> onePhysicalDelivery); @Override Map<String, Boolean> isMultipleDeliveriesAllowed(final long shopId, final Collection<CartItem> items); }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); } OrderSplittingStrategyImpl(final ShopService shopService,
                                      final ProductService productService,
                                      final WarehouseService warehouseService,
                                      final InventoryResolver inventoryResolver); @Override DeliveryBucket determineDeliveryBucket(final CartItem item,
                                                  final ShoppingCart cart); @Override Map<DeliveryBucket, List<CartItem>> determineDeliveryBuckets(final long shopId,
                                                                        final Collection<CartItem> items,
                                                                        final Map<String, Boolean> onePhysicalDelivery); @Override Map<String, Boolean> isMultipleDeliveriesAllowed(final long shopId, final Collection<CartItem> items);  }