SimpleGenericAllocator.java

  1. /*
  2.  * Copyright 2005-2025 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.openwms.wms.inventory.allocation.spi;

  17. import jakarta.validation.constraints.NotNull;
  18. import org.ameba.annotation.Measured;
  19. import org.ameba.annotation.TxService;
  20. import org.openwms.core.lang.Triple;
  21. import org.openwms.wms.inventory.ReservationService;
  22. import org.openwms.wms.inventory.TransportUnitReservation;
  23. import org.openwms.wms.inventory.allocation.Allocation;
  24. import org.openwms.wms.transport.TransportUnitService;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;

  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.UUID;

  30. import static org.openwms.wms.InventoryLoggerCategories.ALLOCATION;

  31. /**
  32.  * A SimpleGenericAllocator.
  33.  *
  34.  * @author Heiko Scherrer
  35.  */
  36. @TxService
  37. class SimpleGenericAllocator implements GenericAllocator {

  38.     private static final Logger ALLOCATION_LOGGER = LoggerFactory.getLogger(ALLOCATION);
  39.     private final TransportUnitService transportUnitService;
  40.     private final ReservationService reservationService;

  41.     SimpleGenericAllocator(TransportUnitService transportUnitService, ReservationService reservationService) {
  42.         this.transportUnitService = transportUnitService;
  43.         this.reservationService = reservationService;
  44.     }

  45.     /**
  46.      * {@inheritDoc}
  47.      */
  48.     @Measured
  49.     @Override
  50.     public @NotNull List<Allocation> allocate(@NotNull List<Triple<String, Object, Class<?>>> searchAttributes, List<String> sourceLocationGroupNames) {
  51.         if (ALLOCATION_LOGGER.isDebugEnabled()) {
  52.             ALLOCATION_LOGGER.debug("Trying to allocate TransportUnits with the following search criteria [{}] in LocationGroups {}",
  53.                     searchAttributes, sourceLocationGroupNames);
  54.         }

  55.         var transportUnitBKOpt = searchAttributes.stream().filter(sa -> "transportUnitBK".equals(sa.key())).findFirst();
  56.         var result = new ArrayList<Allocation>(0);
  57.         if (transportUnitBKOpt.isPresent()) {
  58.             if (ALLOCATION_LOGGER.isDebugEnabled()) {
  59.                 ALLOCATION_LOGGER.debug("Found TransportUnit with transportUnitBK [{}] for allocation", transportUnitBKOpt.get().key());
  60.             }
  61.             var transportUnitOpt = transportUnitService.findOneBy(transportUnitBKOpt.get().valueAs(String.class));
  62.             transportUnitOpt.ifPresent(transportUnit -> {
  63.                 var reservation = new TransportUnitReservation(transportUnit, UUID.randomUUID().toString());
  64.                 reservationService.saveReservation(reservation);
  65.                 ALLOCATION_LOGGER.debug("A reservation [{}] has been added for the TransportUnit [{}]", reservation, transportUnit);
  66.                 result.add(Allocation.AllocationBuilder.anAllocation()
  67.                         .transportUnit(transportUnit)
  68.                         .reservationId(reservation.getReservedBy())
  69.                         .build()
  70.                 );
  71.             });
  72.         }
  73.         if (ALLOCATION_LOGGER.isDebugEnabled()) {
  74.             ALLOCATION_LOGGER.debug("Allocated TransportUnit [{}] for allocation", result);
  75.         }
  76.         return result;
  77.     }
  78. }