Skip to content

Commit

Permalink
Support for relationships #4
Browse files Browse the repository at this point in the history
  • Loading branch information
abstratt committed Apr 18, 2018
1 parent 828df16 commit 00cea26
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class KirraSpringInstanceBridge {
@Autowired
private lateinit var kirraSpringMetamodel: KirraSpringMetamodel

fun toInstances(elements: Iterable<BaseEntity>): Iterable<Instance> {
return elements.map {
val toConvert = it
toInstance(toConvert, InstanceManagement.DataProfile.Full)
}.toMutableList()
}

/**
* Converts a JPA instance to a Kirra instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,8 @@ class KirraSpringInstanceManagement : InstanceManagement {
val entityClass : Class<BaseEntity>? = kirraSpringMetamodel.getEntityClass(namespace, entityName)
val asService : BaseService<BaseEntity,*> = 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<Instance> filterInstances(Map<String, List<Object>> criteria, String namespace, String name, DataProfile profile) {}
Expand Down Expand Up @@ -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<Instance> {
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<Instance> {
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<BaseEntity> ?: emptyList()
return kirraSpringInstanceBridge.toInstances(relatedObjects).toMutableList()
}

override fun unlinkInstances(relationship: Relationship?, sourceId: String?, destinationId: InstanceRef?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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<Operation> {
val entityFunctions = kirraSpringMetamodel.getInstanceFunctions(entityType.javaType.kotlin)
return entityFunctions.map { buildOperation(it as KFunction<*>,true) }
Expand Down Expand Up @@ -168,7 +177,7 @@ class KirraSpringSchemaBuilder : SchemaBuilder {
return parameter
}

private fun buildRelationships(entityClass: EntityType<*>): List<Relationship> {
private fun buildStoredRelationships(entityClass: EntityType<*>): List<Relationship> {
return kirraSpringMetamodel.getRelationships(entityClass).map { this.buildRelationship(it) }
}

Expand Down Expand Up @@ -273,6 +282,8 @@ class KirraSpringSchemaBuilder : SchemaBuilder {
return relationship
}



private fun isUserVisible(attribute: Attribute<*, *>) : Boolean {
val javaMember = attribute.javaMember
return isUserVisible(javaMember)
Expand Down

0 comments on commit 00cea26

Please sign in to comment.