|
| 1 | +package eu.pretix.libpretixsync.sync |
| 2 | + |
| 3 | +import app.cash.sqldelight.TransactionWithoutReturn |
| 4 | +import app.cash.sqldelight.db.QueryResult |
| 5 | +import eu.pretix.libpretixsync.api.PretixApi |
| 6 | +import eu.pretix.libpretixsync.db.Migrations |
| 7 | +import eu.pretix.libpretixsync.sqldelight.Question |
| 8 | +import eu.pretix.libpretixsync.sqldelight.SyncDatabase |
| 9 | +import eu.pretix.libpretixsync.sync.SyncManager.ProgressFeedback |
| 10 | +import eu.pretix.libpretixsync.utils.JSONUtils |
| 11 | +import org.json.JSONException |
| 12 | +import org.json.JSONObject |
| 13 | + |
| 14 | +class QuestionSyncAdapter( |
| 15 | + db: SyncDatabase, |
| 16 | + fileStorage: FileStorage, |
| 17 | + eventSlug: String, |
| 18 | + api: PretixApi, |
| 19 | + syncCycleId: String, |
| 20 | + feedback: ProgressFeedback?, |
| 21 | +) : SqBaseConditionalSyncAdapter<Question, Long>( |
| 22 | + db = db, |
| 23 | + fileStorage = fileStorage, |
| 24 | + eventSlug = eventSlug, |
| 25 | + api = api, |
| 26 | + syncCycleId = syncCycleId, |
| 27 | + feedback = feedback, |
| 28 | +) { |
| 29 | + |
| 30 | + override fun getResourceName(): String = "questions" |
| 31 | + |
| 32 | + override fun getId(obj: Question): Long = obj.server_id!! |
| 33 | + |
| 34 | + override fun getId(obj: JSONObject): Long = obj.getLong("id") |
| 35 | + |
| 36 | + override fun getJSON(obj: Question): JSONObject = JSONObject(obj.json_data!!) |
| 37 | + |
| 38 | + override fun queryKnownIDs(): MutableSet<Long>? { |
| 39 | + val res = mutableSetOf<Long>() |
| 40 | + db.questionQueries.selectServerIdsByEventSlug(event_slug = eventSlug).execute { cursor -> |
| 41 | + while (cursor.next().value) { |
| 42 | + val id = cursor.getLong(0) ?: throw RuntimeException("id column not available") |
| 43 | + res.add(id) |
| 44 | + } |
| 45 | + |
| 46 | + QueryResult.Unit |
| 47 | + } |
| 48 | + |
| 49 | + return res |
| 50 | + } |
| 51 | + |
| 52 | + override fun insert(jsonobj: JSONObject) { |
| 53 | + val questionId = db.questionQueries.transactionWithResult { |
| 54 | + db.questionQueries.insert( |
| 55 | + event_slug = eventSlug, |
| 56 | + json_data = jsonobj.toString(), |
| 57 | + position = jsonobj.getLong("position"), |
| 58 | + required = jsonobj.optBoolean("required", false), |
| 59 | + server_id = jsonobj.getLong("id"), |
| 60 | + ) |
| 61 | + db.compatQueries.getLastInsertedQuestionId().executeAsOne() |
| 62 | + } |
| 63 | + |
| 64 | + upsertItemRelations(questionId, emptySet(), jsonobj) |
| 65 | + } |
| 66 | + |
| 67 | + override fun update(obj: Question, jsonobj: JSONObject) { |
| 68 | + val existingRelations = db.questionQueries.selectRelationsForQuestion(obj.id) |
| 69 | + .executeAsList() |
| 70 | + .map { it.ItemId } |
| 71 | + .toSet() |
| 72 | + |
| 73 | + db.questionQueries.updateFromJson( |
| 74 | + event_slug = eventSlug, |
| 75 | + json_data = jsonobj.toString(), |
| 76 | + position = jsonobj.getLong("position"), |
| 77 | + required = jsonobj.optBoolean("required", false), |
| 78 | + id = obj.id, |
| 79 | + ) |
| 80 | + |
| 81 | + upsertItemRelations(obj.id, existingRelations, jsonobj) |
| 82 | + } |
| 83 | + |
| 84 | + private fun upsertItemRelations(questionId: Long, existingIds: Set<Long>, jsonobj: JSONObject) { |
| 85 | + val itemsarr = jsonobj.getJSONArray("items") |
| 86 | + val itemids = ArrayList<Long>(itemsarr.length()) |
| 87 | + for (i in 0 until itemsarr.length()) { |
| 88 | + itemids.add(itemsarr.getLong(i)) |
| 89 | + } |
| 90 | + val newIds = db.itemQueries.selectByServerIdListAndEventSlug( |
| 91 | + server_id = itemids, |
| 92 | + event_slug = eventSlug, |
| 93 | + ).executeAsList().map { it.id }.toSet() |
| 94 | + |
| 95 | + for (newId in newIds - existingIds) { |
| 96 | + db.questionQueries.insertItemRelation( |
| 97 | + item_id = newId, |
| 98 | + question_id = questionId, |
| 99 | + ) |
| 100 | + } |
| 101 | + for (oldId in existingIds - newIds) { |
| 102 | + db.questionQueries.deleteItemRelation( |
| 103 | + item_id = oldId, |
| 104 | + question_id = questionId, |
| 105 | + ) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + override fun delete(key: Long) { |
| 110 | + db.questionQueries.deleteItemRelationsForQuestion(key) |
| 111 | + db.questionQueries.deleteByServerId(key) |
| 112 | + } |
| 113 | + |
| 114 | + override fun runInTransaction(body: TransactionWithoutReturn.() -> Unit) { |
| 115 | + db.questionQueries.transaction(false, body) |
| 116 | + } |
| 117 | + |
| 118 | + override fun runBatch(parameterBatch: List<Long>): List<Question> = |
| 119 | + db.questionQueries.selectByServerIdListAndEventSlug( |
| 120 | + server_id = parameterBatch, |
| 121 | + event_slug = eventSlug, |
| 122 | + ).executeAsList() |
| 123 | + |
| 124 | + @Throws(JSONException::class) |
| 125 | + fun standaloneRefreshFromJSON(data: JSONObject) { |
| 126 | + val known = db.questionQueries.selectByServerId(data.getLong("id")).executeAsOneOrNull() |
| 127 | + |
| 128 | + // Store object |
| 129 | + data.put("__libpretixsync_dbversion", Migrations.CURRENT_VERSION) |
| 130 | + data.put("__libpretixsync_syncCycleId", syncCycleId) |
| 131 | + if (known == null) { |
| 132 | + insert(data) |
| 133 | + } else { |
| 134 | + val old = JSONObject(known.json_data!!) |
| 135 | + if (!JSONUtils.similar(data, old)) { |
| 136 | + update(known, data) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments