use of io.spine.server.event.EventFactory in project core-java by SpineEventEngine.
the class Aggregate method apply.
/**
* Applies event messages.
*
* @param eventMessages the event messages or events to apply
* @param origin the envelope of a message which caused the events
* @see #ensureEventMessage(Message)
*/
void apply(Iterable<? extends Message> eventMessages, MessageEnvelope origin) {
final List<? extends Message> messages = newArrayList(eventMessages);
final EventFactory eventFactory = EventFactory.on(origin, getProducerId());
final List<Event> events = newArrayListWithCapacity(messages.size());
Version projectedEventVersion = getVersion();
for (Message eventOrMessage : messages) {
/* Applying each message would increment the entity version.
Therefore, we should simulate this behaviour. */
projectedEventVersion = Versions.increment(projectedEventVersion);
final Message eventMessage = ensureEventMessage(eventOrMessage);
final Event event;
if (eventOrMessage instanceof Event) {
/* If we get instances of Event, it means we are dealing with an import command,
which contains these events in the body. So we deal with a command envelope.
*/
final CommandEnvelope ce = (CommandEnvelope) origin;
event = importEvent((Event) eventOrMessage, ce.getCommandContext(), projectedEventVersion);
} else {
event = eventFactory.createEvent(eventMessage, projectedEventVersion);
}
events.add(event);
}
play(events);
uncommittedEvents.addAll(events);
}
Aggregations