Search in sources :

Example 26 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class ResourceRepositoryAdapter method findAll.

public JsonApiResponse findAll(QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @SuppressWarnings("rawtypes")
        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            Object resources;
            if (isAnnotated) {
                resources = ((AnnotatedResourceRepositoryAdapter) resourceRepository).findAll(queryAdapter);
            } else if (resourceRepository instanceof ResourceRepositoryV2) {
                QuerySpec querySpec = request.getQuerySpec(resourceInformation);
                resources = ((ResourceRepositoryV2) resourceRepository).findAll(querySpec);
            } else {
                resources = ((ResourceRepository) resourceRepository).findAll(request.getQueryParams());
            }
            return getResponse(resourceRepository, resources, request);
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindAll(moduleRegistry, resourceInformation, queryAdapter);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRepository(io.crnk.legacy.repository.ResourceRepository)

Example 27 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class ResponseRepositoryAdapter method enrichLinksInformation.

private LinksInformation enrichLinksInformation(LinksInformation linksInformation, Iterable<?> resources, RepositoryRequestSpec requestSpec) {
    QueryAdapter queryAdapter = requestSpec.getQueryAdapter();
    LinksInformation enrichedLinksInformation = linksInformation;
    if (queryAdapter instanceof QuerySpecAdapter && resources instanceof ResourceList && requestSpec.getResponseResourceInformation().getPagingBehavior() != null && requestSpec.getResponseResourceInformation().getPagingBehavior().isRequired(queryAdapter.getPagingSpec())) {
        enrichedLinksInformation = enrichPageLinksInformation(enrichedLinksInformation, (ResourceList<?>) resources, queryAdapter, requestSpec);
    }
    return enrichedLinksInformation;
}
Also used : ResourceList(io.crnk.core.resource.list.ResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) DefaultPagedLinksInformation(io.crnk.core.resource.links.DefaultPagedLinksInformation) LinksInformation(io.crnk.core.resource.links.LinksInformation) PagedLinksInformation(io.crnk.core.resource.links.PagedLinksInformation) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter)

Example 28 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class HasNextBasedPagedLinksInformationTest method setup.

@Before
public void setup() {
    HasNextPageTestRepository.clear();
    super.setup();
    RegistryEntry registryEntry = resourceRegistry.getEntry(Task.class);
    HasNextPageTestRepository repo = (HasNextPageTestRepository) registryEntry.getResourceRepository(null).getResourceRepository();
    repo = Mockito.spy(repo);
    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);
    }
}
Also used : Task(io.crnk.core.mock.models.Task) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Before(org.junit.Before)

Example 29 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class QuerySpecRepositoryTest method findAllWithResourceListResult.

@Test
public void findAllWithResourceListResult() {
    QuerySpec querySpec = new QuerySpec(Schedule.class);
    QueryAdapter adapter = new QuerySpecAdapter(querySpec, resourceRegistry);
    JsonApiResponse response = scheduleAdapter.findAll(adapter);
    Assert.assertTrue(response.getEntity() instanceof ScheduleRepository.ScheduleList);
    Assert.assertTrue(response.getLinksInformation() instanceof ScheduleRepository.ScheduleListLinks);
    Assert.assertTrue(response.getMetaInformation() instanceof ScheduleRepository.ScheduleListMeta);
}
Also used : QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ScheduleRepository(io.crnk.core.mock.repository.ScheduleRepository) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 30 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class ClientDocumentMapper method newResourceMapper.

@Override
protected ResourceMapper newResourceMapper(final DocumentMapperUtil util, boolean client, ObjectMapper objectMapper) {
    return new ResourceMapper(util, client, objectMapper, null) {

        @Override
        protected void setRelationship(Resource resource, ResourceField field, Object entity, ResourceInformation resourceInformation, QueryAdapter queryAdapter, ResourceMappingConfig mappingConfig) {
            // we also include relationship data if it is not null and not a
            // unloaded proxy
            boolean includeRelation = true;
            Object relationshipId = null;
            if (field.hasIdField()) {
                Object relationshipValue = field.getIdAccessor().getValue(entity);
                ResourceInformation oppositeInformation = resourceRegistry.getEntry(field.getOppositeResourceType()).getResourceInformation();
                if (relationshipValue instanceof Collection) {
                    List ids = new ArrayList();
                    for (Object elem : (Collection<?>) relationshipValue) {
                        ids.add(oppositeInformation.toResourceIdentifier(elem));
                    }
                    relationshipId = ids;
                } else if (relationshipValue != null) {
                    relationshipId = oppositeInformation.toResourceIdentifier(relationshipValue);
                }
                includeRelation = relationshipId != null || field.getSerializeType() != SerializeType.LAZY;
            } else {
                Object relationshipValue = field.getAccessor().getValue(entity);
                if (relationshipValue instanceof ObjectProxy) {
                    includeRelation = ((ObjectProxy) relationshipValue).isLoaded();
                } else {
                    // TODO for fieldSets handling in the future the lazy
                    // handling must be different
                    includeRelation = relationshipValue != null || field.getSerializeType() != SerializeType.LAZY && !field.isCollection();
                }
                if (relationshipValue != null && includeRelation) {
                    if (relationshipValue instanceof Collection) {
                        relationshipId = util.toResourceIds((Collection<?>) relationshipValue);
                    } else {
                        relationshipId = util.toResourceId(relationshipValue);
                    }
                }
            }
            if (includeRelation) {
                Relationship relationship = new Relationship();
                relationship.setData(Nullable.of((Object) relationshipId));
                resource.getRelationships().put(field.getJsonName(), relationship);
            }
        }
    };
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceMappingConfig(io.crnk.core.engine.internal.document.mapper.ResourceMappingConfig) Relationship(io.crnk.core.engine.document.Relationship) ResourceMapper(io.crnk.core.engine.internal.document.mapper.ResourceMapper) Collection(java.util.Collection) ArrayList(java.util.ArrayList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy)

Aggregations

QueryAdapter (io.crnk.core.engine.query.QueryAdapter)35 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)15 QuerySpec (io.crnk.core.queryspec.QuerySpec)14 ResourceField (io.crnk.core.engine.information.resource.ResourceField)13 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)12 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)12 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)9 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)6 Resource (io.crnk.core.engine.document.Resource)5 Task (io.crnk.core.mock.models.Task)5 Serializable (java.io.Serializable)5 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)4 AnnotatedRelationshipRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter)4 Before (org.junit.Before)4 Test (org.junit.Test)4 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)3 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)3