Search in sources :

Example 6 with DatastorePersistentEntity

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.

the class DefaultDatastoreEntityConverter method read.

@Override
@SuppressWarnings("unchecked")
public <R> R read(Class<R> clazz, BaseEntity entity) {
    if (entity == null) {
        return null;
    }
    DatastorePersistentEntity<R> ostensiblePersistentEntity = (DatastorePersistentEntity<R>) this.mappingContext.getPersistentEntity(clazz);
    if (ostensiblePersistentEntity == null) {
        throw new DatastoreDataException("Unable to convert Datastore Entity to " + clazz);
    }
    EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider(entity, this.conversions);
    DatastorePersistentEntity<?> persistentEntity = getDiscriminationPersistentEntity(ostensiblePersistentEntity, propertyValueProvider);
    ParameterValueProvider<DatastorePersistentProperty> parameterValueProvider = new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null);
    EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity);
    Object instance;
    try {
        instance = instantiator.createInstance(persistentEntity, parameterValueProvider);
        PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance);
        persistentEntity.doWithColumnBackedProperties(datastorePersistentProperty -> {
            // if a property is a constructor argument, it was already computed on instantiation
            if (!persistentEntity.isConstructorArgument(datastorePersistentProperty)) {
                Object value = propertyValueProvider.getPropertyValue(datastorePersistentProperty);
                if (value != null) {
                    accessor.setProperty(datastorePersistentProperty, value);
                }
            }
        });
    } catch (DatastoreDataException ex) {
        throw new DatastoreDataException("Unable to read " + persistentEntity.getName() + " entity", ex);
    }
    return (R) instance;
}
Also used : DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) EntityInstantiator(org.springframework.data.mapping.model.EntityInstantiator) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Example 7 with DatastorePersistentEntity

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.

the class DefaultDatastoreEntityConverter method getDiscriminationPersistentEntity.

public <T> DatastorePersistentEntity<T> getDiscriminationPersistentEntity(Class<T> entityClass, BaseEntity<?> entity) {
    DatastorePersistentEntity ostensiblePersistentEntity = this.mappingContext.getPersistentEntity(entityClass);
    if (ostensiblePersistentEntity == null) {
        throw new DatastoreDataException("Unable to convert Datastore Entity to " + entityClass);
    }
    EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider(entity, this.conversions);
    return getDiscriminationPersistentEntity(ostensiblePersistentEntity, propertyValueProvider);
}
Also used : DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)

Example 8 with DatastorePersistentEntity

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.

the class GqlDatastoreQuery method setGqlResolvedEntityClassName.

private void setGqlResolvedEntityClassName() {
    Matcher matcher = CLASS_NAME_PATTERN.matcher(GqlDatastoreQuery.this.originalGql);
    String result = GqlDatastoreQuery.this.originalGql;
    while (matcher.find()) {
        String matched = matcher.group();
        String className = matched.substring(1, matched.length() - 1);
        try {
            Class entityClass = Class.forName(className);
            DatastorePersistentEntity datastorePersistentEntity = GqlDatastoreQuery.this.datastoreMappingContext.getPersistentEntity(entityClass);
            if (datastorePersistentEntity == null) {
                throw new DatastoreDataException("The class used in the GQL statement is not a Cloud Datastore persistent entity: " + className);
            }
            result = result.replace(matched, datastorePersistentEntity.kindName());
        } catch (ClassNotFoundException ex) {
            throw new DatastoreDataException("The class name does not refer to an available entity type: " + className);
        }
    }
    this.gqlResolvedEntityClassName = result;
}
Also used : Matcher(java.util.regex.Matcher) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)

Example 9 with DatastorePersistentEntity

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplateTests method findAllReferenceLoopTest.

@Test
void findAllReferenceLoopTest() {
    Entity referenceTestDatastoreEntity = Entity.newBuilder(this.key1).set("sibling", this.key1).set("lazyChildren", ListValue.of(this.key2)).set("lazyChild", this.childKey2).build();
    Entity child = Entity.newBuilder(this.key1).build();
    Entity child2 = Entity.newBuilder(this.childKey2).build();
    when(this.datastore.fetch(this.key1)).thenReturn(Collections.singletonList(referenceTestDatastoreEntity));
    when(this.datastore.fetch(this.key2)).thenReturn(Collections.singletonList(child));
    when(this.datastore.fetch(this.childKey2)).thenReturn(Collections.singletonList(child2));
    ReferenceTestEntity referenceTestEntity = new ReferenceTestEntity();
    ReferenceTestEntity childEntity = new ReferenceTestEntity();
    ReferenceTestEntity childEntity2 = new ReferenceTestEntity();
    DatastorePersistentEntity referenceTestPersistentEntity = new DatastoreMappingContext().getDatastorePersistentEntity(ReferenceTestEntity.class);
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(referenceTestDatastoreEntity))).thenAnswer(invocationOnMock -> referenceTestEntity);
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(eq(ReferenceTestEntity.class), same(referenceTestDatastoreEntity))).thenReturn(referenceTestPersistentEntity);
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(child))).thenAnswer(invocationOnMock -> childEntity);
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(eq(ReferenceTestEntity.class), same(child))).thenReturn(referenceTestPersistentEntity);
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(child2))).thenAnswer(invocationOnMock -> childEntity2);
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(eq(ReferenceTestEntity.class), same(child2))).thenReturn(referenceTestPersistentEntity);
    verifyBeforeAndAfterEvents(null, new AfterFindByKeyEvent(Collections.singletonList(referenceTestEntity), Collections.singleton(this.key1)), () -> {
        ReferenceTestEntity readReferenceTestEntity = this.datastoreTemplate.findById(this.key1, ReferenceTestEntity.class);
        assertThat(readReferenceTestEntity.sibling).isSameAs(readReferenceTestEntity);
        verify(this.datastore, times(1)).fetch(any());
        assertThat(readReferenceTestEntity.lazyChildren).hasSize(1);
        verify(this.datastore, times(2)).fetch(any());
        verify(this.datastore, times(1)).fetch(this.key1);
        verify(this.datastore, times(1)).fetch(this.key2);
        assertThat(readReferenceTestEntity.lazyChild.toString()).isNotNull();
        verify(this.datastore, times(3)).fetch(any());
        verify(this.datastore, times(1)).fetch(this.childKey2);
    }, x -> {
    });
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) Test(org.junit.jupiter.api.Test)

Example 10 with DatastorePersistentEntity

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplate method resolveDescendantProperties.

private <T> void resolveDescendantProperties(DatastorePersistentEntity datastorePersistentEntity, BaseEntity entity, T convertedObject, ReadContext context) {
    datastorePersistentEntity.doWithDescendantProperties(descendantPersistentProperty -> {
        Class descendantType = descendantPersistentProperty.getComponentType();
        Key entityKey = (Key) entity.getKey();
        Key ancestorKey = KeyUtil.getKeyWithoutAncestors(entityKey);
        DatastorePersistentEntity descendantEntityType = this.datastoreMappingContext.getPersistentEntity(descendantType);
        Filter ancestorFilter = descendantEntityType.getDiscriminationFieldName() != null ? StructuredQuery.CompositeFilter.and(PropertyFilter.eq(descendantEntityType.getDiscriminationFieldName(), descendantEntityType.getDiscriminatorValue()), PropertyFilter.hasAncestor(ancestorKey)) : PropertyFilter.hasAncestor(ancestorKey);
        EntityQuery descendantQuery = Query.newEntityQueryBuilder().setKind(descendantEntityType.kindName()).setFilter(ancestorFilter).build();
        List entities = convertEntitiesForRead(getDatastoreReadWriter().run(descendantQuery), descendantType, context);
        datastorePersistentEntity.getPropertyAccessor(convertedObject).setProperty(descendantPersistentProperty, // Converting the collection type.
        this.datastoreEntityConverter.getConversions().convertOnRead(entities, descendantPersistentProperty.getType(), descendantType));
    });
}
Also used : DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Filter(com.google.cloud.datastore.StructuredQuery.Filter) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) BaseKey(com.google.cloud.datastore.BaseKey)

Aggregations

DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)14 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)10 Key (com.google.cloud.datastore.Key)8 Entity (com.google.cloud.datastore.Entity)7 FullEntity (com.google.cloud.datastore.FullEntity)7 IncompleteKey (com.google.cloud.datastore.IncompleteKey)7 ArrayList (java.util.ArrayList)7 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)6 DatastorePersistentProperty (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)6 BaseEntity (com.google.cloud.datastore.BaseEntity)5 BaseKey (com.google.cloud.datastore.BaseKey)5 EntityQuery (com.google.cloud.datastore.EntityQuery)5 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)5 Filter (com.google.cloud.datastore.StructuredQuery.Filter)5 PropertyFilter (com.google.cloud.datastore.StructuredQuery.PropertyFilter)5 AfterFindByKeyEvent (com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent)5 Cursor (com.google.cloud.datastore.Cursor)4 Datastore (com.google.cloud.datastore.Datastore)4 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)4 Builder (com.google.cloud.datastore.Entity.Builder)4