Search in sources :

Example 26 with MetaAttribute

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

the class JpaMetaProviderAttributeTest method testPrimaryKey.

@Test
public void testPrimaryKey() {
    MetaEntity meta = metaProvider.discoverMeta(TestEntity.class);
    MetaAttribute attr = meta.getAttribute("id");
    Assert.assertFalse(attr.isAssociation());
    Assert.assertEquals("id", attr.getName());
    Assert.assertEquals(TestEntity.class.getName() + ".id", attr.getId());
    Assert.assertFalse(attr.isDerived());
    Assert.assertFalse(attr.isVersion());
    Assert.assertFalse(attr.isLazy());
    Assert.assertFalse(attr.isCascaded());
    Assert.assertNull(attr.getOppositeAttribute());
}
Also used : MetaAttribute(io.crnk.meta.model.MetaAttribute) Test(org.junit.Test)

Example 27 with MetaAttribute

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

the class AbstractEntityMetaFactory method setKey.

private void setKey(T meta) {
    if (meta.getPrimaryKey() == null) {
        boolean generated = false;
        ArrayList<MetaAttribute> pkElements = new ArrayList<>();
        for (MetaAttribute attr : meta.getAttributes()) {
            if (attr.getAnnotation(Id.class) != null || attr.getAnnotation(EmbeddedId.class) != null) {
                pkElements.add(attr);
                attr.setPrimaryKeyAttribute(true);
                boolean attrGenerated = attr.getAnnotation(GeneratedValue.class) != null;
                if (pkElements.size() == 1) {
                    generated = attrGenerated;
                } else if (generated != attrGenerated) {
                    throw new IllegalStateException("cannot mix generated and not-generated primary key elements for " + meta.getId());
                }
            }
        }
        if (!pkElements.isEmpty()) {
            MetaPrimaryKey primaryKey = new MetaPrimaryKey();
            primaryKey.setName(meta.getName() + "$primaryKey");
            primaryKey.setElements(pkElements);
            primaryKey.setUnique(true);
            primaryKey.setParent(meta, true);
            primaryKey.setGenerated(generated);
            meta.setPrimaryKey(primaryKey);
            if (pkElements.size() == 1) {
                // single pk element cannot be nullable
                MetaAttribute pkElement = pkElements.get(0);
                pkElement.setNullable(false);
            }
        }
    }
}
Also used : MetaPrimaryKey(io.crnk.meta.model.MetaPrimaryKey) ArrayList(java.util.ArrayList) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 28 with MetaAttribute

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

the class JpaResourceInformationProviderTest method testReadOnlyField.

@Test
public void testReadOnlyField() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    ResourceInformation info = builder.build(AnnotationTestEntity.class);
    ResourceField field = info.findAttributeFieldByName("readOnlyValue");
    Assert.assertFalse(field.getAccess().isPostable());
    Assert.assertFalse(field.getAccess().isPatchable());
    MetaDataObject meta = jpaMetaProvider.discoverMeta(AnnotationTestEntity.class).asDataObject();
    MetaAttribute attribute = meta.getAttribute("readOnlyValue");
    Assert.assertFalse(attribute.isInsertable());
    Assert.assertFalse(attribute.isUpdatable());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) Test(org.junit.Test)

Example 29 with MetaAttribute

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

the class MethodAnnotatedEntityTest method testMeta.

@Test
public void testMeta() {
    MethodAnnotatedEntity entity = new MethodAnnotatedEntity();
    entity.setId(13L);
    entity.setStringValue("test");
    MetaEntity meta = jpaMetaProvider.getMeta(MethodAnnotatedEntity.class);
    MetaKey primaryKey = meta.getPrimaryKey();
    Assert.assertNotNull(primaryKey);
    Assert.assertEquals(1, primaryKey.getElements().size());
    MetaAttribute stringValueAttr = meta.getAttribute("stringValue");
    Assert.assertNotNull(stringValueAttr);
    Assert.assertEquals("stringValue", stringValueAttr.getName());
    Assert.assertEquals("test", stringValueAttr.getValue(entity));
    MetaAttribute idAttr = meta.getAttribute("id");
    Assert.assertNotNull(idAttr);
    Assert.assertEquals("id", idAttr.getName());
    Assert.assertEquals(13L, idAttr.getValue(entity));
}
Also used : MethodAnnotatedEntity(io.crnk.jpa.model.MethodAnnotatedEntity) MetaKey(io.crnk.meta.model.MetaKey) MetaEntity(io.crnk.jpa.meta.MetaEntity) MetaAttribute(io.crnk.meta.model.MetaAttribute) Test(org.junit.Test)

