@Override public String type() { return inspector.type(); }
/** * Returns the aggregate type stored by this repository. * * @return the aggregate type stored by this repository */ protected Class<? extends T> getAggregateType() { return aggregateModel.entityClass(); }
@Override public Object identifier() { return inspector.getIdentifier(aggregateRoot); }
@SuppressWarnings("unchecked") private Object handle(CommandMessage<?> commandMessage) throws Exception { List<AnnotatedCommandHandlerInterceptor<? super T>> interceptors = inspector.commandHandlerInterceptors() .stream() .filter(chi -> chi.canHandle(commandMessage)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, aggregateRoot)) .collect(Collectors.toList()); MessageHandlingMember<? super T> handler = inspector.commandHandlers() .stream() .filter(mh -> mh.canHandle(commandMessage)) .findFirst() .orElseThrow(() -> new NoHandlerForCommandException(format("No handler available to handle command [%s]", commandMessage.getCommandName()))); Object result; if (interceptors.isEmpty()) { result = handler.handle(commandMessage, aggregateRoot); } else { result = new DefaultInterceptorChain<>( (UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> handler.handle(commandMessage, aggregateRoot) ).proceed(); } if (aggregateRoot == null) { aggregateRoot = (T) result; return identifierAsString(); } return result; }
private Object handle(EventMessage<?> eventMessage) { inspector.publish(eventMessage, aggregateRoot); return null; }
@Override public Long version() { return inspector.getVersion(aggregateRoot); }
private List<MessageHandler<CommandMessage<?>>> initializeHandlers(AggregateModel<T> aggregateModel) { List<MessageHandler<CommandMessage<?>>> handlersFound = new ArrayList<>(); aggregateModel.commandHandlers().forEach(handler -> { handler.unwrap(CommandMessageHandlingMember.class).ifPresent(cmh -> { if (cmh.isFactoryHandler()) { handlersFound.add(new AggregateConstructorCommandHandler(handler)); supportedCommandNames.add(cmh.commandName()); } else { handlersFound.add(new AggregateCommandHandler(handler)); supportedCommandNames.add(cmh.commandName()); } }); }); return handlersFound; }
@SuppressWarnings("unchecked") private Object handle(CommandMessage<?> commandMessage) throws Exception { List<AnnotatedCommandHandlerInterceptor<? super T>> interceptors = inspector.commandHandlerInterceptors() .stream() .filter(chi -> chi.canHandle(commandMessage)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, aggregateRoot)) .collect(Collectors.toList()); MessageHandlingMember<? super T> handler = inspector.commandHandlers() .stream() .filter(mh -> mh.canHandle(commandMessage)) .findFirst() .orElseThrow(() -> new NoHandlerForCommandException(format("No handler available to handle command [%s]", commandMessage.getCommandName()))); Object result; if (interceptors.isEmpty()) { result = handler.handle(commandMessage, aggregateRoot); } else { result = new DefaultInterceptorChain<>( (UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> handler.handle(commandMessage, aggregateRoot) ).proceed(); } if (aggregateRoot == null) { aggregateRoot = (T) result; return identifierAsString(); } return result; }
/** * Publish an event to the aggregate root and its entities first and external event handlers (using the given * event bus) later. * * @param msg the event message to publish */ protected void publish(EventMessage<?> msg) { if (msg instanceof DomainEventMessage) { lastKnownSequence = ((DomainEventMessage) msg).getSequenceNumber(); } inspector.publish(msg, aggregateRoot); publishOnEventBus(msg); }
@Override public Long version() { return inspector.getVersion(aggregateRoot); }
private List<MessageHandler<CommandMessage<?>>> initializeHandlers(AggregateModel<T> aggregateModel) { List<MessageHandler<CommandMessage<?>>> handlersFound = new ArrayList<>(); aggregateModel.commandHandlers().forEach(handler -> { handler.unwrap(CommandMessageHandlingMember.class).ifPresent(cmh -> { if (cmh.isFactoryHandler()) { handlersFound.add(new AggregateConstructorCommandHandler(handler)); supportedCommandNames.add(cmh.commandName()); } else { handlersFound.add(new AggregateCommandHandler(handler)); supportedCommandNames.add(cmh.commandName()); } }); }); return handlersFound; }
@Override public boolean canResolve(ScopeDescriptor scopeDescription) { return scopeDescription instanceof AggregateScopeDescriptor && Objects.equals(model.type(), ((AggregateScopeDescriptor) scopeDescription).getType()); } }
@Override public A newInstance(Callable<T> factoryMethod) throws Exception { UnitOfWork<?> uow = CurrentUnitOfWork.get(); AtomicReference<A> aggregateReference = new AtomicReference<>(); // a constructor may apply events, and the persistence of an aggregate must take precedence over publishing its events. uow.onPrepareCommit(x -> { A aggregate = aggregateReference.get(); // aggregate construction may have failed with an exception. In that case, no action is required on commit if (aggregate != null) { prepareForCommit(aggregate); } }); A aggregate = doCreateNew(factoryMethod); aggregateReference.set(aggregate); Assert.isTrue(aggregateModel.entityClass().isAssignableFrom(aggregate.rootType()), () -> "Unsuitable aggregate for this repository: wrong type"); Map<String, A> aggregates = managedAggregates(uow); Assert.isTrue(aggregates.putIfAbsent(aggregate.identifierAsString(), aggregate) == null, () -> "The Unit of Work already has an Aggregate with the same identifier"); uow.onRollback(u -> aggregates.remove(aggregate.identifierAsString())); return aggregate; }
private Object handle(EventMessage<?> eventMessage) { inspector.publish(eventMessage, aggregateRoot); return null; }
if (identifier != null) { return new GenericDomainEventMessage<>(getType(), getAggregateIdentifier(), getSequenceNumber(), getPayload(), getMetaData(), getIdentifier(), getTimestamp()); } else { return new LazyIdentifierDomainEventMessage<>(getType(), getSequenceNumber(), getPayload(), if (identifier != null) { return new GenericDomainEventMessage<>(getType(), getAggregateIdentifier(), getSequenceNumber(), getPayload(), getMetaData(), getIdentifier(), getTimestamp()) .andMetaData(additionalMetaData); } else {
@Override public boolean canResolve(ScopeDescriptor scopeDescription) { return scopeDescription instanceof AggregateScopeDescriptor && Objects.equals(aggregateModel.type(), ((AggregateScopeDescriptor) scopeDescription).getType()); }
/** * Returns the aggregate type stored by this repository. * * @return the aggregate type stored by this repository */ protected Class<? extends T> getAggregateType() { return aggregateModel.entityClass(); }
/** * Publish an event to the aggregate root and its entities first and external event handlers (using the given * event bus) later. * * @param msg the event message to publish */ protected void publish(EventMessage<?> msg) { if (msg instanceof DomainEventMessage) { lastKnownSequence = ((DomainEventMessage) msg).getSequenceNumber(); } inspector.publish(msg, aggregateRoot); publishOnEventBus(msg); }
@Override public Object identifier() { return inspector.getIdentifier(aggregateRoot); }
@Override public String type() { return inspector.type(); }