Refine search
BaseEntity(BaseEntity<K> from) { this.key = from.getKey(); this.properties = from.properties; }
@SuppressWarnings("unchecked") public <T> T getPropertyValue(String fieldName, EmbeddedType embeddedType, TypeInformation targetTypeInformation) { if (!this.entity.contains(fieldName)) { return null; } try { return this.conversion.convertOnRead(this.entity.getValue(fieldName).get(), embeddedType, targetTypeInformation); } catch (ConversionException | DatastoreDataException ex) { throw new DatastoreDataException("Unable to read property " + fieldName, ex); } } }
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; }
/** * The problem is ProjectionEntity; there's no way to create an EntityValue with a ProjectionEntity * so we can't use the standard translation system for {@code Value<Entity>}. Instead of making the * translation API really complicated, just convert it to a FullEntity. */ private EntityValue makeLoadEntityValue(final BaseEntity<?> ent) { if (ent instanceof FullEntity<?>) { return EntityValue.of((FullEntity<?>)ent); } else { // Sadly there's no more graceful way of doing this final Builder<?> builder = FullEntity.newBuilder(ent.getKey()); for (final String name : ent.getNames()) { final Value<?> value = ent.getValue(name); builder.set(name, value); } return EntityValue.of(builder.build()); } }
private Object findReferenced(BaseEntity entity, DatastorePersistentProperty referencePersistentProperty) { String fieldName = referencePersistentProperty.getFieldName(); try { Object referenced; if (!entity.contains(fieldName)) { referenced = null; } else if (referencePersistentProperty.isCollectionLike()) { Class referencedType = referencePersistentProperty.getComponentType(); List<Value<Key>> keyValues = entity.getList(fieldName); referenced = this.datastoreEntityConverter.getConversions() .convertOnRead( findAllById( keyValues.stream().map(Value::get).collect(Collectors.toList()), referencedType), referencePersistentProperty.getType(), referencedType); } else { referenced = findById(entity.getKey(fieldName), referencePersistentProperty.getType()); } return referenced; } catch (ClassCastException ex) { throw new DatastoreDataException( "Error loading reference property " + fieldName + "." + "Reference properties must be stored as Keys or lists of Keys" + " in Cloud Datastore for singular or multiple references, respectively."); } }
@Test public void testGetList() throws Exception { BaseEntity<Key> entity = builder.build(); List<? extends Value<?>> list = entity.getList("list1"); assertEquals(3, list.size()); assertEquals(NullValue.of(), list.get(0)); assertEquals("foo", list.get(1).get()); assertEquals(LAT_LNG, list.get(2).get()); list = entity.getList("list2"); assertEquals(2, list.size()); assertEquals(Long.valueOf(10), list.get(0).get()); assertEquals(Double.valueOf(2), list.get(1).get()); list = entity.getList("list3"); assertEquals(1, list.size()); assertEquals(Boolean.TRUE, list.get(0).get()); entity = builder.set("list1", ListValue.of(list)).build(); assertEquals(list, entity.getList("list1")); List<Value<?>> stringList = entity.getList("stringList"); assertEquals( ImmutableList.of(StringValue.of("s1"), StringValue.of("s2"), StringValue.of("s3")), stringList); List<Value<Double>> doubleList = entity.getList("doubleList"); assertEquals( ImmutableList.of(DoubleValue.of(12.3), DoubleValue.of(4.56), DoubleValue.of(.789)), doubleList); List<EntityValue> entityList = entity.getList("entityList"); assertEquals( ImmutableList.of(EntityValue.of(ENTITY), EntityValue.of(PARTIAL_ENTITY)), entityList); }
/** * Unmarshals the property with the given metadata and sets the unmarshalled value on the given * <code>target</code> object. * * @param propertyMetadata * the metadata of the property * @param target * the target object to set the unmarshalled value on * @param nativeEntity * the native entity containing the source property * @throws Throwable * propagated */ private static void unmarshalProperty(PropertyMetadata propertyMetadata, Object target, BaseEntity<?> nativeEntity) throws Throwable { // The datastore may not have every property that the entity class has // defined. For example, if we are running a projection query or if the // entity class added a new field without updating existing data...So // make sure there is a property or else, we get an exception from the // datastore. if (nativeEntity.contains(propertyMetadata.getMappedName())) { Value<?> datastoreValue = nativeEntity.getValue(propertyMetadata.getMappedName()); Object entityValue = propertyMetadata.getMapper().toModel(datastoreValue); MethodHandle writeMethod = propertyMetadata.getWriteMethod(); writeMethod.invoke(target, entityValue); } }
/** * Returns true if property is an instance of NullValue. * * @throws DatastoreException if no such property */ public boolean isNull(String name) { return getValue(name) instanceof NullValue; }
@Test public void testNames() throws Exception { Set<String> names = ImmutableSet.<String>builder() .add("string", "stringValue", "boolean", "double", "long", "list1", "list2", "list3") .add("entity", "partialEntity", "null", "timestamp", "blob", "key", "blobList") .add( "booleanList", "timestampList", "doubleList", "keyList", "entityList", "stringList") .add("longList", "latLng", "latLngList") .build(); BaseEntity<Key> entity = builder.build(); assertEquals(names, entity.getNames()); }
@SuppressWarnings("unchecked") public <T> T getPropertyValue(String fieldName, EmbeddedType embeddedType, TypeInformation targetTypeInformation) { if (!this.entity.contains(fieldName)) { return null; } try { return this.conversion.convertOnRead(this.entity.getValue(fieldName).get(), embeddedType, targetTypeInformation); } catch (ConversionException | DatastoreDataException ex) { throw new DatastoreDataException("Unable to read property " + fieldName, ex); } } }
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; }
private Object findReferenced(BaseEntity entity, DatastorePersistentProperty referencePersistentProperty) { String fieldName = referencePersistentProperty.getFieldName(); try { Object referenced; if (!entity.contains(fieldName)) { referenced = null; } else if (referencePersistentProperty.isCollectionLike()) { Class referencedType = referencePersistentProperty.getComponentType(); List<Value<Key>> keyValues = entity.getList(fieldName); referenced = this.datastoreEntityConverter.getConversions() .convertOnRead( findAllById( keyValues.stream().map(Value::get).collect(Collectors.toList()), referencedType), referencePersistentProperty.getType(), referencedType); } else { referenced = findById(entity.getKey(fieldName), referencePersistentProperty.getType()); } return referenced; } catch (ClassCastException ex) { throw new DatastoreDataException( "Error loading reference property " + fieldName + "." + "Reference properties must be stored as Keys or lists of Keys" + " in Cloud Datastore for singular or multiple references, respectively."); } }
FullEntity<?> nativeEmbeddedEntity = null; String propertyName = embeddedMetadata.getMappedName(); if (nativeEntity.contains(propertyName)) { Value<?> nativeValue = nativeEntity.getValue(propertyName); if (nativeValue instanceof NullValue) { embeddedMetadata.getWriteMethod().invoke(target, embeddedObject);
@Test(expected = DatastoreException.class) public void testGetValueNotFound() throws Exception { BaseEntity<Key> entity = builder.clear().build(); entity.getValue("blob"); }
@Override public <T, R> Map<T, R> readAsMap(Class<T> keyType, TypeInformation<R> componentType, BaseEntity entity) { if (entity == null) { return null; } Map<T, R> result = new HashMap<>(); EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider( entity, this.conversions); Set<String> fieldNames = entity.getNames(); for (String field : fieldNames) { result.put(this.conversions.convertOnRead(field, null, keyType), propertyValueProvider.getPropertyValue(field, EmbeddedType.of(componentType), componentType)); } return result; }
@Test public void testKey() throws Exception { builder.setKey(KEY); BaseEntity<Key> entity = builder.build(); assertEquals(KEY, entity.getKey()); } }