Example 30 with MetaAttribute

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

the class JpaMetaEndToEndTest method testAttributeInsertableUpdatable.

@Test
public void testAttributeInsertableUpdatable() {
    MetaResource versionMeta = resourceMetaProvider.getMeta(VersionedEntity.class);
    MetaAttribute idAttr = versionMeta.getAttribute("id");
    MetaAttribute valueAttr = versionMeta.getAttribute("longValue");
    Assert.assertTrue(idAttr.isInsertable());
    Assert.assertFalse(idAttr.isUpdatable());
    Assert.assertTrue(valueAttr.isInsertable());
    Assert.assertTrue(valueAttr.isUpdatable());
    MetaResourceBase annotationMeta = resourceMetaProvider.getMeta(AnnotationTestEntity.class);
    MetaAttribute fieldAnnotatedAttr = annotationMeta.getAttribute("fieldAnnotatedValue");
    MetaAttribute columnAnnotatedAttr = annotationMeta.getAttribute("columnAnnotatedValue");
    Assert.assertTrue(fieldAnnotatedAttr.isInsertable());
    Assert.assertFalse(fieldAnnotatedAttr.isUpdatable());
    Assert.assertFalse(fieldAnnotatedAttr.isSortable());
    Assert.assertFalse(fieldAnnotatedAttr.isFilterable());
    Assert.assertFalse(columnAnnotatedAttr.isInsertable());
    Assert.assertTrue(columnAnnotatedAttr.isUpdatable());
    Assert.assertTrue(columnAnnotatedAttr.isSortable());
    Assert.assertTrue(columnAnnotatedAttr.isFilterable());
    MetaAttribute embeddableValueAttr = annotationMeta.getAttribute("embeddableValue");
    Assert.assertFalse(embeddableValueAttr.isInsertable());
    Assert.assertTrue(embeddableValueAttr.isUpdatable());
    Assert.assertTrue(embeddableValueAttr.isSortable());
    Assert.assertFalse(embeddableValueAttr.isFilterable());
    MetaResourceBase superMeta = resourceMetaProvider.getMeta(AnnotationMappedSubtypeEntity.class);
    fieldAnnotatedAttr = superMeta.getAttribute("fieldAnnotatedValue");
    columnAnnotatedAttr = superMeta.getAttribute("columnAnnotatedValue");
    MetaAttribute lobAttr = superMeta.getAttribute("lobValue");
    Assert.assertTrue(fieldAnnotatedAttr.isInsertable());
    Assert.assertFalse(fieldAnnotatedAttr.isUpdatable());
    Assert.assertFalse(fieldAnnotatedAttr.isSortable());
    Assert.assertFalse(fieldAnnotatedAttr.isFilterable());
    Assert.assertFalse(columnAnnotatedAttr.isInsertable());
    Assert.assertTrue(columnAnnotatedAttr.isUpdatable());
    Assert.assertTrue(columnAnnotatedAttr.isSortable());
    Assert.assertTrue(columnAnnotatedAttr.isFilterable());
    Assert.assertTrue(lobAttr.isInsertable());
    Assert.assertTrue(lobAttr.isUpdatable());
    Assert.assertFalse(lobAttr.isSortable());
    Assert.assertFalse(lobAttr.isFilterable());
}
Also used : MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaResourceBase(io.crnk.meta.model.resource.MetaResourceBase) Test(org.junit.Test) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest)

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