use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class MetaDataObjectProviderBase method createAttributes.
protected void createAttributes(T meta) {
Class<?> implClass = meta.getImplementationClass();
List<Field> fields = ClassUtils.getClassFields(implClass);
List<Method> getters = ClassUtils.getClassGetters(implClass);
Map<String, Field> fieldMap = toFieldMap(fields);
Map<String, Method> getterMap = toGetterMethodMap(getters);
List<String> propertyNames = getOrderedPropertyNames(fields, getters, fieldMap);
for (String name : propertyNames) {
Method getterMethod = getterMap.get(name);
if (getterMethod == null) {
continue;
}
Method setterMethod = ClassUtils.findSetter(implClass, name, getterMethod.getReturnType());
if (getterMethod.getDeclaringClass() != implClass) {
// contained in super type
continue;
}
MetaAttribute attribute = createAttribute(meta, MetaUtils.firstToLower(name));
attribute.setReadMethod(getterMethod);
attribute.setWriteMethod(setterMethod);
attribute.setSortable(true);
attribute.setFilterable(true);
if (setterMethod != null) {
attribute.setInsertable(true);
attribute.setUpdatable(true);
}
initAttribute(attribute);
}
}
use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class JpaRelationshipRepository method removeRelations.
@Override
public void removeRelations(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.removeValue(sourceEntity, target);
if (target != null && oppositeAttrMeta != null) {
if (oppositeAttrMeta.getType().isCollection()) {
oppositeAttrMeta.removeValue(target, sourceEntity);
} else {
oppositeAttrMeta.setValue(target, null);
}
}
}
}
use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class JpaRelationshipRepository method setRelations.
@Override
public void setRelations(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();
Collection<Object> targets = attrMeta.getType().asCollection().newInstance();
for (J targetId : targetIds) {
Object target = em.find(targetType, targetId);
targets.add(target);
}
// detach current
if (oppositeAttrMeta != null) {
Collection<?> col = (Collection<?>) attrMeta.getValue(sourceEntity);
Iterator<?> iterator = col.iterator();
while (iterator.hasNext()) {
Object prevTarget = iterator.next();
iterator.remove();
if (oppositeAttrMeta.getType().isCollection()) {
oppositeAttrMeta.removeValue(prevTarget, sourceEntity);
} else {
oppositeAttrMeta.setValue(prevTarget, null);
}
}
}
// attach new targets
for (Object target : targets) {
if (oppositeAttrMeta != null) {
if (oppositeAttrMeta.getType().isCollection()) {
oppositeAttrMeta.addValue(target, sourceEntity);
} else {
oppositeAttrMeta.setValue(target, sourceEntity);
}
em.persist(target);
}
}
attrMeta.setValue(sourceEntity, targets);
}
use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class QuerydslQueryBackend method getParentIdExpression.
private Expression<Object> getParentIdExpression(MetaDataObject parentMeta, MetaAttribute parentAttr) {
MetaKey primaryKey = parentMeta.getPrimaryKey();
PreconditionUtil.assertNotNull("no primary key specified for parentAttribute " + parentAttr.getId(), parentMeta);
List<MetaAttribute> elements = primaryKey.getElements();
PreconditionUtil.assertEquals("composite primary keys not supported yet", 1, elements.size());
MetaAttribute primaryKeyAttr = elements.get(0);
return QuerydslUtils.get(parentFrom, primaryKeyAttr.getName());
}
use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class JpaMetaEndToEndTest method testProjectedVersion.
@Test
public void testProjectedVersion() {
MetaResource metaResource = resourceMetaProvider.getMeta(VersionedEntity.class);
MetaAttribute versionAttr = metaResource.getAttribute("version");
Assert.assertTrue(versionAttr.isVersion());
}
Aggregations