@Override
public SagaInstance create(Data sagaData, Optional<String> resource) {
SagaInstance sagaInstance = new SagaInstance(getSagaType(),
null,
"????",
null,
SagaDataSerde.serializeSagaData(sagaData), new HashSet<>());
sagaInstanceRepository.save(sagaInstance);
String sagaId = sagaInstance.getId();
resource.ifPresent( r -> Assert.isTrue(sagaLockManager.claimLock(getSagaType(), sagaId, r), "Cannot claim lock for resource"));
SagaActions<Data> actions = getStateDefinition().getStartingHandler().get().apply(sagaData);
List<CommandWithDestination> commands = actions.getCommands();
sagaData = actions.getUpdatedSagaData().orElse(sagaData);
sagaInstance.setLastRequestId(sendCommands(sagaId, commands));
sagaInstance.setSerializedSagaData(SagaDataSerde.serializeSagaData(sagaData));
publishEvents(sagaId, actions.getEventsToPublish(), actions.getUpdatedState());
Optional<String> possibleNewState = actions.getUpdatedState();
maybeUpdateState(sagaInstance, possibleNewState);
maybePerformEndStateActions(sagaId, sagaInstance, possibleNewState);
sagaInstanceRepository.update(sagaInstance);
updateEnlistedAggregates(sagaId, actions.getEnlistedAggregates());
updateEventInstanceSubscriptions(sagaData, sagaId, sagaInstance.getStateName());
return sagaInstance;
}