Skip to content

Commit

Permalink
Use FileInstanceParser interface
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Apr 27, 2024
1 parent d577bdc commit 5de51a4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.javarosa.xform.util.XFormUtils
import org.odk.collect.android.dynamicpreload.DynamicPreloadXFormParserFactory
import org.odk.collect.android.entities.EntitiesRepositoryProvider
import org.odk.collect.android.logic.actions.setgeopoint.CollectSetGeopointActionHandler
import org.odk.collect.entities.OfflineEntitiesExternalInstanceParserFactory
import org.odk.collect.entities.LocalEntitiesExternalInstanceParserFactory
import org.odk.collect.metadata.PropertyManager
import org.odk.collect.settings.SettingsProvider
import org.odk.collect.settings.keys.ProjectKeys
Expand Down Expand Up @@ -46,11 +46,11 @@ class JavaRosaInitializer(

XFormUtils.setXFormParserFactory(dynamicPreloadXFormParserFactory)

val offlineEntitiesExternalInstanceParserFactory = OfflineEntitiesExternalInstanceParserFactory(
val localEntitiesExternalInstanceParserFactory = LocalEntitiesExternalInstanceParserFactory(
entitiesRepositoryProvider::get,
{ settingsProvider.getUnprotectedSettings().getBoolean(ProjectKeys.KEY_LOCAL_ENTITIES) }
)

XFormUtils.setExternalInstanceParserFactory(offlineEntitiesExternalInstanceParserFactory)
XFormUtils.setExternalInstanceParserFactory(localEntitiesExternalInstanceParserFactory)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.odk.collect.entities

import org.javarosa.core.model.data.StringData
import org.javarosa.core.model.instance.TreeElement
import org.javarosa.xform.parse.ExternalInstanceParser
import org.javarosa.xform.parse.ExternalInstanceParser.FileInstanceParser
import org.javarosa.xform.parse.ExternalInstanceParserFactory

class LocalEntitiesExternalInstanceParserFactory(
private val entitiesRepositoryProvider: () -> EntitiesRepository,
private val enabled: () -> Boolean
) : ExternalInstanceParserFactory {
override fun getExternalInstanceParser(): ExternalInstanceParser {
val parser = ExternalInstanceParser()

if (enabled()) {
parser.addFileInstanceParser(LocalEntitiesFileInstanceParser(entitiesRepositoryProvider))
}

return parser
}
}

internal class LocalEntitiesFileInstanceParser(private val entitiesRepositoryProvider: () -> EntitiesRepository) :
FileInstanceParser {

override fun parse(instanceId: String, path: String): TreeElement {
val root = TreeElement("root", 0)

val entitiesRepository = entitiesRepositoryProvider()
entitiesRepository.getEntities(instanceId).forEachIndexed { index, entity ->
val name = TreeElement(EntityItemElement.ID)
name.value = StringData(entity.id)

val label = TreeElement(EntityItemElement.LABEL)
label.value = StringData(entity.label)

val version = TreeElement(EntityItemElement.VERSION)
version.value = StringData(entity.version.toString())

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

entity.properties.forEach { property ->
addChild(item, property)
}

root.addChild(item)
}

return root
}

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

private fun addChild(
element: TreeElement,
nameAndValue: Pair<String, String>
) {
element.addChild(
TreeElement(nameAndValue.first).also {
it.value = StringData(nameAndValue.second)
}
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package org.odk.collect.entities
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import org.mockito.kotlin.mock

class OfflineEntitiesExternalInstanceParserTest {
class LocalEntitiesFileInstanceParserTest {

private val entitiesRepository = InMemEntitiesRepository()

Expand All @@ -20,8 +19,8 @@ class OfflineEntitiesExternalInstanceParserTest {
)
entitiesRepository.save(entity)

val parser = OfflineEntitiesExternalInstanceParser(entitiesRepository)
val instance = parser.parse(mock(), "people", "people.csv")
val parser = LocalEntitiesFileInstanceParser { entitiesRepository }
val instance = parser.parse("people", "people.csv")
assertThat(instance.numChildren, equalTo(1))

val item = instance.getChildAt(0)!!
Expand All @@ -41,8 +40,8 @@ class OfflineEntitiesExternalInstanceParserTest {
)
entitiesRepository.save(entity)

val parser = OfflineEntitiesExternalInstanceParser(entitiesRepository)
val instance = parser.parse(mock(), "people", "people.csv")
val parser = LocalEntitiesFileInstanceParser { entitiesRepository }
val instance = parser.parse("people", "people.csv")
assertThat(instance.numChildren, equalTo(1))

val item = instance.getChildAt(0)!!
Expand Down

0 comments on commit 5de51a4

Please sign in to comment.