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());
}
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);
}
}
}
}
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());
}
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));
}
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());
}
Aggregations