use of io.spine.core.Version in project core-java by SpineEventEngine.
the class Aggregate method restore.
/**
* Restores the state and version from the passed snapshot.
*
* <p>If this method is called during a {@linkplain #play(AggregateStateRecord) replay}
* (because the snapshot was encountered) the method uses the state
* {@linkplain #getBuilder() builder}, which is used during the replay.
*
* <p>If not in replay, the method sets the state and version directly to the aggregate.
*
* @param snapshot the snapshot with the state to restore
*/
void restore(Snapshot snapshot) {
final S stateToRestore = AnyPacker.unpack(snapshot.getState());
final Version versionFromSnapshot = snapshot.getVersion();
setInitialState(stateToRestore, versionFromSnapshot);
}
use of io.spine.core.Version in project core-java by SpineEventEngine.
the class Transaction method commit.
/**
* Applies all the outstanding modifications to the enclosed entity.
*
* @throws InvalidEntityStateException in case the new entity state is not valid
* @throws IllegalStateException in case of a generic error
*/
protected void commit() throws InvalidEntityStateException, IllegalStateException {
final TransactionListener<I, E, S, B> listener = getListener();
final B builder = getBuilder();
final Version pendingVersion = getVersion();
// The state is only updated, if at least some changes were made to the builder.
if (builder.isDirty()) {
try {
final S newState = builder.build();
markStateChanged();
listener.onBeforeCommit(getEntity(), newState, pendingVersion, getLifecycleFlags());
entity.updateState(newState, pendingVersion);
commitAttributeChanges();
} catch (ValidationException exception) {
/* Could only happen if the state
has been injected not using
the builder setters. */
final InvalidEntityStateException invalidStateException = of(exception);
rollback(invalidStateException);
throw invalidStateException;
} catch (RuntimeException genericException) {
rollback(genericException);
throw illegalStateWithCauseOf(genericException);
} finally {
releaseTx();
}
} else {
// The state isn't modified, but other attributes may have been modified.
final S unmodifiedState = getEntity().getState();
listener.onBeforeCommit(getEntity(), unmodifiedState, pendingVersion, getLifecycleFlags());
// Set the version if it has changed.
if (!pendingVersion.equals(entity.getVersion())) {
entity.updateState(unmodifiedState, pendingVersion);
}
commitAttributeChanges();
releaseTx();
}
}
use of io.spine.core.Version in project core-java by SpineEventEngine.
the class TransactionShould method propagate_exception_wrapped_as_ISE_if_commit_fails.
@Test
public void propagate_exception_wrapped_as_ISE_if_commit_fails() {
final E entity = createEntity(someViolations());
final S newState = createNewState();
final Version version = someVersion();
final Transaction<I, E, S, B> tx = createTxWithState(entity, newState, version);
try {
tx.commit();
fail("Expected an exception due to a failed commit.");
} catch (IllegalStateException e) {
assertEquals(InvalidEntityStateException.class, e.getCause().getClass());
}
}
use of io.spine.core.Version in project core-java by SpineEventEngine.
the class TransactionShould method not_propagate_changes_to_entity_on_rollback.
@Test
public void not_propagate_changes_to_entity_on_rollback() {
final E entity = createEntity();
final Transaction<I, E, S, B> tx = createTx(entity);
final Event event = createEvent(createEventMessage());
applyEvent(tx, event);
final S stateBeforeRollback = entity.getState();
final Version versionBeforeRollback = entity.getVersion();
tx.rollback(new RuntimeException("that triggers rollback"));
final S stateAfterRollback = entity.getState();
final Version versionAfterRollback = entity.getVersion();
assertEquals(stateBeforeRollback, stateAfterRollback);
assertEquals(versionBeforeRollback, versionAfterRollback);
}
use of io.spine.core.Version in project core-java by SpineEventEngine.
the class TransactionShould method set_txEntityVersion_from_EventContext.
@Test
public void set_txEntityVersion_from_EventContext() {
final E entity = createEntity();
final Transaction<I, E, S, B> tx = createTx(entity);
final Event event = createEvent(createEventMessage());
final Version ctxVersion = event.getContext().getVersion();
assertNotEquals(tx.getVersion(), ctxVersion);
applyEvent(tx, event);
final Version modifiedVersion = tx.getVersion();
assertEquals(modifiedVersion, tx.getVersion());
}
Aggregations