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

ai.stapi.graphsystem.commandvalidation.model.exceptions.CannotDispatchCommand Maven / Gradle / Ivy

package ai.stapi.graphsystem.commandvalidation.model.exceptions;

import ai.stapi.graphsystem.commandvalidation.model.CommandConstrainViolation;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class CannotDispatchCommand extends RuntimeException {

  private final transient List commandConstrainViolations;

  private CannotDispatchCommand(
      String commandName,
      String becauseMessage,
      List commandConstrainViolations
  ) {
    super(
        String.format(
            "Cannot dispatch Command '%s', %s",
            commandName,
            becauseMessage
        )
    );
    this.commandConstrainViolations = commandConstrainViolations;
  }

  public static CannotDispatchCommand becauseThereWereConstrainViolation(
      String commandName,
      String targetAggregateIdentifier,
      List commandConstrainViolations
  ) {
    return new CannotDispatchCommand(
        commandName,
        ("""
            because there were constrain violations.
            Target Aggregate Identifier: '%s'
            
            %s
            """
        ).formatted(
            targetAggregateIdentifier,
            commandConstrainViolations.stream()
                .sorted(Comparator.comparing(CommandConstrainViolation::getLevel))
                .map(CommandConstrainViolation::getMessage)
                .collect(Collectors.joining("\n\n"))
        ),
        commandConstrainViolations
    );
  }

  public static CannotDispatchCommand becauseItDoesNotImplementCommandInterface(
      String commandName,
      Class actualClass
  ) {
    return new CannotDispatchCommand(
        commandName,
        "because it does not implement Command interface. Actual className: "
            + actualClass.getSimpleName(),
        List.of()
    );
  }

  public List getCommandConstrainViolations() {
    return commandConstrainViolations;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy