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