package com.etesync.syncadapter.model; import java.util.LinkedList; import java.util.List; import io.requery.Column; import io.requery.Convert; import io.requery.Converter; import io.requery.Entity; import io.requery.ForeignKey; import io.requery.Generated; import io.requery.Index; import io.requery.Key; import io.requery.ManyToOne; import io.requery.Persistable; import io.requery.PostLoad; import io.requery.ReferentialAction; import io.requery.sql.EntityDataStore; public class JournalModel { @Entity public static abstract class Journal { @Key @Generated int id; @Column(length = 64, unique = true, nullable = false) String uid; @Convert(CollectionInfoConverter.class) CollectionInfo info; long service; boolean deleted; @PostLoad void afterLoad() { this.info.serviceID = service; this.info.uid = uid; } public Journal() { this.deleted = false; } public Journal(CollectionInfo info) { this(); this.info = info; this.uid = info.uid; this.service = info.serviceID; } public static List getCollections(EntityDataStore data, long service) { List ret = new LinkedList<>(); List journals = data.select(JournalEntity.class).where(JournalEntity.SERVICE.eq(service).and(JournalEntity.DELETED.eq(false))).get().toList(); for (JournalEntity journal : journals) { // FIXME: For some reason this isn't always being called, manually do it here. journal.afterLoad(); ret.add(journal.getInfo()); } return ret; } public static JournalEntity fetch(EntityDataStore data, String url) { JournalEntity ret = data.select(JournalEntity.class).where(JournalEntity.UID.eq(url)).limit(1).get().firstOrNull(); if (ret != null) { // FIXME: For some reason this isn't always being called, manually do it here. ret.afterLoad(); } return ret; } public static JournalEntity fetchOrCreate(EntityDataStore data, CollectionInfo collection) { JournalEntity journalEntity = fetch(data, collection.uid); if (journalEntity == null) { journalEntity = new JournalEntity(collection); } else { journalEntity.setInfo(collection); } return journalEntity; } public String getLastUid(EntityDataStore data) { EntryEntity last = data.select(EntryEntity.class).where(EntryEntity.JOURNAL.eq(this)).orderBy(EntryEntity.ID.desc()).limit(1).get().firstOrNull(); if (last != null) { return last.getUid(); } return null; } } @Entity public static abstract class Entry { @Key @Generated int id; @Column(length = 64, unique = true, nullable = false) String uid; @Convert(SyncEntryConverter.class) SyncEntry content; @Index("journal_index") @ForeignKey(update = ReferentialAction.CASCADE) @ManyToOne Journal journal; } static class CollectionInfoConverter implements Converter { @Override public Class getMappedType() { return CollectionInfo.class; } @Override public Class getPersistedType() { return String.class; } @Override public Integer getPersistedSize() { return null; } @Override public String convertToPersisted(CollectionInfo value) { return value == null ? null : value.toJson(); } @Override public CollectionInfo convertToMapped(Class type, String value) { 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); } } }