use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class PersistentResourceFetcher method upsertOrUpdateObjects.
/**
* handle UPSERT or UPDATE operation.
* @param context Environment encapsulating graphQL's request environment
* @param updateFunc controls the behavior of how the update (or upsert) is performed.
* @return Connection object.
*/
private ConnectionContainer upsertOrUpdateObjects(Environment context, Executor<?> updateFunc, RelationshipOp operation) {
/* sanity check for id and data argument w UPSERT/UPDATE */
if (context.ids.isPresent()) {
throw new BadRequestException(operation + " must not include ids");
}
if (!context.data.isPresent()) {
throw new BadRequestException(operation + " must include data argument");
}
Type<?> entityClass;
EntityDictionary dictionary = context.requestScope.getDictionary();
if (context.isRoot()) {
entityClass = dictionary.getEntityClass(context.field.getName(), context.requestScope.getApiVersion());
} else {
assert context.parentResource != null;
entityClass = dictionary.getParameterizedType(context.parentResource.getResourceType(), context.field.getName());
}
/* form entities */
Optional<Entity> parentEntity;
if (!context.isRoot()) {
assert context.parentResource != null;
parentEntity = Optional.of(new Entity(Optional.empty(), null, context.parentResource.getResourceType(), context.requestScope));
} else {
parentEntity = Optional.empty();
}
LinkedHashSet<Entity> entitySet = new LinkedHashSet<>();
for (Map<String, Object> input : context.data.orElseThrow(IllegalStateException::new)) {
entitySet.add(new Entity(parentEntity, input, entityClass, context.requestScope));
}
/* apply function to upsert/update the object */
for (Entity entity : entitySet) {
graphWalker(entity, updateFunc, context);
}
/* fixup relationships */
for (Entity entity : entitySet) {
graphWalker(entity, this::updateRelationship, context);
PersistentResource<?> childResource = entity.toPersistentResource();
if (!context.isRoot()) {
/* add relation between parent and nested entity */
assert context.parentResource != null;
context.parentResource.addRelation(context.field.getName(), childResource);
}
}
String entityName = dictionary.getJsonAliasFor(entityClass);
Set<PersistentResource> resources = entitySet.stream().map(Entity::toPersistentResource).collect(Collectors.toCollection(LinkedHashSet::new));
return new ConnectionContainer(resources, Optional.empty(), entityName);
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class PersistentResourceFetcher method updateAttributes.
/**
* Updates an object.
* @param toUpdate Entities to update
* @param entity Resource entity
* @param attributes Set of entity attributes
* @return Persistence Resource object
*/
private PersistentResource<?> updateAttributes(PersistentResource<?> toUpdate, Entity entity, Set<Entity.Attribute> attributes) {
EntityDictionary dictionary = entity.getRequestScope().getDictionary();
Type<?> entityClass = entity.getEntityClass();
String idFieldName = dictionary.getIdFieldName(entityClass);
/* iterate through each attribute provided */
for (Entity.Attribute attribute : attributes) {
if (dictionary.isAttribute(entityClass, attribute.getName())) {
Type<?> attributeType = dictionary.getType(entityClass, attribute.getName());
Object attributeValue;
if (ClassType.MAP_TYPE.isAssignableFrom(attributeType)) {
attributeValue = MapEntryContainer.translateFromGraphQLMap(attribute);
} else {
attributeValue = attribute.getValue();
}
toUpdate.updateAttribute(attribute.getName(), attributeValue);
} else if (!Objects.equals(attribute.getName(), idFieldName)) {
throw new IllegalStateException("Unrecognized attribute passed to 'data': " + attribute.getName());
}
}
return toUpdate;
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class MultiplexManagerTest method checkLoading.
@Test
public void checkLoading() {
EntityDictionary entityDictionary = multiplexManager.getDictionary();
assertNotNull(entityDictionary.getJsonAliasFor(ClassType.of(FirstBean.class)));
assertNotNull(entityDictionary.getJsonAliasFor(ClassType.of(OtherBean.class)));
assertNotNull(entityDictionary.getJsonAliasFor(ClassType.of(ComplexAttribute.class)));
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class Entity method setAttributes.
/**
* Extract the attributes of the entity.
*/
private void setAttributes() {
if (this.data != null) {
this.attributes = new LinkedHashSet<>();
EntityDictionary dictionary = this.requestScope.getDictionary();
String idFieldName = dictionary.getIdFieldName(this.entityClass);
for (Map.Entry<String, Object> entry : this.data.entrySet()) {
if (dictionary.isAttribute(this.entityClass, entry.getKey())) {
this.attributes.add(new Attribute(entry.getKey(), entry.getValue()));
}
if (Objects.equals(entry.getKey(), idFieldName)) {
this.attributes.add(new Attribute(entry.getKey(), entry.getValue()));
}
}
}
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class Entity method getId.
/**
* Get the id of the entity.
* @return the optional id
*/
public Optional<String> getId() {
EntityDictionary dictionary = this.requestScope.getDictionary();
String idFieldName = dictionary.getIdFieldName(this.entityClass);
return this.attributes.stream().filter(entry -> idFieldName.equalsIgnoreCase(entry.name)).map(e -> (String) e.value).findFirst();
}
Aggregations