Search in sources :

Example 51 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class IncludeLookupSetterInheritanceTest method setup.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setup() {
    super.setup();
    // get repositories
    ResourceRepositoryAdapter taskRepository = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    RelationshipRepositoryAdapter relRepositoryTaskToProject = resourceRegistry.getEntry(Task.class).getRelationshipRepository("projects", null);
    ResourceRepositoryAdapter projectRepository = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
    // setup test data
    ResourceInformation taskInfo = resourceRegistry.getEntry(Task.class).getResourceInformation();
    ResourceField includedProjectsField = taskInfo.findRelationshipFieldByName("includedProjects");
    QueryAdapter emptyProjectQuery = new QuerySpecAdapter(new QuerySpec(Project.class), resourceRegistry);
    QueryAdapter emptyTaskQuery = new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry);
    Project project1 = new Project();
    project1.setId(3L);
    projectRepository.create(project1, emptyProjectQuery);
    FancyProject project2 = new FancyProject();
    project2.setId(ProjectRepository.FANCY_PROJECT_ID);
    projectRepository.create(project2, emptyProjectQuery);
    Task task = new Task();
    task.setId(1L);
    taskRepository.create(task, emptyTaskQuery);
    relRepositoryTaskToProject.addRelations(task, Collections.singletonList(project1.getId()), includedProjectsField, emptyProjectQuery);
    relRepositoryTaskToProject.addRelations(task, Collections.singletonList(project2.getId()), includedProjectsField, emptyProjectQuery);
}
Also used : Project(io.crnk.core.mock.models.Project) FancyProject(io.crnk.core.mock.models.FancyProject) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) FancyProject(io.crnk.core.mock.models.FancyProject) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Before(org.junit.Before)

Example 52 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class TestResourceInformationProvider method build.

@Override
public ResourceInformation build(Class<?> resourceClass) {
    ResourceField idField = new ResourceFieldImpl("testId", "id", ResourceFieldType.ID, Integer.class, null, null);
    List<ResourceField> fields = Arrays.asList(idField);
    TypeParser typeParser = context.getTypeParser();
    ResourceInformation info = new ResourceInformation(typeParser, resourceClass, resourceClass.getSimpleName(), null, fields, new OffsetLimitPagingBehavior());
    return info;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) TypeParser(io.crnk.core.engine.parser.TypeParser)

Example 53 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class DefaultRegistryEntryBuilder method contributeFields.

private void contributeFields(ResourceInformation resourceInformation) {
    // TODO make service discovery the primary target to resolve all objects => wrapped it with module
    List<ResourceFieldContributor> contributors = new ArrayList<>();
    contributors.addAll(moduleRegistry.getServiceDiscovery().getInstancesByType(ResourceFieldContributor.class));
    for (Object repo : moduleRegistry.getRepositories()) {
        if (repo instanceof ResourceFieldContributor && !contributors.contains(repo)) {
            contributors.add((ResourceFieldContributor) repo);
        }
    }
    for (ResourceFieldContributor contributor : contributors) {
        List<ResourceField> contributedFields = contributor.getResourceFields(new ResourceFieldContributorContext() {

            @Override
            public ResourceInformation getResourceInformation() {
                return resourceInformation;
            }

            @Override
            public InformationBuilder getInformationBuilder() {
                return new DefaultInformationBuilder(moduleRegistry.getTypeParser());
            }
        });
        List<ResourceField> fields = new ArrayList<>();
        fields.addAll(resourceInformation.getFields());
        fields.addAll(contributedFields);
        resourceInformation.setFields(fields);
    }
}
Also used : InformationBuilder(io.crnk.core.engine.information.InformationBuilder) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) ResourceFieldContributor(io.crnk.core.engine.information.contributor.ResourceFieldContributor) ArrayList(java.util.ArrayList) ResourceFieldContributorContext(io.crnk.core.engine.information.contributor.ResourceFieldContributorContext)

Example 54 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class DefaultRegistryEntryBuilder method build.

@Override
public RegistryEntry build() {
    ResourceInformation resourceInformation = buildResource();
    ResourceEntry resourceEntry = buildResourceRepository(resourceInformation);
    Map<ResourceField, ResponseRelationshipEntry> relationshipEntries = buildRelationships(resourceInformation);
    RegistryEntry entry = new RegistryEntry(resourceEntry, relationshipEntries);
    entry.initialize(moduleRegistry);
    return entry;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResponseRelationshipEntry(io.crnk.core.engine.registry.ResponseRelationshipEntry) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) ResourceEntry(io.crnk.core.engine.registry.ResourceEntry) AnnotatedResourceEntry(io.crnk.legacy.registry.AnnotatedResourceEntry) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 55 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method removeRelations.

@SuppressWarnings("rawtypes")
public JsonApiResponse removeRelations(T source, Iterable<J> targetIds, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object source = request.getEntity();
            Iterable<?> targetIds = request.getIds();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            if (isAnnotated) {
                ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).removeRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).removeRelations(source, targetIds, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).removeRelations(source, targetIds, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.DELETE, source, queryAdapter, targetIds, field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Aggregations

ResourceField (io.crnk.core.engine.information.resource.ResourceField)109 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)75 Test (org.junit.Test)41 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)36 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)13 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 ArrayList (java.util.ArrayList)9 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)8 Response (io.crnk.core.engine.dispatcher.Response)8 Resource (io.crnk.core.engine.document.Resource)8 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 HashSet (java.util.HashSet)8 Document (io.crnk.core.engine.document.Document)7 Task (io.crnk.core.mock.models.Task)7 Serializable (java.io.Serializable)7 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)6