Search in sources :

Example 6 with ValidationException

use of org.apache.cayenne.validation.ValidationException in project cayenne by apache.

the class DbEntityTab method setQualifier.

void setQualifier(String qualifier) {
    if (qualifier != null && qualifier.trim().length() == 0) {
        qualifier = null;
    }
    DbEntity ent = mediator.getCurrentDbEntity();
    if (ent != null && !Util.nullSafeEquals(ent.getQualifier(), qualifier)) {
        ExpressionConvertor convertor = new ExpressionConvertor();
        try {
            String oldQualifier = convertor.valueAsString(ent.getQualifier());
            if (!Util.nullSafeEquals(oldQualifier, qualifier)) {
                Expression exp = (Expression) convertor.stringAsValue(qualifier);
                ent.setQualifier(exp);
                mediator.fireDbEntityEvent(new EntityEvent(this, ent));
            }
        } catch (IllegalArgumentException ex) {
            // unparsable qualifier
            throw new ValidationException(ex.getMessage());
        }
    }
}
Also used : ValidationException(org.apache.cayenne.validation.ValidationException) DbEntity(org.apache.cayenne.map.DbEntity) Expression(org.apache.cayenne.exp.Expression) EntityEvent(org.apache.cayenne.map.event.EntityEvent) ExpressionConvertor(org.apache.cayenne.modeler.util.ExpressionConvertor)

Example 7 with ValidationException

use of org.apache.cayenne.validation.ValidationException in project cayenne by apache.

the class NestedDataContextValidationIT method testValidateOnCommitToParent.

