use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.
the class ResourceRepositoryAdapter method findAll.
public JsonApiResponse findAll(Iterable ids, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@SuppressWarnings("rawtypes")
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
QueryAdapter queryAdapter = request.getQueryAdapter();
Iterable<?> ids = request.getIds();
Object resources;
if (isAnnotated) {
resources = ((AnnotatedResourceRepositoryAdapter) resourceRepository).findAll(ids, queryAdapter);
} else if (resourceRepository instanceof ResourceRepositoryV2) {
resources = ((ResourceRepositoryV2) resourceRepository).findAll(ids, request.getQuerySpec(resourceInformation));
} else {
resources = ((ResourceRepository) resourceRepository).findAll(ids, request.getQueryParams());
}
return getResponse(resourceRepository, resources, request);
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindIds(moduleRegistry, resourceInformation, queryAdapter, ids);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.
the class IncludeLookupSetter method setIncludedElements.
public void setIncludedElements(Document document, Object entity, QueryAdapter queryAdapter, DocumentMappingConfig mappingConfig) {
QueryAdapter inclusionQueryAdapter = queryAdapter;
if (!allowPagination && !(queryAdapter instanceof QueryParamsAdapter) && queryAdapter != null) {
// offset/limit cannot properly work for nested inclusions if becomes cyclic
inclusionQueryAdapter = queryAdapter.duplicate();
if (queryAdapter.getResourceInformation().getPagingBehavior() != null) {
inclusionQueryAdapter.setPagingSpec(queryAdapter.getResourceInformation().getPagingBehavior().createEmptyPagingSpec());
}
}
List<Object> entityList = DocumentMapperUtil.toList(entity);
List<Resource> dataList = DocumentMapperUtil.toList(document.getData().get());
Map<ResourceIdentifier, Resource> dataMap = new HashMap<>();
Map<ResourceIdentifier, Object> entityMap = new HashMap<>();
for (int i = 0; i < dataList.size(); i++) {
Resource dataElement = dataList.get(i);
ResourceIdentifier id = dataElement.toIdentifier();
entityMap.put(id, entityList.get(i));
dataMap.put(id, dataElement);
}
Map<ResourceIdentifier, Resource> resourceMap = new HashMap<>();
resourceMap.putAll(dataMap);
Set<ResourceIdentifier> inclusions = new HashSet<>();
PopulatedCache populatedCache = new PopulatedCache();
RepositoryMethodParameterProvider parameterProvider = mappingConfig.getParameterProvider();
Set<String> fieldsWithEnforcedIdSerialization = mappingConfig.getFieldsWithEnforcedIdSerialization();
ResourceMappingConfig resourceMappingConfig = mappingConfig.getResourceMapping();
ArrayList<ResourceField> stack = new ArrayList<>();
populate(dataList, inclusions, resourceMap, entityMap, stack, inclusionQueryAdapter, parameterProvider, fieldsWithEnforcedIdSerialization, populatedCache, resourceMappingConfig);
// no need to include resources included in the data section
inclusions.removeAll(dataMap.keySet());
// setup included section
ArrayList<Resource> included = new ArrayList<>();
for (ResourceIdentifier inclusionId : inclusions) {
Resource includedResource = resourceMap.get(inclusionId);
PreconditionUtil.assertNotNull("resource not found", includedResource);
included.add(includedResource);
}
Collections.sort(included);
LOGGER.debug("Extracted included resources {}", included.toString());
document.setIncluded(included);
}
use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.
the class HttpRequestProcessorImpl method dispatchRequest.
/**
* Dispatch the request from a client
*
* @param path built represents the URI sent in the request
* @param method type of the request e.g. POST, GET, PATCH
* @param parameterProvider repository method legacy provider
* @param requestBody deserialized body of the client request
* @return the response form the Crnk
*/
@Override
public Response dispatchRequest(String path, String method, Map<String, Set<String>> parameters, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
JsonPath jsonPath = new PathBuilder(moduleRegistry.getResourceRegistry()).build(path);
try {
BaseController controller = controllerRegistry.getController(jsonPath, method);
ResourceInformation resourceInformation = getRequestedResource(jsonPath);
QueryAdapter queryAdapter = queryAdapterBuilder.build(resourceInformation, parameters);
DefaultFilterRequestContext context = new DefaultFilterRequestContext(jsonPath, queryAdapter, parameterProvider, requestBody, method);
DefaultFilterChain chain = new DefaultFilterChain(controller);
return chain.doFilter(context);
} catch (Exception e) {
Optional<JsonApiExceptionMapper> exceptionMapper = exceptionMapperRegistry.findMapperFor(e.getClass());
if (exceptionMapper.isPresent()) {
// noinspection unchecked
logger.debug("dispatching exception to mapper", e);
return exceptionMapper.get().toErrorResponse(e).toResponse();
} else {
logger.error("failed to process request", e);
throw e;
}
}
}
use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.
the class TotalBasedPagedLinksInformationTest method setup.
@Before
public void setup() {
TotalResourceCountTestRepository.clear();
super.setup();
RegistryEntry registryEntry = resourceRegistry.findEntry(Task.class);
TotalResourceCountTestRepository repo = (TotalResourceCountTestRepository) registryEntry.getResourceRepository(null).getResourceRepository();
adapter = registryEntry.getResourceRepository(null);
QueryAdapter queryAdapter = new QuerySpecAdapter(querySpec(), resourceRegistry);
for (long i = 0; i < 5; i++) {
Task task = new Task();
task.setId(i);
task.setName("myTask");
adapter.create(task, queryAdapter);
}
}
use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.
the class QuerySpecRepositoryTest method testCrudWithQuerySpec.
@Test
public void testCrudWithQuerySpec() {
QuerySpec querySpec = new QuerySpec(Task.class);
QueryAdapter adapter = new QuerySpecAdapter(querySpec, resourceRegistry);
checkCrud(adapter);
}
Aggregations