@@ -94,7 +94,7 @@ class SyncManager private constructor(private val context: Context) {
94
94
settings.edit().putString(" LastWifiSSID" , wifiInfo.ssid).apply ()
95
95
}
96
96
isSyncing = true
97
- create(context, R .mipmap.ic_launcher, " Syncing data" , " Please wait..." )
97
+ create(context, R .mipmap.ic_launcher, " Syncing data" , " Please wait..." )
98
98
mRealm = dbService.realmInstance
99
99
TransactionSyncManager .syncDb(mRealm, " tablet_users" )
100
100
myLibraryTransactionSync()
@@ -144,50 +144,111 @@ class SyncManager private constructor(private val context: Context) {
144
144
}
145
145
}
146
146
147
- @Throws(IOException ::class )
148
147
private fun syncResource (dbClient : ApiInterface ? ) {
149
148
val newIds: MutableList <String ?> = ArrayList ()
150
- val allDocs = dbClient?.getJsonObject(Utilities .header, Utilities .getUrl() + " /resources/_all_docs?include_doc=false" )
151
- val all = allDocs?.execute()
152
- val rows = getJsonArray(" rows" , all?.body())
153
- val keys: MutableList <String > = ArrayList ()
154
- for (i in 0 until rows.size()) {
155
- val `object ` = rows[i].asJsonObject
156
- if (! TextUtils .isEmpty(getString(" id" , `object `))) keys.add(getString(" key" , `object `))
157
- if (i == rows.size() - 1 || keys.size == 1000 ) {
158
- val obj = JsonObject ()
159
- obj.add(" keys" , Gson ().fromJson(Gson ().toJson(keys), JsonArray ::class .java))
160
- val response = dbClient?.findDocs(Utilities .header, " application/json" , Utilities .getUrl() + " /resources/_all_docs?include_docs=true" , obj)?.execute()
161
- if (response?.body() != null ) {
162
- val ids: List <String ?> = save(getJsonArray(" rows" , response.body()), mRealm)
163
- newIds.addAll(ids)
149
+ try {
150
+ val allDocs = dbClient?.getJsonObject(Utilities .header, " ${Utilities .getUrl()} /resources/_all_docs?include_doc=false" )
151
+ val all = allDocs?.execute()
152
+ if (all?.isSuccessful != true ) {
153
+ return
154
+ }
155
+
156
+ val rows = getJsonArray(" rows" , all.body())
157
+ val keys: MutableList <String > = ArrayList ()
158
+ val failedIds: MutableList <String > = ArrayList ()
159
+
160
+ for (i in 0 until rows.size()) {
161
+ val `object ` = rows[i].asJsonObject
162
+ if (! TextUtils .isEmpty(getString(" id" , `object `))) {
163
+ keys.add(getString(" key" , `object `))
164
+ }
165
+
166
+ if (i == rows.size() - 1 || keys.size == 1000 ) {
167
+ val obj = JsonObject ()
168
+ obj.add(" keys" , Gson ().fromJson(Gson ().toJson(keys), JsonArray ::class .java))
169
+ val response = dbClient.findDocs(Utilities .header, " application/json" , " ${Utilities .getUrl()} /resources/_all_docs?include_docs=true" , obj).execute()
170
+
171
+ when {
172
+ response.isSuccessful == true -> {
173
+ response.body()?.let { body ->
174
+ val ids: List <String ?> = save(getJsonArray(" rows" , body), mRealm)
175
+ newIds.addAll(ids)
176
+ }
177
+ }
178
+ response.code() == 404 -> {
179
+ failedIds.addAll(keys)
180
+ }
181
+ else -> {
182
+ val errorMessage = " Failed to sync resources: ${response.code()} "
183
+ handleException(errorMessage)
184
+
185
+ when (response.code()) {
186
+ in 500 .. 599 -> {
187
+ addToRetryQueue(keys)
188
+ }
189
+ 401 , 403 -> {
190
+ handleAuthenticationError()
191
+ }
192
+ else -> {
193
+ failedIds.addAll(keys)
194
+ }
195
+ }
196
+ }
197
+ }
198
+ keys.clear()
164
199
}
165
- keys.clear()
200
+ }
201
+ } catch (e: Exception ) {
202
+ e.printStackTrace()
203
+ } finally {
204
+ try {
205
+ removeDeletedResource(newIds, mRealm)
206
+ } catch (e: Exception ) {
207
+ e.printStackTrace()
166
208
}
167
209
}
168
- removeDeletedResource(newIds, mRealm)
210
+ }
211
+
212
+ private fun addToRetryQueue (keys : List <String >) {
213
+ settings.edit().apply {
214
+ val existingQueue = settings.getStringSet(" retry_queue" , setOf ()) ? : setOf ()
215
+ putStringSet(" retry_queue" , existingQueue + keys)
216
+ apply ()
217
+ }
218
+ }
219
+
220
+ private fun handleAuthenticationError () {
221
+ settings.edit().remove(" credentials" ).apply ()
222
+ handleException(" Authentication failed." )
169
223
}
170
224
171
225
private fun myLibraryTransactionSync () {
172
226
val apiInterface = client?.create(ApiInterface ::class .java)
173
227
try {
174
- val res = apiInterface?.getDocuments(Utilities .header, Utilities .getUrl() + " /shelf/_all_docs" )?.execute()?.body()
175
- for (i in res?.rows!! .indices) {
176
- shelfDoc = res.rows!! [i]
177
- populateShelfItems(apiInterface)
228
+ val response = apiInterface?.getDocuments(Utilities .header, " ${Utilities .getUrl()} /shelf/_all_docs" )?.execute()
229
+
230
+ val res = response?.body()
231
+ res?.rows?.let { rows ->
232
+ for (i in rows.indices) {
233
+ shelfDoc = rows[i]
234
+ populateShelfItems(apiInterface)
235
+ }
178
236
}
179
237
} catch (e: IOException ) {
180
238
e.printStackTrace()
181
239
}
182
240
}
183
241
184
- private fun populateShelfItems (apiInterface : ApiInterface ) {
242
+ private fun populateShelfItems (apiInterface : ApiInterface ? ) {
185
243
try {
186
- val jsonDoc = apiInterface.getJsonObject(Utilities .header, Utilities .getUrl() + " /shelf/" + shelfDoc?.id).execute().body()
187
- for (i in Constants .shelfDataList.indices) {
188
- val shelfData = Constants .shelfDataList[i]
189
- val array = getJsonArray(shelfData.key, jsonDoc)
190
- memberShelfData(array, shelfData)
244
+ val response = apiInterface?.getJsonObject(Utilities .header, " ${Utilities .getUrl()} /shelf/${shelfDoc?.id} " )?.execute()
245
+
246
+ response?.body()?.let { jsonDoc ->
247
+ for (i in Constants .shelfDataList.indices) {
248
+ val shelfData = Constants .shelfDataList[i]
249
+ val array = getJsonArray(shelfData.key, jsonDoc)
250
+ memberShelfData(array, shelfData)
251
+ }
191
252
}
192
253
} catch (err: Exception ) {
193
254
err.printStackTrace()
@@ -217,10 +278,27 @@ class SyncManager private constructor(private val context: Context) {
217
278
}
218
279
219
280
private fun validateDocument (arrayCategoryIds : JsonArray , x : Int ) {
220
- val apiInterface = client!! .create(ApiInterface ::class .java)
281
+ val apiInterface = client? .create(ApiInterface ::class .java)
221
282
try {
222
- val resourceDoc = apiInterface.getJsonObject(Utilities .header, Utilities .getUrl() + " /" + stringArray[2 ] + " /" + arrayCategoryIds[x].asString).execute().body()
223
- resourceDoc?.let { triggerInsert(stringArray, it) }
283
+ val response = apiInterface?.getJsonObject(Utilities .header, " ${Utilities .getUrl()} /${stringArray[2 ]} /${arrayCategoryIds[x].asString} " )?.execute()
284
+
285
+ when {
286
+ response?.isSuccessful == true -> {
287
+ response.body()?.let { resourceDoc ->
288
+ triggerInsert(stringArray, resourceDoc)
289
+ }
290
+ }
291
+ response?.code() == 404 -> {
292
+ return
293
+ }
294
+ else -> {
295
+ val errorMessage = " Failed to validate document: ${response?.code()} "
296
+ handleException(errorMessage)
297
+ if (response?.code() in 500 .. 599 ) {
298
+ throw IOException (errorMessage)
299
+ }
300
+ }
301
+ }
224
302
} catch (e: IOException ) {
225
303
e.printStackTrace()
226
304
}
0 commit comments