From 42a644cabbb71da3e0e82d759648666ea909e985 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 9 Mar 2017 20:08:54 +0000 Subject: [PATCH] EntryEntity: make content a SyncEntry instead of a string. It was always of this type, this change just makes it so it's automatically converted instead of manually. --- .../syncadapter/model/JournalModel.java | 33 +++++++++++++++++-- .../etesync/syncadapter/model/SyncEntry.java | 6 +++- .../syncadapter/syncadapter/SyncManager.java | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/model/JournalModel.java b/app/src/main/java/com/etesync/syncadapter/model/JournalModel.java index 9ce224cf..cede1a5a 100644 --- a/app/src/main/java/com/etesync/syncadapter/model/JournalModel.java +++ b/app/src/main/java/com/etesync/syncadapter/model/JournalModel.java @@ -88,7 +88,8 @@ public class JournalModel { @Column(length = 64, unique = true, nullable = false) String uid; - String content; + @Convert(SyncEntryConverter.class) + SyncEntry content; @Index("journal_index") @ForeignKey(update = ReferentialAction.CASCADE) @@ -96,7 +97,7 @@ public class JournalModel { Journal journal; } - public static class CollectionInfoConverter implements Converter { + static class CollectionInfoConverter implements Converter { @Override public Class getMappedType() { return CollectionInfo.class; @@ -122,4 +123,32 @@ public class JournalModel { return value == null ? null : CollectionInfo.fromJson(value); } } + + + static class SyncEntryConverter implements Converter { + @Override + public Class getMappedType() { + return SyncEntry.class; + } + + @Override + public Class getPersistedType() { + return String.class; + } + + @Override + public Integer getPersistedSize() { + return null; + } + + @Override + public String convertToPersisted(SyncEntry value) { + return value == null ? null : value.toJson(); + } + + @Override + public SyncEntry convertToMapped(Class type, String value) { + return value == null ? null : SyncEntry.fromJson(value); + } + } } diff --git a/app/src/main/java/com/etesync/syncadapter/model/SyncEntry.java b/app/src/main/java/com/etesync/syncadapter/model/SyncEntry.java index 6afd715f..98368f96 100644 --- a/app/src/main/java/com/etesync/syncadapter/model/SyncEntry.java +++ b/app/src/main/java/com/etesync/syncadapter/model/SyncEntry.java @@ -42,7 +42,11 @@ public class SyncEntry { } public static SyncEntry fromJournalEntry(String keyBase64, JournalEntryManager.Entry entry) { - return GsonHelper.gson.fromJson(entry.getContent(keyBase64), SyncEntry.class); + return fromJson(entry.getContent(keyBase64)); + } + + static SyncEntry fromJson(String json) { + return GsonHelper.gson.fromJson(json, SyncEntry.class); } public String toJson() { diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.java b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.java index 8850869a..f7cfac78 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.java +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.java @@ -231,7 +231,7 @@ abstract public class SyncManager { private void persistSyncEntry(String uid, SyncEntry syncEntry) { EntryEntity entry = new EntryEntity(); entry.setUid(uid); - entry.setContent(syncEntry.toJson()); + entry.setContent(syncEntry); entry.setJournal(getJournalEntity()); data.insert(entry); }