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

ai.stapi.graphsystem.commandEventGraphMappingProvider.GenericCommandEventGraphMappingProvider Maven / Gradle / Ivy

package ai.stapi.graphsystem.commandEventGraphMappingProvider;

import ai.stapi.graphsystem.commandEventGraphMappingProvider.exception.GraphMappingProviderException;
import ai.stapi.graphsystem.commandEventGraphMappingProvider.specific.SpecificCommandEventGraphMappingProvider;
import ai.stapi.graphsystem.messaging.event.AggregateGraphUpdatedEvent;
import ai.stapi.identity.UniqueIdentifier;
import ai.stapi.graphsystem.messaging.command.AbstractCommand;
import ai.stapi.graphoperations.objectGraphLanguage.ObjectGraphMapping;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;

public class GenericCommandEventGraphMappingProvider {

  private final List mappingProviders;

  public GenericCommandEventGraphMappingProvider(
      List mappingProviders
  ) {
    this.mappingProviders = mappingProviders;
  }

  public Map>, ObjectGraphMapping> provideGraphMappings(
      AbstractCommand command
  ) {
    return this.resolveAllProviders(command);
  }

  public boolean containsFor(AbstractCommand command) {
    return this.mappingProviders.stream().anyMatch(
        specificObjectGraphMapper -> specificObjectGraphMapper.supports(
            command.getSerializationType()
        )
    );
  }

  private HashMap>, ObjectGraphMapping> resolveAllProviders(
      AbstractCommand command
  ) {
    var collectedProviderResults =
        new HashMap>, ObjectGraphMapping>();
    var providers = this.getSupportingProviders(command.getSerializationType());
    providers.stream()
        .map(provider -> provider.provideGraphMapping(command))
        .forEach(collectedProviderResults::putAll);
    return collectedProviderResults;
  }

  private List getSupportingProviders(
      String deserializationType
  ) {
    var listOfSupportingProviders = this.mappingProviders.stream()
        .filter(
            specificObjectGraphMapper -> specificObjectGraphMapper.supports(deserializationType))
        .toList();
    if (listOfSupportingProviders.isEmpty()) {
      throw GraphMappingProviderException.becauseThereIsNoSupportingSpecificGraphMappingProvider(
          deserializationType);
    }
    return listOfSupportingProviders;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy