]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
add profile setting for default currency (db part)
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / MobileLedgerProfile.java
index 7cadf8eb06f2d6154203fcafe0a06c36559e5d41..8c1f1e5a38186861541c47e353b5aed8e0122c64 100644 (file)
@@ -46,6 +46,8 @@ public final class MobileLedgerProfile {
     private String uuid;
     private String name;
     private boolean permitPosting;
+    private boolean showCommodityByDefault;
+    private String defaultCommodity;
     private String preferredAccountsFilter;
     private String url;
     private boolean authEnabled;
@@ -65,6 +67,7 @@ public final class MobileLedgerProfile {
         uuid = origin.uuid;
         name = origin.name;
         permitPosting = origin.permitPosting;
+        showCommodityByDefault = origin.showCommodityByDefault;
         preferredAccountsFilter = origin.preferredAccountsFilter;
         url = origin.url;
         authEnabled = origin.authEnabled;
@@ -74,6 +77,7 @@ public final class MobileLedgerProfile {
         orderNo = origin.orderNo;
         futureDates = origin.futureDates;
         apiVersion = origin.apiVersion;
+        defaultCommodity = origin.defaultCommodity;
     }
     // loads all profiles into Data.profiles
     // returns the profile with the given UUID
@@ -83,8 +87,9 @@ public final class MobileLedgerProfile {
         SQLiteDatabase db = App.getDatabase();
         try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
                                          "auth_password, permit_posting, theme, order_no, " +
-                                         "preferred_accounts_filter, future_dates, api_version " +
-                                         "FROM " + "profiles order by order_no", null))
+                                         "preferred_accounts_filter, future_dates, api_version, " +
+                                         "show_commodity_by_default, default_commodity FROM " +
+                                         "profiles order by order_no", null))
         {
             while (cursor.moveToNext()) {
                 MobileLedgerProfile item = new MobileLedgerProfile(cursor.getString(0));
@@ -99,6 +104,8 @@ public final class MobileLedgerProfile {
                 item.setPreferredAccountsFilter(cursor.getString(9));
                 item.setFutureDates(cursor.getInt(10));
                 item.setApiVersion(cursor.getInt(11));
+                item.setShowCommodityByDefault(cursor.getInt(12) == 1);
+                item.setDefaultCommodity(cursor.getString(13));
                 list.add(item);
                 if (item.getUuid()
                         .equals(currentProfileUUID))
@@ -125,6 +132,24 @@ public final class MobileLedgerProfile {
             db.endTransaction();
         }
     }
+    public boolean getShowCommodityByDefault() {
+        return showCommodityByDefault;
+    }
+    public void setShowCommodityByDefault(boolean showCommodityByDefault) {
+        this.showCommodityByDefault = showCommodityByDefault;
+    }
+    public String getDefaultCommodity() {
+        return defaultCommodity;
+    }
+    public void setDefaultCommodity(String defaultCommodity) {
+        this.defaultCommodity = defaultCommodity;
+    }
+    public void setDefaultCommodity(CharSequence defaultCommodity) {
+        if (defaultCommodity == null)
+            this.defaultCommodity = null;
+        else
+            this.defaultCommodity = String.valueOf(defaultCommodity);
+    }
     public SendTransactionTask.API getApiVersion() {
         return apiVersion;
     }
@@ -212,13 +237,15 @@ public final class MobileLedgerProfile {
 //                                            "themeHue=%d", uuid, name, url,
 //                    permitPosting ? "TRUE" : "FALSE", authEnabled ? "TRUE" : "FALSE", themeHue));
             db.execSQL("REPLACE INTO profiles(uuid, name, permit_posting, url, " +
-                       "use_authentication, auth_user, " +
-                       "auth_password, theme, order_no, preferred_accounts_filter, future_dates, " +
-                       "api_version) " + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+                       "use_authentication, auth_user, auth_password, theme, order_no, " +
+                       "preferred_accounts_filter, future_dates, api_version, " +
+                       "show_commodity_by_default, default_commodity) " +
+                       "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                     new Object[]{uuid, name, permitPosting, url, authEnabled,
                                  authEnabled ? authUserName : null,
                                  authEnabled ? authPassword : null, themeHue, orderNo,
-                                 preferredAccountsFilter, futureDates.toInt(), apiVersion.toInt()
+                                 preferredAccountsFilter, futureDates.toInt(), apiVersion.toInt(),
+                                 showCommodityByDefault, defaultCommodity
                     });
             db.setTransactionSuccessful();
         }
@@ -506,6 +533,25 @@ public final class MobileLedgerProfile {
 
         return result;
     }
+    Currency loadCurrencyByName(String name) {
+        SQLiteDatabase db = App.getDatabase();
+        Currency result = tryLoadCurrencyByName(db, name);
+        if (result == null)
+            throw new RuntimeException(String.format("Unable to load currency '%s'", name));
+        return result;
+    }
+    private Currency tryLoadCurrencyByName(SQLiteDatabase db, String name) {
+        try (Cursor cursor = db.rawQuery(
+                "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.name=?",
+                new String[]{name}))
+        {
+            if (cursor.moveToFirst()) {
+                return new Currency(cursor.getInt(0), cursor.getString(1),
+                        Currency.Position.valueOf(cursor.getInt(2)), cursor.getInt(3) == 1);
+            }
+            return null;
+        }
+    }
     public enum FutureDates {
         None(0), OneWeek(7), TwoWeeks(14), OneMonth(30), TwoMonths(60), ThreeMonths(90),
         SixMonths(180), OneYear(365), All(-1);