use of io.spine.validate.ValidationException 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.validate.ValidationException in project core-java by SpineEventEngine.
the class TransactionShould method throw_InvalidEntityStateException_if_constraints_violated_on_commit.
@Test(expected = InvalidEntityStateException.class)
public void throw_InvalidEntityStateException_if_constraints_violated_on_commit() {
final E entity = createEntity();
final S newState = createNewState();
final Version version = someVersion();
final Transaction<I, E, S, B> tx = createTxWithState(entity, newState, version);
final ValidationException toThrow = validationException();
breakEntityValidation(entity, toThrow);
tx.commit();
}
Aggregations