Search in sources :

Example 1 with BaseEntity

use of com.google.cloud.datastore.BaseEntity in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplate method convertEntityResolveDescendantsAndReferences.

private <T> T convertEntityResolveDescendantsAndReferences(Class<T> entityClass, DatastorePersistentEntity datastorePersistentEntity, BaseKey key, ReadContext context) {
    T convertedObject;
    if (context.converted(key)) {
        convertedObject = (T) context.getConvertedEntity(key);
    } else {
        BaseEntity readEntity = context.getReadEntity(key);
        convertedObject = this.datastoreEntityConverter.read(entityClass, readEntity);
        // the parent entity should be put into context BEFORE referenced and descendant entities
        // are being resolved to avoid infinite loops
        context.putConvertedEntity(key, convertedObject);
        // raw Datastore entity is no longer needed
        context.removeReadEntity(key);
        if (convertedObject != null) {
            resolveDescendantProperties(datastorePersistentEntity, readEntity, convertedObject, context);
            resolveReferenceProperties(datastorePersistentEntity, readEntity, convertedObject, context);
        }
    }
    return convertedObject;
}
Also used : BaseEntity(com.google.cloud.datastore.BaseEntity)

Example 2 with BaseEntity

use of com.google.cloud.datastore.BaseEntity in project spring-cloud-gcp by spring-cloud.

the class GqlDatastoreQuery method getNonEntityObjectFromRow.

private static Object getNonEntityObjectFromRow(Object x) {
    Object mappedResult;
    if (x instanceof Key) {
        mappedResult = x;
    } else {
        BaseEntity entity = (BaseEntity) x;
        Set<String> colNames = entity.getNames();
        if (colNames.size() > 1) {
            throw new DatastoreDataException("The query method returns non-entity types, but the query result has " + "more than one column. Use a Projection entity type instead.");
        }
        mappedResult = entity.getValue((String) colNames.toArray()[0]).get();
    }
    return mappedResult;
}
Also used : DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) BaseEntity(com.google.cloud.datastore.BaseEntity) Key(com.google.cloud.datastore.Key)

Example 3 with BaseEntity

use of com.google.cloud.datastore.BaseEntity in project spring-cloud-gcp by spring-cloud.

the class DefaultDatastoreEntityConverterTests method testEmbeddedEntity.

