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

ai.stapi.graphsystem.dynamiccommandprocessor.GenericDynamicCommandProcessor Maven / Gradle / Ivy

package ai.stapi.graphsystem.dynamiccommandprocessor;

import ai.stapi.graphsystem.messaging.event.AggregateGraphUpdatedEvent;
import ai.stapi.graph.Graph;
import ai.stapi.identity.UniqueIdentifier;
import ai.stapi.graphsystem.messaging.command.AbstractCommand;
import ai.stapi.graphoperations.objectGraphMapper.model.MissingFieldResolvingStrategy;
import java.util.List;

public class GenericDynamicCommandProcessor implements DynamicCommandProcessor {

  private final List specificDynamicCommandProcessors;

  public GenericDynamicCommandProcessor(
      List specificDynamicCommandProcessors
  ) {
    this.specificDynamicCommandProcessors = specificDynamicCommandProcessors;
  }

  @Override
  public List> processCommand(
      AbstractCommand command,
      Graph currentAggregateState,
      MissingFieldResolvingStrategy missingFieldResolvingStrategy
  ) {
    var supporting = this.getSupportingProcessor(command);
    return supporting.processCommand(command, currentAggregateState, missingFieldResolvingStrategy);
  }

  private SpecificDynamicCommandProcessor getSupportingProcessor(
      AbstractCommand command
  ) {
    var supporting = this.specificDynamicCommandProcessors.stream()
        .filter(specific -> specific.supports(command))
        .toList();

    if (supporting.size() != 1) {
      throw new NoSupportingSpecificDynamicCommandProcessor(
          String.format(
              "Command cannot be processed, because there is no supporting command processor.%n" +
                  "Command type: '%s'" +
                  "Target aggregate identifier: '%s'",
              command.getSerializationType(),
              command.getTargetIdentifier()
          )
      );
    }
    return supporting.get(0);
  }

  private static class NoSupportingSpecificDynamicCommandProcessor extends RuntimeException {

    public NoSupportingSpecificDynamicCommandProcessor(String message) {
      super(message);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy