Search in sources :

Example 11 with MetaType

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

the class JpaMetaFilter method onInitialized.

@Override
public void onInitialized(MetaElement element) {
    if (element.getParent() instanceof MetaJpaDataObject && element instanceof MetaAttribute) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaDataObject parent = attr.getParent();
        Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName());
        MetaType metaType = (MetaType) partition.allocateMetaElement(implementationType).get();
        attr.setType(metaType);
    }
    if (element.getParent() instanceof MetaJpaDataObject && element instanceof MetaAttribute && ((MetaAttribute) element).getOppositeAttribute() == null) {
        MetaAttribute attr = (MetaAttribute) element;
        String mappedBy = getMappedBy(attr);
        if (mappedBy != null) {
            MetaType attrType = attr.getType();
            MetaDataObject oppositeType = attrType.getElementType().asDataObject();
            if (!mappedBy.contains(".")) {
                MetaAttribute oppositeAttr = oppositeType.getAttribute(mappedBy);
                attr.setOppositeAttribute(oppositeAttr);
            } else {
            // references within embeddables not yet supported
            }
        }
    }
}
Also used : Type(java.lang.reflect.Type) MetaType(io.crnk.meta.model.MetaType) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaJpaDataObject(io.crnk.jpa.meta.MetaJpaDataObject) MetaType(io.crnk.meta.model.MetaType)

Example 12 with MetaType

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

the class QueryFilterBuilder method enhanceAttributePath.

public MetaAttributePath enhanceAttributePath(MetaAttributePath attrPath, Object value) {
    MetaAttribute attr = attrPath.getLast();
    MetaType valueType = attr.getType();
    if (valueType instanceof MetaMapType) {
        valueType = valueType.getElementType();
    }
    boolean anyType = AnyTypeObject.class.isAssignableFrom(valueType.getImplementationClass());
    if (anyType) {
        // we have and AnyType and do need to select the proper attribute of
        // the embeddable
        MetaAttribute anyAttr = AnyUtils.findAttribute((MetaDataObject) valueType, value);
        return attrPath.concat(anyAttr);
    } else {
        return attrPath;
    }
}
Also used : MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaMapType(io.crnk.meta.model.MetaMapType) MetaType(io.crnk.meta.model.MetaType)

Example 13 with MetaType

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

the class MetaLookupTest method testObjectArrayMeta.

@Test
public void testObjectArrayMeta() {
    MetaArrayType meta = metaProvider.discoverMeta(TestEntity[].class);
    MetaType elementType = meta.getElementType();
    Assert.assertTrue(elementType instanceof MetaDataObject);
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaArrayType(io.crnk.meta.model.MetaArrayType) MetaType(io.crnk.meta.model.MetaType) Test(org.junit.Test)

Example 14 with MetaType

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

the class TSMetaDataObjectTransformation method generateResourceField.

private static void generateResourceField(MetaAttribute attr, TSMetaTransformationContext context, TSInterfaceType interfaceType, TSInterfaceType attributesType, TSInterfaceType relationshipsType) {
    MetaType metaElementType = attr.getType().getElementType();
    TSType elementType = (TSType) context.transform(metaElementType, TSMetaTransformationOptions.EMPTY);
    TSField field = new TSField();
    field.setName(attr.getName());
    field.setType(elementType);
    field.setNullable(true);
    if (attr.isAssociation()) {
        TSType relationshipType = attr.getType().isCollection() ? NgrxJsonApiLibrary.TYPED_MANY_RESOURCE_RELATIONSHIP : NgrxJsonApiLibrary.TYPED_ONE_RESOURCE_RELATIONSHIP;
        field.setType(new TSParameterizedType(relationshipType, elementType));
        relationshipsType.getDeclaredMembers().add(field);
        field.setParent(relationshipsType);
    } else if (attr instanceof MetaResourceField && ((MetaResourceField) attr).isMeta()) {
        field.setName("meta");
        interfaceType.getDeclaredMembers().add(field);
        field.setParent(interfaceType);
    } else if (attr instanceof MetaResourceField && ((MetaResourceField) attr).isLinks()) {
        field.setName("links");
        interfaceType.getDeclaredMembers().add(field);
        field.setParent(interfaceType);
    } else {
        attributesType.getDeclaredMembers().add(field);
        field.setParent(attributesType);
    }
}
Also used : MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) TSField(io.crnk.gen.typescript.model.TSField) TSParameterizedType(io.crnk.gen.typescript.model.TSParameterizedType) TSType(io.crnk.gen.typescript.model.TSType) MetaType(io.crnk.meta.model.MetaType)

Example 15 with MetaType

use of io.crnk.meta.model.MetaType 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)

Aggregations

MetaType (io.crnk.meta.model.MetaType)21 Test (org.junit.Test)16 MetaAttribute (io.crnk.meta.model.MetaAttribute)5 MetaDataObject (io.crnk.meta.model.MetaDataObject)3 TSField (io.crnk.gen.typescript.model.TSField)2 MetaResource (io.crnk.meta.model.resource.MetaResource)2 Type (java.lang.reflect.Type)2 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)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 SerializeType (io.crnk.core.resource.annotations.SerializeType)1 TSParameterizedType (io.crnk.gen.typescript.model.TSParameterizedType)1 TSType (io.crnk.gen.typescript.model.TSType)1 MetaJpaDataObject (io.crnk.jpa.meta.MetaJpaDataObject)1 TestEntity (io.crnk.jpa.model.TestEntity)1 MetaArrayType (io.crnk.meta.model.MetaArrayType)1 MetaMapType (io.crnk.meta.model.MetaMapType)1 MetaResourceField (io.crnk.meta.model.resource.MetaResourceField)1 ParameterizedType (java.lang.reflect.ParameterizedType)1