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;
}
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);
}
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;
}
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 -> {
});
}
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));
});
}
Aggregations