Search in sources :

Example 16 with MetaDataObject

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

the class TSMetaDataObjectTransformation method transform.

@Override
public TSElement transform(MetaElement element, TSMetaTransformationContext context, TSMetaTransformationOptions options) {
    MetaDataObject metaDataObject = (MetaDataObject) element;
    TSInterfaceType interfaceType = new TSInterfaceType();
    interfaceType.setName(metaDataObject.getName());
    interfaceType.setExported(true);
    if (metaDataObject instanceof MetaResource) {
        MetaResource metaResource = (MetaResource) metaDataObject;
        String resourceType = metaResource.getResourceType();
        interfaceType.setPrivateData(TSMetaDataObjectTransformation.PRIVATE_DATA_RESOURCE_TYPE, resourceType);
    }
    interfaceType.setPrivateData(TSMetaDataObjectTransformation.PRIVATE_DATA_META_ELEMENT_ID, element.getId());
    context.putMapping(metaDataObject, interfaceType);
    if (options.getParent() == null) {
        setupParent(context, interfaceType, metaDataObject);
    } else {
        options.getParent().addElement(interfaceType);
    }
    if (generateAsResource(metaDataObject)) {
        if (metaDataObject.getSuperType() == null) {
            interfaceType.getImplementedInterfaces().add(NgrxJsonApiLibrary.STORE_RESOURCE);
        } else {
            // trigger generation of super type, fully attach during post processing
            MetaDataObject superType = metaDataObject.getSuperType();
            if (generateAsResource(superType)) {
                TSElement superInterface = context.transform(superType, TSMetaTransformationOptions.EMPTY);
                interfaceType.getImplementedInterfaces().add((TSInterfaceType) superInterface);
            }
        }
        generateResourceFields(context, interfaceType, metaDataObject);
    } else {
        if (metaDataObject.getSuperType() != null) {
            TSInterfaceType superInterface = (TSInterfaceType) context.transform(metaDataObject.getSuperType(), TSMetaTransformationOptions.EMPTY);
            interfaceType.getImplementedInterfaces().add(superInterface);
        }
        for (MetaDataObject interfaceMeta : metaDataObject.getInterfaces()) {
            TSInterfaceType implementedinterfaceType = (TSInterfaceType) context.transform(interfaceMeta, TSMetaTransformationOptions.EMPTY);
            interfaceType.getImplementedInterfaces().add(implementedinterfaceType);
        }
        generateAttributes(context, interfaceType, metaDataObject);
    }
    for (MetaDataObject subType : metaDataObject.getSubTypes()) {
        context.transform(subType, TSMetaTransformationOptions.EMPTY);
    }
    return interfaceType;
}
Also used : MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaResource(io.crnk.meta.model.resource.MetaResource) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType) TSElement(io.crnk.gen.typescript.model.TSElement)

Example 17 with MetaDataObject

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

the class TSMetaDataObjectTransformation method setAttributeSuperType.

private void setAttributeSuperType(TSInterfaceType interfaceType, MetaDataObject metaDataObject, TSMetaTransformationContext context) {
    // iterate over super types till (non-empty) one is found with a attributes 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 attributesType = TypescriptUtils.getNestedInterface(interfaceType, ATTRIBUTES_CLASS_NAME, false);
        if (attributesType != null) {
            TSInterfaceType superAttributeType = TypescriptUtils.getNestedInterface(superInterface, ATTRIBUTES_CLASS_NAME, false);
            if (superAttributeType != null) {
                attributesType.getImplementedInterfaces().add(superAttributeType);
                break;
            }
        }
        current = superType;
    }
}
Also used : MetaDataObject(io.crnk.meta.model.MetaDataObject) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 18 with MetaDataObject

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

the class TSMetaDataObjectTransformation method postTransform.

@Override
public void postTransform(TSElement element, TSMetaTransformationContext context) {
    if (element instanceof TSInterfaceType) {
        TSInterfaceType interfaceType = (TSInterfaceType) element;
        String metaId = interfaceType.getPrivateData(TSMetaDataObjectTransformation.PRIVATE_DATA_META_ELEMENT_ID, String.class);
        MetaElement metaElement = context.getMeta(metaId);
        if (metaElement instanceof MetaDataObject) {
            MetaDataObject metaDataObject = (MetaDataObject) metaElement;
            if (generateAsResource(metaDataObject)) {
                // link to super type, only available once all fields are initialized
                MetaDataObject superType = metaDataObject.getSuperType();
                if (superType != null) {
                    setAttributeSuperType(interfaceType, metaDataObject, context);
                    setRelationshipsSuperType(interfaceType, metaDataObject, context);
                }
            }
        }
    }
}
Also used : MetaElement(io.crnk.meta.model.MetaElement) MetaDataObject(io.crnk.meta.model.MetaDataObject) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 19 with MetaDataObject

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

the class ResourceMetaFilter method onInitialized.

