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

sync manager: write transaction exceptions #3938

Open
wants to merge 6 commits into
base: master
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
146 changes: 86 additions & 60 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import android.text.TextUtils
import android.util.Base64
import android.util.Log
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
Expand All @@ -18,12 +19,12 @@ import org.ole.planet.myplanet.MainApplication.Companion.context
import org.ole.planet.myplanet.model.RealmMyLibrary.Companion.createStepResource
import org.ole.planet.myplanet.model.RealmStepExam.Companion.insertCourseStepsExams
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
import org.ole.planet.myplanet.utilities.DownloadUtils.extractLinks
import org.ole.planet.myplanet.utilities.JsonUtils
import org.ole.planet.myplanet.utilities.Utilities
import java.io.File
import java.io.FileWriter
import java.io.IOException
import java.util.regex.Pattern

open class RealmMyCourse : RealmObject() {
@PrimaryKey
Expand Down Expand Up @@ -72,46 +73,76 @@ open class RealmMyCourse : RealmObject() {
companion object {
private val gson = Gson()
private val concatenatedLinks = ArrayList<String>()
val courseDataList: MutableList<Array<String>> = mutableListOf()
private val courseDataList: MutableList<Array<String>> = mutableListOf()

@JvmStatic
fun insertMyCourses(userId: String?, myCoursesDoc: JsonObject?, mRealm: Realm) {
context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
fun insertMyCourses(userId: String?, myCousesDoc: JsonObject?, mRealm: Realm) {
try {
if (!mRealm.isInTransaction) {
mRealm.executeTransaction { realm ->
insertOrUpdateCourse(userId, myCousesDoc, realm)
}
} else {
insertOrUpdateCourse(userId, myCousesDoc, mRealm)
}
} catch (e: Exception) {
Log.e("RealmMyCourse1", "Error during insertion into courses: ${e.message}")
throw e
}
val id = JsonUtils.getString("_id", myCoursesDoc)
}

fun writeCsv(filePath: String, data: List<Array<String>>) {
try {
val file = File(filePath)
file.parentFile?.mkdirs()
val writer = CSVWriter(FileWriter(file))
writer.writeNext(arrayOf("courseId", "course_rev", "languageOfInstruction", "courseTitle", "memberLimit", "description", "method", "gradeLevel", "subjectLevel", "createdDate", "steps"))
for (row in data) {
writer.writeNext(row)
}
writer.close()
} catch (e: IOException) {
e.printStackTrace()
}
}

fun courseWriteCsv() {
writeCsv("${context.getExternalFilesDir(null)}/ole/course.csv", courseDataList)
}

private fun insertOrUpdateCourse(userId: String?, myCousesDoc: JsonObject?, mRealm: Realm) {
val id = JsonUtils.getString("_id", myCousesDoc)
var myMyCoursesDB = mRealm.where(RealmMyCourse::class.java).equalTo("id", id).findFirst()
if (myMyCoursesDB == null) {
myMyCoursesDB = mRealm.createObject(RealmMyCourse::class.java, id)
}
myMyCoursesDB?.setUserId(userId)
myMyCoursesDB?.courseId = JsonUtils.getString("_id", myCoursesDoc)
myMyCoursesDB?.courseRev = JsonUtils.getString("_rev", myCoursesDoc)
myMyCoursesDB?.languageOfInstruction = JsonUtils.getString("languageOfInstruction", myCoursesDoc)
myMyCoursesDB?.courseTitle = JsonUtils.getString("courseTitle", myCoursesDoc)
myMyCoursesDB?.memberLimit = JsonUtils.getInt("memberLimit", myCoursesDoc)
myMyCoursesDB?.description = JsonUtils.getString("description", myCoursesDoc)
val description = JsonUtils.getString("description", myCoursesDoc)
myMyCoursesDB?.courseId = JsonUtils.getString("_id", myCousesDoc)
myMyCoursesDB?.courseRev = JsonUtils.getString("_rev", myCousesDoc)
myMyCoursesDB?.languageOfInstruction = JsonUtils.getString("languageOfInstruction", myCousesDoc)
myMyCoursesDB?.courseTitle = JsonUtils.getString("courseTitle", myCousesDoc)
myMyCoursesDB?.memberLimit = JsonUtils.getInt("memberLimit", myCousesDoc)
myMyCoursesDB?.description = JsonUtils.getString("description", myCousesDoc)
val description = JsonUtils.getString("description", myCousesDoc)
val links = extractLinks(description)
val baseUrl = Utilities.getUrl()
for (link in links) {
val concatenatedLink = "$baseUrl/$link"
concatenatedLinks.add(concatenatedLink)
}
myMyCoursesDB?.method = JsonUtils.getString("method", myCoursesDoc)
myMyCoursesDB?.gradeLevel = JsonUtils.getString("gradeLevel", myCoursesDoc)
myMyCoursesDB?.subjectLevel = JsonUtils.getString("subjectLevel", myCoursesDoc)
myMyCoursesDB?.createdDate = JsonUtils.getLong("createdDate", myCoursesDoc)
myMyCoursesDB?.setNumberOfSteps(JsonUtils.getJsonArray("steps", myCoursesDoc).size())
val courseStepsJsonArray = JsonUtils.getJsonArray("steps", myCoursesDoc)
myMyCoursesDB?.method = JsonUtils.getString("method", myCousesDoc)
myMyCoursesDB?.gradeLevel = JsonUtils.getString("gradeLevel", myCousesDoc)
myMyCoursesDB?.subjectLevel = JsonUtils.getString("subjectLevel", myCousesDoc)
myMyCoursesDB?.createdDate = JsonUtils.getLong("createdDate", myCousesDoc)
myMyCoursesDB?.setNumberOfSteps(JsonUtils.getJsonArray("steps", myCousesDoc).size())
val courseStepsJsonArray = JsonUtils.getJsonArray("steps", myCousesDoc)
val courseStepsList = mutableListOf<RealmCourseStep>()

for (i in 0 until courseStepsJsonArray.size()) {
val stepId = Base64.encodeToString(courseStepsJsonArray[i].toString().toByteArray(), Base64.NO_WRAP)
val step_id = Base64.encodeToString(courseStepsJsonArray[i].toString().toByteArray(), Base64.NO_WRAP)
val stepJson = courseStepsJsonArray[i].asJsonObject
val step = RealmCourseStep()
step.id = stepId
step.id = step_id
step.stepTitle = JsonUtils.getString("stepTitle", stepJson)
step.description = JsonUtils.getString("description", stepJson)
val stepDescription = JsonUtils.getString("description", stepJson)
Expand All @@ -120,58 +151,49 @@ open class RealmMyCourse : RealmObject() {
val concatenatedLink = "$baseUrl/$stepLink"
concatenatedLinks.add(concatenatedLink)
}
insertCourseStepsAttachments(myMyCoursesDB?.courseId, stepId, JsonUtils.getJsonArray("resources", stepJson), mRealm)
insertExam(stepJson, mRealm, stepId, i + 1, myMyCoursesDB?.courseId)
insertSurvey(stepJson, mRealm, stepId, i + 1, myMyCoursesDB?.courseId, myMyCoursesDB?.createdDate)
insertCourseStepsAttachments(myMyCoursesDB?.courseId, step_id, JsonUtils.getJsonArray("resources", stepJson), mRealm)
insertExam(stepJson, mRealm, step_id, i + 1, myMyCoursesDB?.courseId)
step.noOfResources = JsonUtils.getJsonArray("resources", stepJson).size()
step.courseId = myMyCoursesDB?.courseId
courseStepsList.add(step)
}

if (mRealm.isInTransaction) {
mRealm.commitTransaction()
mRealm.executeTransaction {
myMyCoursesDB?.courseSteps?.clear()
myMyCoursesDB?.courseSteps?.addAll(courseStepsList)
}

if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}
myMyCoursesDB?.courseSteps = RealmList()
myMyCoursesDB?.courseSteps?.addAll(courseStepsList)
mRealm.commitTransaction()

val csvRow = arrayOf(
JsonUtils.getString("_id", myCoursesDoc),
JsonUtils.getString("_rev", myCoursesDoc),
JsonUtils.getString("languageOfInstruction", myCoursesDoc),
JsonUtils.getString("courseTitle", myCoursesDoc),
JsonUtils.getInt("memberLimit", myCoursesDoc).toString(),
JsonUtils.getString("description", myCoursesDoc),
JsonUtils.getString("method", myCoursesDoc),
JsonUtils.getString("gradeLevel", myCoursesDoc),
JsonUtils.getString("subjectLevel", myCoursesDoc),
JsonUtils.getLong("createdDate", myCoursesDoc).toString(),
JsonUtils.getJsonArray("steps", myCoursesDoc).toString()
JsonUtils.getString("_id", myCousesDoc),
JsonUtils.getString("_rev", myCousesDoc),
JsonUtils.getString("languageOfInstruction", myCousesDoc),
JsonUtils.getString("courseTitle", myCousesDoc),
JsonUtils.getInt("memberLimit", myCousesDoc).toString(),
JsonUtils.getString("description", myCousesDoc),
JsonUtils.getString("method", myCousesDoc),
JsonUtils.getString("gradeLevel", myCousesDoc),
JsonUtils.getString("subjectLevel", myCousesDoc),
JsonUtils.getLong("createdDate", myCousesDoc).toString(),
JsonUtils.getJsonArray("steps", myCousesDoc).toString()
)
courseDataList.add(csvRow)
}

fun writeCsv(filePath: String, data: List<Array<String>>) {
try {
val file = File(filePath)
file.parentFile?.mkdirs()
val writer = CSVWriter(FileWriter(file))
writer.writeNext(arrayOf("courseId", "course_rev", "languageOfInstruction", "courseTitle", "memberLimit", "description", "method", "gradeLevel", "subjectLevel", "createdDate", "steps"))
for (row in data) {
writer.writeNext(row)
private fun extractLinks(text: String?): ArrayList<String> {
val links = ArrayList<String>()
val pattern = Pattern.compile("!\\[.*?]\\((.*?)\\)")
val matcher = text?.let { pattern.matcher(it) }
if (matcher != null) {
while (matcher.find()) {
val link = matcher.group(1)
if (link != null) {
if (link.isNotEmpty()) {
links.add(link)
}
}
}
writer.close()
} catch (e: IOException) {
e.printStackTrace()
}
}

fun courseWriteCsv() {
writeCsv("${context.getExternalFilesDir(null)}/ole/course.csv", courseDataList)
return links
}

@JvmStatic
Expand Down Expand Up @@ -275,7 +297,11 @@ open class RealmMyCourse : RealmObject() {

@JvmStatic
fun insert(mRealm: Realm, myCoursesDoc: JsonObject?) {
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}
insertMyCourses("", myCoursesDoc, mRealm)
mRealm.commitTransaction()
}

@JvmStatic
Expand Down
24 changes: 21 additions & 3 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmMyLibrary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.ole.planet.myplanet.model

import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import android.util.Log
import com.google.gson.JsonArray
import com.google.gson.JsonNull
import com.google.gson.JsonObject
Expand Down Expand Up @@ -241,7 +243,11 @@ open class RealmMyLibrary : RealmObject() {

@JvmStatic
fun insertMyLibrary(userId: String?, doc: JsonObject, mRealm: Realm) {
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}
insertMyLibrary(userId, "", "", doc, mRealm)
mRealm.commitTransaction()
}

@JvmStatic
Expand All @@ -253,11 +259,23 @@ open class RealmMyLibrary : RealmObject() {
mRealm.commitTransaction()
}

@JvmStatic
fun insertMyLibrary(userId: String?, stepId: String?, courseId: String?, doc: JsonObject, mRealm: Realm) {
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
try {
if (!mRealm.isInTransaction) {
mRealm.executeTransaction { realm ->
insertOrUpdateLibrary(userId, stepId, courseId, doc, realm)
}
} else {
insertOrUpdateLibrary(userId, stepId, courseId, doc, mRealm)
}
} catch (e: Exception) {
Log.e("RealmMyLibrary", "Error during insertion into library: ${e.message}")
throw e
}
}

@JvmStatic
fun insertOrUpdateLibrary(userId: String?, stepId: String?, courseId: String?, doc: JsonObject, mRealm: Realm) {
val resourceId = JsonUtils.getString("_id", doc)
val settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
var resource = mRealm.where(RealmMyLibrary::class.java).equalTo("id", resourceId).findFirst()
Expand Down
Loading