Search in sources :

Example 11 with ValidationResult

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;
}
Also used : ValidationException(org.apache.cayenne.validation.ValidationException) GraphDiff(org.apache.cayenne.graph.GraphDiff) ValidationResult(org.apache.cayenne.validation.ValidationResult)

Example 12 with ValidationResult

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;
}
Also used : ObjectNameGenerator(org.apache.cayenne.dbsync.naming.ObjectNameGenerator) ModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ModelMergeDelegate) ProxyModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate) ValidationResult(org.apache.cayenne.validation.ValidationResult) MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext) LinkedList(java.util.LinkedList) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) ObjEntity(org.apache.cayenne.map.ObjEntity) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) ProxyModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate)

Example 13 with ValidationResult

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;
}
Also used : ValidationException(org.apache.cayenne.validation.ValidationException) Validating(org.apache.cayenne.Validating) Persistent(org.apache.cayenne.Persistent) ValidationResult(org.apache.cayenne.validation.ValidationResult)

Example 14 with ValidationResult

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;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ValidationResult(org.apache.cayenne.validation.ValidationResult) Embeddable(org.apache.cayenne.map.Embeddable)

Example 15 with ValidationResult

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);
    }
}
Also used : DataSourceWizard(org.apache.cayenne.modeler.dialog.db.DataSourceWizard) ArrayList(java.util.ArrayList) ValidationResultBrowser(org.apache.cayenne.modeler.dialog.ValidationResultBrowser) ValidationResult(org.apache.cayenne.validation.ValidationResult) DbGenerator(org.apache.cayenne.access.DbGenerator)

Aggregations

ValidationResult (org.apache.cayenne.validation.ValidationResult)18 Test (org.junit.Test)7 ValidationFailure (org.apache.cayenne.validation.ValidationFailure)6 ProjectValidator (org.apache.cayenne.project.validation.ProjectValidator)4 Artist (org.apache.cayenne.testdo.testmap.Artist)3 BeanValidationFailure (org.apache.cayenne.validation.BeanValidationFailure)3 ArrayList (java.util.ArrayList)2 DbEntity (org.apache.cayenne.map.DbEntity)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2 MeaningfulFK (org.apache.cayenne.testdo.relationships.MeaningfulFK)2 SimpleValidationFailure (org.apache.cayenne.validation.SimpleValidationFailure)2 ValidationException (org.apache.cayenne.validation.ValidationException)2 File (java.io.File)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 Persistent (org.apache.cayenne.Persistent)1 Validating (org.apache.cayenne.Validating)1