use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class SearchDataTransaction method search.
/**
* Perform the full-text search.
* @param entityType The class to search
* @param filterExpression The filter expression to apply
* @param sorting Optional sorting
* @param pagination Optional pagination
* @return A list of records of type entityClass.
*/
private <T> List<T> search(Type<?> entityType, FilterExpression filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination) {
Query query;
Class<?> entityClass = null;
if (entityType != null) {
Preconditions.checkState(entityType instanceof ClassType);
entityClass = ((ClassType) entityType).getCls();
}
try {
query = filterExpression.accept(new FilterExpressionToLuceneQuery(em, entityClass));
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
}
FullTextQuery fullTextQuery = em.createFullTextQuery(query, entityClass);
if (mustSort(sorting)) {
fullTextQuery = fullTextQuery.setSort(buildSort(sorting.get(), entityType));
}
if (pagination.isPresent()) {
fullTextQuery = fullTextQuery.setMaxResults(pagination.get().getLimit());
fullTextQuery = fullTextQuery.setFirstResult(pagination.get().getOffset());
}
List<T[]> results = fullTextQuery.setProjection(ProjectionConstants.THIS).getResultList();
if (pagination.filter(Pagination::returnPageTotals).isPresent()) {
pagination.get().setPageTotals((long) fullTextQuery.getResultSize());
}
if (results.isEmpty()) {
return Collections.emptyList();
}
return results.stream().map(result -> result[0]).collect(Collectors.toList());
}
use of com.yahoo.elide.core.exceptions.BadRequestException 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.exceptions.BadRequestException in project elide by yahoo.
the class PersistentResourceFetcher method updateObject.
private PersistentResource updateObject(Entity entity, Environment context) {
Set<Entity.Attribute> attributes = entity.getAttributes();
Optional<String> id = entity.getId();
RequestScope requestScope = entity.getRequestScope();
PersistentResource<?> updatedResource;
if (!id.isPresent()) {
throw new BadRequestException("UPDATE data objects must include ids");
}
Set<PersistentResource> loadedResource = fetchObject(requestScope, entity.getProjection(), Optional.of(Collections.singletonList(id.get()))).getPersistentResources();
updatedResource = loadedResource.iterator().next();
return updateAttributes(updatedResource, entity, attributes);
}
use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class GraphQLEntityProjectionMaker method addSorting.
/**
* Creates a {@link Sorting} object from sorting GraphQL argument value and attaches it to the entity sorted
* according to the newly created {@link Sorting} object.
*
* @param argument An argument that contains the value of sorting spec
* @param projectionBuilder projection that is being built
*/
private void addSorting(Argument argument, EntityProjectionBuilder projectionBuilder) {
String sortRule = (String) variableResolver.resolveValue(argument.getValue());
try {
Sorting sorting = SortingImpl.parseSortRule(sortRule, projectionBuilder.getType(), projectionBuilder.getAttributes(), entityDictionary);
projectionBuilder.sorting(sorting);
} catch (InvalidValueException e) {
throw new BadRequestException("Invalid sorting clause " + sortRule + " for type " + entityDictionary.getJsonAliasFor(projectionBuilder.getType()));
}
}
use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class VariableResolver method addVariable.
/**
* Resolve {@link VariableDefinition} and store result in the variable map.
* We don't need to worry about resolving graphql {@link graphql.language.TypeName} here because Elide-core
* knows the correct type of each field/argument.
*
* @param definition definition to resolve
*/
private void addVariable(VariableDefinition definition) {
Type variableType = definition.getType();
String variableName = definition.getName();
Value defaultValue = definition.getDefaultValue();
if (defaultValue == null) {
if (variableType instanceof NonNullType && scopeVariables.get(variableName) == null) {
// value of non-null variable must be resolvable
throw new BadRequestException("Undefined non-null variable " + variableName);
}
// this would put 'null' for this variable if it is not stored in the map
scopeVariables.put(variableName, scopeVariables.get(variableName));
} else {
if (!scopeVariables.containsKey(variableName)) {
// create a new variable with default value
scopeVariables.put(variableName, resolveValue(defaultValue));
}
}
}
Aggregations