Search in sources :

Example 11 with MetaAttribute

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);
    }
}
Also used : Field(java.lang.reflect.Field) MetaAttribute(io.crnk.meta.model.MetaAttribute) Method(java.lang.reflect.Method)

Example 12 with MetaAttribute

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);
            }
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 13 with MetaAttribute

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);
}
Also used : EntityManager(javax.persistence.EntityManager) Collection(java.util.Collection) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 14 with MetaAttribute

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());
}
Also used : MetaKey(io.crnk.meta.model.MetaKey) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 15 with MetaAttribute

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());
}
Also used : MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) Test(org.junit.Test) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest)

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