Skip to content

Commit

Permalink
Use data adapter for parsing as well
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Jul 17, 2024
1 parent 856363c commit 4ef9c1c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LocalEntitiesFilterStrategy(entitiesRepository: EntitiesRepository) :
evaluationContext: EvaluationContext,
next: Supplier<MutableList<TreeReference>>
): List<TreeReference> {
if (!dataAdapter.supportsInstance(sourceInstance)) {
if (!dataAdapter.supportsInstance(sourceInstance.instanceId)) {
return next.get()
}

Expand All @@ -35,10 +35,17 @@ class LocalEntitiesFilterStrategy(entitiesRepository: EntitiesRepository) :
val child = candidate.nodeSide.steps[0].name.name
val value = candidate.evalContextSide(sourceInstance, evaluationContext)

val results = dataAdapter.queryEq(sourceInstance, child, value as String)
val results = dataAdapter.queryEq(
sourceInstance.instanceId,
child,
value as String
)
return if (results != null) {
sourceInstance.replacePartialElements(results)
results.map { it.ref }
results.map {
it.parent = sourceInstance.root
it.ref
}
} else {
next.get()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.odk.collect.entities.javarosa.intance

import org.javarosa.core.model.data.StringData
import org.javarosa.core.model.instance.DataInstance
import org.javarosa.core.model.instance.TreeElement
import org.odk.collect.entities.browser.EntityItemElement
import org.odk.collect.entities.storage.EntitiesRepository
Expand All @@ -11,64 +10,71 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos

private val lists = entitiesRepository.getLists()

fun supportsInstance(sourceInstance: DataInstance<*>): Boolean {
return lists.contains(sourceInstance.instanceId)
fun supportsInstance(instanceId: String): Boolean {
return lists.contains(instanceId)
}

fun queryEq(sourceInstance: DataInstance<*>, child: String, value: String): List<TreeElement>? {
fun getAll(instanceId: String, partial: Boolean): List<TreeElement> {
return entitiesRepository.getEntities(instanceId).map { entity ->
convertToElement(entity, partial)
}
}

fun queryEq(instanceId: String, child: String, value: String): List<TreeElement>? {
return when {
child == "name" -> {
val entity = entitiesRepository.getById(
sourceInstance.instanceId,
instanceId,
value
)

if (entity != null) {
val element = convertToElement(sourceInstance, entity)
listOf(element)
listOf(convertToElement(entity, false))
} else {
emptyList()
}
}

!listOf("label", "__version").contains(child) -> {
val entities = entitiesRepository.getAllByProperty(
sourceInstance.instanceId,
instanceId,
child,
value
)

entities.map { convertToElement(sourceInstance, it) }
entities.map { convertToElement(it, false) }
}

else -> null
}
}

private fun convertToElement(
sourceInstance: DataInstance<*>,
entity: Entity.Saved
): TreeElement {
private fun convertToElement(entity: Entity.Saved, partial: Boolean): TreeElement {
val name = TreeElement(EntityItemElement.ID)
val label = TreeElement(EntityItemElement.LABEL)
val version = TreeElement(EntityItemElement.VERSION)

name.value = StringData(entity.id)
label.value = StringData(entity.label)
version.value = StringData(entity.version.toString())
if (!partial) {
name.value = StringData(entity.id)
label.value = StringData(entity.label)
version.value = StringData(entity.version.toString())
}

val item = TreeElement("item", entity.index)
val item = TreeElement("item", entity.index, partial)
item.addChild(name)
item.addChild(label)
item.addChild(version)

entity.properties.forEach { property ->
val propertyElement = TreeElement(property.first)
propertyElement.value = StringData(property.second)

if (!partial) {
propertyElement.value = StringData(property.second)
}

item.addChild(propertyElement)
}

item.parent = sourceInstance.root
return item
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.odk.collect.entities.javarosa.intance

import org.javarosa.core.model.data.StringData
import org.javarosa.core.model.instance.TreeElement
import org.javarosa.xform.parse.ExternalInstanceParser
import org.odk.collect.entities.browser.EntityItemElement
import org.odk.collect.entities.storage.EntitiesRepository

internal class LocalEntitiesInstanceProvider(private val entitiesRepositoryProvider: () -> EntitiesRepository) :
Expand All @@ -15,42 +13,13 @@ internal class LocalEntitiesInstanceProvider(private val entitiesRepositoryProvi

override fun get(instanceId: String, instanceSrc: String, partial: Boolean): TreeElement {
val root = TreeElement("root", 0)

val entitiesRepository = entitiesRepositoryProvider()
entitiesRepository.getEntities(instanceId).forEach { entity ->
val name = TreeElement(EntityItemElement.ID)
val label = TreeElement(EntityItemElement.LABEL)
val version = TreeElement(EntityItemElement.VERSION)

if (!partial) {
name.value = StringData(entity.id)
label.value = StringData(entity.label)
version.value = StringData(entity.version.toString())
}

val item = TreeElement("item", entity.index, partial)
item.addChild(name)
item.addChild(label)
item.addChild(version)

entity.properties.forEach { property ->
val propertyElement = TreeElement(property.first)

if (!partial) {
propertyElement.value = StringData(property.second)
}

item.addChild(propertyElement)
}

root.addChild(item)
}

createDataAdapter().getAll(instanceId, partial).forEach { root.addChild(it) }
return root
}

override fun isSupported(instanceId: String, instanceSrc: String): Boolean {
val entitiesRepository = entitiesRepositoryProvider()
return entitiesRepository.getLists().contains(instanceId)
return createDataAdapter().supportsInstance(instanceId)
}

private fun createDataAdapter() = LocalEntitiesInstanceAdapter(entitiesRepositoryProvider())
}

0 comments on commit 4ef9c1c

Please sign in to comment.