Search in sources :

Example 1 with MetaAttribute

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

the class ValidationMetaProviderTest method testNotNullDisablesNullablity.

@Test
public void testNotNullDisablesNullablity() {
    setup(true);
    MetaResourceBase meta = resourceMetaProvider.getMeta(Task.class);
    MetaAttribute attr = meta.getAttribute("name");
    Assert.assertFalse(attr.isNullable());
}
Also used : MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaResourceBase(io.crnk.meta.model.resource.MetaResourceBase) Test(org.junit.Test)

Example 2 with MetaAttribute

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

the class TSMetaDataObjectTransformation method generateResourceFields.

private static void generateResourceFields(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject meta) {
    TSInterfaceType attributesType = new TSInterfaceType();
    attributesType.setName(ATTRIBUTES_CLASS_NAME);
    attributesType.setExported(true);
    TSInterfaceType relationshipsType = new TSInterfaceType();
    relationshipsType.setName(RELATIONSHIPS_CLASS_NAME);
    relationshipsType.setExported(true);
    TSIndexSignature relationshipsIndexSignature = new TSIndexSignature();
    relationshipsIndexSignature.setKeyType(TSPrimitiveType.STRING);
    relationshipsIndexSignature.setValueType(NgrxJsonApiLibrary.RESOURCE_RELATIONSHIP);
    relationshipsIndexSignature.setParent(relationshipsType);
    relationshipsType.setIndexSignature(relationshipsIndexSignature);
    // TODO remo: interface support
    MetaKey primaryKey = meta.getPrimaryKey();
    for (MetaAttribute attr : meta.getDeclaredAttributes()) {
        if (primaryKey != null && primaryKey.getUniqueElement().equals(attr)) {
            continue;
        }
        generateResourceField(attr, context, interfaceType, attributesType, relationshipsType);
    }
    if (!isEmpty(relationshipsType)) {
        TSModule module = TypescriptUtils.getNestedTypeContainer(interfaceType, true);
        module.getElements().add(relationshipsType);
        relationshipsType.setParent(module);
        TSField relationshipsField = new TSField();
        relationshipsField.setName("relationships");
        relationshipsField.setType(relationshipsType);
        relationshipsField.setNullable(true);
        interfaceType.getDeclaredMembers().add(relationshipsField);
    }
    if (!isEmpty(attributesType)) {
        TSModule module = TypescriptUtils.getNestedTypeContainer(interfaceType, true);
        module.getElements().add(attributesType);
        attributesType.setParent(module);
        TSField attributesField = new TSField();
        attributesField.setName("attributes");
        attributesField.setType(attributesType);
        attributesField.setNullable(true);
        interfaceType.getDeclaredMembers().add(attributesField);
    }
}
Also used : TSIndexSignature(io.crnk.gen.typescript.model.TSIndexSignature) TSField(io.crnk.gen.typescript.model.TSField) MetaKey(io.crnk.meta.model.MetaKey) TSModule(io.crnk.gen.typescript.model.TSModule) MetaAttribute(io.crnk.meta.model.MetaAttribute) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 3 with MetaAttribute

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

the class TSMetaDataObjectTransformation method generateAttributes.

private static void generateAttributes(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject element) {
    for (MetaAttribute attr : element.getDeclaredAttributes()) {
        MetaType elementType = attr.getType().getElementType();
        TSField field = new TSField();
        field.setName(attr.getName());
        field.setType((TSType) context.transform(elementType, TSMetaTransformationOptions.EMPTY));
        field.setNullable(true);
        interfaceType.getDeclaredMembers().add(field);
    }
}
Also used : TSField(io.crnk.gen.typescript.model.TSField) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaType(io.crnk.meta.model.MetaType)

Example 4 with MetaAttribute

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

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

the class MetaFilteringTest method checkReadOnlyAttribute.

@Test
public void checkReadOnlyAttribute() throws IOException {
    RegistryEntry entry = boot.getResourceRegistry().getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.POST))).thenReturn(FilterBehavior.FORBIDDEN);
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "Tasks"));
    ResourceList<MetaResource> list = repository.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    MetaResource taskMeta = list.get(0);
    Assert.assertTrue(taskMeta.hasAttribute("project"));
    MetaAttribute projectAttr = taskMeta.getAttribute("project");
    Assert.assertFalse(projectAttr.isInsertable());
    Assert.assertFalse(projectAttr.isUpdatable());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Aggregations

MetaAttribute (io.crnk.meta.model.MetaAttribute)54 Test (org.junit.Test)32 MetaResource (io.crnk.meta.model.resource.MetaResource)16 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)9 MetaDataObject (io.crnk.meta.model.MetaDataObject)7 MetaKey (io.crnk.meta.model.MetaKey)7 MetaResourceBase (io.crnk.meta.model.resource.MetaResourceBase)7 FilterSpec (io.crnk.core.queryspec.FilterSpec)5 MetaType (io.crnk.meta.model.MetaType)5 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)4 QuerySpec (io.crnk.core.queryspec.QuerySpec)4 MetaElement (io.crnk.meta.model.MetaElement)4 EntityManager (javax.persistence.EntityManager)4 Type (java.lang.reflect.Type)3 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)2 TSField (io.crnk.gen.typescript.model.TSField)2 MetaEntity (io.crnk.jpa.meta.MetaEntity)2 MetaJpaDataObject (io.crnk.jpa.meta.MetaJpaDataObject)2 NamingTestEntity (io.crnk.jpa.model.NamingTestEntity)2