From 00cea2679b2a4ee9a893aec22d118918dc559baa Mon Sep 17 00:00:00 2001 From: Rafael Chaves Date: Wed, 18 Apr 2018 01:28:18 -0300 Subject: [PATCH] Support for relationships #4 --- .../kirra/spring/KirraSpringInstanceBridge.kt | 7 +++++++ .../spring/KirraSpringInstanceManagement.kt | 16 ++++++++++------ .../kirra/spring/KirraSpringSchemaBuilder.kt | 15 +++++++++++++-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/abstratt/kirra/spring/KirraSpringInstanceBridge.kt b/src/main/java/com/abstratt/kirra/spring/KirraSpringInstanceBridge.kt index 1f9c018..e4575eb 100644 --- a/src/main/java/com/abstratt/kirra/spring/KirraSpringInstanceBridge.kt +++ b/src/main/java/com/abstratt/kirra/spring/KirraSpringInstanceBridge.kt @@ -17,6 +17,13 @@ class KirraSpringInstanceBridge { @Autowired private lateinit var kirraSpringMetamodel: KirraSpringMetamodel + fun toInstances(elements: Iterable): Iterable { + return elements.map { + val toConvert = it + toInstance(toConvert, InstanceManagement.DataProfile.Full) + }.toMutableList() + } + /** * Converts a JPA instance to a Kirra instance. */ diff --git a/src/main/java/com/abstratt/kirra/spring/KirraSpringInstanceManagement.kt b/src/main/java/com/abstratt/kirra/spring/KirraSpringInstanceManagement.kt index 724bac8..3208367 100644 --- a/src/main/java/com/abstratt/kirra/spring/KirraSpringInstanceManagement.kt +++ b/src/main/java/com/abstratt/kirra/spring/KirraSpringInstanceManagement.kt @@ -230,10 +230,8 @@ class KirraSpringInstanceManagement : InstanceManagement { val entityClass : Class? = kirraSpringMetamodel.getEntityClass(namespace, entityName) val asService : BaseService = getEntityService(TypeRef(namespace, entityName, TypeRef.TypeKind.Entity)) val listed = asService.list() - return listed.content.map { - val toConvert = it - kirraSpringInstanceBridge.toInstance(toConvert, InstanceManagement.DataProfile.Full) - }.toMutableList() + val elements = listed.content + return kirraSpringInstanceBridge.toInstances(elements).toMutableList() } //public List filterInstances(Map> criteria, String namespace, String name, DataProfile profile) {} @@ -323,8 +321,14 @@ class KirraSpringInstanceManagement : InstanceManagement { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun getRelatedInstances(namespace: String?, name: String?, externalId: String?, relationship: String?, dataProfile: InstanceManagement.DataProfile?): MutableList { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + override fun getRelatedInstances(namespace: String, entityName: String, externalId: String, relationshipName: String, dataProfile: InstanceManagement.DataProfile?): MutableList { + val found = retrieveJavaInstance(namespace, entityName, externalId) + KirraException.ensure(found != null, KirraException.Kind.OBJECT_NOT_FOUND, {null}) + val toConvert = found!! + val relationshipKProperty = toConvert::class.memberProperties.firstOrNull { it.name == relationshipName } + KirraException.ensure(relationshipKProperty != null, KirraException.Kind.ELEMENT_NOT_FOUND, {null}) + val relatedObjects = relationshipKProperty!!.call(toConvert) as Iterable ?: emptyList() + return kirraSpringInstanceBridge.toInstances(relatedObjects).toMutableList() } override fun unlinkInstances(relationship: Relationship?, sourceId: String?, destinationId: InstanceRef?) { diff --git a/src/main/java/com/abstratt/kirra/spring/KirraSpringSchemaBuilder.kt b/src/main/java/com/abstratt/kirra/spring/KirraSpringSchemaBuilder.kt index cdfd54a..8a9f6f4 100644 --- a/src/main/java/com/abstratt/kirra/spring/KirraSpringSchemaBuilder.kt +++ b/src/main/java/com/abstratt/kirra/spring/KirraSpringSchemaBuilder.kt @@ -76,7 +76,12 @@ class KirraSpringSchemaBuilder : SchemaBuilder { val derivedProperties = entityAsKotlinClass.memberProperties.filter { it.javaField == null && !(it is KMutableProperty<*>) }.map { buildDerivedProperty(it, tmpObject) } val allProperties = storedProperties + derivedProperties newEntity.properties = allProperties - newEntity.relationships = this.buildRelationships(entityType) + + val storedRelationships = this.buildStoredRelationships(entityType) + val derivedRelationships = entityAsKotlinClass.memberProperties.filter { kirraSpringMetamodel.isEntityClass(it.returnType.javaClass) && it.javaField == null && !(it is KMutableProperty<*>) }.map { buildDerivedRelationship(it) } + val allRelationships = storedRelationships + derivedRelationships + newEntity.relationships = allRelationships + val allOperations = this.buildInstanceOperations(entityType).toMutableList() try { val serviceClass = entityAsJavaClass.classLoader.loadClass(entityAsJavaClass.name + "Service") @@ -128,6 +133,10 @@ class KirraSpringSchemaBuilder : SchemaBuilder { return newEntity } + private fun buildDerivedRelationship(ktProperty: KProperty<*>): Relationship { + TODO("buildDerivedRelationship not implemented yet" ) + } + private fun buildInstanceOperations(entityType: EntityType<*>): List { val entityFunctions = kirraSpringMetamodel.getInstanceFunctions(entityType.javaType.kotlin) return entityFunctions.map { buildOperation(it as KFunction<*>,true) } @@ -168,7 +177,7 @@ class KirraSpringSchemaBuilder : SchemaBuilder { return parameter } - private fun buildRelationships(entityClass: EntityType<*>): List { + private fun buildStoredRelationships(entityClass: EntityType<*>): List { return kirraSpringMetamodel.getRelationships(entityClass).map { this.buildRelationship(it) } } @@ -273,6 +282,8 @@ class KirraSpringSchemaBuilder : SchemaBuilder { return relationship } + + private fun isUserVisible(attribute: Attribute<*, *>) : Boolean { val javaMember = attribute.javaMember return isUserVisible(javaMember)