Search in sources :

Example 1 with ValidationException

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();
    }
}
Also used : ValidationException(io.spine.validate.ValidationException) Version(io.spine.core.Version)

Example 2 with ValidationException

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();
}
Also used : ValidationException(io.spine.validate.ValidationException) Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) Test(org.junit.Test)

Aggregations

Version (io.spine.core.Version)2 ValidationException (io.spine.validate.ValidationException)2 Versions.newVersion (io.spine.core.Versions.newVersion)1 Test (org.junit.Test)1