Skip to content

Commit 22efb39

Browse files
committed
Migrate EventSyncAdapter
1 parent 15dc2da commit 22efb39

File tree

5 files changed

+151
-88
lines changed

5 files changed

+151
-88
lines changed

libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/EventSyncAdapter.java

-85
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package eu.pretix.libpretixsync.sync
2+
3+
import app.cash.sqldelight.TransactionWithoutReturn
4+
import eu.pretix.libpretixsync.api.PretixApi
5+
import eu.pretix.libpretixsync.db.Migrations
6+
import eu.pretix.libpretixsync.sqldelight.Event
7+
import eu.pretix.libpretixsync.sqldelight.SyncDatabase
8+
import eu.pretix.libpretixsync.sync.SyncManager.ProgressFeedback
9+
import eu.pretix.libpretixsync.utils.JSONUtils
10+
import org.joda.time.format.ISODateTimeFormat
11+
import org.json.JSONException
12+
import org.json.JSONObject
13+
14+
class EventSyncAdapter(
15+
db: SyncDatabase,
16+
eventSlug: String,
17+
key: String,
18+
api: PretixApi,
19+
syncCycleId: String,
20+
feedback: ProgressFeedback? = null,
21+
) : SqBaseSingleObjectSyncAdapter<Event>(
22+
db,
23+
eventSlug,
24+
key,
25+
api,
26+
syncCycleId,
27+
feedback,
28+
) {
29+
30+
override fun getKnownObject(): Event? {
31+
val known = db.eventQueries.selectBySlug(eventSlug).executeAsList()
32+
33+
return if (known.isEmpty()) {
34+
null
35+
} else if (known.size == 1) {
36+
known[0]
37+
} else {
38+
// What's going on here? Let's delete and re-fetch
39+
db.eventQueries.deleteBySlug(eventSlug)
40+
null
41+
}
42+
}
43+
44+
override fun getResourceName(): String = "events"
45+
46+
override fun getUrl(): String = api.organizerResourceUrl("events/$key")
47+
48+
override fun getJSON(obj: Event): JSONObject = JSONObject(obj.json_data!!)
49+
50+
override fun insert(jsonobj: JSONObject) {
51+
val dateFrom =
52+
ISODateTimeFormat.dateTimeParser().parseDateTime(jsonobj.getString("date_from"))
53+
.toDate()
54+
55+
val dateTo = if (!jsonobj.isNull("date_to")) {
56+
ISODateTimeFormat.dateTimeParser().parseDateTime(jsonobj.getString("date_to")).toDate()
57+
} else {
58+
null
59+
}
60+
61+
db.eventQueries.insert(
62+
currency = jsonobj.getString("currency"),
63+
date_to = dateTo,
64+
date_from = dateFrom,
65+
has_subevents = jsonobj.getBoolean("has_subevents"),
66+
json_data = jsonobj.toString(),
67+
live = jsonobj.getBoolean("live"),
68+
slug = jsonobj.getString("slug"),
69+
)
70+
}
71+
72+
override fun update(obj: Event, jsonobj: JSONObject) {
73+
val dateFrom =
74+
ISODateTimeFormat.dateTimeParser().parseDateTime(jsonobj.getString("date_from"))
75+
.toDate()
76+
77+
val dateTo = if (!jsonobj.isNull("date_to")) {
78+
ISODateTimeFormat.dateTimeParser().parseDateTime(jsonobj.getString("date_to")).toDate()
79+
} else {
80+
null
81+
}
82+
83+
db.eventQueries.insert(
84+
currency = jsonobj.getString("currency"),
85+
date_to = dateTo,
86+
date_from = dateFrom,
87+
has_subevents = jsonobj.getBoolean("has_subevents"),
88+
json_data = jsonobj.toString(),
89+
live = jsonobj.getBoolean("live"),
90+
slug = obj.slug,
91+
)
92+
}
93+
94+
override fun runInTransaction(body: TransactionWithoutReturn.() -> Unit) {
95+
db.eventQueries.transaction(false, body)
96+
}
97+
98+
@Throws(JSONException::class)
99+
fun standaloneRefreshFromJSON(data: JSONObject) {
100+
// Store object
101+
data.put("__libpretixsync_dbversion", Migrations.CURRENT_VERSION)
102+
data.put("__libpretixsync_syncCycleId", syncCycleId)
103+
val known = getKnownObject()
104+
if (known == null) {
105+
insert(data)
106+
} else {
107+
val old = JSONObject(known.json_data!!)
108+
if (!JSONUtils.similar(data, old)) {
109+
update(known, data)
110+
}
111+
}
112+
}
113+
}

libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ protected void downloadData(ProgressFeedback feedback, Boolean skip_orders, Stri
416416
subEvent = overrideSubeventId;
417417
}
418418
try {
419-
download(new EventSyncAdapter(dataStore, eventSlug, eventSlug, api, configStore.getSyncCycleId(), feedback));
419+
download(new EventSyncAdapter(db, eventSlug, eventSlug, api, configStore.getSyncCycleId(), feedback));
420420
} catch (PermissionDeniedApiException e) {
421421
e.eventSlug = eventSlug;
422422
throw e;

libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Event.sq

+35
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,38 @@ selectBySlug:
77
SELECT *
88
FROM Event
99
WHERE slug = ?;
10+
11+
deleteBySlug:
12+
DELETE FROM Event
13+
WHERE slug = ?;
14+
15+
insert:
16+
INSERT INTO Event (
17+
currency,
18+
date_from,
19+
date_to,
20+
has_subevents,
21+
json_data,
22+
live,
23+
slug
24+
)
25+
VALUES (
26+
?,
27+
?,
28+
?,
29+
?,
30+
?,
31+
?,
32+
?
33+
);
34+
35+
updateFromJson:
36+
UPDATE Event
37+
SET
38+
currency = ?,
39+
date_from = ?,
40+
date_to = ?,
41+
has_subevents = ?,
42+
json_data = ?,
43+
live = ?
44+
WHERE slug = ?;

libpretixsync/src/test/java/eu/pretix/libpretixsync/check/AsyncCheckProviderTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class AsyncCheckProviderTest : BaseDatabaseTest() {
3030
fakeApi = FakePretixApi()
3131
p = AsyncCheckProvider(configStore!!, dataStore)
3232

33-
EventSyncAdapter(dataStore, "demo", "demo", fakeApi, "", null).standaloneRefreshFromJSON(jsonResource("events/event1.json"))
34-
EventSyncAdapter(dataStore, "demo", "demo", fakeApi, "", null).standaloneRefreshFromJSON(jsonResource("events/event2.json"))
33+
EventSyncAdapter(db, "demo", "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("events/event1.json"))
34+
EventSyncAdapter(db, "demo", "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("events/event2.json"))
3535
ItemSyncAdapter(db, FakeFileStorage(), "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("items/item1.json"))
3636
ItemSyncAdapter(db, FakeFileStorage(), "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("items/item2.json"))
3737
ItemSyncAdapter(db, FakeFileStorage(), "demo2", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("items/event2-item3.json"))

0 commit comments

Comments
 (0)