2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * Mobile-Ledger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.model;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.util.Log;
24 import net.ktnx.mobileledger.utils.MLDB;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
30 public final class MobileLedgerProfile {
34 private boolean authEnabled;
35 private String authUserName;
36 private String authPassword;
37 public MobileLedgerProfile(String uuid, String name, String url, boolean authEnabled,
38 String authUserName, String authPassword) {
42 this.authEnabled = authEnabled;
43 this.authUserName = authUserName;
44 this.authPassword = authPassword;
46 public MobileLedgerProfile(CharSequence name, CharSequence url, boolean authEnabled,
47 CharSequence authUserName, CharSequence authPassword) {
48 this.uuid = String.valueOf(UUID.randomUUID());
49 this.name = String.valueOf(name);
50 this.url = String.valueOf(url);
51 this.authEnabled = authEnabled;
52 this.authUserName = String.valueOf(authUserName);
53 this.authPassword = String.valueOf(authPassword);
55 public static List<MobileLedgerProfile> loadAllFromDB() {
56 List<MobileLedgerProfile> result = new ArrayList<>();
57 SQLiteDatabase db = MLDB.getReadableDatabase();
58 try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
59 "auth_password FROM profiles", null))
61 while (cursor.moveToNext()) {
62 result.add(new MobileLedgerProfile(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3) == 1, cursor.getString(4),
63 cursor.getString(5)));
68 public static List<MobileLedgerProfile> createInitialProfileList() {
69 List<MobileLedgerProfile> result = new ArrayList<>();
70 MobileLedgerProfile first =
71 new MobileLedgerProfile(UUID.randomUUID().toString(), "default", "", false, "", "");
77 public static MobileLedgerProfile loadUUIDFromDB(String profileUUID) {
78 SQLiteDatabase db = MLDB.getReadableDatabase();
83 Boolean useAuthentication;
84 try (Cursor cursor = db.rawQuery("SELECT name, url, use_authentication, auth_user, " +
85 "auth_password FROM profiles WHERE uuid=?",
86 new String[]{profileUUID}))
88 if (cursor.moveToNext()) {
89 name = cursor.getString(0);
90 url = cursor.getString(1);
91 useAuthentication = cursor.getInt(2) == 1;
92 authUser = useAuthentication ? cursor.getString(3) : null;
93 authPassword = useAuthentication ? cursor.getString(4) : null;
96 name = "Unknown profile";
97 url = "Https://server/url";
98 useAuthentication = false;
99 authUser = authPassword = null;
103 return new MobileLedgerProfile(profileUUID, name, url, useAuthentication, authUser,
106 public String getUuid() {
109 public String getName() {
112 public void setName(CharSequence text) {
113 setName(String.valueOf(text));
115 public void setName(String name) {
118 public String getUrl() {
121 public void setUrl(CharSequence text) {
122 setUrl(String.valueOf(text));
124 public void setUrl(String url) {
127 public boolean isAuthEnabled() {
130 public void setAuthEnabled(boolean authEnabled) {
131 this.authEnabled = authEnabled;
133 public String getAuthUserName() {
136 public void setAuthUserName(CharSequence text) {
137 setAuthUserName(String.valueOf(text));
139 public void setAuthUserName(String authUserName) {
140 this.authUserName = authUserName;
142 public String getAuthPassword() {
145 public void setAuthPassword(CharSequence text) {
146 setAuthPassword(String.valueOf(text));
148 public void setAuthPassword(String authPassword) {
149 this.authPassword = authPassword;
151 public void storeInDB() {
152 SQLiteDatabase db = MLDB.getWritableDatabase();
153 db.beginTransaction();
155 db.execSQL("REPLACE INTO profiles(uuid, name, url, use_authentication, auth_user, " +
156 "auth_password) VALUES(?, ?, ?, ?, ?, ?)",
157 new Object[]{uuid, name, url, authEnabled, authEnabled ? authUserName : null,
158 authEnabled ? authPassword : null
160 db.setTransactionSuccessful();
166 public void storeAccount(String name) {
167 SQLiteDatabase db = MLDB.getWritableDatabase();
170 LedgerAccount acc = new LedgerAccount(name);
171 db.execSQL("replace into accounts(profile, name, name_upper, level, keep) values(?, " +
173 new Object[]{this.uuid, name, name.toUpperCase(), acc.getLevel()});
174 name = acc.getParentName();
175 } while (name != null);
177 public void storeAccountValue(String name, String currency, Float amount) {
178 SQLiteDatabase db = MLDB.getWritableDatabase();
179 db.execSQL("replace into account_values(profile, account, " +
180 "currency, value, keep) values(?, ?, ?, ?, 1);",
181 new Object[]{uuid, name, currency, amount});
183 public void storeTransaction(LedgerTransaction tr) {
184 SQLiteDatabase db = MLDB.getWritableDatabase();
186 db.execSQL("DELETE from transactions WHERE profile=? and id=?",
187 new Object[]{uuid, tr.getId()});
188 db.execSQL("DELETE from transaction_accounts WHERE profile = ? and transaction_id=?",
189 new Object[]{uuid, tr.getId()});
191 db.execSQL("INSERT INTO transactions(profile, id, date, description, data_hash, keep) " +
192 "values(?,?,?,?,?,1)",
193 new Object[]{uuid, tr.getId(), tr.getDate(), tr.getDescription(), tr.getDataHash()
196 for (LedgerTransactionAccount item : tr.getAccounts()) {
197 db.execSQL("INSERT INTO transaction_accounts(profile, transaction_id, " +
198 "account_name, amount, currency) values(?, ?, ?, ?, ?)",
199 new Object[]{uuid, tr.getId(), item.getAccountName(), item.getAmount(),
203 Log.d("profile", String.format("Transaction %d stored", tr.getId()));
205 public String get_option_value(String name, String default_value) {
206 SQLiteDatabase db = MLDB.getReadableDatabase();
207 try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
208 new String[]{uuid, name}))
210 if (cursor.moveToFirst()) {
211 String result = cursor.getString(0);
213 if (result == null) {
214 Log.d("profile", "returning default value for " + name);
215 result = default_value;
217 else Log.d("profile", String.format("option %s=%s", name, result));
221 else return default_value;
223 catch (Exception e) {
224 Log.d("db", "returning default value for " + name, e);
225 return default_value;
228 public long get_option_value(String name, long default_value) {
230 String result = get_option_value(name, "");
231 if ((result == null) || result.isEmpty()) {
232 Log.d("profile", String.format("Returning default value for option %s", name));
233 longResult = default_value;
237 longResult = Long.parseLong(result);
238 Log.d("profile", String.format("option %s=%s", name, result));
240 catch (Exception e) {
241 Log.d("profile", String.format("Returning default value for option %s", name), e);
242 longResult = default_value;
248 public void set_option_value(String name, String value) {
249 Log.d("profile", String.format("setting option %s=%s", name, value));
250 SQLiteDatabase db = MLDB.getWritableDatabase();
251 db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);",
252 new String[]{uuid, name, value});
254 public void set_option_value(String name, long value) {
255 set_option_value(name, String.valueOf(value));
257 public void removeFromDB() {
258 SQLiteDatabase db = MLDB.getWritableDatabase();
259 Log.d("db", String.format("removinf progile %s from DB", uuid));
260 db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});