Search in sources :

Example 6 with EventFactory

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);
}
Also used : Events.getMessage(io.spine.core.Events.getMessage) Message(com.google.protobuf.Message) Version(io.spine.core.Version) EventFactory(io.spine.server.event.EventFactory) Event(io.spine.core.Event) CommandEnvelope(io.spine.core.CommandEnvelope)

Aggregations

EventFactory (io.spine.server.event.EventFactory)6 StringValue (com.google.protobuf.StringValue)4 Event (io.spine.core.Event)3 Message (com.google.protobuf.Message)2 GivenEvent (io.spine.core.given.GivenEvent)2 Test (org.junit.Test)2 Timestamp (com.google.protobuf.Timestamp)1 TestActorRequestFactory (io.spine.client.TestActorRequestFactory)1 Command (io.spine.core.Command)1 CommandEnvelope (io.spine.core.CommandEnvelope)1 Events.getMessage (io.spine.core.Events.getMessage)1 Events.getTimestamp (io.spine.core.Events.getTimestamp)1 Version (io.spine.core.Version)1 PrjProjectCreated (io.spine.test.projection.event.PrjProjectCreated)1 TypeName (io.spine.type.TypeName)1 Before (org.junit.Before)1