Skip to content

Commit

Permalink
add common resources to correct resources that has domain and package
Browse files Browse the repository at this point in the history
  • Loading branch information
hknots committed Sep 9, 2024
1 parent fc41898 commit bd59a6d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/no/fintlabs/metamodel/MetadataCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class MetadataCache {

metaData.packageName?.let { packageName ->
val domainPackageKey = domain to packageName.lowercase()
domainPackageCache[domainPackageKey] = domainPackageCache.getOrDefault(domainPackageKey, mutableListOf()) + metaData
domainPackageCache[domainPackageKey] =
domainPackageCache.getOrDefault(domainPackageKey, mutableListOf()) + metaData
}

val resourceKey = Triple(domain, metaData.packageName?.lowercase() ?: "", metaData.resourceName.lowercase())
Expand Down
95 changes: 74 additions & 21 deletions src/main/kotlin/no/fintlabs/metamodel/ReflectionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,90 @@ import no.fintlabs.metamodel.model.FintRelationMetadata
import no.fintlabs.metamodel.model.Metadata
import org.reflections.Reflections
import org.springframework.stereotype.Service
import java.util.stream.Collectors

@Service
class ReflectionService(
val metadataCache: MetadataCache,
) {

val clazzMap: Map<String, Class<out FintModelObject>> = createClazzMap()

@PostConstruct
fun fillCache() {
Reflections("no.fint.model")
.getSubTypesOf(FintModelObject::class.java)
.forEach { clazz ->
val fintModelObject = newInstanceOfFintModelObject(clazz)
val relationsMetadata = fintModelObject.relations.map { relation ->
FintRelationMetadata(
relation.name.lowercase(),
relation.multiplicity,
relation.packageName
)
clazzMap.forEach { createClazzMetadata(it.value) }
}

private fun createClazzMetadata(clazz: Class<out FintModelObject>) {
val packageName = clazz.packageName
if (packageName.split(".").size == 4) return

val fintModelObject = newInstanceOfFintModelObject(clazz)
val relationsMetadatas = createRelationMetadatas(fintModelObject)
val existingRelationPackages: MutableSet<String> = mutableSetOf()
relationsMetadatas.forEach {
if (it.classPackageName.split(".").size == 5)
clazzMap[it.classPackageName.lowercase()]?.let { relationClazz ->
createClazzMetadata(clazz, relationClazz, existingRelationPackages)
}
}

metadataCache.add(
Metadata(
clazz.packageName.split(".")[3],
clazz.packageName.split(".")[4],
clazz.simpleName.lowercase(),
fintModelObject.identifikators.keys,
fintModelObject.isWriteable,
relationsMetadatas
)
)
}

metadataCache.add(
Metadata(
clazz.packageName.split(".")[3],
clazz.packageName.split(".").getOrNull(4),
clazz.simpleName.lowercase(),
fintModelObject.identifikators.keys,
fintModelObject.isWriteable,
relationsMetadata
)
)
}
private fun createClazzMetadata(
clazz: Class<out FintModelObject>,
relationClazz: Class<out FintModelObject>,
existingRelationPackages: MutableSet<String>
) {
if (!existingRelationPackages.add(relationClazz.name)) return
val fintModelObject = newInstanceOfFintModelObject(relationClazz)
val relationsMetadatas = createRelationMetadatas(fintModelObject)
relationsMetadatas.forEach {
if (it.classPackageName.split(".").size == 4)
clazzMap[it.classPackageName]?.let { relationClazz ->
createClazzMetadata(clazz, relationClazz, existingRelationPackages)
}
}

metadataCache.add(
Metadata(
clazz.packageName.split(".")[3],
clazz.packageName.split(".")[4],
relationClazz.simpleName.lowercase(),
fintModelObject.identifikators.keys,
fintModelObject.isWriteable,
relationsMetadatas
)
)

}

private fun createClazzMap(): Map<String, Class<out FintModelObject>> =
Reflections("no.fint.model")
.getSubTypesOf(FintModelObject::class.java).stream()
.collect(Collectors.toMap(
{ clazz -> clazz.getName().lowercase() },
{ clazz -> clazz }
))

private fun createRelationMetadatas(fintModelObject: FintModelObject): List<FintRelationMetadata> {
return fintModelObject.relations.map { relation ->
FintRelationMetadata(
relation.name.lowercase(),
relation.multiplicity,
relation.packageName
)
}
}

fun newInstanceOfFintModelObject(clazz: Class<out FintModelObject>): FintModelObject {
Expand Down

0 comments on commit bd59a6d

Please sign in to comment.