Search in sources :

Example 1 with ReadOnlyResourceRepositoryBase

use of io.crnk.core.repository.ReadOnlyResourceRepositoryBase 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

ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)1 ReadOnlyResourceRepositoryBase (io.crnk.core.repository.ReadOnlyResourceRepositoryBase)1 MetaElement (io.crnk.meta.model.MetaElement)1