]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/db/Profile.java
new transaction: currency can't be null
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / Profile.java
index 1ed8a21b1d1666498c88850e4a31f83de4c8d0a0..030ae1ba51d5215d1d4d5b04b6592317ba64e05c 100644 (file)
 
 package net.ktnx.mobileledger.db;
 
+import android.os.AsyncTask;
+
 import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
 import androidx.room.ColumnInfo;
 import androidx.room.Entity;
+import androidx.room.PrimaryKey;
+import androidx.room.Transaction;
 
-/*
- create table profiles(uuid varchar not null primary key, name not null, url not null,
- use_authentication boolean not null, auth_user varchar, auth_password varchar, order_no
- integer, permit_posting boolean default 0, theme integer default -1, preferred_accounts_filter
- varchar, future_dates integer, api_version integer, show_commodity_by_default boolean default
- 0, default_commodity varchar, show_comments_by_default boolean default 1,
- detected_version_pre_1_19 boolean, detected_version_major integer, detected_version_minor
- integer);
-*/
-@Entity(tableName = "profiles", primaryKeys = {"uuid"})
+import net.ktnx.mobileledger.dao.AccountDAO;
+import net.ktnx.mobileledger.dao.OptionDAO;
+import net.ktnx.mobileledger.dao.TransactionDAO;
+import net.ktnx.mobileledger.utils.Misc;
+
+import org.jetbrains.annotations.NotNull;
+
+@Entity(tableName = "profiles")
 public class Profile {
-    @NonNull
+    public static final long NO_PROFILE_ID = 0;
     @ColumnInfo
-    private String uuid = "invalid";
+    @PrimaryKey(autoGenerate = true)
+    private long id;
     @NonNull
     @ColumnInfo
     private String name = "";
+    @ColumnInfo(name = "deprecated_uuid")
+    private String deprecatedUUID;
     @NonNull
     @ColumnInfo
     private String url = "";
@@ -71,12 +77,17 @@ public class Profile {
     private int detectedVersionMajor;
     @ColumnInfo(name = "detected_version_minor")
     private int detectedVersionMinor;
-    @NonNull
-    public String getUuid() {
-        return uuid;
+    public String getDeprecatedUUID() {
+        return deprecatedUUID;
+    }
+    public void setDeprecatedUUID(String deprecatedUUID) {
+        this.deprecatedUUID = deprecatedUUID;
     }
-    public void setUuid(@NonNull String uuid) {
-        this.uuid = uuid;
+    public long getId() {
+        return id;
+    }
+    public void setId(long id) {
+        this.id = id;
     }
     @NonNull
     public String getName() {
@@ -152,11 +163,12 @@ public class Profile {
     public void setShowCommodityByDefault(boolean showCommodityByDefault) {
         this.showCommodityByDefault = showCommodityByDefault;
     }
+    @NotNull
     public String getDefaultCommodity() {
         return defaultCommodity;
     }
-    public void setDefaultCommodity(String defaultCommodity) {
-        this.defaultCommodity = defaultCommodity;
+    public void setDefaultCommodity(@org.jetbrains.annotations.Nullable String defaultCommodity) {
+        this.defaultCommodity = Misc.nullIsEmpty(defaultCommodity);
     }
     public boolean getShowCommentsByDefault() {
         return showCommentsByDefault;
@@ -187,4 +199,42 @@ public class Profile {
     public String toString() {
         return getName();
     }
+    @Override
+    public boolean equals(@Nullable Object o) {
+        if (!(o instanceof Profile))
+            return false;
+        Profile p = (Profile) o;
+        return id == p.id && Misc.equalStrings(name, p.name) &&
+               Misc.equalStrings(deprecatedUUID, p.deprecatedUUID) &&
+               Misc.equalStrings(url, p.url) && useAuthentication == p.useAuthentication &&
+               Misc.equalStrings(authUser, p.authUser) &&
+               Misc.equalStrings(authPassword, p.authPassword) && orderNo == p.orderNo &&
+               permitPosting == p.permitPosting && theme == p.theme &&
+               Misc.equalStrings(preferredAccountsFilter, p.preferredAccountsFilter) &&
+               futureDates == p.futureDates && apiVersion == p.apiVersion &&
+               showCommentsByDefault == p.showCommentsByDefault &&
+               Misc.equalStrings(defaultCommodity, p.defaultCommodity) &&
+               showCommentsByDefault == p.showCommentsByDefault &&
+               detectedVersionPre_1_19 == p.detectedVersionPre_1_19 &&
+               detectedVersionMajor == p.detectedVersionMajor &&
+               detectedVersionMinor == p.detectedVersionMinor;
+    }
+    @Transaction
+    public void wipeAllDataSync() {
+        OptionDAO optDao = DB.get()
+                             .getOptionDAO();
+        optDao.deleteSync(optDao.allForProfileSync(id));
+
+        AccountDAO accDao = DB.get()
+                              .getAccountDAO();
+        accDao.deleteSync(accDao.allForProfileSync(id));
+
+        TransactionDAO trnDao = DB.get()
+                                  .getTransactionDAO();
+        trnDao.deleteSync(trnDao.getAllForProfileUnorderedSync(id));
+    }
+    public void wipeAllData() {
+        AsyncTask.execute(this::wipeAllDataSync);
+    }
+
 }