diff --git a/app/src/main/java/core/notevault/data/MusicDatabase.java b/app/src/main/java/core/notevault/data/MusicDatabase.java index 3266150..20b8e27 100644 --- a/app/src/main/java/core/notevault/data/MusicDatabase.java +++ b/app/src/main/java/core/notevault/data/MusicDatabase.java @@ -2,10 +2,10 @@ package core.notevault.data; import android.content.Context; import androidx.room.*; -import core.notevault.data.sync.SyncResponse; +import core.notevault.data.sync.SyncDataObject; import core.notevault.data.sync.SyncStatusConverter; -@Database(entities = {MusicNote.class, NoteSheet.class, Concert.class, ConcertSong.class, SyncResponse.class}, version = 2, exportSchema = false) +@Database(entities = {MusicNote.class, NoteSheet.class, Concert.class, ConcertSong.class, SyncDataObject.class}, version = 2, exportSchema = false) @TypeConverters(DateConverter.class) public abstract class MusicDatabase extends RoomDatabase { public abstract MusicNoteDAO musicNoteDao(); diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index fe32391..e742c7a 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -1,7 +1,8 @@ package core.notevault.data; import androidx.room.*; -import core.notevault.data.sync.SyncResponse; + +import core.notevault.data.sync.SyncDataObject; import core.notevault.data.sync.SyncStatus; import core.notevault.sync.synchronisation.songs.creation.SongCreationResponse; @@ -81,4 +82,8 @@ public interface MusicNoteDAO { @Update void updateNoteSheet(NoteSheet noteSheet); + + @Transaction + @Insert(onConflict = OnConflictStrategy.REPLACE) // oder andere Strategie je nach Bedarf + void insertSyncDataObjects(List syncDataObjects); } diff --git a/app/src/main/java/core/notevault/data/sync/SyncAction.java b/app/src/main/java/core/notevault/data/sync/SyncAction.java deleted file mode 100644 index 0e721f0..0000000 --- a/app/src/main/java/core/notevault/data/sync/SyncAction.java +++ /dev/null @@ -1,6 +0,0 @@ -package core.notevault.data.sync; - -public enum SyncAction { - TOBEUPLOADED, - TOBEDOWNLOADED -} diff --git a/app/src/main/java/core/notevault/data/sync/SyncActionConverter.java b/app/src/main/java/core/notevault/data/sync/SyncActionConverter.java deleted file mode 100644 index 51c4caa..0000000 --- a/app/src/main/java/core/notevault/data/sync/SyncActionConverter.java +++ /dev/null @@ -1,15 +0,0 @@ -package core.notevault.data.sync; - -import androidx.room.TypeConverter; - -public class SyncActionConverter { - @TypeConverter - public static SyncAction fromInt(int value) { - return SyncAction.values()[value]; - } - - @TypeConverter - public static int toInt(SyncAction action) { - return action.ordinal(); - } -} diff --git a/app/src/main/java/core/notevault/data/sync/SyncDataObject.java b/app/src/main/java/core/notevault/data/sync/SyncDataObject.java new file mode 100644 index 0000000..344da9d --- /dev/null +++ b/app/src/main/java/core/notevault/data/sync/SyncDataObject.java @@ -0,0 +1,49 @@ +package core.notevault.data.sync; + +import androidx.room.Entity; +import androidx.room.Ignore; +import androidx.room.PrimaryKey; +import androidx.room.TypeConverters; + +@Entity(tableName = "sync_data_objects") +@TypeConverters(SyncDataObjectTypeConverter.class) +public class SyncDataObject { + @PrimaryKey(autoGenerate = true) + private int id; + + private String serverID; + private SyncDataObjectType type; + + @Ignore + public SyncDataObject(String serverID, SyncDataObjectType type) { + this.serverID = serverID; + this.type = type; + } + + public SyncDataObject() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getServerID() { + return serverID; + } + + public void setServerID(String serverID) { + this.serverID = serverID; + } + + public SyncDataObjectType getType() { + return type; + } + + public void setType(SyncDataObjectType type) { + this.type = type; + } +} diff --git a/app/src/main/java/core/notevault/data/sync/SyncDataObjectType.java b/app/src/main/java/core/notevault/data/sync/SyncDataObjectType.java new file mode 100644 index 0000000..82e184c --- /dev/null +++ b/app/src/main/java/core/notevault/data/sync/SyncDataObjectType.java @@ -0,0 +1,5 @@ +package core.notevault.data.sync; + +public enum SyncDataObjectType { + MUSIC_NOTE +} diff --git a/app/src/main/java/core/notevault/data/sync/SyncDataObjectTypeConverter.java b/app/src/main/java/core/notevault/data/sync/SyncDataObjectTypeConverter.java new file mode 100644 index 0000000..86385a2 --- /dev/null +++ b/app/src/main/java/core/notevault/data/sync/SyncDataObjectTypeConverter.java @@ -0,0 +1,15 @@ +package core.notevault.data.sync; + +import androidx.room.TypeConverter; + +public class SyncDataObjectTypeConverter { + @TypeConverter + public static SyncDataObjectType fromInt(int value) { + return SyncDataObjectType.values()[value]; + } + + @TypeConverter + public static int toInt(SyncDataObjectType objectType) { + return objectType.ordinal(); + } +} diff --git a/app/src/main/java/core/notevault/data/sync/SyncObject.java b/app/src/main/java/core/notevault/data/sync/SyncObject.java deleted file mode 100644 index 3a7ad53..0000000 --- a/app/src/main/java/core/notevault/data/sync/SyncObject.java +++ /dev/null @@ -1,5 +0,0 @@ -package core.notevault.data.sync; - -public enum SyncObject { - CONCERT -} diff --git a/app/src/main/java/core/notevault/data/sync/SyncObjectConverter.java b/app/src/main/java/core/notevault/data/sync/SyncObjectConverter.java deleted file mode 100644 index e2e67b5..0000000 --- a/app/src/main/java/core/notevault/data/sync/SyncObjectConverter.java +++ /dev/null @@ -1,15 +0,0 @@ -package core.notevault.data.sync; - -import androidx.room.TypeConverter; - -public class SyncObjectConverter { - @TypeConverter - public static SyncObject fromInt(int value) { - return SyncObject.values()[value]; - } - - @TypeConverter - public static int toInt(SyncObject object) { - return object.ordinal(); - } -} diff --git a/app/src/main/java/core/notevault/data/sync/SyncResponse.java b/app/src/main/java/core/notevault/data/sync/SyncResponse.java deleted file mode 100644 index 1acefc5..0000000 --- a/app/src/main/java/core/notevault/data/sync/SyncResponse.java +++ /dev/null @@ -1,59 +0,0 @@ -package core.notevault.data.sync; - - -import androidx.room.Entity; -import androidx.room.Ignore; -import androidx.room.PrimaryKey; -import androidx.room.TypeConverters; - -@Entity(tableName = "sync_actions") -@TypeConverters({SyncStatusConverter.class, SyncObjectConverter.class}) -public class SyncResponse { - @PrimaryKey(autoGenerate = true) - private long id; - private String serverUUID; - private SyncAction syncAction; - private SyncObject syncObject; - - @Ignore - public SyncResponse(String serverUUID, SyncAction syncAction, SyncObject syncObject) { - this.serverUUID = serverUUID; - this.syncAction = syncAction; - this.syncObject = syncObject; - } - - public SyncResponse() { - } - - public String getServerUUID() { - return serverUUID; - } - - public void setServerUUID(String serverUUID) { - this.serverUUID = serverUUID; - } - - public SyncAction getSyncAction() { - return syncAction; - } - - public void setSyncAction(SyncAction syncAction) { - this.syncAction = syncAction; - } - - public SyncObject getSyncObject() { - return syncObject; - } - - public void setSyncObject(SyncObject syncObject) { - this.syncObject = syncObject; - } - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } -} diff --git a/app/src/main/java/core/notevault/sync/SyncWorker.java b/app/src/main/java/core/notevault/sync/SyncWorker.java index dc45e0c..72246ce 100644 --- a/app/src/main/java/core/notevault/sync/SyncWorker.java +++ b/app/src/main/java/core/notevault/sync/SyncWorker.java @@ -1,29 +1,13 @@ package core.notevault.sync; import android.content.Context; -import android.util.Log; import androidx.annotation.NonNull; import androidx.work.Worker; import androidx.work.WorkerParameters; -import core.notevault.data.Concert; import core.notevault.data.MusicDatabase; -import core.notevault.data.MusicNoteDAO; -import core.notevault.data.sync.SyncAction; -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; -import java.io.IOException; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.stream.Collectors; public class SyncWorker extends Worker{ private SongSyncWorker songSyncWorker; @@ -42,6 +26,7 @@ public class SyncWorker extends Worker{ songSyncWorker.syncSongModifications(); songSyncWorker.syncSongDeletions(); songSyncWorker.uploadNoteSheets(); + songSyncWorker.fetchModifiedSongs(); return Result.success(); } catch (Exception e) { e.printStackTrace(); diff --git a/app/src/main/java/core/notevault/sync/synchronisation/FetchRequest.java b/app/src/main/java/core/notevault/sync/synchronisation/FetchRequest.java new file mode 100644 index 0000000..360f883 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/FetchRequest.java @@ -0,0 +1,19 @@ +package core.notevault.sync.synchronisation; + +import java.time.LocalDateTime; + +public class FetchRequest { + private LocalDateTime client_last_sync; + + public FetchRequest(LocalDateTime client_last_sync) { + this.client_last_sync = client_last_sync; + } + + public LocalDateTime getClient_last_sync() { + return client_last_sync; + } + + public void setClient_last_sync(LocalDateTime client_last_sync) { + this.client_last_sync = client_last_sync; + } +} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/FetchResponse.java b/app/src/main/java/core/notevault/sync/synchronisation/FetchResponse.java new file mode 100644 index 0000000..8ff6f71 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/FetchResponse.java @@ -0,0 +1,19 @@ +package core.notevault.sync.synchronisation; + +import java.util.List; + +public class FetchResponse { + private List serverIDs; + + public FetchResponse(List serverIDs) { + this.serverIDs = serverIDs; + } + + public List getServerIDs() { + return serverIDs; + } + + public void setServerIDs(List serverIDs) { + this.serverIDs = serverIDs; + } +} 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 d63bda1..00d9ffd 100644 --- a/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java +++ b/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java @@ -1,6 +1,6 @@ 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 core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchRequest; @@ -11,10 +11,9 @@ import core.notevault.sync.synchronisation.songs.notesheets.UploadResponse; import okhttp3.MultipartBody; import okhttp3.RequestBody; import retrofit2.Call; -import retrofit2.http.Body; -import retrofit2.http.Multipart; -import retrofit2.http.POST; -import retrofit2.http.Part; +import retrofit2.http.*; + +import java.time.LocalDateTime; public interface SyncService { @@ -31,4 +30,7 @@ public interface SyncService { @POST("/sync/songs/note_sheet/upload") Call uploadNoteSheet(@Part("serverID") RequestBody serverID, @Part("fileName") RequestBody fileName, @Part MultipartBody.Part image); + + @GET("/sync/songs/fetch") + Call fetchModifiedSongs(@Query("last_client_sync") LocalDateTime last_client_sync); } 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 index 5ced875..d85d92a 100644 --- a/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java @@ -6,7 +6,11 @@ import android.widget.Toast; import core.notevault.data.MusicNote; import core.notevault.data.MusicNoteDAO; import core.notevault.data.NoteSheet; +import core.notevault.data.sync.SyncDataObject; +import core.notevault.data.sync.SyncDataObjectType; import core.notevault.data.sync.SyncStatus; +import core.notevault.sync.synchronisation.FetchRequest; +import core.notevault.sync.synchronisation.FetchResponse; import core.notevault.sync.synchronisation.SyncService; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse; @@ -164,8 +168,32 @@ public class SongSyncWorker { } }); } - - } } + + public void fetchModifiedSongs() { + FetchRequest request = new FetchRequest(LocalDateTime.MIN); + + syncService.fetchModifiedSongs(LocalDateTime.MIN).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful() && response.body() != null) { + executorService.execute(() -> { + List syncDataObjects = new ArrayList<>(); + for(String serverID: response.body().getServerIDs()) { + syncDataObjects.add(new SyncDataObject(serverID, SyncDataObjectType.MUSIC_NOTE)); + } + database.insertSyncDataObjects(syncDataObjects); + }); + } else { + Toast.makeText(context, "Fehler beim Fetch: " + response.code(), Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onFailure(Call call, Throwable throwable) { + Toast.makeText(context, "Fehler beim Fetch: " + throwable.getMessage(), Toast.LENGTH_LONG).show(); + } + }); + } }