Search in sources :

Example 1 with MetaEntity

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;
}
Also used : MetaKey(io.crnk.meta.model.MetaKey) MetaElement(io.crnk.meta.model.MetaElement) MetaEntity(io.crnk.jpa.meta.MetaEntity) JpaResource(io.crnk.jpa.annotations.JpaResource) MetaJpaDataObject(io.crnk.jpa.meta.MetaJpaDataObject)

Example 2 with MetaEntity

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));
        }
    }
}
Also used : MetaEntity(io.crnk.jpa.meta.MetaEntity) Test(org.junit.Test)

Example 3 with MetaEntity

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"));
}
Also used : MetaEntity(io.crnk.jpa.meta.MetaEntity) Test(org.junit.Test)

Example 4 with MetaEntity

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));
}
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 5 with MetaEntity

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());
    }
}
Also used : DefaultPagedMetaInformation(io.crnk.core.resource.meta.DefaultPagedMetaInformation) Serializable(java.io.Serializable) MetaEntity(io.crnk.jpa.meta.MetaEntity)

Aggregations

MetaEntity (io.crnk.jpa.meta.MetaEntity)6 MetaKey (io.crnk.meta.model.MetaKey)3 Test (org.junit.Test)3 MetaAttribute (io.crnk.meta.model.MetaAttribute)2 DefaultPagedMetaInformation (io.crnk.core.resource.meta.DefaultPagedMetaInformation)1 JpaResource (io.crnk.jpa.annotations.JpaResource)1 MetaJpaDataObject (io.crnk.jpa.meta.MetaJpaDataObject)1 MethodAnnotatedEntity (io.crnk.jpa.model.MethodAnnotatedEntity)1 MetaElement (io.crnk.meta.model.MetaElement)1 Serializable (java.io.Serializable)1