Search in sources :

Example 1 with DbActionExecutionResult

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

the class JdbcAggregateChangeExecutionContext method executeInsertRoot.

<T> void executeInsertRoot(DbAction.InsertRoot<T> insert) {
    RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(insert.getEntityType());
    Object id;
    if (persistentEntity.hasVersionProperty()) {
        RelationalPersistentProperty versionProperty = persistentEntity.getVersionProperty();
        Assert.state(versionProperty != null, "Version property must not be null at this stage.");
        long initialVersion = versionProperty.getActualType().isPrimitive() ? 1L : 0;
        T rootEntity = // 
        RelationalEntityVersionUtils.setVersionNumberOnEntity(insert.getEntity(), initialVersion, persistentEntity, converter);
        id = accessStrategy.insert(rootEntity, insert.getEntityType(), Identifier.empty());
        setNewVersion(initialVersion);
    } else {
        id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), Identifier.empty());
    }
    add(new DbActionExecutionResult(insert, id));
}
Also used : DbActionExecutionResult(org.springframework.data.relational.core.conversion.DbActionExecutionResult) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Example 2 with DbActionExecutionResult

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

the class JdbcAggregateChangeExecutionContext method getPotentialGeneratedIdFrom.

private Object getPotentialGeneratedIdFrom(DbAction.WithEntity<?> idOwningAction) {
    if (idOwningAction instanceof DbAction.WithGeneratedId) {
        Object generatedId;
        DbActionExecutionResult dbActionExecutionResult = results.get(idOwningAction);
        generatedId = dbActionExecutionResult == null ? null : dbActionExecutionResult.getId();
        if (generatedId != null) {
            return generatedId;
        }
    }
    return getIdFrom(idOwningAction);
}
Also used : DbActionExecutionResult(org.springframework.data.relational.core.conversion.DbActionExecutionResult)

Example 3 with DbActionExecutionResult

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

the class JdbcAggregateChangeExecutionContext method executeInsert.

<T> void executeInsert(DbAction.Insert<T> insert) {
    Identifier parentKeys = getParentKeys(insert, converter);
    Object id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), parentKeys);
    add(new DbActionExecutionResult(insert, id));
}
Also used : Identifier(org.springframework.data.jdbc.core.convert.Identifier) DbActionExecutionResult(org.springframework.data.relational.core.conversion.DbActionExecutionResult)

Example 4 with DbActionExecutionResult

use of org.springframework.data.relational.core.conversion.DbActionExecutionResult 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)

Example 5 with DbActionExecutionResult

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

the class JdbcAggregateChangeExecutionContext method executeMerge.

<T> void executeMerge(DbAction.Merge<T> merge) {
    // temporary implementation
    if (!accessStrategy.update(merge.getEntity(), merge.getEntityType())) {
        Object id = accessStrategy.insert(merge.getEntity(), merge.getEntityType(), getParentKeys(merge, converter));
        add(new DbActionExecutionResult(merge, id));
    } else {
        add(new DbActionExecutionResult());
    }
}
Also used : DbActionExecutionResult(org.springframework.data.relational.core.conversion.DbActionExecutionResult)

Aggregations

DbActionExecutionResult (org.springframework.data.relational.core.conversion.DbActionExecutionResult)5 ArrayList (java.util.ArrayList)1 Identifier (org.springframework.data.jdbc.core.convert.Identifier)1 DbAction (org.springframework.data.relational.core.conversion.DbAction)1 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)1 Nullable (org.springframework.lang.Nullable)1