11package org.ole.planet.myplanet.model
22
33import android.text.TextUtils
4- import android.util.Base64
54import com.google.gson.JsonArray
65import com.google.gson.JsonObject
76import io.realm.Realm
87import io.realm.RealmList
98import io.realm.RealmObject
109import io.realm.annotations.Index
1110import io.realm.annotations.PrimaryKey
12- import org.ole.planet.myplanet.model.RealmMyLibrary.Companion.createStepResource
13- import org.ole.planet.myplanet.model.RealmStepExam.Companion.insertCourseStepsExams
1411import org.ole.planet.myplanet.services.SharedPrefManager
15- import org.ole.planet.myplanet.utils.DownloadUtils.extractLinks
1612import org.ole.planet.myplanet.utils.JsonUtils
17- import org.ole.planet.myplanet.utils.UrlUtils
1813
1914open class RealmMyCourse : RealmObject () {
2015 @PrimaryKey
@@ -65,62 +60,13 @@ open class RealmMyCourse : RealmObject() {
6560 private val concatenatedLinks = HashSet <String >()
6661
6762 @JvmStatic
68- fun insertMyCourses (userId : String? , myCoursesDoc : JsonObject ? , mRealm : Realm , spm : SharedPrefManager ) {
69- val id = JsonUtils .getString(" _id" , myCoursesDoc)
70- var myMyCoursesDB = mRealm.where(RealmMyCourse ::class .java).equalTo(" id" , id).findFirst()
71- if (myMyCoursesDB == null ) {
72- myMyCoursesDB = mRealm.createObject(RealmMyCourse ::class .java, id)
73- }
74- myMyCoursesDB?.setUserId(userId)
75- myMyCoursesDB?.courseId = JsonUtils .getString(" _id" , myCoursesDoc)
76- myMyCoursesDB?.courseRev = JsonUtils .getString(" _rev" , myCoursesDoc)
77- myMyCoursesDB?.languageOfInstruction = JsonUtils .getString(" languageOfInstruction" , myCoursesDoc)
78- myMyCoursesDB?.courseTitle = JsonUtils .getString(" courseTitle" , myCoursesDoc)
79- myMyCoursesDB?.memberLimit = JsonUtils .getInt(" memberLimit" , myCoursesDoc)
80- val description = JsonUtils .getString(" description" , myCoursesDoc)
81- myMyCoursesDB?.description = description
82- val links = extractLinks(description)
83- val baseUrl = UrlUtils .getUrl()
63+ fun addConcatenatedLink (link : String ) {
8464 synchronized(concatenatedLinks) {
85- for (link in links) {
86- concatenatedLinks.add(" $baseUrl /$link " )
87- }
65+ concatenatedLinks.add(link)
8866 }
89- myMyCoursesDB?.method = JsonUtils .getString(" method" , myCoursesDoc)
90- myMyCoursesDB?.gradeLevel = JsonUtils .getString(" gradeLevel" , myCoursesDoc)
91- myMyCoursesDB?.subjectLevel = JsonUtils .getString(" subjectLevel" , myCoursesDoc)
92- myMyCoursesDB?.createdDate = JsonUtils .getLong(" createdDate" , myCoursesDoc)
93- val courseStepsJsonArray = JsonUtils .getJsonArray(" steps" , myCoursesDoc)
94- val stepsSize = courseStepsJsonArray.size()
95- myMyCoursesDB?.setNumberOfSteps(stepsSize)
96- val courseStepsList = mutableListOf<RealmCourseStep >()
97-
98- for (i in 0 until stepsSize) {
99- val stepElement = courseStepsJsonArray[i]
100- val stepId = Base64 .encodeToString(stepElement.toString().toByteArray(), Base64 .NO_WRAP )
101- val stepJson = stepElement.asJsonObject
102- val step = RealmCourseStep ()
103- step.id = stepId
104- step.stepTitle = JsonUtils .getString(" stepTitle" , stepJson)
105- val stepDescription = JsonUtils .getString(" description" , stepJson)
106- step.description = stepDescription
107- val stepLinks = extractLinks(stepDescription)
108- synchronized(concatenatedLinks) {
109- for (stepLink in stepLinks) {
110- concatenatedLinks.add(" $baseUrl /$stepLink " )
111- }
112- }
113- insertCourseStepsAttachments(myMyCoursesDB?.courseId, stepId, JsonUtils .getJsonArray(" resources" , stepJson), mRealm, spm)
114- insertExam(stepJson, mRealm, stepId, i + 1 , myMyCoursesDB?.courseId)
115- insertSurvey(stepJson, mRealm, stepId, i + 1 , myMyCoursesDB?.courseId, myMyCoursesDB?.createdDate)
116- step.noOfResources = JsonUtils .getJsonArray(" resources" , stepJson).size()
117- step.courseId = myMyCoursesDB?.courseId
118- courseStepsList.add(step)
119- }
120- myMyCoursesDB?.courseSteps = RealmList ()
121- myMyCoursesDB?.courseSteps?.addAll(courseStepsList)
12267 }
12368
69+
12470 @JvmStatic
12571 fun saveConcatenatedLinksToPrefs (spm : SharedPrefManager ) {
12672 val existingJsonLinks = spm.getConcatenatedLinks()
@@ -142,49 +88,9 @@ open class RealmMyCourse : RealmObject() {
14288 }
14389
14490
145- private fun insertExam (stepContainer : JsonObject , mRealm : Realm , stepId : String , i : Int , myCoursesID : String? ) {
146- if (stepContainer.has(" exam" )) {
147- val `object ` = stepContainer.getAsJsonObject(" exam" )
148- `object `.addProperty(" stepNumber" , i)
149- insertCourseStepsExams(myCoursesID, stepId, `object `, mRealm)
150- }
151- }
15291
153- private fun insertSurvey (stepContainer : JsonObject , mRealm : Realm , stepId : String , i : Int , myCoursesID : String? , createdDate : Long? ) {
154- if (stepContainer.has(" survey" )) {
155- val `object ` = stepContainer.getAsJsonObject(" survey" )
156- `object `.addProperty(" stepNumber" , i)
157- `object `.addProperty(" createdDate" , createdDate)
158- insertCourseStepsExams(myCoursesID, stepId, `object `, mRealm)
159- }
160- }
16192
162- private fun insertCourseStepsAttachments (myCoursesID : String? , stepId : String? , resources : JsonArray , mRealm : Realm ? , spm : SharedPrefManager ) {
163- resources.forEach { resource ->
164- if (mRealm != null ) {
165- createStepResource(mRealm, resource.asJsonObject, myCoursesID, stepId, spm)
166- }
167- }
168- }
16993
170- @JvmStatic
171- fun insert (mRealm : Realm , myCoursesDoc : JsonObject ? , spm : SharedPrefManager ) {
172- val startedTransaction = ! mRealm.isInTransaction
173- if (startedTransaction) {
174- mRealm.beginTransaction()
175- }
176- try {
177- insertMyCourses(" " , myCoursesDoc, mRealm, spm)
178- if (startedTransaction) {
179- mRealm.commitTransaction()
180- }
181- } catch (e: Exception ) {
182- if (startedTransaction && mRealm.isInTransaction) {
183- mRealm.cancelTransaction()
184- }
185- throw e
186- }
187- }
18894
18995 @JvmStatic
19096 fun serialize (course : RealmMyCourse , realm : Realm ): JsonObject {
0 commit comments