@Test
public void testEmbeddedEntity() {
    EmbeddedEntity embeddedEntityA = new EmbeddedEntity("item 0");
    EmbeddedEntity embeddedEntityB = new EmbeddedEntity("item 1");
    List<EmbeddedEntity> embeddedEntities = Arrays.asList(embeddedEntityA, embeddedEntityB);
    Map<String, String> mapSimpleValues = new HashMap<>();
    mapSimpleValues.put("a", "valueA");
    mapSimpleValues.put("b", "valueB");
    Map<String, String[]> mapListValues = new HashMap<>();
    mapListValues.put("a", new String[] { "valueA" });
    mapListValues.put("b", new String[] { "valueB" });
    Map<String, EmbeddedEntity> embeddedEntityMapEmbeddedEntity = new HashMap<>();
    embeddedEntityMapEmbeddedEntity.put("a", embeddedEntityA);
    embeddedEntityMapEmbeddedEntity.put("b", embeddedEntityB);
    Map<String, List<EmbeddedEntity>> embeddedEntityMapListOfEmbeddedEntities = new HashMap<>();
    embeddedEntityMapListOfEmbeddedEntities.put("a", Arrays.asList(embeddedEntityA));
    embeddedEntityMapListOfEmbeddedEntities.put("b", Arrays.asList(embeddedEntityB));
    Map<String, Map<Long, Map<String, String>>> nestedEmbeddedMap = new HashMap<>();
    Map<Long, Map<String, String>> nestedInnerEmbeddedMap = new HashMap<>();
    nestedInnerEmbeddedMap.put(1L, mapSimpleValues);
    nestedEmbeddedMap.put("outer1", nestedInnerEmbeddedMap);
    Map<TestDatastoreItem.Color, String> enumKeysMap = new HashMap<>();
    enumKeysMap.put(TestDatastoreItem.Color.BLACK, "black");
    enumKeysMap.put(TestDatastoreItem.Color.WHITE, "white");
    CustomMap customMap = new CustomMap();
    customMap.put("key1", "val1");
    TestItemWithEmbeddedEntity item = new TestItemWithEmbeddedEntity(123, new EmbeddedEntity("abc"), embeddedEntities, mapSimpleValues, mapListValues, embeddedEntityMapEmbeddedEntity, embeddedEntityMapListOfEmbeddedEntities, enumKeysMap, customMap);
    item.setNestedEmbeddedMaps(nestedEmbeddedMap);
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new DatastoreServiceObjectToKeyFactory(() -> this.datastore));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    assertThat(entity.getList("listOfEmbeddedEntities").stream().map((val) -> ((BaseEntity<?>) val.get()).getString("stringField")).collect(Collectors.toList())).as("validate embedded entity").isEqualTo(Arrays.asList("item 0", "item 1"));
    assertThat(entity.getEntity("embeddedEntityField").getString("stringField")).as("validate embedded entity").isEqualTo("abc");
    assertThat(entity.getLong("intField")).as("validate int field").isEqualTo(123L);
    assertThat(entity.getEntity("nestedEmbeddedMaps").getEntity("outer1").getEntity("1").getString("a")).isEqualTo("valueA");
    assertThat(entity.getEntity("nestedEmbeddedMaps").getEntity("outer1").getEntity("1").getString("b")).isEqualTo("valueB");
    assertThat(entity.getEntity("embeddedMapSimpleValues").getString("a")).isEqualTo("valueA");
    assertThat(entity.getEntity("embeddedMapSimpleValues").getString("b")).isEqualTo("valueB");
    assertThat(entity.getEntity("embeddedMapListOfValues").getList("a")).contains(StringValue.of("valueA"));
    assertThat(entity.getEntity("embeddedMapListOfValues").getList("b")).contains(StringValue.of("valueB"));
    assertThat(entity.getEntity("embeddedEntityMapEmbeddedEntity").getEntity("a").getString("stringField")).isEqualTo("item 0");
    assertThat(entity.getEntity("embeddedEntityMapEmbeddedEntity").getEntity("b").getString("stringField")).isEqualTo("item 1");
    List<Value> embeddedMapValuesEmbeddedEntityA = entity.getEntity("embeddedEntityMapListOfEmbeddedEntities").getList("a");
    List<Value> embeddedMapValuesEmbeddedEntityB = entity.getEntity("embeddedEntityMapListOfEmbeddedEntities").getList("b");
    assertThat(((BaseEntity) embeddedMapValuesEmbeddedEntityA.get(0).get()).getString("stringField")).isEqualTo("item 0");
    assertThat(embeddedMapValuesEmbeddedEntityA.size()).isEqualTo(1);
    assertThat(((BaseEntity) embeddedMapValuesEmbeddedEntityB.get(0).get()).getString("stringField")).isEqualTo("item 1");
    assertThat(embeddedMapValuesEmbeddedEntityB.size()).isEqualTo(1);
    TestItemWithEmbeddedEntity read = entityConverter.read(TestItemWithEmbeddedEntity.class, entity);
    assertThat(read.getNestedEmbeddedMaps().get("outer1").get(1L).get("a")).isEqualTo("valueA");
    assertThat(read.getNestedEmbeddedMaps().get("outer1").get(1L).get("b")).isEqualTo("valueB");
    assertThat(entity.getEntity("customMap").getString("key1")).isEqualTo("val1");
    assertThat(read).as("read objects equals the original one").isEqualTo(item);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) EmbeddedEntity(org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) HashMap(java.util.HashMap) CustomMap(org.springframework.cloud.gcp.data.datastore.entities.CustomMap) ArrayList(java.util.ArrayList) List(java.util.List) BaseEntity(com.google.cloud.datastore.BaseEntity) EmbeddedEntity(org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) DiscriminatorValue(org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorValue) NullValue(com.google.cloud.datastore.NullValue) EntityValue(com.google.cloud.datastore.EntityValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue) ListValue(com.google.cloud.datastore.ListValue) HashMap(java.util.HashMap) Map(java.util.Map) CustomMap(org.springframework.cloud.gcp.data.datastore.entities.CustomMap) Test(org.junit.Test)

Aggregations

BaseEntity (com.google.cloud.datastore.BaseEntity)3 Entity (com.google.cloud.datastore.Entity)1 EntityValue (com.google.cloud.datastore.EntityValue)1 FullEntity (com.google.cloud.datastore.FullEntity)1 Key (com.google.cloud.datastore.Key)1 ListValue (com.google.cloud.datastore.ListValue)1 NullValue (com.google.cloud.datastore.NullValue)1 StringValue (com.google.cloud.datastore.StringValue)1 Value (com.google.cloud.datastore.Value)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1 EmbeddedEntity (org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity)1 DatastoreDataException (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)1 DatastoreMappingContext (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext)1 DiscriminatorValue (org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorValue)1 CustomMap (org.springframework.cloud.gcp.data.datastore.entities.CustomMap)1