Search in sources :

Example 1 with MetaPrimaryKey

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

the class MetaEntityTest method testPrimaryKeyOnMappedSuperClass.

@Test
public void testPrimaryKeyOnMappedSuperClass() {
    MetaMappedSuperclass meta = metaProvider.discoverMeta(TestMappedSuperclassWithPk.class);
    MetaPrimaryKey primaryKey = meta.getPrimaryKey();
    Assert.assertNotNull(primaryKey);
    Assert.assertEquals(1, primaryKey.getElements().size());
    Assert.assertEquals("id", primaryKey.getElements().get(0).getName());
    Assert.assertTrue(primaryKey.getElements().get(0).isPrimaryKeyAttribute());
    Assert.assertFalse(primaryKey.isGenerated());
}
Also used : MetaPrimaryKey(io.crnk.meta.model.MetaPrimaryKey) Test(org.junit.Test)

Example 2 with MetaPrimaryKey

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

the class MetaEntityTest method testPrimaryKeyOnParentMappedSuperClass.

@Test
public void testPrimaryKeyOnParentMappedSuperClass() {
    MetaEntity meta = metaProvider.discoverMeta(TestSubclassWithSuperclassPk.class);
    MetaPrimaryKey primaryKey = meta.getPrimaryKey();
    Assert.assertNotNull(primaryKey);
    Assert.assertEquals(1, primaryKey.getElements().size());
    Assert.assertEquals("id", primaryKey.getElements().get(0).getName());
    Assert.assertTrue(primaryKey.getElements().get(0).isPrimaryKeyAttribute());
    Assert.assertFalse(primaryKey.isGenerated());
}
Also used : MetaPrimaryKey(io.crnk.meta.model.MetaPrimaryKey) Test(org.junit.Test)

Example 3 with MetaPrimaryKey

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

the class AbstractEntityMetaFactory method setKey.

private void setKey(T meta) {
    if (meta.getPrimaryKey() == null) {
        boolean generated = false;
        ArrayList<MetaAttribute> pkElements = new ArrayList<>();
        for (MetaAttribute attr : meta.getAttributes()) {
            if (attr.getAnnotation(Id.class) != null || attr.getAnnotation(EmbeddedId.class) != null) {
                pkElements.add(attr);
                attr.setPrimaryKeyAttribute(true);
                boolean attrGenerated = attr.getAnnotation(GeneratedValue.class) != null;
                if (pkElements.size() == 1) {
                    generated = attrGenerated;
                } else if (generated != attrGenerated) {
                    throw new IllegalStateException("cannot mix generated and not-generated primary key elements for " + meta.getId());
                }
            }
        }
        if (!pkElements.isEmpty()) {
            MetaPrimaryKey primaryKey = new MetaPrimaryKey();
            primaryKey.setName(meta.getName() + "$primaryKey");
            primaryKey.setElements(pkElements);
            primaryKey.setUnique(true);
            primaryKey.setParent(meta, true);
            primaryKey.setGenerated(generated);
            meta.setPrimaryKey(primaryKey);
            if (pkElements.size() == 1) {
                // single pk element cannot be nullable
                MetaAttribute pkElement = pkElements.get(0);
                pkElement.setNullable(false);
            }
        }
    }
}
Also used : MetaPrimaryKey(io.crnk.meta.model.MetaPrimaryKey) ArrayList(java.util.ArrayList) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 4 with MetaPrimaryKey

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

the class MetaEntityTest method testGeneratedPrimaryKey.

@Test
public void testGeneratedPrimaryKey() {
    MetaDataObject meta = metaProvider.discoverMeta(SequenceEntity.class).asDataObject();
    MetaPrimaryKey primaryKey = meta.getPrimaryKey();
    Assert.assertNotNull(primaryKey);
    Assert.assertEquals(1, primaryKey.getElements().size());
    Assert.assertEquals("id", primaryKey.getElements().get(0).getName());
    Assert.assertTrue(primaryKey.isGenerated());
}
Also used : SequenceEntity(io.crnk.jpa.model.SequenceEntity) MetaPrimaryKey(io.crnk.meta.model.MetaPrimaryKey) MetaDataObject(io.crnk.meta.model.MetaDataObject) Test(org.junit.Test)

Example 5 with MetaPrimaryKey

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

the class ResourceMetaFilter method onInitialized.

