use of io.crnk.jpa.meta.MetaEntity in project crnk-framework by crnk-project.
the class JpaResourceInformationProvider method accept.
@Override
public boolean accept(Class<?> resourceClass) {
// needs to be configured for being exposed
if (resourceClass.getAnnotation(JpaResource.class) != null) {
return true;
}
if (JpaMetaUtils.isJpaType(resourceClass)) {
// needs to be an entity
MetaElement meta = metaProvider.discoverMeta(resourceClass);
if (meta instanceof MetaEntity) {
MetaEntity metaEntity = (MetaEntity) meta;
MetaKey primaryKey = metaEntity.getPrimaryKey();
return primaryKey != null && primaryKey.getElements().size() == 1;
} else {
// note that DTOs cannot be handled here
return meta instanceof MetaJpaDataObject;
}
}
return false;
}
use of io.crnk.jpa.meta.MetaEntity in project crnk-framework by crnk-project.
the class AbstractInheritanceTest method testOrderBySubtypeAttribute.
@Test
public void testOrderBySubtypeAttribute() {
// FIXME subtype lookup
Assert.assertTrue(module.getJpaMetaProvider().getMeta(childClass) instanceof MetaEntity);
List<B> list = baseBuilder().addSortBy(Arrays.asList("intValue"), Direction.DESC).buildExecutor().getResultList();
Assert.assertEquals(10, list.size());
for (int i = 0; i < 10; i++) {
B entity = list.get(i);
MetaEntity meta = module.getJpaMetaProvider().getMeta(entity.getClass());
if (i < 5) {
Assert.assertTrue(childClass.isInstance(entity));
Assert.assertEquals(4 - i, meta.getAttribute("intValue").getValue(entity));
} else {
Assert.assertFalse(childClass.isInstance(entity));
// order by primary key by default second order criteria
Assert.assertEquals(Long.valueOf(i - 5), meta.getAttribute("id").getValue(entity));
}
}
}
use of io.crnk.jpa.meta.MetaEntity in project crnk-framework by crnk-project.
the class AbstractInheritanceTest method testMeta.
@Test
public void testMeta() {
MetaEntity baseMeta = module.getJpaMetaProvider().getMeta(baseClass);
MetaEntity childMeta = module.getJpaMetaProvider().getMeta(childClass);
Assert.assertSame(baseMeta, childMeta.getSuperType());
Assert.assertEquals(1, childMeta.getDeclaredAttributes().size());
Assert.assertEquals(2, baseMeta.getAttributes().size());
Assert.assertEquals(3, childMeta.getAttributes().size());
Assert.assertNotNull(baseMeta.getAttribute("id"));
Assert.assertNotNull(baseMeta.getAttribute("stringValue"));
try {
Assert.assertNull(baseMeta.getAttribute("intValue"));
Assert.fail();
} catch (Exception e) {
// ok
}
Assert.assertNotNull(childMeta.getAttribute("id"));
Assert.assertNotNull(childMeta.getAttribute("stringValue"));
Assert.assertNotNull(childMeta.getAttribute("intValue"));
}
use of io.crnk.jpa.meta.MetaEntity 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.jpa.meta.MetaEntity in project crnk-framework by crnk-project.
the class JpaModule method setupRepository.
private void setupRepository(JpaRepositoryConfig<?> repositoryConfig) {
if (repositoryConfig.getListMetaClass() == DefaultPagedMetaInformation.class && !isTotalResourceCountUsed()) {
// TODO not that nice...
repositoryConfig.setListMetaClass(DefaultHasMoreResourcesMetaInformation.class);
}
Class<?> resourceClass = repositoryConfig.getResourceClass();
MetaEntity metaEntity = jpaMetaProvider.getMeta(repositoryConfig.getEntityClass());
if (isValidEntity(metaEntity)) {
JpaRepositoryFactory repositoryFactory = config.getRepositoryFactory();
JpaEntityRepository<?, Serializable> jpaRepository = repositoryFactory.createEntityRepository(this, repositoryConfig);
ResourceRepositoryV2<?, ?> repository = filterResourceCreation(resourceClass, jpaRepository);
context.addRepository(repository);
setupRelationshipRepositories(resourceClass, repositoryConfig.getResourceClass() != repositoryConfig.getEntityClass());
}
}
Aggregations