Search in sources :

Example 26 with ResourceInformation

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

the class ResourceMetaFilter method adjustResourceForRequest.

private MetaElement adjustResourceForRequest(MetaResource metaResource) {
    Module.ModuleContext moduleContext = context.getModuleContext();
    RegistryEntry entry = moduleContext.getResourceRegistry().getEntry(metaResource.getResourceType());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceFilterDirectory filterBehaviorProvider = moduleContext.getResourceFilterDirectory();
    boolean readable = metaResource.isReadable() && filterBehaviorProvider.get(resourceInformation, HttpMethod.GET) == FilterBehavior.NONE;
    boolean insertable = metaResource.isInsertable() && filterBehaviorProvider.get(resourceInformation, HttpMethod.POST) == FilterBehavior.NONE;
    boolean updatable = metaResource.isUpdatable() && filterBehaviorProvider.get(resourceInformation, HttpMethod.PATCH) == FilterBehavior.NONE;
    boolean deletable = metaResource.isDeletable() && filterBehaviorProvider.get(resourceInformation, HttpMethod.DELETE) == FilterBehavior.NONE;
    // hide element if no permission
    if (!readable && !insertable && !updatable && !deletable) {
        return null;
    }
    // update element if necessary
    if (metaResource.isReadable() != readable || metaResource.isUpdatable() != updatable || metaResource.isInsertable() != insertable || metaResource.isDeletable() != deletable) {
        MetaResource clone = (MetaResource) metaResource.duplicate();
        clone.setReadable(readable);
        clone.setInsertable(insertable);
        clone.setUpdatable(updatable);
        clone.setDeletable(deletable);
        return clone;
    }
    return metaResource;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) Module(io.crnk.core.module.Module) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceFilterDirectory(io.crnk.core.engine.filter.ResourceFilterDirectory)

Example 27 with ResourceInformation

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

the class ResourceMetaFilter method adjustFieldForRequest.

private MetaElement adjustFieldForRequest(MetaResourceField field) {
    MetaResource metaResource = (MetaResource) field.getParent();
    Module.ModuleContext moduleContext = context.getModuleContext();
    RegistryEntry entry = moduleContext.getResourceRegistry().getEntry(metaResource.getResourceType());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField fieldInformation = resourceInformation.findFieldByUnderlyingName(field.getName());
    ResourceFilterDirectory filterBehaviorProvider = moduleContext.getResourceFilterDirectory();
    boolean readable = metaResource.isReadable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.GET) == FilterBehavior.NONE;
    boolean insertable = metaResource.isInsertable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.POST) == FilterBehavior.NONE;
    boolean updatable = metaResource.isUpdatable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.PATCH) == FilterBehavior.NONE;
    // hide element if no permission
    if (!readable && !insertable && !updatable) {
        return null;
    }
    if (field.isUpdatable() != updatable || field.isInsertable() != insertable) {
        MetaResourceField clone = (MetaResourceField) field.duplicate();
        clone.setInsertable(insertable);
        clone.setUpdatable(updatable);
        return clone;
    }
    return field;
}
Also used : MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) ResourceField(io.crnk.core.engine.information.resource.ResourceField) MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) Module(io.crnk.core.module.Module) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceFilterDirectory(io.crnk.core.engine.filter.ResourceFilterDirectory)

Example 28 with ResourceInformation

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

the class JpaPartialEntityExposureTest method testInformationBuilder.

@Test
public void testInformationBuilder() {
    EntityManager em = null;
    JpaResourceInformationProvider builder = new JpaResourceInformationProvider(new NullPropertiesProvider());
    ResourceInformation info = builder.build(TestEntity.class);
    List<ResourceField> relationshipFields = info.getRelationshipFields();
    Assert.assertEquals(0, relationshipFields.size());
}
Also used : JpaResourceInformationProvider(io.crnk.jpa.internal.JpaResourceInformationProvider) ResourceField(io.crnk.core.engine.information.resource.ResourceField) EntityManager(javax.persistence.EntityManager) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider)

Example 29 with ResourceInformation

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

the class DefaultRegistryEntryBuilder method setupImplicitRelationshipRepository.

