BaseEntity(BaseEntity<K> from) { this.key = from.getKey(); this.properties = from.properties; }
@Test public void testKey() throws Exception { builder.setKey(KEY); BaseEntity<Key> entity = builder.build(); assertEquals(KEY, entity.getKey()); } }
/** Constructor to use when you're saving an entity with a known key */ public LoadException(final BaseEntity<?> entity, final String message, final Throwable cause) { super("Error loading " + entity.getKey() + ": " + message, cause); this.entity = entity; }
/** * Converts a datastore entity into a typed pojo object * @return an assembled pojo, or the Entity itself if the kind is not registered, or null if the input value was null */ @SuppressWarnings("unchecked") public <T> T load(final BaseEntity<com.google.cloud.datastore.Key> ent, final LoadContext ctx) { if (ent == null) return null; final EntityMetadata<T> meta = ofy.factory().getMetadata(ent.getKey().getKind()); if (meta == null) return (T)ent; else return meta.load(ent, ctx); } }
/** * Converts an entity to an object of the appropriate type for this metadata structure. * Does not check that the entity is appropriate; that should be done when choosing * which EntityMetadata to call. */ public P load(final BaseEntity<?> ent, final LoadContext ctx) { try { // The context needs to know the root entity for any given point ctx.setCurrentRoot(Key.create((com.google.cloud.datastore.Key)ent.getKey())); final EntityValue entityValue = makeLoadEntityValue(ent); return translator.load(entityValue, ctx, Path.root()); } catch (LoadException ex) { throw ex; } catch (Exception ex) { throw new LoadException(ent, ex.getMessage(), ex); } }
/** * 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()); } }
BaseEntity(BaseEntity<K> from) { this.key = from.getKey(); this.properties = from.properties; }
/** * Unamrshals the entity's key and parent key. * * @throws Throwable * propagated * */ private void unmarshalKeyAndParentKey() throws Throwable { KeyMetadata keyMetadata = entityMetadata.getKeyMetadata(); if (keyMetadata != null) { MethodHandle writeMethod = keyMetadata.getWriteMethod(); Key entityKey = (Key) nativeEntity.getKey(); writeMethod.invoke(entity, new DefaultDatastoreKey(entityKey)); } ParentKeyMetadata parentKeyMetadata = entityMetadata.getParentKeyMetadata(); if (parentKeyMetadata != null) { MethodHandle writeMethod = parentKeyMetadata.getWriteMethod(); Key parentKey = nativeEntity.getKey().getParent(); if (parentKey != null) { writeMethod.invoke(entity, new DefaultDatastoreKey(parentKey)); } } }
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."); } }
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."); } }
/** * Unamrshals the identifier. * * @throws Throwable * propagated */ private void unmarshalIdentifier() throws Throwable { IdentifierMetadata identifierMetadata = entityMetadata.getIdentifierMetadata(); Object id = ((Key) nativeEntity.getKey()).getNameOrId(); // If the ID is not a simple type... IdClassMetadata idClassMetadata = identifierMetadata.getIdClassMetadata(); if (idClassMetadata != null) { Object wrappedId = idClassMetadata.getConstructor().invoke(id); id = wrappedId; } // Now set the ID (either simple or complex) on the Entity MethodHandle writeMethod = identifierMetadata.getWriteMethod(); writeMethod.invoke(entity, id); }
private <T> void resolveDescendantProperties(DatastorePersistentEntity datastorePersistentEntity, BaseEntity entity, T convertedObject) { datastorePersistentEntity .doWithDescendantProperties((descendantPersistentProperty) -> { Class descendantType = descendantPersistentProperty .getComponentType(); EntityQuery descendantQuery = Query.newEntityQueryBuilder() .setKind(this.datastoreMappingContext .getPersistentEntity(descendantType).kindName()) .setFilter(PropertyFilter.hasAncestor((Key) entity.getKey())) .build(); datastorePersistentEntity.getPropertyAccessor(convertedObject) .setProperty(descendantPersistentProperty, // Converting the collection type. this.datastoreEntityConverter.getConversions() .convertOnRead( convertEntitiesForRead( getDatastoreReadWriter() .run(descendantQuery), descendantType), descendantPersistentProperty .getType(), descendantType)); }); }
private <T> void resolveDescendantProperties(DatastorePersistentEntity datastorePersistentEntity, BaseEntity entity, T convertedObject) { datastorePersistentEntity .doWithDescendantProperties((descendantPersistentProperty) -> { Class descendantType = descendantPersistentProperty .getComponentType(); EntityQuery descendantQuery = Query.newEntityQueryBuilder() .setKind(this.datastoreMappingContext .getPersistentEntity(descendantType).kindName()) .setFilter(PropertyFilter.hasAncestor((Key) entity.getKey())) .build(); datastorePersistentEntity.getPropertyAccessor(convertedObject) .setProperty(descendantPersistentProperty, // Converting the collection type. this.datastoreEntityConverter.getConversions() .convertOnRead( convertEntitiesForRead( getDatastoreReadWriter() .run(descendantQuery), descendantType), descendantPersistentProperty .getType(), descendantType)); }); }