Search in sources :

Example 1 with MetaJpaDataObject

use of io.crnk.jpa.meta.MetaJpaDataObject 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 2 with MetaJpaDataObject

use of io.crnk.jpa.meta.MetaJpaDataObject in project crnk-framework by crnk-project.

the class JpaMetaFilter method onInitialized.

@Override
public void onInitialized(MetaElement element) {
    if (element.getParent() instanceof MetaJpaDataObject && element instanceof MetaAttribute) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaDataObject parent = attr.getParent();
        Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName());
        MetaType metaType = (MetaType) partition.allocateMetaElement(implementationType).get();
        attr.setType(metaType);
    }
    if (element.getParent() instanceof MetaJpaDataObject && element instanceof MetaAttribute && ((MetaAttribute) element).getOppositeAttribute() == null) {
        MetaAttribute attr = (MetaAttribute) element;
        String mappedBy = getMappedBy(attr);
        if (mappedBy != null) {
            MetaType attrType = attr.getType();
            MetaDataObject oppositeType = attrType.getElementType().asDataObject();
            if (!mappedBy.contains(".")) {
                MetaAttribute oppositeAttr = oppositeType.getAttribute(mappedBy);
                attr.setOppositeAttribute(oppositeAttr);
            } else {
            // references within embeddables not yet supported
            }
        }
    }
}
Also used : Type(java.lang.reflect.Type) MetaType(io.crnk.meta.model.MetaType) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaJpaDataObject(io.crnk.jpa.meta.MetaJpaDataObject) MetaType(io.crnk.meta.model.MetaType)

Example 3 with MetaJpaDataObject

use of io.crnk.jpa.meta.MetaJpaDataObject in project crnk-framework by crnk-project.

the class JpaMetaEnricher method onInitialized.

@Override
public void onInitialized(MetaElement element) {
    if (element instanceof MetaJsonObject) {
        MetaJsonObject jsonDataObject = (MetaJsonObject) element;
        Class<?> implementationClass = jsonDataObject.getImplementationClass();
        if (metaProvider.hasMeta(implementationClass)) {
            MetaJpaDataObject jpaDataObject = metaProvider.getMeta(implementationClass);
            if (jpaDataObject.getPrimaryKey() != null && jsonDataObject.getPrimaryKey() != null) {
                jsonDataObject.getPrimaryKey().setGenerated(jpaDataObject.getPrimaryKey().isGenerated());
            }
            List<? extends MetaAttribute> declaredAttributes = jsonDataObject.getDeclaredAttributes();
            for (MetaAttribute declaredAttribute : declaredAttributes) {
                String name = declaredAttribute.getName();
                if (jpaDataObject.hasAttribute(name)) {
                    MetaAttribute jpaAttribute = jpaDataObject.getAttribute(name);
                    declaredAttribute.setLob(jpaAttribute.isLob());
                    declaredAttribute.setVersion(jpaAttribute.isVersion());
                    declaredAttribute.setNullable(jpaAttribute.isNullable());
                    declaredAttribute.setCascaded(jpaAttribute.isCascaded());
                }
            }
        }
    }
}
Also used : MetaJsonObject(io.crnk.meta.model.resource.MetaJsonObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaJpaDataObject(io.crnk.jpa.meta.MetaJpaDataObject)

Aggregations

MetaJpaDataObject (io.crnk.jpa.meta.MetaJpaDataObject)3 MetaAttribute (io.crnk.meta.model.MetaAttribute)2 JpaResource (io.crnk.jpa.annotations.JpaResource)1 MetaEntity (io.crnk.jpa.meta.MetaEntity)1 MetaDataObject (io.crnk.meta.model.MetaDataObject)1 MetaElement (io.crnk.meta.model.MetaElement)1 MetaKey (io.crnk.meta.model.MetaKey)1 MetaType (io.crnk.meta.model.MetaType)1 MetaJsonObject (io.crnk.meta.model.resource.MetaJsonObject)1 Type (java.lang.reflect.Type)1