@Override
public void onInitialized(MetaElement element) {
    if (element instanceof MetaResourceBase) {
        MetaResourceBase metaResource = (MetaResourceBase) element;
        ResourceInformation information = getResourceInformation(metaResource, true);
        PreconditionUtil.assertNotNull(information.getResourceType(), metaResource);
        for (ResourceField field : information.getRelationshipFields()) {
            if (field.getOppositeName() != null) {
                String oppositeType = field.getOppositeResourceType();
                MetaResource oppositeMeta = (MetaResource) context.getMetaElement(partition.getId(oppositeType)).get();
                MetaAttribute attr = metaResource.getAttribute(field.getUnderlyingName());
                MetaAttribute oppositeAttr = oppositeMeta.getAttribute(field.getOppositeName());
                PreconditionUtil.assertNotNull(attr.getId() + " opposite not found", oppositeAttr);
                attr.setOppositeAttribute(oppositeAttr);
            }
        }
        ResourceField idField = information.getIdField();
        if (idField != null) {
            MetaAttribute idAttr = metaResource.getAttribute(idField.getUnderlyingName());
            idAttr.setPrimaryKeyAttribute(true);
            if (metaResource.getSuperType() == null || metaResource.getSuperType().getPrimaryKey() == null) {
                MetaPrimaryKey primaryKey = new MetaPrimaryKey();
                primaryKey.setName(metaResource.getName() + "$primaryKey");
                primaryKey.setName(metaResource.getId() + "$primaryKey");
                primaryKey.setElements(Arrays.asList(idAttr));
                primaryKey.setUnique(true);
                primaryKey.setParent(metaResource, true);
                metaResource.setPrimaryKey(primaryKey);
                partition.addElement(null, primaryKey);
            }
        }
    }
    if (element instanceof MetaAttribute && element.getParent() instanceof MetaResourceBase) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaResourceBase parent = (MetaResourceBase) attr.getParent();
        ResourceInformation information = getResourceInformation(parent, true);
        ResourceField field = information.findFieldByUnderlyingName(attr.getName());
        PreconditionUtil.assertNotNull(attr.getName(), field);
        if (field.getResourceFieldType() == ResourceFieldType.RELATIONSHIP) {
            String oppositeType = field.getOppositeResourceType();
            String oppositeId = partition.getId(oppositeType);
            Optional<MetaElement> optOppositeMeta = context.getMetaElement(oppositeId);
            if (!optOppositeMeta.isPresent()) {
                throw new IllegalStateException("opposite meta element '" + oppositeId + "' for element '" + element.getId() + "' not found");
            }
            MetaResource oppositeMeta = (MetaResource) optOppositeMeta.get();
            if (field.isCollection()) {
                boolean isSet = Set.class.isAssignableFrom(field.getType());
                String suffix = (isSet ? "$set" : "$list");
                Optional<MetaElement> optMetaCollection = context.getMetaElement(oppositeId + suffix);
                MetaCollectionType metaCollection;
                if (optMetaCollection.isPresent()) {
                    metaCollection = (MetaCollectionType) optMetaCollection.get();
                } else {
                    metaCollection = isSet ? new MetaSetType() : new MetaListType();
                    metaCollection.setId(oppositeMeta.getId() + suffix);
                    metaCollection.setName(oppositeMeta.getName() + suffix);
                    metaCollection.setImplementationType(field.getGenericType());
                    metaCollection.setElementType(oppositeMeta);
                    partition.addElement(null, metaCollection);
                }
                attr.setType(metaCollection);
            } else {
                attr.setType(oppositeMeta);
            }
        } else {
            Type implementationType = field.getGenericType();
            MetaElement metaType = partition.allocateMetaElement(implementationType).get();
            attr.setType(metaType.asType());
        }
    } else if (element instanceof MetaAttribute && element.getParent() instanceof MetaJsonObject) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaDataObject parent = attr.getParent();
        Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName());
        MetaElement metaType = partition.allocateMetaElement(implementationType).get();
        attr.setType(metaType.asType());
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaPrimaryKey(io.crnk.meta.model.MetaPrimaryKey) MetaElement(io.crnk.meta.model.MetaElement) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaResourceBase(io.crnk.meta.model.resource.MetaResourceBase) MetaCollectionType(io.crnk.meta.model.MetaCollectionType) ResourceField(io.crnk.core.engine.information.resource.ResourceField) MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) MetaListType(io.crnk.meta.model.MetaListType) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) MetaSetType(io.crnk.meta.model.MetaSetType) MetaCollectionType(io.crnk.meta.model.MetaCollectionType) Type(java.lang.reflect.Type) MetaListType(io.crnk.meta.model.MetaListType) MetaSetType(io.crnk.meta.model.MetaSetType) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaJsonObject(io.crnk.meta.model.resource.MetaJsonObject) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Aggregations

MetaPrimaryKey (io.crnk.meta.model.MetaPrimaryKey)5 Test (org.junit.Test)3 MetaAttribute (io.crnk.meta.model.MetaAttribute)2 MetaDataObject (io.crnk.meta.model.MetaDataObject)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 SequenceEntity (io.crnk.jpa.model.SequenceEntity)1 MetaCollectionType (io.crnk.meta.model.MetaCollectionType)1 MetaElement (io.crnk.meta.model.MetaElement)1 MetaListType (io.crnk.meta.model.MetaListType)1 MetaSetType (io.crnk.meta.model.MetaSetType)1 MetaJsonObject (io.crnk.meta.model.resource.MetaJsonObject)1 MetaResource (io.crnk.meta.model.resource.MetaResource)1 MetaResourceBase (io.crnk.meta.model.resource.MetaResourceBase)1 MetaResourceField (io.crnk.meta.model.resource.MetaResourceField)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1