From 5e67f2d14f03bff17c65c478749fecf43157bbd9 Mon Sep 17 00:00:00 2001 From: Vivian Li Date: Thu, 25 Jul 2024 09:19:39 -0400 Subject: [PATCH] update --- .../planet/myplanet/model/RealmMyCourse.kt | 143 ++++++++++-------- .../planet/myplanet/model/RealmMyLibrary.kt | 1 - .../planet/myplanet/service/SyncManager.kt | 133 +++------------- 3 files changed, 100 insertions(+), 177 deletions(-) diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt b/app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt index c016af3da0..fcf87e9048 100644 --- a/app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt +++ b/app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt @@ -86,77 +86,88 @@ open class RealmMyCourse : RealmObject() { @JvmStatic fun insertMyCourses(userId: String?, myCoursesDoc: JsonObject?, mRealm: Realm) { - if(mRealm.isInTransaction) { - Log.e("RealmMyCourse", "insertMyCourses: Transaction is already in progress") - } else{ - Log.e("RealmMyCourse", "insertMyCourses: Transaction is not in progress") - } - val id = JsonUtils.getString("_id", myCoursesDoc) - 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) - 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) - val courseStepsList = mutableListOf() - - for (i in 0 until courseStepsJsonArray.size()) { - val stepId = Base64.encodeToString(courseStepsJsonArray[i].toString().toByteArray(), Base64.NO_WRAP) - val stepJson = courseStepsJsonArray[i].asJsonObject - val step = RealmCourseStep() - step.id = stepId - step.stepTitle = JsonUtils.getString("stepTitle", stepJson) - step.description = JsonUtils.getString("description", stepJson) - val stepDescription = JsonUtils.getString("description", stepJson) - val stepLinks = extractLinks(stepDescription) - for (stepLink in stepLinks) { - val concatenatedLink = "$baseUrl/$stepLink" + try { + if (!mRealm.isInTransaction) { + Log.e("RealmMyCourse", "insertMyCourses: Transaction is not in progress") + return + } else { + Log.e("RealmMyCourse", "insertMyCourses: Transaction is already in progress") + } + val settings: SharedPreferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE) + val id = JsonUtils.getString("_id", myCoursesDoc) + 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) + val links = extractLinks(description) + val baseUrl = Utilities.getUrl() + for (link in links) { + val concatenatedLink = "$baseUrl/$link" concatenatedLinks.add(concatenatedLink) } - insertCourseStepsAttachments(myMyCoursesDB?.courseId, stepId, JsonUtils.getJsonArray("resources", stepJson), mRealm) - insertExam(stepJson, mRealm, stepId, i + 1, myMyCoursesDB?.courseId) - step.noOfResources = JsonUtils.getJsonArray("resources", stepJson).size() - step.courseId = myMyCoursesDB?.courseId - courseStepsList.add(step) - } + 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?.numberOfSteps = JsonUtils.getJsonArray("steps", myCoursesDoc).size() + val courseStepsJsonArray = JsonUtils.getJsonArray("steps", myCoursesDoc) + val courseStepsList = mutableListOf() + + for (i in 0 until courseStepsJsonArray.size()) { + val step_id = Base64.encodeToString( + courseStepsJsonArray[i].toString().toByteArray(), + Base64.NO_WRAP + ) + val stepJson = courseStepsJsonArray[i].asJsonObject + val step = RealmCourseStep() + step.id = step_id + step.stepTitle = JsonUtils.getString("stepTitle", stepJson) + step.description = JsonUtils.getString("description", stepJson) + val stepDescription = JsonUtils.getString("description", stepJson) + val stepLinks = extractLinks(stepDescription) + for (stepLink in stepLinks) { + val concatenatedLink = "$baseUrl/$stepLink" + concatenatedLinks.add(concatenatedLink) + } + 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) + } - myMyCoursesDB?.courseSteps = RealmList() - myMyCoursesDB?.courseSteps?.addAll(courseStepsList) + myMyCoursesDB?.courseSteps = RealmList() + myMyCoursesDB?.courseSteps?.addAll(courseStepsList) - mRealm.commitTransaction() + 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() - ) - courseDataList.add(csvRow) + 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() + ) + courseDataList.add(csvRow) + } catch (e: Exception) { + Log.e("RealmMyCourse", "Error during insertion into courses: ${e.message}") + throw e + } } fun writeCsv(filePath: String, data: List>) { diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmMyLibrary.kt b/app/src/main/java/org/ole/planet/myplanet/model/RealmMyLibrary.kt index 30102a8b5b..c5d8888e32 100644 --- a/app/src/main/java/org/ole/planet/myplanet/model/RealmMyLibrary.kt +++ b/app/src/main/java/org/ole/planet/myplanet/model/RealmMyLibrary.kt @@ -413,7 +413,6 @@ open class RealmMyLibrary : RealmObject() { resource?.setTag(JsonUtils.getJsonArray("tags", doc), resource) resource?.isPrivate = JsonUtils.getBoolean("private", doc) resource?.setLanguages(JsonUtils.getJsonArray("languages", doc), resource) - mRealm.commitTransaction() val csvRow = arrayOf( JsonUtils.getString("_id", doc), diff --git a/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt b/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt index d6094f0893..77cd8d17ff 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt +++ b/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt @@ -87,47 +87,6 @@ class SyncManager private constructor(private val context: Context) { td?.start() } -// private fun startSync() { -// try { -// val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager -// val wifiInfo = wifiManager.connectionInfo -// if (wifiInfo.supplicantState == SupplicantState.COMPLETED) { -// settings.edit().putString("LastWifiSSID", wifiInfo.ssid).apply() -// } -// isSyncing = true -// create(context, R.mipmap.ic_launcher, " Syncing data", "Please wait...") -// mRealm = dbService.realmInstance -// TransactionSyncManager.syncDb(mRealm, "tablet_users") -// myLibraryTransactionSync() -// TransactionSyncManager.syncDb(mRealm, "courses") -// TransactionSyncManager.syncDb(mRealm, "exams") -// TransactionSyncManager.syncDb(mRealm, "ratings") -// TransactionSyncManager.syncDb(mRealm, "courses_progress") -// TransactionSyncManager.syncDb(mRealm, "achievements") -// TransactionSyncManager.syncDb(mRealm, "tags") -// TransactionSyncManager.syncDb(mRealm, "submissions") -// TransactionSyncManager.syncDb(mRealm, "news") -// TransactionSyncManager.syncDb(mRealm, "feedback") -// TransactionSyncManager.syncDb(mRealm, "teams") -// TransactionSyncManager.syncDb(mRealm, "tasks") -// TransactionSyncManager.syncDb(mRealm, "login_activities") -// TransactionSyncManager.syncDb(mRealm, "meetups") -// TransactionSyncManager.syncDb(mRealm, "health") -// TransactionSyncManager.syncDb(mRealm, "certifications") -// TransactionSyncManager.syncDb(mRealm, "team_activities") -// TransactionSyncManager.syncDb(mRealm, "chat_history") -// ManagerSync.instance?.syncAdmin() -// resourceTransactionSync() -// onSynced(mRealm, settings) -// mRealm.close() -// } catch (err: Exception) { -// err.printStackTrace() -// handleException(err.message) -// } finally { -// destroy() -// } -// } - private fun startSync() { try { val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager @@ -293,24 +252,6 @@ class SyncManager private constructor(private val context: Context) { } } -// private fun triggerInsert(stringArray: Array, resourceDoc: JsonObject) { -// when (stringArray[2]) { -// "resources" -> insertMyLibrary(stringArray[0], resourceDoc, mRealm) -// "meetups" -> insert(mRealm, resourceDoc) -// "courses" -> { -// if (!mRealm.isInTransaction){ -// mRealm.beginTransaction() -// } -// insertMyCourses(stringArray[0], resourceDoc, mRealm) -// if (mRealm.isInTransaction){ -// mRealm.commitTransaction() -// } -// } -// "teams" -> insertMyTeams(resourceDoc, mRealm) -// } -// saveConcatenatedLinksToPrefs() -// } - private fun triggerInsert(stringArray: Array, resourceDoc: JsonObject) { val realm = Realm.getDefaultInstance() try { @@ -324,18 +265,10 @@ class SyncManager private constructor(private val context: Context) { // Perform insertion based on category when (stringArray[2]) { - "resources" -> { - insertMyLibrary(stringArray[0], resourceDoc, realm) - } - "meetups" -> { - insert(realm, resourceDoc) - } - "courses" -> { - insertMyCourses(stringArray[0], resourceDoc, realm) - } - "teams" -> { - insertMyTeams(resourceDoc, realm) - } + "resources" -> insertMyLibrary(stringArray[0], resourceDoc, realm) + "meetups" -> insert(realm, resourceDoc) + "courses" -> insertMyCourses(stringArray[0], resourceDoc, realm) + "teams" -> insertMyTeams(resourceDoc, realm) } // Commit transaction if it is still in progress @@ -357,51 +290,31 @@ class SyncManager private constructor(private val context: Context) { } saveConcatenatedLinksToPrefs() } -// when (stringArray[2]) { -// "resources" -> { -// mRealm.beginTransaction() -// try { -// insertMyLibrary(stringArray[0], resourceDoc, mRealm) -// mRealm.commitTransaction() -// } catch (e: Exception) { -// mRealm.cancelTransaction() -// Log.e("SyncManager", "Error inserting into resources: ${e.message}") -// } -// } -// "meetups" -> { -// mRealm.beginTransaction() -// try { -// insert(mRealm, resourceDoc) -// mRealm.commitTransaction() -// } catch (e: Exception) { -// mRealm.cancelTransaction() -// Log.e("SyncManager", "Error inserting into meetups: ${e.message}") -// } -// } -// "courses" -> { -// mRealm.beginTransaction() -// try { -// insertMyCourses(stringArray[0], resourceDoc, mRealm) -// mRealm.commitTransaction() -// } catch (e: Exception) { -// mRealm.cancelTransaction() -// Log.e("SyncManager", "Error inserting into courses: ${e.message}") -// } -// } -// "teams" -> { -// mRealm.beginTransaction() -// try { -// insertMyTeams(resourceDoc, mRealm) -// mRealm.commitTransaction() -// } catch (e: Exception) { -// mRealm.cancelTransaction() -// Log.e("SyncManager", "Error inserting into teams: ${e.message}") +// private fun triggerInsert(stringArray: Array, resourceDoc: JsonObject) { +// val realm = Realm.getDefaultInstance() +// try { +// realm.executeTransaction { transactionRealm -> +// Log.d("SyncManager", "Transaction started for ${stringArray[2]}") +// +// // Perform insertion based on category +// when (stringArray[2]) { +// "resources" -> insertMyLibrary(stringArray[0], resourceDoc, transactionRealm) +// "meetups" -> insert(transactionRealm, resourceDoc) +// "courses" -> insertMyCourses(stringArray[0], resourceDoc, transactionRealm) +// "teams" -> insertMyTeams(resourceDoc, transactionRealm) // } +// +// Log.d("SyncManager", "Transaction committed for ${stringArray[2]}") // } +// } catch (e: Exception) { +// Log.e("SyncManager", "Error inserting into ${stringArray[2]}: ${e.message}") +// } finally { +// realm.close() // } // saveConcatenatedLinksToPrefs() // } + companion object { private var ourInstance: SyncManager? = null val instance: SyncManager?