Search in sources :

Example 1 with RelationshipRepository

use of io.crnk.legacy.repository.RelationshipRepository in project crnk-framework by crnk-project.

the class CrnkClient method allocateRepositoryRelation.

private RelationshipRepositoryAdapter allocateRepositoryRelation(Class sourceClass, Class targetClass) {
    // allocate relations as well
    ClientResourceRegistry clientResourceRegistry = (ClientResourceRegistry) resourceRegistry;
    if (!clientResourceRegistry.isInitialized(sourceClass)) {
        allocateRepository(sourceClass);
    }
    if (!clientResourceRegistry.isInitialized(targetClass)) {
        allocateRepository(targetClass);
    }
    RegistryEntry sourceEntry = resourceRegistry.getEntry(sourceClass);
    final RegistryEntry targetEntry = resourceRegistry.getEntry(targetClass);
    String targetResourceType = targetEntry.getResourceInformation().getResourceType();
    Map relationshipEntries = sourceEntry.getRelationshipEntries();
    DirectResponseRelationshipEntry relationshipEntry = (DirectResponseRelationshipEntry) relationshipEntries.get(targetResourceType);
    if (!relationshipEntries.containsKey(targetResourceType)) {
        final RelationshipRepositoryStubImpl relationshipRepositoryStub = new RelationshipRepositoryStubImpl(this, sourceClass, targetClass, sourceEntry.getResourceInformation(), urlBuilder);
        RepositoryInstanceBuilder<RelationshipRepository> relationshipRepositoryInstanceBuilder = new RepositoryInstanceBuilder<RelationshipRepository>(null, null) {

            @Override
            public RelationshipRepository buildRepository() {
                return relationshipRepositoryStub;
            }
        };
        relationshipEntry = new DirectResponseRelationshipEntry(relationshipRepositoryInstanceBuilder) {

            @Override
            public String getTargetResourceType() {
                return targetEntry.getResourceInformation().getResourceType();
            }
        };
        relationshipEntries.put(targetResourceType, relationshipEntry);
    }
    Object repoInstance = relationshipEntry.getRepositoryInstanceBuilder();
    return new RelationshipRepositoryAdapter(sourceEntry.getResourceInformation(), moduleRegistry, repoInstance);
}
Also used : DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) RelationshipRepository(io.crnk.legacy.repository.RelationshipRepository) RelationshipRepositoryStubImpl(io.crnk.client.internal.RelationshipRepositoryStubImpl) RepositoryInstanceBuilder(io.crnk.legacy.registry.RepositoryInstanceBuilder) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Example 2 with RelationshipRepository

use of io.crnk.legacy.repository.RelationshipRepository in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method findOneTarget.

@SuppressWarnings("rawtypes")
public JsonApiResponse findOneTarget(I sourceId, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Serializable sourceId = request.getId();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            Object resource;
            if (isAnnotated) {
                resource = ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).findOneTarget(sourceId, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                RelationshipRepositoryV2 querySpecRepository = (RelationshipRepositoryV2) relationshipRepository;
                ResourceInformation targetResourceInformation = moduleRegistry.getResourceRegistry().getEntry(field.getOppositeResourceType()).getResourceInformation();
                resource = querySpecRepository.findOneTarget(sourceId, field.getUnderlyingName(), request.getQuerySpec(targetResourceInformation));
            } else {
                resource = ((RelationshipRepository) relationshipRepository).findOneTarget(sourceId, field.getUnderlyingName(), request.getQueryParams());
            }
            return getResponse(relationshipRepository, resource, request);
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindTarget(moduleRegistry, queryAdapter, Arrays.asList(sourceId), field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) RelationshipRepository(io.crnk.legacy.repository.RelationshipRepository) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Example 3 with RelationshipRepository

use of io.crnk.legacy.repository.RelationshipRepository in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method findManyTargets.

@SuppressWarnings("rawtypes")
public JsonApiResponse findManyTargets(I sourceId, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Serializable sourceId = request.getId();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            Object resources;
            if (isAnnotated) {
                resources = ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).findManyTargets(sourceId, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                RelationshipRepositoryV2 querySpecRepository = (RelationshipRepositoryV2) relationshipRepository;
                ResourceInformation targetResourceInformation = moduleRegistry.getResourceRegistry().getEntry(field.getOppositeResourceType()).getResourceInformation();
                resources = querySpecRepository.findManyTargets(sourceId, field.getUnderlyingName(), request.getQuerySpec(targetResourceInformation));
            } else {
                resources = ((RelationshipRepository) relationshipRepository).findManyTargets(sourceId, field.getUnderlyingName(), request.getQueryParams());
            }
            return getResponse(relationshipRepository, resources, request);
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindTarget(moduleRegistry, queryAdapter, Arrays.asList(sourceId), field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) RelationshipRepository(io.crnk.legacy.repository.RelationshipRepository) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Aggregations

RelationshipRepository (io.crnk.legacy.repository.RelationshipRepository)3 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)2 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)2 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)2 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)2 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)2 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)2 Serializable (java.io.Serializable)2 RelationshipRepositoryStubImpl (io.crnk.client.internal.RelationshipRepositoryStubImpl)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 DirectResponseRelationshipEntry (io.crnk.legacy.internal.DirectResponseRelationshipEntry)1 RepositoryInstanceBuilder (io.crnk.legacy.registry.RepositoryInstanceBuilder)1