Search in sources :

Example 1 with MetaDataObject

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

the class TSMetaDataObjectTransformation method setRelationshipsSuperType.

private void setRelationshipsSuperType(TSInterfaceType interfaceType, MetaDataObject metaDataObject, TSMetaTransformationContext context) {
    // iterate over super types till (non-empty) one is found with a relationship interface definition
    MetaDataObject current = metaDataObject;
    while (current.getSuperType() != null && generateAsResource(current.getSuperType())) {
        MetaDataObject superType = current.getSuperType();
        TSInterfaceType superInterface = (TSInterfaceType) context.transform(superType, TSMetaTransformationOptions.EMPTY);
        TSInterfaceType relationshipsType = TypescriptUtils.getNestedInterface(interfaceType, RELATIONSHIPS_CLASS_NAME, false);
        if (relationshipsType != null) {
            TSInterfaceType superRelationshipType = TypescriptUtils.getNestedInterface(superInterface, RELATIONSHIPS_CLASS_NAME, false);
            if (superRelationshipType != null) {
                relationshipsType.getImplementedInterfaces().add(superRelationshipType);
                break;
            }
        }
        current = superType;
    }
}
Also used : MetaDataObject(io.crnk.meta.model.MetaDataObject) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 2 with MetaDataObject

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

the class JpaResourceInformationProvider method build.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public ResourceInformation build(final Class<?> resourceClass) {
    String resourceType = getResourceType(resourceClass);
    MetaDataObject meta = metaProvider.discoverMeta(resourceClass).asDataObject();
    DefaultResourceInstanceBuilder instanceBuilder = new DefaultResourceInstanceBuilder(resourceClass);
    List<ResourceField> fields = getResourceFields(resourceClass);
    Class<?> superclass = resourceClass.getSuperclass();
    String superResourceType = superclass != Object.class && superclass.getAnnotation(MappedSuperclass.class) == null ? context.getResourceType(superclass) : null;
    TypeParser typeParser = context.getTypeParser();
    ResourceInformation info = new ResourceInformation(typeParser, resourceClass, resourceType, superResourceType, instanceBuilder, fields, new OffsetLimitPagingBehavior());
    info.setValidator(new JpaOptimisticLockingValidator(meta));
    info.setIdStringMapper(new JpaIdMapper(meta));
    return info;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) TypeParser(io.crnk.core.engine.parser.TypeParser) MetaDataObject(io.crnk.meta.model.MetaDataObject) MappedSuperclass(javax.persistence.MappedSuperclass) DefaultResourceInstanceBuilder(io.crnk.core.engine.internal.information.resource.DefaultResourceInstanceBuilder)

Example 3 with MetaDataObject

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

the class TSMetaResourceRepositoryTransformation method transform.

@Override
public TSElement transform(MetaElement element, TSMetaTransformationContext context, TSMetaTransformationOptions options) {
    MetaResourceRepository metaRepository = (MetaResourceRepository) element;
    MetaResource metaResource = metaRepository.getResourceType();
    TSType resourceType = context.transform(metaResource, TSMetaTransformationOptions.EMPTY).asType();
    TSContainerElement parent = (TSContainerElement) resourceType.getParent();
    MetaDataObject metaListLinks = metaRepository.getListLinksType();
    MetaDataObject metaListMeta = metaRepository.getListMetaType();
    TSInterfaceType oneResultType = new TSInterfaceType();
    oneResultType.setName(metaResource.getName() + "Result");
    oneResultType.setExported(true);
    oneResultType.getImplementedInterfaces().add(NgrxJsonApiLibrary.ONE_QUERY_RESULT);
    oneResultType.addDeclaredMember(newDataField(context, resourceType, false));
    parent.addElement(oneResultType);
    TSInterfaceType manyResultType = new TSInterfaceType();
    manyResultType.setName(metaResource.getName() + "ListResult");
    manyResultType.setExported(true);
    manyResultType.getImplementedInterfaces().add(NgrxJsonApiLibrary.MANY_QUERY_RESULT);
    manyResultType.addDeclaredMember(newDataField(context, resourceType, true));
    parent.addElement(manyResultType);
    if (metaListLinks != null) {
        TSMetaTransformationOptions listOptions = new TSMetaTransformationOptions();
        listOptions.setParent(TypescriptUtils.getNestedTypeContainer(manyResultType, true));
        TSType linksType = context.transform(metaListLinks, listOptions).asType();
        TSField field = new TSField();
        field.setName("links");
        field.setNullable(true);
        field.setType(linksType);
        manyResultType.addDeclaredMember(field);
    }
    if (metaListMeta != null) {
        TSMetaTransformationOptions listOptions = new TSMetaTransformationOptions();
        listOptions.setParent(TypescriptUtils.getNestedTypeContainer(manyResultType, true));
        TSType metaType = context.transform(metaListMeta, listOptions).asType();
        TSField field = new TSField();
        field.setName("meta");
        field.setNullable(true);
        field.setType(metaType);
        manyResultType.addDeclaredMember(field);
    }
    return null;
}
Also used : TSField(io.crnk.gen.typescript.model.TSField) TSContainerElement(io.crnk.gen.typescript.model.TSContainerElement) MetaResourceRepository(io.crnk.meta.model.resource.MetaResourceRepository) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaResource(io.crnk.meta.model.resource.MetaResource) TSType(io.crnk.gen.typescript.model.TSType) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 4 with MetaDataObject

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

the class JoinRegistry method getEntityAttribute.

public E getEntityAttribute(MetaAttributePath attrPath) {
    MetaAttributePath associationPath = extractAssociationPath(attrPath);
    MetaAttributePath primitivePath = attrPath.subPath(associationPath.length());
    @SuppressWarnings("unchecked") E from = (E) getOrCreateJoin(associationPath);
    if (primitivePath.length() == 0) {
        return from;
    }
    MetaAttributePath currentPath = associationPath;
    E criteriaPath = null;
    for (MetaAttribute pathElement : primitivePath) {
        currentPath = currentPath.concat(pathElement);
        E currentCriteriaPath = criteriaPath != null ? criteriaPath : from;
        if (pathElement instanceof MetaMapAttribute) {
            if (criteriaPath != null)
                throw new IllegalStateException("Cannot join to map");
            criteriaPath = joinMap(currentCriteriaPath, pathElement);
        } else {
            // we may need to downcast if attribute is defined on a subtype
            MetaDataObject parent = pathElement.getParent().asDataObject();
            Class<?> pathType = parent.getImplementationClass();
            Class<?> currentType = backend.getJavaElementType(currentCriteriaPath);
            boolean isSubType = !pathType.isAssignableFrom(currentType);
            if (isSubType) {
                currentCriteriaPath = backend.joinSubType(currentCriteriaPath, pathType);
            }
            criteriaPath = backend.getAttribute(currentCriteriaPath, pathElement);
        }
    }
    return criteriaPath;
}
Also used : MetaMapAttribute(io.crnk.meta.model.MetaMapAttribute) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaAttributePath(io.crnk.meta.model.MetaAttributePath)

Example 5 with MetaDataObject

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

the class MetaDataObjectProvider method onInitialized.

@Override
public void onInitialized(MetaElement element) {
    if (element instanceof MetaAttribute && element.getParent().getClass() == getMetaClass()) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaDataObject parent = attr.getParent();
        Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName());
        MetaElement metaType = context.allocate(implementationType);
        attr.setType(metaType.asType());
    }
}
Also used : Type(java.lang.reflect.Type) MetaElement(io.crnk.meta.model.MetaElement) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Aggregations

MetaDataObject (io.crnk.meta.model.MetaDataObject)23 MetaAttribute (io.crnk.meta.model.MetaAttribute)7 MetaElement (io.crnk.meta.model.MetaElement)6 ResourceField (io.crnk.core.engine.information.resource.ResourceField)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)5 TSInterfaceType (io.crnk.gen.typescript.model.TSInterfaceType)5 Test (org.junit.Test)5 MetaAttributePath (io.crnk.meta.model.MetaAttributePath)3 MetaResource (io.crnk.meta.model.resource.MetaResource)3 Type (java.lang.reflect.Type)3 MetaPrimaryKey (io.crnk.meta.model.MetaPrimaryKey)2 MetaType (io.crnk.meta.model.MetaType)2 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)1 DefaultResourceInstanceBuilder (io.crnk.core.engine.internal.information.resource.DefaultResourceInstanceBuilder)1 TypeParser (io.crnk.core.engine.parser.TypeParser)1 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 IncludeFieldSpec (io.crnk.core.queryspec.IncludeFieldSpec)1 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)1 TSContainerElement (io.crnk.gen.typescript.model.TSContainerElement)1 TSElement (io.crnk.gen.typescript.model.TSElement)1