Search in sources :

Example 1 with DbAction

use of org.springframework.data.relational.core.conversion.DbAction in project spring-data-jdbc by spring-projects.

the class JdbcAggregateChangeExecutionContext method populateIdsIfNecessary.

@SuppressWarnings("unchecked")
@Nullable
<T> T populateIdsIfNecessary() {
    T newRoot = null;
    // have the results so that the inserts on the leaves come first.
    List<DbActionExecutionResult> reverseResults = new ArrayList<>(results.values());
    Collections.reverse(reverseResults);
    StagedValues cascadingValues = new StagedValues();
    for (DbActionExecutionResult result : reverseResults) {
        DbAction<?> action = result.getAction();
        if (!(action instanceof DbAction.WithGeneratedId)) {
            continue;
        }
        DbAction.WithEntity<?> withEntity = (DbAction.WithGeneratedId<?>) action;
        Object newEntity = setIdAndCascadingProperties(withEntity, result.getId(), cascadingValues);
        // the id property was immutable so we have to propagate changes up the tree
        if (newEntity != withEntity.getEntity()) {
            if (action instanceof DbAction.Insert) {
                DbAction.Insert<?> insert = (DbAction.Insert<?>) action;
                Pair<?, ?> qualifier = insert.getQualifier();
                cascadingValues.stage(insert.getDependingOn(), insert.getPropertyPath(), qualifier == null ? null : qualifier.getSecond(), newEntity);
            } else if (action instanceof DbAction.InsertRoot) {
                newRoot = (T) newEntity;
            }
        }
    }
    return newRoot;
}
Also used : ArrayList(java.util.ArrayList) DbActionExecutionResult(org.springframework.data.relational.core.conversion.DbActionExecutionResult) DbAction(org.springframework.data.relational.core.conversion.DbAction) Nullable(org.springframework.lang.Nullable)

Aggregations

ArrayList (java.util.ArrayList)1 DbAction (org.springframework.data.relational.core.conversion.DbAction)1 DbActionExecutionResult (org.springframework.data.relational.core.conversion.DbActionExecutionResult)1 Nullable (org.springframework.lang.Nullable)1