From 3428e0ce1be447af345efde0c8cb6657ca8a754a Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Mon, 6 Jan 2025 20:38:23 +0100 Subject: [PATCH] Sync Creations with Server --- .../java/core/notevault/data/MusicNote.java | 24 ++++- .../core/notevault/data/MusicNoteDAO.java | 3 + .../java/core/notevault/sync/SyncWorker.java | 96 ++----------------- .../synchronisation/SyncChangeRequest.java | 29 ------ .../synchronisation/SyncCreateResponse.java | 22 ----- .../sync/synchronisation/SyncRequest.java | 50 ---------- .../synchronisation/SyncResponseModel.java | 42 -------- .../sync/synchronisation/SyncService.java | 6 +- .../synchronisation/songs/SongSyncWorker.java | 57 +++++++++++ .../creation/SongCreationBatchRequest.java | 11 +++ .../creation/SongCreationBatchResponse.java | 19 ++++ .../songs/creation/SongCreationRequest.java | 17 ++++ .../songs/creation/SongCreationResponse.java | 27 ++++++ 13 files changed, 169 insertions(+), 234 deletions(-) delete mode 100644 app/src/main/java/core/notevault/sync/synchronisation/SyncChangeRequest.java delete mode 100644 app/src/main/java/core/notevault/sync/synchronisation/SyncCreateResponse.java delete mode 100644 app/src/main/java/core/notevault/sync/synchronisation/SyncRequest.java delete mode 100644 app/src/main/java/core/notevault/sync/synchronisation/SyncResponseModel.java create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationBatchRequest.java create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationBatchResponse.java create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationRequest.java create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationResponse.java diff --git a/app/src/main/java/core/notevault/data/MusicNote.java b/app/src/main/java/core/notevault/data/MusicNote.java index 90135fb..36fb3ea 100644 --- a/app/src/main/java/core/notevault/data/MusicNote.java +++ b/app/src/main/java/core/notevault/data/MusicNote.java @@ -3,12 +3,16 @@ package core.notevault.data; import androidx.room.Entity; import androidx.room.Ignore; import androidx.room.PrimaryKey; +import core.notevault.data.sync.SyncStatus; @Entity(tableName = "music_notes") public class MusicNote { @PrimaryKey(autoGenerate = true) private long musicNoteId; + private String serverID; + private SyncStatus syncStatus; + private String title; private String composer; private int year; @@ -20,16 +24,16 @@ public class MusicNote { this.composer = composer; this.year = year; this.genre = genre; + this.syncStatus = SyncStatus.CREATED; } public MusicNote(long musicNoteId, String title) { this.musicNoteId = musicNoteId; this.title = title; + this.syncStatus = SyncStatus.CREATED; } @Ignore - - public MusicNote() { } @@ -72,4 +76,20 @@ public class MusicNote { public void setYear(int year) { this.year = year; } + + public String getServerID() { + return serverID; + } + + public void setServerID(String serverID) { + this.serverID = serverID; + } + + public SyncStatus getSyncStatus() { + return syncStatus; + } + + public void setSyncStatus(SyncStatus syncStatus) { + this.syncStatus = syncStatus; + } } diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index c6e1677..c58e171 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -25,6 +25,9 @@ public interface MusicNoteDAO { @Query("SELECT * FROM concerts WHERE syncStatus = :status") List getConcertsWithSyncStatus(SyncStatus status); + @Query("SELECT * FROM music_notes WHERE syncStatus = :status") + List getSongsWithSyncStatus(SyncStatus status); + @Insert void insertConcert(Concert concert); diff --git a/app/src/main/java/core/notevault/sync/SyncWorker.java b/app/src/main/java/core/notevault/sync/SyncWorker.java index eda9cfe..c3b978e 100644 --- a/app/src/main/java/core/notevault/sync/SyncWorker.java +++ b/app/src/main/java/core/notevault/sync/SyncWorker.java @@ -12,6 +12,8 @@ import core.notevault.data.sync.SyncObject; import core.notevault.data.sync.SyncResponse; import core.notevault.data.sync.SyncStatus; import core.notevault.sync.synchronisation.*; +import core.notevault.sync.synchronisation.songs.SongSyncWorker; +import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -24,104 +26,24 @@ import java.util.concurrent.CountDownLatch; import java.util.stream.Collectors; public class SyncWorker extends Worker{ - private SyncService syncService; - private MusicNoteDAO database; + private SongSyncWorker songSyncWorker; public SyncWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); - this.syncService = ApiClient.getRetrofitInstance(context).create(SyncService.class); + SyncService syncService = ApiClient.getRetrofitInstance(context).create(SyncService.class); MusicDatabase musicDatabase = MusicDatabase.getDatabase(context); - database = musicDatabase.musicNoteDao(); + this.songSyncWorker = new SongSyncWorker(syncService, musicDatabase.musicNoteDao(), context); } @NonNull @Override public Result doWork() { - SyncRequest concertSync = buildConcertSyncRequest(); - - final CountDownLatch latch = new CountDownLatch(1); - final Result[] result = {Result.failure()}; - - syncWithServer(concertSync, new Callback() { - @Override - public void onResponse(Call call, Response response) { - if(response.isSuccessful()) { - result[0] = Result.success(); - Log.d("SyncWorker", "Sync finished, process now"); - processConcertSynResponse(response.body()); - } else { - Log.d("SyncWorker", "Sync failed"); - if (response.errorBody() != null) { - try { - String errorBody = response.errorBody().string(); - Log.d("SyncWorker", "Error Body: " + errorBody); - } catch (IOException e) { - e.printStackTrace(); - } - } - result[0] = Result.failure(); - } - latch.countDown(); - } - - @Override - public void onFailure(Call call, Throwable throwable) { - result[0] = Result.failure(); - Log.d("SyncWorker", "Sync failure"); - latch.countDown(); - } - }); - try { - latch.await(); - } catch (InterruptedException e) { -// throw new RuntimeException(e); + songSyncWorker.syncSongCreations(); + return Result.success(); + } catch (Exception e) { + return Result.failure(); } - return result[0]; } - private SyncRequest buildConcertSyncRequest() { - List createdConcerts = database.getConcertsWithSyncStatus(SyncStatus.CREATED); - List deletedConcerts = database.getConcertsWithSyncStatus(SyncStatus.DELETED); - List modifiedConcerts = database.getConcertsWithSyncStatus(SyncStatus.MODIFIED); - - List deviceIDsCreatedConcerts = createdConcerts.stream().map(Concert::getId).collect(Collectors.toList()); - List modifiedSyncRequests = modifiedConcerts.stream().map(concert -> - new SyncChangeRequest(concert.getServerSpecificConcertID(), concert.getLastModified())).collect(Collectors.toList()); - List deletedSyncRequests = deletedConcerts.stream().map(concert -> - new SyncChangeRequest(concert.getServerSpecificConcertID(), concert.getLastModified())).collect(Collectors.toList()); - - Log.d("SyncWorker", "Build SyncRequest"); - // Datenbankabfrage: Nur IDs mit Zustand "Modified" oder "Deleted" abrufen - // Dies ist abhängig von der spezifischen Implementierung der Datenbank - return new SyncRequest(database.getLatestSyncTimestamp(), deletedSyncRequests, modifiedSyncRequests, deviceIDsCreatedConcerts); - } - - private void syncWithServer(SyncRequest syncRequest, Callback callback) { - syncService.syncConcerts(syncRequest).enqueue(callback); - } - - private void processConcertSynResponse(SyncResponseModel syncResponse) { - Log.d("SyncWorker", "Process SyncResponse"); - //Assign local Concert entries the provided Server ID - List syncResponses = new ArrayList<>(); - for(SyncCreateResponse createResponse : syncResponse.getCreateResponses()) { - database.updateConcertServerUUID(createResponse.getServerObjectUUID(), createResponse.getDeviceSpecificObjectID()); - syncResponses.add(new SyncResponse(createResponse.getServerObjectUUID(), SyncAction.TOBEUPLOADED, SyncObject.CONCERT)); - } - - //Delete Concerts that the server has marked as deleted - database.deleteConcerts(syncResponse.getToBeDeleted()); - - //Persist SyncResponse - - for(String toBeDownloaded : syncResponse.getToBeDownloaded()) { - syncResponses.add(new SyncResponse(toBeDownloaded, SyncAction.TOBEDOWNLOADED, SyncObject.CONCERT)); - } - - for(String toBeUploaded : syncResponse.getToBeUploaded()) { - syncResponses.add(new SyncResponse(toBeUploaded, SyncAction.TOBEUPLOADED, SyncObject.CONCERT)); - } - database.insertSyncResponses(syncResponses); - } } diff --git a/app/src/main/java/core/notevault/sync/synchronisation/SyncChangeRequest.java b/app/src/main/java/core/notevault/sync/synchronisation/SyncChangeRequest.java deleted file mode 100644 index 213b2ff..0000000 --- a/app/src/main/java/core/notevault/sync/synchronisation/SyncChangeRequest.java +++ /dev/null @@ -1,29 +0,0 @@ -package core.notevault.sync.synchronisation; - -import java.time.LocalDateTime; - -public class SyncChangeRequest { - private String serverUUID; - private LocalDateTime lastModified; - - public SyncChangeRequest(String serverUUID, LocalDateTime lastModified) { - this.serverUUID = serverUUID; - this.lastModified = lastModified; - } - - public String getServerUUID() { - return serverUUID; - } - - public void setServerUUID(String serverUUID) { - this.serverUUID = serverUUID; - } - - public LocalDateTime getLastModified() { - return lastModified; - } - - public void setLastModified(LocalDateTime lastModified) { - this.lastModified = lastModified; - } -} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/SyncCreateResponse.java b/app/src/main/java/core/notevault/sync/synchronisation/SyncCreateResponse.java deleted file mode 100644 index 1514b6d..0000000 --- a/app/src/main/java/core/notevault/sync/synchronisation/SyncCreateResponse.java +++ /dev/null @@ -1,22 +0,0 @@ -package core.notevault.sync.synchronisation; - -public class SyncCreateResponse { - private String serverObjectUUID; - private int deviceSpecificObjectID; - - public String getServerObjectUUID() { - return serverObjectUUID; - } - - public void setServerObjectUUID(String serverObjectUUID) { - this.serverObjectUUID = serverObjectUUID; - } - - public int getDeviceSpecificObjectID() { - return deviceSpecificObjectID; - } - - public void setDeviceSpecificObjectID(int deviceSpecificObjectID) { - this.deviceSpecificObjectID = deviceSpecificObjectID; - } -} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/SyncRequest.java b/app/src/main/java/core/notevault/sync/synchronisation/SyncRequest.java deleted file mode 100644 index a16dcdb..0000000 --- a/app/src/main/java/core/notevault/sync/synchronisation/SyncRequest.java +++ /dev/null @@ -1,50 +0,0 @@ -package core.notevault.sync.synchronisation; - -import java.time.LocalDateTime; -import java.util.List; - -public class SyncRequest { - private LocalDateTime lastSync; - private List deletedConcerts; - private List modifiedConcerts; - private List createdConcerts; - - public SyncRequest(LocalDateTime lastSync, List deletedConcerts, List modifiedConcerts, List createdConcerts) { - this.lastSync = lastSync; - this.deletedConcerts = deletedConcerts; - this.modifiedConcerts = modifiedConcerts; - this.createdConcerts = createdConcerts; - } - - public LocalDateTime getLastSync() { - return lastSync; - } - - public void setLastSync(LocalDateTime lastSync) { - this.lastSync = lastSync; - } - - public List getDeletedConcerts() { - return deletedConcerts; - } - - public void setDeletedConcerts(List deletedConcerts) { - this.deletedConcerts = deletedConcerts; - } - - public List getModifiedConcerts() { - return modifiedConcerts; - } - - public void setModifiedConcerts(List modifiedConcerts) { - this.modifiedConcerts = modifiedConcerts; - } - - public List getCreatedConcerts() { - return createdConcerts; - } - - public void setCreatedConcerts(List createdConcerts) { - this.createdConcerts = createdConcerts; - } -} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/SyncResponseModel.java b/app/src/main/java/core/notevault/sync/synchronisation/SyncResponseModel.java deleted file mode 100644 index d678bc7..0000000 --- a/app/src/main/java/core/notevault/sync/synchronisation/SyncResponseModel.java +++ /dev/null @@ -1,42 +0,0 @@ -package core.notevault.sync.synchronisation; - -import java.util.List; - -public class SyncResponseModel { - private List toBeUploaded; - private List toBeDownloaded; - private List toBeDeleted; - private List createResponses; - - public List getToBeUploaded() { - return toBeUploaded; - } - - public void setToBeUploaded(List toBeUploaded) { - this.toBeUploaded = toBeUploaded; - } - - public List getToBeDownloaded() { - return toBeDownloaded; - } - - public void setToBeDownloaded(List toBeDownloaded) { - this.toBeDownloaded = toBeDownloaded; - } - - public List getToBeDeleted() { - return toBeDeleted; - } - - public void setToBeDeleted(List toBeDeleted) { - this.toBeDeleted = toBeDeleted; - } - - public List getCreateResponses() { - return createResponses; - } - - public void setCreateResponses(List createResponses) { - this.createResponses = createResponses; - } -} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java b/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java index c9df3cd..7d967b5 100644 --- a/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java +++ b/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java @@ -1,12 +1,14 @@ package core.notevault.sync.synchronisation; import core.notevault.data.sync.SyncResponse; +import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest; +import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.POST; public interface SyncService { - @POST("api/v1/sync/concerts") - Call syncConcerts(@Body SyncRequest syncRequest); + @POST("/sync/songs/create") + Call performSongCreation(@Body SongCreationBatchRequest syncRequest); } diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java new file mode 100644 index 0000000..65014fc --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java @@ -0,0 +1,57 @@ +package core.notevault.sync.synchronisation.songs; + +import android.content.Context; +import android.widget.Toast; +import core.notevault.data.MusicNote; +import core.notevault.data.MusicNoteDAO; +import core.notevault.data.sync.SyncStatus; +import core.notevault.sync.synchronisation.SyncService; +import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest; +import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse; +import core.notevault.sync.synchronisation.songs.creation.SongCreationRequest; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +import java.util.ArrayList; +import java.util.List; + +public class SongSyncWorker { + private SyncService syncService; + private MusicNoteDAO database; + private Context context; + + public SongSyncWorker(SyncService syncService, MusicNoteDAO database, Context context) { + this.syncService = syncService; + this.database = database; + this.context = context; + } + + public SongCreationBatchRequest buildCreationRequest() { + List created_songs = database.getSongsWithSyncStatus(SyncStatus.CREATED); + List songCreationRequests = new ArrayList<>(); + for(MusicNote musicNote : created_songs) { + songCreationRequests.add(new SongCreationRequest(musicNote.getMusicNoteId(), musicNote.getTitle(), musicNote.getComposer(), musicNote.getGenre(), musicNote.getYear())); + } + return new SongCreationBatchRequest(songCreationRequests); + } + + public void syncSongCreations() { + SongCreationBatchRequest request = buildCreationRequest(); + syncService.performSongCreation(request).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful() && response.body() != null) { + Toast.makeText(context, "Song creation synced: " + response.body().getSongs().size(), Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(context, "Song creation failed", Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onFailure(Call call, Throwable throwable) { + Toast.makeText(context, "Song creation failed", Toast.LENGTH_LONG).show(); + } + }); + } +} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationBatchRequest.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationBatchRequest.java new file mode 100644 index 0000000..1bb9986 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationBatchRequest.java @@ -0,0 +1,11 @@ +package core.notevault.sync.synchronisation.songs.creation; + +import java.util.List; + +public class SongCreationBatchRequest { + private List songs; + + public SongCreationBatchRequest(List songs) { + this.songs = songs; + } +} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationBatchResponse.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationBatchResponse.java new file mode 100644 index 0000000..f1329b9 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationBatchResponse.java @@ -0,0 +1,19 @@ +package core.notevault.sync.synchronisation.songs.creation; + +import java.util.List; + +public class SongCreationBatchResponse { + private List songs; + + public SongCreationBatchResponse(List songs) { + this.songs = songs; + } + + public List getSongs() { + return songs; + } + + public void setSongs(List songs) { + this.songs = songs; + } +} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationRequest.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationRequest.java new file mode 100644 index 0000000..30cc11a --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationRequest.java @@ -0,0 +1,17 @@ +package core.notevault.sync.synchronisation.songs.creation; + +public class SongCreationRequest { + private long localID; + private String title; + private String composer; + private String genre; + private int year; + + public SongCreationRequest(long localID, String title, String composer, String genre, int year) { + this.localID = localID; + this.title = title; + this.composer = composer; + this.genre = genre; + this.year = year; + } +} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationResponse.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationResponse.java new file mode 100644 index 0000000..fe47041 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/creation/SongCreationResponse.java @@ -0,0 +1,27 @@ +package core.notevault.sync.synchronisation.songs.creation; + +public class SongCreationResponse { + private int localID; + private String serverID; + + public SongCreationResponse(int localID, String serverID) { + this.localID = localID; + this.serverID = serverID; + } + + public int getLocalID() { + return localID; + } + + public void setLocalID(int localID) { + this.localID = localID; + } + + public String getServerID() { + return serverID; + } + + public void setServerID(String serverID) { + this.serverID = serverID; + } +}