Search in sources :

Example 21 with MetaAttribute

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

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

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

the class JpaMetaEnricher method onInitialized.

@Override
public void onInitialized(MetaElement element) {
    if (element instanceof MetaJsonObject) {
        MetaJsonObject jsonDataObject = (MetaJsonObject) element;
        Class<?> implementationClass = jsonDataObject.getImplementationClass();
        if (metaProvider.hasMeta(implementationClass)) {
            MetaJpaDataObject jpaDataObject = metaProvider.getMeta(implementationClass);
            if (jpaDataObject.getPrimaryKey() != null && jsonDataObject.getPrimaryKey() != null) {
                jsonDataObject.getPrimaryKey().setGenerated(jpaDataObject.getPrimaryKey().isGenerated());
            }
            List<? extends MetaAttribute> declaredAttributes = jsonDataObject.getDeclaredAttributes();
            for (MetaAttribute declaredAttribute : declaredAttributes) {
                String name = declaredAttribute.getName();
                if (jpaDataObject.hasAttribute(name)) {
                    MetaAttribute jpaAttribute = jpaDataObject.getAttribute(name);
                    declaredAttribute.setLob(jpaAttribute.isLob());
                    declaredAttribute.setVersion(jpaAttribute.isVersion());
                    declaredAttribute.setNullable(jpaAttribute.isNullable());
                    declaredAttribute.setCascaded(jpaAttribute.isCascaded());
                }
            }
        }
    }
}
Also used : MetaJsonObject(io.crnk.meta.model.resource.MetaJsonObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaJpaDataObject(io.crnk.jpa.meta.MetaJpaDataObject)

Example 24 with MetaAttribute

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

the class DtoMappingTest method testDtoMeta.

@Test
public void testDtoMeta() {
    MetaLookup lookup = metaModule.getLookup();
    MetaResource meta = (MetaResource) lookup.getMetaById().get("resources.testDTO");
    MetaKey primaryKey = meta.getPrimaryKey();
    Assert.assertNotNull(primaryKey);
    Assert.assertEquals(1, primaryKey.getElements().size());
    Assert.assertEquals("id", primaryKey.getElements().get(0).getName());
    MetaAttribute oneRelatedAttr = meta.getAttribute("oneRelatedValue");
    Assert.assertTrue(oneRelatedAttr.isAssociation());
}
Also used : MetaLookup(io.crnk.meta.MetaLookup) MetaKey(io.crnk.meta.model.MetaKey) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 25 with MetaAttribute

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

the class JpaMetaProviderAttributeTest method testCascaded.

@Test
public void testCascaded() {
    MetaEntity meta = metaProvider.discoverMeta(TestEntity.class);
    MetaAttribute oneRelatedAttr = meta.getAttribute("oneRelatedValue");
    MetaAttribute eagerRelatedAttr = meta.getAttribute("eagerRelatedValue");
    Assert.assertTrue(oneRelatedAttr.isCascaded());
    Assert.assertFalse(eagerRelatedAttr.isCascaded());
}
Also used : MetaAttribute(io.crnk.meta.model.MetaAttribute) 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