Search in sources :

Example 1 with EntityUpdateOperation

use of io.spine.server.tenant.EntityUpdateOperation in project core-java by SpineEventEngine.

the class Stand method update.

/**
     * Updates the state of an entity inside of the current instance of {@code Stand}.
     *
     * <p>In case the entity update represents the new
     * {@link io.spine.server.aggregate.Aggregate Aggregate} state,
     * store the new value for the {@code Aggregate} to each of the configured instances of
     * {@link StandStorage}.
     *
     * <p>Each {@code Aggregate} state value is stored as one-to-one to its
     * {@link TypeUrl TypeUrl} obtained via {@link Any#getTypeUrl()}.
     *
     * <p>In case {@code Stand} already contains the state for this {@code Aggregate},
     * the value will be replaced.
     *
     * <p>The state updates which are not originated from the {@code Aggregate} are not
     * stored in the {@code Stand}.
     *
     * <p>In any case, the state update is then propagated to the callbacks.
     * The set of matched callbacks is determined by filtering all the registered callbacks
     * by the entity {@code TypeUrl}.
     *
     * <p>The matching callbacks are executed with the {@link #callbackExecutor}.
     *
     * @param envelope the updated entity state,
     *                 packed as {@linkplain EntityStateEnvelope envelope}
     */
void update(final EntityStateEnvelope<?, ?> envelope) {
    final EntityUpdateOperation op = new EntityUpdateOperation(envelope) {

        @Override
        public void run() {
            final Object id = envelope.getEntityId();
            final Message entityState = envelope.getMessage();
            final Any packedState = AnyPacker.pack(entityState);
            final TypeUrl entityTypeUrl = TypeUrl.of(entityState);
            final boolean aggregateUpdate = typeRegistry.hasAggregateType(entityTypeUrl);
            if (aggregateUpdate) {
                final Optional<Version> entityVersion = envelope.getEntityVersion();
                checkState(entityVersion.isPresent(), "The aggregate version must be set in order to update Stand. " + "Actual envelope: {}", envelope);
                // checked above.
                @SuppressWarnings("OptionalGetWithoutIsPresent") final Version versionValue = entityVersion.get();
                final AggregateStateId aggregateStateId = AggregateStateId.of(id, entityTypeUrl);
                final EntityRecord record = EntityRecord.newBuilder().setState(packedState).setVersion(versionValue).build();
                storage.write(aggregateStateId, record);
            }
            notifyMatchingSubscriptions(id, packedState, entityTypeUrl);
        }
    };
    op.execute();
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) Version(io.spine.base.Version) EntityUpdateOperation(io.spine.server.tenant.EntityUpdateOperation) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any)

Aggregations

Any (com.google.protobuf.Any)1 Message (com.google.protobuf.Message)1 Version (io.spine.base.Version)1 EntityRecord (io.spine.server.entity.EntityRecord)1 EntityUpdateOperation (io.spine.server.tenant.EntityUpdateOperation)1 TypeUrl (io.spine.type.TypeUrl)1