Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
strawberrybread authored and Okuro3499 committed Aug 30, 2024
1 parent 825e310 commit 15c31b1
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 176 deletions.
143 changes: 77 additions & 66 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<RealmCourseStep>()

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<RealmCourseStep>()

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<Array<String>>) {
Expand Down
133 changes: 23 additions & 110 deletions app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -293,24 +252,6 @@ class SyncManager private constructor(private val context: Context) {
}
}

// private fun triggerInsert(stringArray: Array<String?>, 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<String?>, resourceDoc: JsonObject) {
val realm = Realm.getDefaultInstance()
try {
Expand All @@ -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
Expand All @@ -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<String?>, 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?
Expand Down

0 comments on commit 15c31b1

Please sign in to comment.