private ResponseRelationshipEntry setupImplicitRelationshipRepository(ResourceField relationshipField) {
    RelationshipRepositoryBehavior behavior = relationshipField.getRelationshipRepositoryBehavior();
    if (behavior == RelationshipRepositoryBehavior.DEFAULT) {
        if (relationshipField.hasIdField() || relationshipField.getLookupIncludeAutomatically() == LookupIncludeBehavior.NONE) {
            behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
        } else {
            behavior = RelationshipRepositoryBehavior.CUSTOM;
        }
    }
    if (behavior == RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER) {
        behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
    }
    if (behavior == RelationshipRepositoryBehavior.IMPLICIT_GET_OPPOSITE_MODIFY_OWNER) {
        behavior = RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER;
    }
    if (behavior != RelationshipRepositoryBehavior.CUSTOM) {
        ResourceInformation sourceInformation = relationshipField.getParentResourceInformation();
        ResourceFieldAccess fieldAccess = relationshipField.getAccess();
        RepositoryMethodAccess access = new RepositoryMethodAccess(fieldAccess.isPostable(), fieldAccess.isPatchable(), fieldAccess.isReadable(), fieldAccess.isPatchable());
        RelationshipMatcher matcher = new RelationshipMatcher().rule().field(relationshipField).add();
        RelationshipRepositoryInformationImpl implicitRepoInformation = new RelationshipRepositoryInformationImpl(matcher, access);
        ForwardingRelationshipRepository repository;
        if (behavior == RelationshipRepositoryBehavior.FORWARD_OWNER) {
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OWNER, ForwardingDirection.OWNER);
        } else if (behavior == RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER) {
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OWNER);
        } else {
            PreconditionUtil.assertEquals("unknown behavior", RelationshipRepositoryBehavior.FORWARD_OPPOSITE, behavior);
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
        }
        repository.setResourceRegistry(moduleRegistry.getResourceRegistry());
        return setupRelationship(relationshipField, implicitRepoInformation, repository);
    } else {
        return null;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RelationshipRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl) RepositoryMethodAccess(io.crnk.core.engine.information.repository.RepositoryMethodAccess) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) RelationshipRepositoryBehavior(io.crnk.core.resource.annotations.RelationshipRepositoryBehavior) RelationshipMatcher(io.crnk.core.repository.RelationshipMatcher) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess)

Example 30 with ResourceInformation

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

the class DefaultRegistryEntryBuilder method fromImplementation.

@Override
public void fromImplementation(Object repository) {
    RepositoryInformationProvider repositoryInformationBuilder = moduleRegistry.getRepositoryInformationBuilder();
    RepositoryInformationProviderContext builderContext = new DefaultRepositoryInformationProviderContext(moduleRegistry);
    ResourceRepositoryInformation repositoryInformation = (ResourceRepositoryInformation) repositoryInformationBuilder.build(repository, builderContext);
    ResourceInformation resourceInformation = repositoryInformation.getResourceInformation().get();
    resource().from(resourceInformation);
    resourceRepository().information().from(repositoryInformation);
    resourceRepository().instance(repository);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) DefaultRepositoryInformationProviderContext(io.crnk.core.module.internal.DefaultRepositoryInformationProviderContext) DefaultRepositoryInformationProviderContext(io.crnk.core.module.internal.DefaultRepositoryInformationProviderContext) RepositoryInformationProviderContext(io.crnk.core.engine.information.repository.RepositoryInformationProviderContext) RepositoryInformationProvider(io.crnk.core.engine.information.repository.RepositoryInformationProvider)

Aggregations

ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)167 Test (org.junit.Test)79 ResourceField (io.crnk.core.engine.information.resource.ResourceField)76 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)60 QuerySpec (io.crnk.core.queryspec.QuerySpec)16 Resource (io.crnk.core.engine.document.Resource)15 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)15 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)9 Task (io.crnk.core.mock.models.Task)9 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)9 HashSet (java.util.HashSet)9 Collection (java.util.Collection)8 Relationship (io.crnk.core.engine.document.Relationship)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)7 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)7 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)7 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)7 Set (java.util.Set)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6