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());
}
}
}
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();
}
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));
}
}
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;
}
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;
}
Aggregations