Search in sources :

Example 46 with MetaAttribute

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

the class QueryBuilder method applyFilterSpec.

protected void applyFilterSpec() {
    QueryFilterBuilder<P, F> predicateBuilder = new QueryFilterBuilder<>(backend, attributeFinder);
    MetaDataObject meta = query.getMeta();
    List<FilterSpec> filters = query.getFilterSpecs();
    List<P> predicates = predicateBuilder.filterSpecListToPredicateArray(meta, backend.getRoot(), filters);
    if (predicates != null && !predicates.isEmpty()) {
        backend.addPredicate(backend.and(predicates));
    }
    MetaAttribute parentAttr = query.getParentAttr();
    if (parentAttr != null) {
        MetaDataObject parentMeta = query.getParentMeta();
        MetaKey primaryKey = parentMeta.getPrimaryKey();
        if (primaryKey == null) {
            throw new IllegalStateException("primary key not found for " + parentAttr.getId());
        }
        MetaAttribute primaryKeyAttr = primaryKey.getUniqueElement();
        backend.addParentPredicate(primaryKeyAttr);
    }
}
Also used : MetaKey(io.crnk.meta.model.MetaKey) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Example 47 with MetaAttribute

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

the class JpaCriteriaQueryBackend method getParentIdExpression.

private Expression<?> getParentIdExpression(MetaAttribute parentAttr) {
    MetaEntity parentEntity = (MetaEntity) parentAttr.getParent();
    MetaKey primaryKey = parentEntity.getPrimaryKey();
    List<MetaAttribute> elements = primaryKey.getElements();
    PreconditionUtil.assertFalse("composite primary keys not supported yet", elements.size() != 1);
    MetaAttribute primaryKeyAttr = elements.get(0);
    return parentFrom.get(primaryKeyAttr.getName());
}
Also used : MetaKey(io.crnk.meta.model.MetaKey) MetaEntity(io.crnk.jpa.meta.MetaEntity) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 48 with MetaAttribute

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

the class JpaRelationshipRepository method addRelations.

@Override
public void addRelations(S source, Iterable<J> targetIds, String fieldName) {
    MetaAttribute attrMeta = entityMeta.getAttribute(fieldName);
    MetaAttribute oppositeAttrMeta = attrMeta.getOppositeAttribute();
    Class<?> targetType = getElementType(attrMeta);
    Object sourceEntity = sourceMapper.unmap(source);
    EntityManager em = module.getEntityManager();
    for (J targetId : targetIds) {
        Object target = em.find(targetType, targetId);
        attrMeta.addValue(sourceEntity, target);
        if (oppositeAttrMeta != null) {
            if (oppositeAttrMeta.getType().isCollection()) {
                oppositeAttrMeta.addValue(target, sourceEntity);
            } else {
                oppositeAttrMeta.setValue(target, sourceEntity);
            }
            em.persist(target);
        }
    }
    em.persist(sourceEntity);
}
Also used : EntityManager(javax.persistence.EntityManager) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 49 with MetaAttribute

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

the class JpaRelationshipRepository method setRelation.

@Override
public void setRelation(S source, J targetId, String fieldName) {
    MetaAttribute attrMeta = entityMeta.getAttribute(fieldName);
    MetaAttribute oppositeAttrMeta = attrMeta.getOppositeAttribute();
    Class<?> targetType = getElementType(attrMeta);
    Object sourceEntity = sourceMapper.unmap(source);
    EntityManager em = module.getEntityManager();
    Object target = targetId != null ? em.find(targetType, targetId) : null;
    attrMeta.setValue(sourceEntity, target);
    if (target != null && oppositeAttrMeta != null) {
        if (oppositeAttrMeta.getType().isCollection()) {
            oppositeAttrMeta.addValue(target, sourceEntity);
        } else {
            oppositeAttrMeta.setValue(target, sourceEntity);
        }
        em.persist(target);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 50 with MetaAttribute

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

the class MetaDefaultLimitIntTest method limitShouldNotAffectRelationshipsWithSpecOnRelationship.

@Test
public void limitShouldNotAffectRelationshipsWithSpecOnRelationship() {
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.includeRelation(Arrays.asList("attributes"));
    querySpec.addFilter(new FilterSpec(Arrays.asList("resourceType"), FilterOperator.EQ, "tasks"));
    querySpec.getOrCreateQuerySpec(MetaAttribute.class).addSort(new SortSpec(Arrays.asList("id"), Direction.ASC));
    querySpec.getOrCreateQuerySpec(MetaElement.class).addSort(new SortSpec(Arrays.asList("id"), Direction.ASC));
    querySpec.getOrCreateQuerySpec(MetaResourceField.class).addSort(new SortSpec(Arrays.asList("id"), Direction.ASC));
    ResourceList<MetaResource> list = repository.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    MetaResource taskMeta = list.get(0);
    List<? extends MetaAttribute> attributes = taskMeta.getAttributes();
    Assert.assertTrue(attributes.size() > 5);
}
Also used : MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) MetaElement(io.crnk.meta.model.MetaElement) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) SortSpec(io.crnk.core.queryspec.SortSpec) 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