Search in sources :

Example 96 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry 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)

Example 97 with RegistryEntry

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

the class ResourceMetaParitition method discoverElements.

@Override
public void discoverElements() {
    ResourceRegistry resourceRegistry = context.getModuleContext().getResourceRegistry();
    // enforce setup of meta data
    Collection<RegistryEntry> entries = resourceRegistry.getResources();
    for (RegistryEntry entry : entries) {
        ResourceInformation resourceInformation = entry.getResourceInformation();
        MetaResource metaResource = discoverResource(resourceInformation);
        ResourceRepositoryInformation repositoryInformation = entry.getRepositoryInformation();
        ResourceRepositoryAdapter<?, Serializable> resourceRepository = entry.getResourceRepository();
        if (resourceRepository != null) {
            MetaResourceRepository repository = discoverRepository(repositoryInformation, metaResource, resourceRepository);
            context.addElement(repository);
        }
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Serializable(java.io.Serializable) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 98 with RegistryEntry

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

the class ConstraintViolationExceptionMapper method getResourceId.

/**
 * @param resource to get the id from
 * @return id of the given resource
 */
protected String getResourceId(Object resource) {
    ResourceRegistry resourceRegistry = context.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.findEntry(resource.getClass());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField idField = resourceInformation.getIdField();
    Object id = idField.getAccessor().getValue(resource);
    if (id != null) {
        return id.toString();
    }
    return null;
}
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)

Example 99 with RegistryEntry

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

the class ClientResourceUpsert method allocateResources.

public List<Object> allocateResources(List<Resource> resources) {
    List<Object> objects = new ArrayList<>();
    for (Resource resource : resources) {
        RegistryEntry registryEntry = getRegistryEntry(resource.getType());
        ResourceInformation resourceInformation = registryEntry.getResourceInformation();
        Object object = newResource(resourceInformation, resource);
        setId(resource, object, resourceInformation);
        setAttributes(resource, object, resourceInformation);
        setLinks(resource, object, resourceInformation);
        setMeta(resource, object, resourceInformation);
        objects.add(object);
        String uid = getUID(resource);
        resourceMap.put(uid, object);
    }
    return objects;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ArrayList(java.util.ArrayList) Resource(io.crnk.core.engine.document.Resource) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 100 with RegistryEntry

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

the class ClientResourceUpsert method setRelations.

public void setRelations(List<Resource> resources) {
    for (Resource resource : resources) {
        String uid = getUID(resource);
        Object object = resourceMap.get(uid);
        RegistryEntry registryEntry = resourceRegistry.getEntry(resource.getType());
        // no need for any query parameters when doing POST/PATCH
        QueryAdapter queryAdapter = null;
        // no in use on the client side
        RepositoryMethodParameterProvider parameterProvider = null;
        setRelations(object, registryEntry, resource, queryAdapter, parameterProvider, true);
    }
}
Also used : QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider)

Aggregations

RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)119 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)60 Test (org.junit.Test)38 ResourceField (io.crnk.core.engine.information.resource.ResourceField)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)19 QuerySpec (io.crnk.core.queryspec.QuerySpec)18 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 Serializable (java.io.Serializable)14 Task (io.crnk.core.mock.models.Task)13 Response (io.crnk.core.engine.dispatcher.Response)12 Document (io.crnk.core.engine.document.Document)11 Resource (io.crnk.core.engine.document.Resource)11 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)9 FilterSpec (io.crnk.core.queryspec.FilterSpec)9 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)8 Before (org.junit.Before)8 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)6