Search in sources :

Example 11 with MemoizedValue

use of io.spine.server.entity.storage.EntityColumn.MemoizedValue in project core-java by SpineEventEngine.

the class EntityQueryMatcher method getColumnValue.

private static Optional<MemoizedValue> getColumnValue(EntityRecordWithColumns record, EntityColumn column) {
    final String storedName = column.getStoredName();
    if (!record.getColumnNames().contains(storedName)) {
        return Optional.absent();
    }
    final MemoizedValue value = record.getColumnValue(storedName);
    return Optional.of(value);
}
Also used : MemoizedValue(io.spine.server.entity.storage.EntityColumn.MemoizedValue)

Example 12 with MemoizedValue

use of io.spine.server.entity.storage.EntityColumn.MemoizedValue in project core-java by SpineEventEngine.

the class ColumnRecords method feedColumnsTo.

/**
 * Feeds the given {@link EntityRecordWithColumns} to the given record representation.
 *
 * <p>This method deals with the {@linkplain EntityColumn entity columns} only.
 * The {@link io.spine.server.entity.EntityRecord EntityRecord} should be handled separately
 * while persisting a record.
 *
 * @param destination         the representation of a record in the database; implementation
 *                            specific
 * @param recordWithColumns   the {@linkplain EntityRecordWithColumns} to persist
 * @param columnTypeRegistry  the {@link io.spine.server.storage.Storage Storage}
 *                            {@link ColumnTypeRegistry}
 * @param mapColumnIdentifier a {@linkplain Function} mapping the column name
 *                            {@linkplain EntityColumn#getStoredName()} for storing}
 *                            to the database specific column identifier
 * @param <D>                 the type of the database record
 * @param <I>                 the type of the column identifier
 */
public static <D, I> void feedColumnsTo(D destination, EntityRecordWithColumns recordWithColumns, ColumnTypeRegistry<? extends ColumnType<?, ?, D, I>> columnTypeRegistry, Function<String, I> mapColumnIdentifier) {
    checkNotNull(destination);
    checkNotNull(recordWithColumns);
    checkNotNull(columnTypeRegistry);
    checkNotNull(mapColumnIdentifier);
    checkArgument(recordWithColumns.hasColumns(), "Passed record has no Entity Columns.");
    for (String columnName : recordWithColumns.getColumnNames()) {
        final I columnIdentifier = mapColumnIdentifier.apply(columnName);
        checkNotNull(columnIdentifier);
        final MemoizedValue columnValue = recordWithColumns.getColumnValue(columnName);
        final EntityColumn columnMetadata = columnValue.getSourceColumn();
        // We don't know the exact types of the value
        @SuppressWarnings("unchecked") final ColumnType<Object, Object, D, I> columnType = (ColumnType<Object, Object, D, I>) columnTypeRegistry.get(columnMetadata);
        checkArgument(columnType != null, String.format("ColumnType for %s could not be found.", columnMetadata.getType().getCanonicalName()));
        setValue(columnValue, destination, columnIdentifier, columnType);
    }
}
Also used : MemoizedValue(io.spine.server.entity.storage.EntityColumn.MemoizedValue)

Aggregations

MemoizedValue (io.spine.server.entity.storage.EntityColumn.MemoizedValue)12 Test (org.junit.Test)8 TestEntity (io.spine.server.entity.storage.given.ColumnTestEnv.TestEntity)5 BrokenTestEntity (io.spine.server.entity.storage.given.ColumnTestEnv.BrokenTestEntity)3 EqualsTester (com.google.common.testing.EqualsTester)2 EntityRecord (io.spine.server.entity.EntityRecord)2 LifecycleFlags (io.spine.server.entity.LifecycleFlags)1 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)1