@Test
public void testValidateOnCommitToParent() {
    context.setValidatingObjectsOnCommit(true);
    ObjectContext childContext = runtime.newContext(context);
    assertTrue("Child context must have inherited the validation flag from parent", ((DataContext) childContext).isValidatingObjectsOnCommit());
    Artist a1 = childContext.newObject(Artist.class);
    try {
        childContext.commitChangesToParent();
        fail("No validation was performed");
    } catch (ValidationException e) {
    // expected
    }
    assertFalse(context.hasChanges());
    a1.setArtistName("T");
    childContext.commitChangesToParent();
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ValidationException(org.apache.cayenne.validation.ValidationException) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 8 with ValidationException

use of org.apache.cayenne.validation.ValidationException in project cayenne by apache.

the class ServerRuntimeIT method testPerformInTransaction_Local_Callback_Rollback.

@Test
public void testPerformInTransaction_Local_Callback_Rollback() {
    TransactionListener callback = mock(TransactionListener.class);
    try {
        runtime.performInTransaction(new TransactionalOperation<Artist>() {

            @Override
            public Artist perform() {
                Artist localArtist = runtime.newContext().newObject(Artist.class);
                localArtist.getObjectContext().commitChanges();
                return localArtist;
            }
        }, callback);
        fail("Exception expected");
    } catch (ValidationException v) {
        verify(callback).willRollback(any(Transaction.class));
        verify(callback, times(0)).willAddConnection(any(Transaction.class), any(String.class), any(Connection.class));
        verify(callback, times(0)).willCommit(any(Transaction.class));
    }
}
Also used : TransactionListener(org.apache.cayenne.tx.TransactionListener) Artist(org.apache.cayenne.testdo.testmap.Artist) ValidationException(org.apache.cayenne.validation.ValidationException) Test(org.junit.Test)

Example 9 with ValidationException

use of org.apache.cayenne.validation.ValidationException in project cayenne by apache.

the class CayenneContext method doCommitChanges.

GraphDiff doCommitChanges(boolean cascade) {
    int syncType = cascade ? DataChannel.FLUSH_CASCADE_SYNC : DataChannel.FLUSH_NOCASCADE_SYNC;
    GraphDiff commitDiff = null;
    synchronized (graphManager) {
        if (graphManager.hasChanges()) {
            if (isValidatingObjectsOnCommit()) {
                ValidationResult result = new ValidationResult();
                Iterator<?> it = graphManager.dirtyNodes().iterator();
                while (it.hasNext()) {
                    Persistent p = (Persistent) it.next();
                    if (p instanceof Validating) {
                        switch(p.getPersistenceState()) {
                            case PersistenceState.NEW:
                                ((Validating) p).validateForInsert(result);
                                break;
                            case PersistenceState.MODIFIED:
                                ((Validating) p).validateForUpdate(result);
                                break;
                            case PersistenceState.DELETED:
                                ((Validating) p).validateForDelete(result);
                                break;
                        }
                    }
                }
                if (result.hasFailures()) {
                    throw new ValidationException(result);
                }
            }
            graphManager.graphCommitStarted();
            GraphDiff changes = graphManager.getDiffsSinceLastFlush();
            try {
                commitDiff = channel.onSync(this, changes, syncType);
            } catch (Throwable th) {
                graphManager.graphCommitAborted();
                if (th instanceof CayenneRuntimeException) {
                    throw (CayenneRuntimeException) th;
                } else {
                    throw new CayenneRuntimeException("Commit error", th);
                }
            }
            graphManager.graphCommitted(commitDiff);
            // this event is caught by peer nested ObjectContexts to
            // synchronize the
            // state
            fireDataChannelCommitted(this, changes);
        }
    }
    return commitDiff;
}
Also used : ValidationException(org.apache.cayenne.validation.ValidationException) GraphDiff(org.apache.cayenne.graph.GraphDiff) ValidationResult(org.apache.cayenne.validation.ValidationResult)

Example 10 with ValidationException

use of org.apache.cayenne.validation.ValidationException in project cayenne by apache.

the class ObjectStoreGraphDiff method validateAndCheckNoop.

/**
 * Requires external synchronization on ObjectStore.
 */
boolean validateAndCheckNoop() {
    if (getChangesByObjectId().isEmpty()) {
        return true;
    }
    boolean noop = true;
    // build a new collection for validation as validation methods may
    // result in
    // ObjectStore modifications
    Collection<Validating> objectsToValidate = null;
    for (final ObjectDiff diff : getChangesByObjectId().values()) {
        if (!diff.isNoop()) {
            noop = false;
            if (diff.getObject() instanceof Validating) {
                if (objectsToValidate == null) {
                    objectsToValidate = new ArrayList<>();
                }
                objectsToValidate.add((Validating) diff.getObject());
            }
        }
    }
    if (objectsToValidate != null) {
        ValidationResult result = new ValidationResult();
        for (Validating object : objectsToValidate) {
            switch(((Persistent) object).getPersistenceState()) {
                case PersistenceState.NEW:
                    object.validateForInsert(result);
                    break;
                case PersistenceState.MODIFIED:
                    object.validateForUpdate(result);
                    break;
                case PersistenceState.DELETED:
                    object.validateForDelete(result);
                    break;
            }
        }
        if (result.hasFailures()) {
            throw new ValidationException(result);
        }
    }
    return noop;
}
Also used : ValidationException(org.apache.cayenne.validation.ValidationException) Validating(org.apache.cayenne.Validating) Persistent(org.apache.cayenne.Persistent) ValidationResult(org.apache.cayenne.validation.ValidationResult)

Aggregations

ValidationException (org.apache.cayenne.validation.ValidationException)20 FormLayout (com.jgoodies.forms.layout.FormLayout)5 BorderLayout (java.awt.BorderLayout)5 JTextField (javax.swing.JTextField)5 TextAdapter (org.apache.cayenne.modeler.util.TextAdapter)5 DefaultFormBuilder (com.jgoodies.forms.builder.DefaultFormBuilder)4 DataMap (org.apache.cayenne.map.DataMap)4 JLabel (javax.swing.JLabel)3 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)3 QueryEvent (org.apache.cayenne.configuration.event.QueryEvent)3 Expression (org.apache.cayenne.exp.Expression)3 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)3 EntityEvent (org.apache.cayenne.map.event.EntityEvent)3 ExpressionConvertor (org.apache.cayenne.modeler.util.ExpressionConvertor)3 JCayenneCheckBox (org.apache.cayenne.swing.components.JCayenneCheckBox)3 Test (org.junit.Test)3 JButton (javax.swing.JButton)2 JPanel (javax.swing.JPanel)2 JToolBar (javax.swing.JToolBar)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2