Search in sources :

Example 41 with MetaAttribute

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

the class JpaMetaProviderAttributeTest method testRelationMany.

@Test
public void testRelationMany() {
    MetaEntity meta = metaProvider.discoverMeta(TestEntity.class);
    MetaAttribute attr = meta.getAttribute(TestEntity.ATTR_manyRelatedValues);
    Assert.assertTrue(attr.isAssociation());
    Assert.assertEquals(TestEntity.ATTR_manyRelatedValues, attr.getName());
    Assert.assertEquals(TestEntity.class.getName() + "." + TestEntity.ATTR_manyRelatedValues, attr.getId());
    Assert.assertFalse(attr.isDerived());
    Assert.assertFalse(attr.isVersion());
    Assert.assertTrue(attr.isLazy());
    Assert.assertNotNull(attr.getOppositeAttribute());
    MetaCollectionType colType = attr.getType().asCollection();
    Assert.assertTrue(colType.isCollection());
    Assert.assertEquals(RelatedEntity.class, colType.getElementType().getImplementationClass());
    Assert.assertEquals(RelatedEntity.class, attr.getType().getElementType().getImplementationClass());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) WriteOnlyAttributeTestEntity(io.crnk.jpa.model.WriteOnlyAttributeTestEntity) NamingTestEntity(io.crnk.jpa.model.NamingTestEntity) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaCollectionType(io.crnk.meta.model.MetaCollectionType) Test(org.junit.Test)

Example 42 with MetaAttribute

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

the class ValidationMetaProvider method getFilters.

@Override
public Collection<MetaFilter> getFilters() {
    return Arrays.asList((MetaFilter) new MetaFilterBase() {

        @Override
        public void onInitialized(MetaElement element) {
            if (element instanceof MetaAttribute) {
                MetaAttribute attr = (MetaAttribute) element;
                NotNull notNull = attr.getAnnotation(NotNull.class);
                if (notNull != null) {
                    attr.setNullable(false);
                }
            }
        }
    });
}
Also used : MetaElement(io.crnk.meta.model.MetaElement) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaFilterBase(io.crnk.meta.provider.MetaFilterBase) NotNull(javax.validation.constraints.NotNull)

Example 43 with MetaAttribute

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

the class ValidationMetaProviderTest method testNotNullNotDisabledWithoutValidationProvider.

@Test
public void testNotNullNotDisabledWithoutValidationProvider() {
    setup(false);
    MetaResourceBase meta = resourceMetaProvider.getMeta(Task.class);
    MetaAttribute attr = meta.getAttribute("name");
    Assert.assertTrue(attr.isNullable());
}
Also used : MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaResourceBase(io.crnk.meta.model.resource.MetaResourceBase) Test(org.junit.Test)

Example 44 with MetaAttribute

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

the class AnyUtils method setValue.

/**
 * Sets the value of the given anytype.
 *
 * @param partition to use to retrieve information
 * @param dataObject the anytype for which the value is set.
 * @param value      the new value
 */
public static void setValue(MetaPartition partition, AnyTypeObject dataObject, Object value) {
    MetaDataObject meta = (MetaDataObject) partition.getMeta(dataObject.getClass());
    if (value == null) {
        for (MetaAttribute attr : meta.getAttributes()) {
            attr.setValue(dataObject, null);
        }
    } else {
        boolean found = false;
        for (MetaAttribute attr : meta.getAttributes()) {
            if (attr.getName().equals(TYPE_ATTRIBUTE)) {
                continue;
            }
            if (attr.getType().getImplementationClass().isAssignableFrom(value.getClass())) {
                attr.setValue(dataObject, value);
                found = true;
            } else {
                attr.setValue(dataObject, null);
            }
        }
        if (!found) {
            throw new IllegalStateException("cannot assign " + value + " to " + dataObject);
        }
    }
}
Also used : MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 45 with MetaAttribute

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

the class JoinRegistry method getOrCreateJoin.

public F getOrCreateJoin(MetaAttributePath path) {
    if (path.length() == 0)
        return backend.getRoot();
    MetaAttributePath subPath = new MetaAttributePath();
    F from = backend.getRoot();
    for (int i = 0; i < path.length(); i++) {
        MetaAttribute pathElement = path.getElement(i);
        from = getOrCreateJoin(subPath, pathElement);
        subPath = subPath.concat(pathElement);
    }
    return from;
}
Also used : MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaAttributePath(io.crnk.meta.model.MetaAttributePath)

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