Search in sources :

Example 11 with EventContext

use of io.spine.core.EventContext in project core-java by SpineEventEngine.

the class EventFactory method createEvent.

/**
 * Creates an event for the passed event message.
 *
 * <p>The message passed is validated according to the constraints set in its Protobuf
 * definition. In case the message isn't valid, an {@linkplain ValidationException
 * exception} is thrown.
 *
 * <p>In the message is an instance of {@code Any}, it is unpacked for validation.
 *
 * <p>It is recommended to use a corresponding {@linkplain io.spine.validate.ValidatingBuilder
 * ValidatingBuilder} implementation to create a message.
 *
 * @param messageOrAny the message of the event or the message packed into {@code Any}
 * @param version      the version of the entity which produces the event
 * @throws ValidationException if the passed message does not satisfy the constraints
 *                             set for it in its Protobuf definition
 */
public Event createEvent(Message messageOrAny, @Nullable Version version) throws ValidationException {
    checkNotNull(messageOrAny);
    // we must validate it now before emitting the next ID.
    validate(messageOrAny);
    final EventId eventId = Events.generateId();
    final EventContext context = createContext(version);
    final Event result = createEvent(eventId, messageOrAny, context);
    return result;
}
Also used : EventContext(io.spine.core.EventContext) IntegrationEventContext(io.spine.server.integration.IntegrationEventContext) EventId(io.spine.core.EventId) Event(io.spine.core.Event) IntegrationEvent(io.spine.server.integration.IntegrationEvent)

Example 12 with EventContext

use of io.spine.core.EventContext in project core-java by SpineEventEngine.

the class EventFactory method createContext.

private EventContext createContext(@Nullable Version version) {
    final Timestamp timestamp = getCurrentTime();
    final EventContext.Builder builder = EventContext.newBuilder().setTimestamp(timestamp).setProducerId(producerId);
    origin.setOriginFields(builder);
    if (version != null) {
        builder.setVersion(version);
    }
    return builder.build();
}
Also used : EventContext(io.spine.core.EventContext) IntegrationEventContext(io.spine.server.integration.IntegrationEventContext) Timestamp(com.google.protobuf.Timestamp)

Example 13 with EventContext

use of io.spine.core.EventContext in project core-java by SpineEventEngine.

the class MatchFilter method apply.

// OK as we want traceability of exits.
@SuppressWarnings("MethodWithMoreThanThreeNegations")
@Override
public boolean apply(@Nullable Event event) {
    if (event == null) {
        return false;
    }
    final Message message = Events.getMessage(event);
    final EventContext context = event.getContext();
    if (!checkEventType(message)) {
        return false;
    }
    if (!checkAggregateIds(context)) {
        return false;
    }
    if (!checkEventFields(message)) {
        return false;
    }
    final boolean result = checkContextFields(context);
    return result;
}
Also used : EventContext(io.spine.core.EventContext) Message(com.google.protobuf.Message)

Example 14 with EventContext

use of io.spine.core.EventContext in project core-java by SpineEventEngine.

the class Aggregate method importEvent.

/**
 * Creates an event based on the event received in an import command.
 *
 * @param  event          the event to import
 * @param  commandContext the context of the import command
 * @param  version        the version of the aggregate to use for the event
 * @return an event with updated command context and entity version
 */
private static Event importEvent(Event event, CommandContext commandContext, Version version) {
    final EventContext eventContext = event.getContext().toBuilder().setCommandContext(commandContext).setTimestamp(getCurrentTime()).setVersion(version).build();
    final Event result = event.toBuilder().setContext(eventContext).build();
    return result;
}
Also used : EventContext(io.spine.core.EventContext) Event(io.spine.core.Event)

Example 15 with EventContext

use of io.spine.core.EventContext in project core-java by SpineEventEngine.

the class AggregateStorage method toStorageRecord.

private static AggregateEventRecord toStorageRecord(Event event) {
    checkArgument(event.hasContext(), "Event context must be set.");
    final EventContext context = event.getContext();
    final String eventIdStr = Identifier.toString(event.getId());
    checkNotEmptyOrBlank(eventIdStr, "Event ID");
    checkArgument(event.hasMessage(), "Event message must be set.");
    final Timestamp timestamp = checkValid(context.getTimestamp());
    return AggregateEventRecord.newBuilder().setTimestamp(timestamp).setEvent(event).build();
}
Also used : EventContext(io.spine.core.EventContext) TextFormat.shortDebugString(com.google.protobuf.TextFormat.shortDebugString) Timestamp(com.google.protobuf.Timestamp)

Aggregations

EventContext (io.spine.core.EventContext)16 Event (io.spine.core.Event)12 Test (org.junit.Test)6 Timestamp (com.google.protobuf.Timestamp)3 IntegrationEventContext (io.spine.server.integration.IntegrationEventContext)3 CommandContext (io.spine.core.CommandContext)2 IntegrationEvent (io.spine.server.integration.IntegrationEvent)2 Any (com.google.protobuf.Any)1 Message (com.google.protobuf.Message)1 TextFormat.shortDebugString (com.google.protobuf.TextFormat.shortDebugString)1 ActorContext (io.spine.core.ActorContext)1 EventId (io.spine.core.EventId)1 RejectionContext (io.spine.core.RejectionContext)1 TenantId (io.spine.core.TenantId)1 GivenEvent (io.spine.core.given.GivenEvent)1