All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentcs.kernel.workingset.DefaultTransportOrderCleanupApproval Maven / Gradle / Ivy

// SPDX-FileCopyrightText: The openTCS Authors
// SPDX-License-Identifier: MIT
package org.opentcs.kernel.workingset;

import static java.util.Objects.requireNonNull;

import jakarta.inject.Inject;
import java.util.Objects;
import org.opentcs.components.kernel.TransportOrderCleanupApproval;
import org.opentcs.data.order.TransportOrder;
import org.opentcs.data.peripherals.PeripheralJob;

/**
 * Checks whether a transport order may be removed.
 */
public class DefaultTransportOrderCleanupApproval
    implements
      TransportOrderCleanupApproval {

  private final PeripheralJobPoolManager peripheralJobPoolManager;
  private final DefaultPeripheralJobCleanupApproval defaultPeripheralJobCleanupApproval;
  private final CreationTimeThreshold creationTimeThreshold;

  /**
   * Creates a new instance.
   *
   * @param peripheralJobPoolManager The peripheral job pool manager to be used.
   * @param defaultPeripheralJobCleanupApproval Checks whether a peripheral job may be removed.
   * @param creationTimeThreshold Keeps track of the time used to determine whether a transport
   * order should be removed (according to its creation time).
   */
  @Inject
  public DefaultTransportOrderCleanupApproval(
      PeripheralJobPoolManager peripheralJobPoolManager,
      DefaultPeripheralJobCleanupApproval defaultPeripheralJobCleanupApproval,
      CreationTimeThreshold creationTimeThreshold
  ) {
    this.peripheralJobPoolManager = requireNonNull(
        peripheralJobPoolManager,
        "peripheralJobPoolManager"
    );
    this.defaultPeripheralJobCleanupApproval
        = requireNonNull(
            defaultPeripheralJobCleanupApproval,
            "defaultPeripheralJobCleanupApproval"
        );
    this.creationTimeThreshold = requireNonNull(creationTimeThreshold, "creationTimeThreshold");
  }

  @Override
  public boolean test(TransportOrder order) {
    if (!order.getState().isFinalState()) {
      return false;
    }
    if (isRelatedToJobWithNonFinalState(order)) {
      return false;
    }
    if (isRelatedToUnapprovedJob(order)) {
      return false;
    }
    if (order.getCreationTime().isAfter(creationTimeThreshold.getCurrentThreshold())) {
      return false;
    }
    return true;
  }

  private boolean isRelatedToJobWithNonFinalState(TransportOrder order) {
    return peripheralJobPoolManager.getObjectRepo()
        .getObjects(
            PeripheralJob.class,
            job -> Objects.equals(job.getRelatedTransportOrder(), order.getReference())
        )
        .stream()
        .filter(job -> !job.getState().isFinalState())
        .findAny()
        .isPresent();
  }

  private boolean isRelatedToUnapprovedJob(TransportOrder order) {
    return !(peripheralJobPoolManager.getObjectRepo().getObjects(
        PeripheralJob.class,
        job -> Objects.equals(job.getRelatedTransportOrder(), order.getReference())
    )
        .stream()
        .allMatch(defaultPeripheralJobCleanupApproval));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy