use of org.apache.cayenne.validation.ValidationResult 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.ValidationResult in project cayenne by apache.
the class DefaultDbImportAction method applyTokens.
private boolean applyTokens(DataMap targetDataMap, Collection<MergerToken> tokens, DbImportConfiguration config) {
if (tokens.isEmpty()) {
logger.info("");
logger.info("Detected changes: No changes to import.");
return false;
}
final Collection<ObjEntity> loadedObjEntities = new LinkedList<>();
ModelMergeDelegate mergeDelegate = new ProxyModelMergeDelegate(config.createMergeDelegate()) {
@Override
public void objEntityAdded(ObjEntity ent) {
loadedObjEntities.add(ent);
super.objEntityAdded(ent);
}
};
ObjectNameGenerator nameGenerator = config.createNameGenerator();
MergerContext mergerContext = MergerContext.builder(targetDataMap).delegate(mergeDelegate).nameGenerator(nameGenerator).usingPrimitives(config.isUsePrimitives()).usingJava7Types(config.isUseJava7Types()).meaningfulPKFilter(config.createMeaningfulPKFilter()).build();
for (MergerToken token : tokens) {
try {
token.execute(mergerContext);
} catch (Throwable th) {
String message = "Migration Error. Can't apply changes from token: " + token.getTokenName() + " (" + token.getTokenValue() + ")";
logger.error(message, th);
mergerContext.getValidationResult().addFailure(new SimpleValidationFailure(th, message));
}
}
ValidationResult failures = mergerContext.getValidationResult();
if (failures.hasFailures()) {
logger.info("Migration Complete.");
logger.warn("Migration finished. The following problem(s) were encountered and ignored.");
for (ValidationFailure failure : failures.getFailures()) {
logger.warn(failure.toString());
}
} else {
logger.info("Migration Complete Successfully.");
}
flattenManyToManyRelationships(targetDataMap, loadedObjEntities, nameGenerator);
relationshipsSanity(targetDataMap);
return true;
}
use of org.apache.cayenne.validation.ValidationResult 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;
}
use of org.apache.cayenne.validation.ValidationResult in project cayenne by apache.
the class CodeGeneratorControllerBase method validate.
public void validate(GeneratorController validator) {
ValidationResult validationBuffer = new ValidationResult();
if (validator != null) {
for (Object classObj : classes) {
if (classObj instanceof ObjEntity) {
validator.validateEntity(validationBuffer, (ObjEntity) classObj, false);
} else if (classObj instanceof Embeddable) {
validator.validateEmbeddable(validationBuffer, (Embeddable) classObj);
}
}
}
this.validation = validationBuffer;
}
use of org.apache.cayenne.validation.ValidationResult in project cayenne by apache.
the class DBGeneratorOptions method generateSchemaAction.
/**
* Performs configured schema operations via DbGenerator.
*/
public void generateSchemaAction() {
DataSourceWizard connectWizard = new DataSourceWizard(this.getParent(), "Generate DB Schema: Connect to Database");
if (!connectWizard.startupAction()) {
return;
}
this.connectionInfo = connectWizard.getConnectionInfo();
refreshGeneratorAction();
Collection<ValidationResult> failures = new ArrayList<ValidationResult>();
// sanity check...
for (DbGenerator generator : generators) {
if (generator.isEmpty(true)) {
JOptionPane.showMessageDialog(getView(), "Nothing to generate.");
return;
}
try {
generator.runGenerator(connectWizard.getDataSource());
failures.add(generator.getFailures());
} catch (Throwable th) {
reportError("Schema Generation Error", th);
}
}
if (failures.size() == 0) {
JOptionPane.showMessageDialog(getView(), "Schema Generation Complete.");
} else {
new ValidationResultBrowser(this).startupAction("Schema Generation Complete", "Schema generation finished. The following problem(s) were ignored.", failures);
}
}
Aggregations