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 extends UniqueIdentifier> command
) {
return this.resolveAllProviders(command);
}
public boolean containsFor(AbstractCommand extends UniqueIdentifier> command) {
return this.mappingProviders.stream().anyMatch(
specificObjectGraphMapper -> specificObjectGraphMapper.supports(
command.getSerializationType()
)
);
}
private HashMap>, ObjectGraphMapping> resolveAllProviders(
AbstractCommand extends UniqueIdentifier> 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;
}
}