use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class CollectionTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument jsonApiDocument = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Set<PersistentResource> collection = getResourceCollection(requestScope).toList(LinkedHashSet::new).blockingGet();
// Set data
jsonApiDocument.setData(getData(collection, requestScope.getDictionary()));
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(jsonApiDocument, collection, queryParams);
Pagination pagination = parentProjection.getPagination();
if (parent.isPresent()) {
pagination = parentProjection.getRelationship(relationName.orElseThrow(IllegalStateException::new)).get().getProjection().getPagination();
}
// Add pagination meta data
if (!pagination.isDefaultInstance()) {
Map<String, Number> pageMetaData = new HashMap<>();
pageMetaData.put("number", (pagination.getOffset() / pagination.getLimit()) + 1);
pageMetaData.put("limit", pagination.getLimit());
// Get total records if it has been requested and add to the page meta data
if (pagination.returnPageTotals()) {
Long totalRecords = pagination.getPageTotals();
pageMetaData.put("totalPages", totalRecords / pagination.getLimit() + ((totalRecords % pagination.getLimit()) > 0 ? 1 : 0));
pageMetaData.put("totalRecords", totalRecords);
}
Map<String, Object> allMetaData = new HashMap<>();
allMetaData.put("page", pageMetaData);
Meta meta = new Meta(allMetaData);
jsonApiDocument.setMeta(meta);
}
JsonNode responseBody = requestScope.getMapper().toJsonObject(jsonApiDocument);
return () -> Pair.of(HttpStatus.SC_OK, responseBody);
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class FilteredIteratorTest method testFilteredResult.
@Test
public void testFilteredResult() throws Exception {
EntityDictionary dictionary = EntityDictionary.builder().build();
dictionary.bindEntity(Book.class);
Book book1 = new Book();
book1.setTitle("foo");
Book book2 = new Book();
book2.setTitle("bar");
Book book3 = new Book();
book3.setTitle("foobar");
List<Book> books = List.of(book1, book2, book3);
RSQLFilterDialect filterDialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
FilterExpression expression = filterDialect.parse(ClassType.of(Book.class), new HashSet<>(), "title==*bar", NO_VERSION);
RequestScope scope = new TestRequestScope(null, null, dictionary);
Iterator<Book> bookIterator = new FilteredIterator<>(expression, scope, books.iterator());
assertTrue(bookIterator.hasNext());
assertEquals("bar", bookIterator.next().getTitle());
assertTrue(bookIterator.hasNext());
assertEquals("foobar", bookIterator.next().getTitle());
assertFalse(bookIterator.hasNext());
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class PersistentResourceFetcher method fetchObject.
/**
* Fetches a root-level entity.
* @param requestScope Request scope
* @param projection constructed entityProjection for a class
* @param ids List of ids (can be NULL)
* @return {@link PersistentResource} object(s)
*/
public static ConnectionContainer fetchObject(RequestScope requestScope, EntityProjection projection, Optional<List<String>> ids) {
EntityDictionary dictionary = requestScope.getDictionary();
String typeName = dictionary.getJsonAliasFor(projection.getType());
/* fetching a collection */
Observable<PersistentResource> records = ids.map((idList) -> {
/* handle empty list of ids */
if (idList.isEmpty()) {
throw new BadRequestException("Empty list passed to ids");
}
return PersistentResource.loadRecords(projection, idList, requestScope);
}).orElseGet(() -> PersistentResource.loadRecords(projection, new ArrayList<>(), requestScope));
return new ConnectionContainer(records.toList(LinkedHashSet::new).blockingGet(), Optional.ofNullable(projection.getPagination()), typeName);
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class PersistentResourceFetcher method upsertObject.
/**
* updates or creates existing/new entities.
* @param entity Resource entity
* @param context The request context
* @return {@link PersistentResource} object
*/
private PersistentResource upsertObject(Entity entity, Environment context) {
Set<Entity.Attribute> attributes = entity.getAttributes();
Optional<String> id = entity.getId();
RequestScope requestScope = entity.getRequestScope();
PersistentResource upsertedResource;
EntityDictionary dictionary = requestScope.getDictionary();
PersistentResource parentResource = entity.getParentResource().map(Entity::toPersistentResource).orElse(null);
if (!id.isPresent()) {
// If the ID is generated, it is safe to assign a temporary UUID. Otherwise the client must provide one.
if (dictionary.isIdGenerated(entity.getEntityClass())) {
// Assign a temporary UUID.
entity.setId();
id = entity.getId();
}
upsertedResource = PersistentResource.createObject(parentResource, context.field.getName(), entity.getEntityClass(), requestScope, id);
} else {
try {
Set<PersistentResource> loadedResource = fetchObject(requestScope, entity.getProjection(), Optional.of(Collections.singletonList(id.get()))).getPersistentResources();
upsertedResource = loadedResource.iterator().next();
// The ID doesn't exist yet. Let's create the object.
} catch (InvalidObjectIdentifierException | InvalidValueException e) {
upsertedResource = PersistentResource.createObject(parentResource, context.field.getName(), entity.getEntityClass(), requestScope, id);
}
}
return updateAttributes(upsertedResource, entity, attributes);
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class NoopTransactionTest method setup.
@BeforeAll
public void setup() {
dictionary = EntityDictionary.builder().build();
dictionary.bindEntity(NoopBean.class);
requestScope = mock(RequestScope.class);
JsonApiMapper mapper = mock(JsonApiMapper.class);
when(requestScope.getDictionary()).thenReturn(dictionary);
when(requestScope.getObjectEntityCache()).thenReturn(new ObjectEntityCache());
when(requestScope.getMapper()).thenReturn(mapper);
when(mapper.getObjectMapper()).thenReturn(new ObjectMapper());
}
Aggregations