Search in sources :

Example 16 with MetaElement

use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.

the class MetaResourceRepositoryImpl method findOne.

@SuppressWarnings("unchecked")
@Override
public T findOne(String id, QuerySpec querySpec) {
    MetaLookup lookup = lookupSupplier.get();
    MetaElement metaElement = lookup.getMetaById().get(id);
    Class<T> resourceClass = this.getResourceClass();
    if (metaElement != null && resourceClass.isInstance(metaElement)) {
        MetaElement wrappedElement = MetaUtils.adjustForRequest(lookup, metaElement);
        if (wrappedElement != null) {
            return (T) metaElement;
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : MetaLookup(io.crnk.meta.MetaLookup) MetaElement(io.crnk.meta.model.MetaElement) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException)

Example 17 with MetaElement

use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.

the class MetaResourceRepositoryImpl method filterByType.

@SuppressWarnings("unchecked")
private Collection<T> filterByType(Collection<MetaElement> values) {
    Collection<T> results = new ArrayList<>();
    Class<T> resourceClass = this.getResourceClass();
    MetaLookup lookup = lookupSupplier.get();
    for (MetaElement element : values) {
        if (resourceClass.isInstance(element)) {
            MetaElement wrappedElement = MetaUtils.adjustForRequest(lookup, element);
            if (wrappedElement != null) {
                results.add((T) wrappedElement);
            }
        }
    }
    return results;
}
Also used : MetaLookup(io.crnk.meta.MetaLookup) MetaElement(io.crnk.meta.model.MetaElement) ArrayList(java.util.ArrayList)

Example 18 with MetaElement

use of io.crnk.meta.model.MetaElement 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 19 with MetaElement

use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.

the class MetaRelationshipRepositoryImpl method findOneTarget.

@Override
public MetaElement findOneTarget(String sourceId, String fieldName, QuerySpec querySpec) {
    MetaElement source = getSource(sourceId);
    Object value = adjustForRequest(PropertyUtils.getProperty(source, fieldName));
    return (MetaElement) value;
}
Also used : MetaElement(io.crnk.meta.model.MetaElement)

Example 20 with MetaElement

use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.

the class MetaRelationshipRepositoryImpl method findManyTargets.

@SuppressWarnings("unchecked")
@Override
public ResourceList<MetaElement> findManyTargets(String sourceId, String fieldName, QuerySpec querySpec) {
    MetaElement source = getSource(sourceId);
    Object value = adjustForRequest(PropertyUtils.getProperty(source, fieldName));
    return querySpec.apply((Collection<MetaElement>) value);
}
Also used : MetaElement(io.crnk.meta.model.MetaElement)

Aggregations

MetaElement (io.crnk.meta.model.MetaElement)28 MetaDataObject (io.crnk.meta.model.MetaDataObject)6 Test (org.junit.Test)6 MetaResource (io.crnk.meta.model.resource.MetaResource)5 ResourceField (io.crnk.core.engine.information.resource.ResourceField)3 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)3 MetaLookup (io.crnk.meta.MetaLookup)3 MetaAttribute (io.crnk.meta.model.MetaAttribute)3 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)2 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 TSMetaTransformationContext (io.crnk.gen.typescript.transform.TSMetaTransformationContext)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)1 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 ReadOnlyResourceRepositoryBase (io.crnk.core.repository.ReadOnlyResourceRepositoryBase)1 Supplier (io.crnk.core.utils.Supplier)1 TSContainerElement (io.crnk.gen.typescript.model.TSContainerElement)1