Skip to content

Commit

Permalink
Create entity lists automatically for entityList media files
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Jul 10, 2024
1 parent 0e26cf6 commit a5eed5f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ object ServerFormUseCases {
}
}

val dataset = mediaFile.filename.substringBefore(".csv")
if (entitiesRepository.getLists().contains(dataset)) {
if (mediaFile.isEntityList) {
val dataset = mediaFile.filename.substringBefore(".csv")
LocalEntityUseCases.updateLocalEntitiesFromServer(dataset, tempMediaFile, entitiesRepository)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class OpenRosaResponseParserImpl : OpenRosaResponseParser {
continue
}

val type = mediaFileElement.getAttributeValue(null, "type")

val name = mediaFileElement.name
if (name.equals("mediaFile", ignoreCase = true)) {
var filename: String? = null
Expand Down Expand Up @@ -201,7 +203,7 @@ class OpenRosaResponseParserImpl : OpenRosaResponseParser {
return null
}

files.add(MediaFile(filename, hash, downloadUrl))
files.add(MediaFile(filename, hash, downloadUrl, type == "entityList"))
}
}
return files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,29 @@ class OpenRosaResponseParserImplTest {
val formList = OpenRosaResponseParserImpl().parseManifest(Document())
assertThat(formList, equalTo(null))
}

@Test
fun `parseManifest() when media file has type entityList returns isEntityList as true`() {
val response = StringBuilder()
.appendLine("<?xml version='1.0' encoding='UTF-8' ?>")
.appendLine("<manifest xmlns=\"http://openrosa.org/xforms/xformsManifest\">")
.appendLine("<mediaFile type=\"entityList\">")
.appendLine("<filename>badgers.csv</filename>")
.appendLine("<hash>blah</hash>")
.appendLine("<downloadUrl>http://funk.appspot.com/binaryData?blobKey=%3A477e3</downloadUrl>")
.appendLine("</mediaFile>")
.appendLine("</manifest>")
.toString()

val doc = StringReader(response).use { reader ->
val parser = KXmlParser()
parser.setInput(reader)
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true)
Document().also { it.parse(parser) }
}

val mediaFiles = OpenRosaResponseParserImpl().parseManifest(doc)!!
assertThat(mediaFiles.size, equalTo(1))
assertThat(mediaFiles[0].isEntityList, equalTo(true))
}
}
5 changes: 3 additions & 2 deletions forms/src/main/java/org/odk/collect/forms/MediaFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ package org.odk.collect.forms

import java.io.Serializable

data class MediaFile(
data class MediaFile @JvmOverloads constructor(
val filename: String,
val hash: String,
val downloadUrl: String
val downloadUrl: String,
val isEntityList: Boolean = false
) : Serializable

0 comments on commit a5eed5f

Please sign in to comment.