@Override
public void onInitialized(MetaElement element) {
    if (element instanceof MetaResourceBase) {
        MetaResourceBase metaResource = (MetaResourceBase) element;
        ResourceInformation information = getResourceInformation(metaResource, true);
        PreconditionUtil.assertNotNull(information.getResourceType(), metaResource);
        for (ResourceField field : information.getRelationshipFields()) {
            if (field.getOppositeName() != null) {
                String oppositeType = field.getOppositeResourceType();
                MetaResource oppositeMeta = (MetaResource) context.getMetaElement(partition.getId(oppositeType)).get();
                MetaAttribute attr = metaResource.getAttribute(field.getUnderlyingName());
                MetaAttribute oppositeAttr = oppositeMeta.getAttribute(field.getOppositeName());
                PreconditionUtil.assertNotNull(attr.getId() + " opposite not found", oppositeAttr);
                attr.setOppositeAttribute(oppositeAttr);
            }
        }
        ResourceField idField = information.getIdField();
        if (idField != null) {
            MetaAttribute idAttr = metaResource.getAttribute(idField.getUnderlyingName());
            idAttr.setPrimaryKeyAttribute(true);
            if (metaResource.getSuperType() == null || metaResource.getSuperType().getPrimaryKey() == null) {
                MetaPrimaryKey primaryKey = new MetaPrimaryKey();
                primaryKey.setName(metaResource.getName() + "$primaryKey");
                primaryKey.setName(metaResource.getId() + "$primaryKey");
                primaryKey.setElements(Arrays.asList(idAttr));
                primaryKey.setUnique(true);
                primaryKey.setParent(metaResource, true);
                metaResource.setPrimaryKey(primaryKey);
                partition.addElement(null, primaryKey);
            }
        }
    }
    if (element instanceof MetaAttribute && element.getParent() instanceof MetaResourceBase) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaResourceBase parent = (MetaResourceBase) attr.getParent();
        ResourceInformation information = getResourceInformation(parent, true);
        ResourceField field = information.findFieldByUnderlyingName(attr.getName());
        PreconditionUtil.assertNotNull(attr.getName(), field);
        if (field.getResourceFieldType() == ResourceFieldType.RELATIONSHIP) {
            String oppositeType = field.getOppositeResourceType();
            String oppositeId = partition.getId(oppositeType);
            Optional<MetaElement> optOppositeMeta = context.getMetaElement(oppositeId);
            if (!optOppositeMeta.isPresent()) {
                throw new IllegalStateException("opposite meta element '" + oppositeId + "' for element '" + element.getId() + "' not found");
            }
            MetaResource oppositeMeta = (MetaResource) optOppositeMeta.get();
            if (field.isCollection()) {
                boolean isSet = Set.class.isAssignableFrom(field.getType());
                String suffix = (isSet ? "$set" : "$list");
                Optional<MetaElement> optMetaCollection = context.getMetaElement(oppositeId + suffix);
                MetaCollectionType metaCollection;
                if (optMetaCollection.isPresent()) {
                    metaCollection = (MetaCollectionType) optMetaCollection.get();
                } else {
                    metaCollection = isSet ? new MetaSetType() : new MetaListType();
                    metaCollection.setId(oppositeMeta.getId() + suffix);
                    metaCollection.setName(oppositeMeta.getName() + suffix);
                    metaCollection.setImplementationType(field.getGenericType());
                    metaCollection.setElementType(oppositeMeta);
                    partition.addElement(null, metaCollection);
                }
                attr.setType(metaCollection);
            } else {
                attr.setType(oppositeMeta);
            }
        } else {
            Type implementationType = field.getGenericType();
            MetaElement metaType = partition.allocateMetaElement(implementationType).get();
            attr.setType(metaType.asType());
        }
    } else if (element instanceof MetaAttribute && element.getParent() instanceof MetaJsonObject) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaDataObject parent = attr.getParent();
        Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName());
        MetaElement metaType = partition.allocateMetaElement(implementationType).get();
        attr.setType(metaType.asType());
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaPrimaryKey(io.crnk.meta.model.MetaPrimaryKey) MetaElement(io.crnk.meta.model.MetaElement) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaResourceBase(io.crnk.meta.model.resource.MetaResourceBase) MetaCollectionType(io.crnk.meta.model.MetaCollectionType) ResourceField(io.crnk.core.engine.information.resource.ResourceField) MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) MetaListType(io.crnk.meta.model.MetaListType) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) MetaSetType(io.crnk.meta.model.MetaSetType) MetaCollectionType(io.crnk.meta.model.MetaCollectionType) Type(java.lang.reflect.Type) MetaListType(io.crnk.meta.model.MetaListType) MetaSetType(io.crnk.meta.model.MetaSetType) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaJsonObject(io.crnk.meta.model.resource.MetaJsonObject) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 20 with MetaDataObject

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

the class MetaDataObjectProvider method create.

@Override
public MetaElement create(Type type) {
    Class<?> rawClazz = ClassUtils.getRawType(type);
    Class<?> superClazz = rawClazz.getSuperclass();
    MetaElement superMeta = null;
    if (superClazz != Object.class && superClazz != null) {
        superMeta = context.allocate(superClazz);
    }
    MetaDataObject meta = newDataObject();
    meta.setElementType(meta);
    meta.setName(rawClazz.getSimpleName());
    meta.setImplementationType(type);
    meta.setSuperType((MetaDataObject) superMeta);
    if (superMeta != null) {
        ((MetaDataObject) superMeta).addSubType(meta);
    }
    createAttributes(meta);
    return meta;
}
Also used : MetaElement(io.crnk.meta.model.MetaElement) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaDataObject(io.crnk.meta.model.MetaDataObject)

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