]> git.ktnx.net Git - mobile-ledger.git/commitdiff
profile methods for loading currency by name
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 5 Apr 2020 20:33:19 +0000 (23:33 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 5 Apr 2020 20:33:19 +0000 (23:33 +0300)
app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java

index 7cadf8eb06f2d6154203fcafe0a06c36559e5d41..956878e9b12bd0065df61074b6d8001e161e39d2 100644 (file)
@@ -506,6 +506,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);