forked from getodk/collect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ties/src/main/java/org/odk/collect/entities/LocalEntitiesExternalInstanceParserFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) | ||
} | ||
} |
71 changes: 0 additions & 71 deletions
71
...es/src/main/java/org/odk/collect/entities/OfflineEntitiesExternalInstanceParserFactory.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters