Search in sources :

Example 36 with ResourceRegistry

use of io.crnk.core.engine.registry.ResourceRegistry in project crnk-framework by crnk-project.

the class RelationshipsResourcePatchTest method onValidRequestShouldAcceptIt.

@Test
public void onValidRequestShouldAcceptIt() {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("tasks/1/relationships/project");
    ResourceRegistry resourceRegistry = mock(ResourceRegistry.class);
    RelationshipsResourcePatch sut = new RelationshipsResourcePatch(resourceRegistry, typeParser, modificationFilters);
    // WHEN
    boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
    // THEN
    assertThat(result).isTrue();
}
Also used : RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 37 with ResourceRegistry

use of io.crnk.core.engine.registry.ResourceRegistry in project crnk-framework by crnk-project.

the class ResourcePostTest method toQueryAdapter.

private QueryAdapter toQueryAdapter(QueryParams requestParams, Class resourceType) {
    ResourceRegistry resourceRegistry = moduleRegistry.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry(resourceType);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    return new QueryParamsAdapter(resourceInformation, requestParams, moduleRegistry);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 38 with ResourceRegistry

use of io.crnk.core.engine.registry.ResourceRegistry in project crnk-framework by crnk-project.

the class ResourceFieldContributorTest method checkFieldAddedToResourceInformation.

@Test
public void checkFieldAddedToResourceInformation() {
    ResourceRegistry resourceRegistry = boot.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField contributedField = resourceInformation.findFieldByName("contributedProject");
    Assert.assertNotNull(contributedField);
    Assert.assertEquals("projects", contributedField.getOppositeResourceType());
    Assert.assertEquals(SerializeType.LAZY, contributedField.getSerializeType());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 39 with ResourceRegistry

use of io.crnk.core.engine.registry.ResourceRegistry in project crnk-framework by crnk-project.

the class ResourceMetaParitition method computeId.

private String computeId(MetaType element) {
    Type implementationType = element.getImplementationType();
    Class<?> rawType = ClassUtils.getRawType(implementationType);
    Class<?> enclosingClass = rawType.getEnclosingClass();
    boolean isLinks = LinksInformation.class.isAssignableFrom(rawType);
    boolean isMeta = MetaInformation.class.isAssignableFrom(rawType);
    ResourceRegistry resourceRegistry = context.getModuleContext().getResourceRegistry();
    if (enclosingClass != null && (isLinks || isMeta)) {
        RegistryEntry entry = resourceRegistry.getEntry(enclosingClass);
        if (entry != null) {
            String id = getId(entry.getResourceInformation().getResourceType());
            if (isMeta) {
                return id + "$meta";
            } else {
                return id + "$links";
            }
        }
    }
    if (!element.hasId()) {
        PreconditionUtil.assertNotNull("must have package", rawType.getPackage());
        String packageName = rawType.getPackage().getName();
        String closedPackageName = null;
        String closedResourceType = null;
        for (RegistryEntry entry : resourceRegistry.getResources()) {
            ResourceInformation resourceInformation = entry.getResourceInformation();
            Class<?> resourceClass = resourceInformation.getResourceClass();
            String resourcePackageName = resourceClass.getPackage().getName();
            if (packageName.startsWith(resourcePackageName) && (closedPackageName == null || closedPackageName.length() < resourcePackageName.length())) {
                closedPackageName = resourcePackageName;
                closedResourceType = resourceInformation.getResourceType();
            }
            Object resourceRepository = entry.getResourceRepository().getResourceRepository();
            resourcePackageName = resourceRepository.getClass().getPackage().getName();
            if (packageName.startsWith(resourcePackageName) && (closedPackageName == null || closedPackageName.length() < resourcePackageName.length())) {
                closedPackageName = resourcePackageName;
                closedResourceType = resourceInformation.getResourceType();
            }
        }
        if (closedResourceType != null) {
            String resourceId = getId(closedResourceType);
            String basePath = resourceId.substring(0, resourceId.lastIndexOf('.'));
            String relativePath = packageName.substring(closedPackageName.length());
            return basePath + relativePath + "." + element.getName().toLowerCase();
        }
    }
    return idProvider.computeIdPrefixFromPackage(rawType, element) + element.getName().toLowerCase();
}
Also used : MetaType(io.crnk.meta.model.MetaType) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) SerializeType(io.crnk.core.resource.annotations.SerializeType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaDataObject(io.crnk.meta.model.MetaDataObject) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 40 with ResourceRegistry

use of io.crnk.core.engine.registry.ResourceRegistry in project crnk-framework by crnk-project.

the class ResourceMetaParitition method discoverResource.

private MetaResource discoverResource(ResourceInformation information) {
    String id = getId(information.getResourceType());
    // check if already done (as super types get setup recursively)
    Optional<MetaElement> existingElement = context.getMetaElement(id);
    if (existingElement.isPresent()) {
        return (MetaResource) existingElement.get();
    }
    String superResourceType = information.getSuperResourceType();
    MetaResource superMeta = null;
    ResourceInformation superInformation = null;
    if (superResourceType != null) {
        superInformation = context.getModuleContext().getResourceRegistry().getEntry(superResourceType).getResourceInformation();
        superMeta = discoverResource(superInformation);
    }
    String resourceType = information.getResourceType();
    MetaResource resource = new MetaResource();
    resource.setId(id);
    resource.setElementType(resource);
    resource.setImplementationType(information.getResourceClass());
    resource.setName(getName(information));
    resource.setResourceType(resourceType);
    if (superMeta != null) {
        resource.setSuperType(superMeta);
        if (superMeta != null) {
            superMeta.addSubType(resource);
        }
    }
    ResourceRegistry resourceRegistry = context.getModuleContext().getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry(information.getResourceType());
    if (entry != null) {
        boolean readOnlyImpl = entry.getResourceRepository().getResourceRepository() instanceof ReadOnlyResourceRepositoryBase;
        resource.setUpdatable(resource.isUpdatable() && !readOnlyImpl);
        resource.setInsertable(resource.isInsertable() && !readOnlyImpl);
        resource.setDeletable(resource.isDeletable() && !readOnlyImpl);
    }
    List<ResourceField> fields = information.getFields();
    for (ResourceField field : fields) {
        if (superInformation == null || superInformation.findFieldByUnderlyingName(field.getUnderlyingName()) == null) {
            // TODO check whether overriden and changed
            addAttribute(resource, field);
        }
    }
    Class<?> resourceClass = information.getResourceClass();
    addElement(resourceClass, resource);
    return resource;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaElement(io.crnk.meta.model.MetaElement) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ReadOnlyResourceRepositoryBase(io.crnk.core.repository.ReadOnlyResourceRepositoryBase)

Aggregations

ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)48 Test (org.junit.Test)25 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)21 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)19 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)17 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)15 ResourcePath (io.crnk.core.engine.internal.dispatcher.path.ResourcePath)10 ResourceField (io.crnk.core.engine.information.resource.ResourceField)5 PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)4 ModuleRegistry (io.crnk.core.module.ModuleRegistry)4 QueryParamsAdapter (io.crnk.legacy.internal.QueryParamsAdapter)4 Before (org.junit.Before)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 FieldResourceGet (io.crnk.core.engine.internal.dispatcher.controller.FieldResourceGet)3 RelationshipsResourceGet (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceGet)3 ResourceRegistryImpl (io.crnk.core.engine.internal.registry.ResourceRegistryImpl)3 DefaultResourceRegistryPart (io.crnk.core.engine.registry.DefaultResourceRegistryPart)3 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)3 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)2 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)2