Search in sources :

Example 1 with MetaKey

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

the class TSMetaDataObjectTransformation method generateResourceFields.

private static void generateResourceFields(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject meta) {
    TSInterfaceType attributesType = new TSInterfaceType();
    attributesType.setName(ATTRIBUTES_CLASS_NAME);
    attributesType.setExported(true);
    TSInterfaceType relationshipsType = new TSInterfaceType();
    relationshipsType.setName(RELATIONSHIPS_CLASS_NAME);
    relationshipsType.setExported(true);
    TSIndexSignature relationshipsIndexSignature = new TSIndexSignature();
    relationshipsIndexSignature.setKeyType(TSPrimitiveType.STRING);
    relationshipsIndexSignature.setValueType(NgrxJsonApiLibrary.RESOURCE_RELATIONSHIP);
    relationshipsIndexSignature.setParent(relationshipsType);
    relationshipsType.setIndexSignature(relationshipsIndexSignature);
    // TODO remo: interface support
    MetaKey primaryKey = meta.getPrimaryKey();
    for (MetaAttribute attr : meta.getDeclaredAttributes()) {
        if (primaryKey != null && primaryKey.getUniqueElement().equals(attr)) {
            continue;
        }
        generateResourceField(attr, context, interfaceType, attributesType, relationshipsType);
    }
    if (!isEmpty(relationshipsType)) {
        TSModule module = TypescriptUtils.getNestedTypeContainer(interfaceType, true);
        module.getElements().add(relationshipsType);
        relationshipsType.setParent(module);
        TSField relationshipsField = new TSField();
        relationshipsField.setName("relationships");
        relationshipsField.setType(relationshipsType);
        relationshipsField.setNullable(true);
        interfaceType.getDeclaredMembers().add(relationshipsField);
    }
    if (!isEmpty(attributesType)) {
        TSModule module = TypescriptUtils.getNestedTypeContainer(interfaceType, true);
        module.getElements().add(attributesType);
        attributesType.setParent(module);
        TSField attributesField = new TSField();
        attributesField.setName("attributes");
        attributesField.setType(attributesType);
        attributesField.setNullable(true);
        interfaceType.getDeclaredMembers().add(attributesField);
    }
}
Also used : TSIndexSignature(io.crnk.gen.typescript.model.TSIndexSignature) TSField(io.crnk.gen.typescript.model.TSField) MetaKey(io.crnk.meta.model.MetaKey) TSModule(io.crnk.gen.typescript.model.TSModule) MetaAttribute(io.crnk.meta.model.MetaAttribute) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 2 with MetaKey

use of io.crnk.meta.model.MetaKey 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 3 with MetaKey

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

the class MetaRelationshipRepositoryImplTest method findOneTargetReturnsNull.

@Test
public void findOneTargetReturnsNull() {
    MetaResource resource = resourceProvider.getMeta(Task.class);
    resource.setPrimaryKey(null);
    MetaKey key = (MetaKey) repo.findOneTarget(resource.getId(), "primaryKey", new QuerySpec(MetaElement.class));
    Assert.assertNull(key);
}
Also used : MetaKey(io.crnk.meta.model.MetaKey) MetaResource(io.crnk.meta.model.resource.MetaResource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 4 with MetaKey

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

the class JpaRepositoryUtils method getPrimaryKeyAttr.

/**
 * @param meta of the entity
 * @return Gets the primary key attribute of the given entity. Assumes a
 * primary key is available and no compound primary keys are
 * supported.
 */
public static MetaAttribute getPrimaryKeyAttr(MetaDataObject meta) {
    MetaKey primaryKey = meta.getPrimaryKey();
    PreconditionUtil.assertNotNull("no primary key", primaryKey);
    PreconditionUtil.assertEquals("non-compound primary key expected", 1, primaryKey.getElements().size());
    return primaryKey.getElements().get(0);
}
Also used : MetaKey(io.crnk.meta.model.MetaKey)

Example 5 with MetaKey

use of io.crnk.meta.model.MetaKey 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)

Aggregations

MetaKey (io.crnk.meta.model.MetaKey)12 MetaAttribute (io.crnk.meta.model.MetaAttribute)7 Test (org.junit.Test)6 MetaEntity (io.crnk.jpa.meta.MetaEntity)3 MetaResource (io.crnk.meta.model.resource.MetaResource)3 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 MetaLookup (io.crnk.meta.MetaLookup)2 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 TSField (io.crnk.gen.typescript.model.TSField)1 TSIndexSignature (io.crnk.gen.typescript.model.TSIndexSignature)1 TSInterfaceType (io.crnk.gen.typescript.model.TSInterfaceType)1 TSModule (io.crnk.gen.typescript.model.TSModule)1 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)1 JpaResource (io.crnk.jpa.annotations.JpaResource)1 MetaJpaDataObject (io.crnk.jpa.meta.MetaJpaDataObject)1 MethodAnnotatedEntity (io.crnk.jpa.model.MethodAnnotatedEntity)1 MetaDataObject (io.crnk.meta.model.MetaDataObject)1 MetaElement (io.crnk.meta.model.MetaElement)1