Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature to get submitted form data as json in OnFormUploaded ev… #34

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.odk.collect.android.events

import org.json.JSONObject

/**
* Events exporter class for form events. This class contains all the
* events that occur during the lifecycle of a form.
Expand All @@ -23,8 +25,8 @@ object FormEventBus: ODKEventBus<FormStateEvent>() {
state.onNext(FormStateEvent.OnFormSaveError(formId, errorMessage))
}

fun formUploaded(formId: String, instancePath: String) {
state.onNext(FormStateEvent.OnFormUploaded(formId, instancePath))
fun formUploaded(formId: String, submittedData: JSONObject) {
state.onNext(FormStateEvent.OnFormUploaded(formId, submittedData))
}

fun formUploadError(formId: String, errorMessage: String) {
Expand Down Expand Up @@ -54,7 +56,7 @@ sealed class FormStateEvent {
data class OnFormSaveError(val formId: String, val errorMessage: String): FormStateEvent()

/** Called when a form upload is successful. */
data class OnFormUploaded(val formId: String, val instancePath: String): FormStateEvent()
data class OnFormUploaded(val formId: String, val submittedData: JSONObject): FormStateEvent()

/** Called when a form upload fails. */
data class OnFormUploadFailed(val formId: String, val errorMessage: String): FormStateEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.odk.collect.android.projects.ProjectDependencyProvider
import org.odk.collect.android.upload.FormUploadException
import org.odk.collect.forms.instances.Instance
import org.odk.collect.permissions.PermissionsProvider
import org.odk.collect.utilities.XmlJsonUtility
import java.io.File

class InstanceAutoSender(
private val instanceAutoSendFetcher: InstanceAutoSendFetcher,
Expand Down Expand Up @@ -46,7 +48,12 @@ class InstanceAutoSender(
val result: Map<Instance, FormUploadException?> = instanceSubmitter.submitInstances(toUpload)
result.entries.stream().forEach { entry ->
if (entry.value == null) {
FormEventBus.formUploaded(entry.key.formId, entry.key.instanceFilePath)
try {
val submittedData = XmlJsonUtility.convertToJson(File(entry.key.instanceFilePath))
FormEventBus.formUploaded(entry.key.formId, submittedData)
} catch (e: Exception) {
FormEventBus.formUploadError(entry.key.formId, e.message ?: "Failed to read submitted data!")
}
}
else {
FormEventBus.formUploadError(entry.key.formId, entry.value!!.message)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.odk.collect.utilities

import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import org.xmlpull.v1.XmlPullParserFactory
import java.io.File
import java.io.IOException
import java.io.StringReader

object XmlJsonUtility {

@Throws(XmlPullParserException::class, IOException::class, JSONException::class)
fun convertToJson(xml: String?): JSONObject {
val factory = XmlPullParserFactory.newInstance()
val parser = factory.newPullParser()
parser.setInput(StringReader(xml))
val jsonObject = JSONObject()
var jsonArray = JSONArray()
var tagName = ""
var eventType = parser.eventType
while (eventType != XmlPullParser.END_DOCUMENT) {
when (eventType) {
XmlPullParser.START_DOCUMENT -> {}
XmlPullParser.START_TAG -> {
tagName = parser.name
if (jsonObject.has(tagName)) {
// If the tag already exists in the object, convert it to an array
val existingValue = jsonObject[tagName]
if (existingValue is JSONArray) {
jsonArray = existingValue
} else {
jsonArray = JSONArray()
jsonArray.put(existingValue)
jsonObject.put(tagName, jsonArray)
}
}
}
XmlPullParser.TEXT -> if (tagName.isNotEmpty()) {
jsonArray.put(parser.text)
}
XmlPullParser.END_TAG -> {
if (tagName.isNotEmpty()) {
if (jsonArray.length() > 0) {
jsonObject.put(tagName, jsonArray)
jsonArray = JSONArray()
} else {
jsonObject.put(tagName, "")
}
}
tagName = ""
}
}
eventType = parser.next()
}
return jsonObject
}

@Throws(IOException::class)
fun convertToJson(xmlFile: File): JSONObject {
val xmlString = xmlFile.readText()
return convertToJson(xmlString)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ODKFeatureTesterActivity : AppCompatActivity(), View.OnClickListener {
is FormStateEvent.OnFormSaveError -> Timber.tag("FORM EVENT").d("Form with id: %s could not be saved. Reason: %s", event.formId, event.errorMessage)
is FormStateEvent.OnFormSaved -> Timber.tag("FORM EVENT").d("Form with id: %s was saved. Saved instance path: %s", event.formId, event.instancePath)
is FormStateEvent.OnFormUploadFailed -> Timber.tag("FORM EVENT").d("Form upload failed for form id: %s. Reason: %s", event.formId, event.errorMessage)
is FormStateEvent.OnFormUploaded -> Timber.tag("FORM EVENT").d("Form with id: %s was uploaded", event.formId)
is FormStateEvent.OnFormUploaded -> Timber.tag("FORM EVENT").d("Form with id: %s was uploaded. Form response: %s", event.formId, event.submittedData)
}
progressBar.visibility = View.INVISIBLE
}
Expand Down