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.Globals;
25 import net.ktnx.mobileledger.utils.MLDB;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.UUID;
31 public final class MobileLedgerProfile {
35 private boolean authEnabled;
36 private String authUserName;
37 private String authPassword;
38 public MobileLedgerProfile(String uuid, String name, String url, boolean authEnabled,
39 String authUserName, String authPassword) {
43 this.authEnabled = authEnabled;
44 this.authUserName = authUserName;
45 this.authPassword = authPassword;
47 public MobileLedgerProfile(CharSequence name, CharSequence url, boolean authEnabled,
48 CharSequence authUserName, CharSequence authPassword) {
49 this.uuid = String.valueOf(UUID.randomUUID());
50 this.name = String.valueOf(name);
51 this.url = String.valueOf(url);
52 this.authEnabled = authEnabled;
53 this.authUserName = String.valueOf(authUserName);
54 this.authPassword = String.valueOf(authPassword);
56 public static List<MobileLedgerProfile> loadAllFromDB() {
57 List<MobileLedgerProfile> result = new ArrayList<>();
58 SQLiteDatabase db = MLDB.getReadableDatabase();
59 try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
60 "auth_password FROM profiles order by order_no", null))
62 while (cursor.moveToNext()) {
63 result.add(new MobileLedgerProfile(cursor.getString(0), cursor.getString(1),
64 cursor.getString(2), cursor.getInt(3) == 1, cursor.getString(4),
65 cursor.getString(5)));
70 public static void storeProfilesOrder() {
71 SQLiteDatabase db = MLDB.getWritableDatabase();
72 db.beginTransaction();
75 for (MobileLedgerProfile p : Data.profiles.getList()) {
76 db.execSQL("update profiles set order_no=? where uuid=?",
77 new Object[]{orderNo, p.getUuid()});
80 db.setTransactionSuccessful();
86 public static List<MobileLedgerProfile> createInitialProfileList() {
87 List<MobileLedgerProfile> result = new ArrayList<>();
88 MobileLedgerProfile first =
89 new MobileLedgerProfile(UUID.randomUUID().toString(), "default", "", false, "", "");
95 public static MobileLedgerProfile loadUUIDFromDB(String profileUUID) {
96 SQLiteDatabase db = MLDB.getReadableDatabase();
101 Boolean useAuthentication;
102 try (Cursor cursor = db.rawQuery("SELECT name, url, use_authentication, auth_user, " +
103 "auth_password FROM profiles WHERE uuid=?",
104 new String[]{profileUUID}))
106 if (cursor.moveToNext()) {
107 name = cursor.getString(0);
108 url = cursor.getString(1);
109 useAuthentication = cursor.getInt(2) == 1;
110 authUser = useAuthentication ? cursor.getString(3) : null;
111 authPassword = useAuthentication ? cursor.getString(4) : null;
114 name = "Unknown profile";
115 url = "Https://server/url";
116 useAuthentication = false;
117 authUser = authPassword = null;
121 return new MobileLedgerProfile(profileUUID, name, url, useAuthentication, authUser,
124 public String getUuid() {
127 public String getName() {
130 public void setName(String name) {
133 public void setName(CharSequence text) {
134 setName(String.valueOf(text));
136 public String getUrl() {
139 public void setUrl(String url) {
142 public void setUrl(CharSequence text) {
143 setUrl(String.valueOf(text));
145 public boolean isAuthEnabled() {
148 public void setAuthEnabled(boolean authEnabled) {
149 this.authEnabled = authEnabled;
151 public String getAuthUserName() {
154 public void setAuthUserName(String authUserName) {
155 this.authUserName = authUserName;
157 public void setAuthUserName(CharSequence text) {
158 setAuthUserName(String.valueOf(text));
160 public String getAuthPassword() {
163 public void setAuthPassword(String authPassword) {
164 this.authPassword = authPassword;
166 public void setAuthPassword(CharSequence text) {
167 setAuthPassword(String.valueOf(text));
169 public void storeInDB() {
170 SQLiteDatabase db = MLDB.getWritableDatabase();
171 db.beginTransaction();
173 db.execSQL("REPLACE INTO profiles(uuid, name, url, use_authentication, auth_user, " +
174 "auth_password) VALUES(?, ?, ?, ?, ?, ?)",
175 new Object[]{uuid, name, url, authEnabled, authEnabled ? authUserName : null,
176 authEnabled ? authPassword : null
178 db.setTransactionSuccessful();
184 public void storeAccount(LedgerAccount acc) {
185 SQLiteDatabase db = MLDB.getWritableDatabase();
187 // replace into is a bad idea because it would reset hidden to its default value
188 // we like the default, but for new accounts only
189 db.execSQL("update accounts set level = ?, keep = 1 where profile=? and name = ?",
190 new Object[]{acc.getLevel(), uuid, acc.getName()});
191 db.execSQL("insert into accounts(profile, name, name_upper, parent_name, level) " +
192 "select ?,?,?,?,? where (select changes() = 0)",
193 new Object[]{uuid, acc.getName(), acc.getName().toUpperCase(), acc.getParentName(),
197 public void storeAccountValue(String name, String currency, Float amount) {
198 SQLiteDatabase db = MLDB.getWritableDatabase();
199 db.execSQL("replace into account_values(profile, account, " +
200 "currency, value, keep) values(?, ?, ?, ?, 1);",
201 new Object[]{uuid, name, currency, amount});
203 public void storeTransaction(LedgerTransaction tr) {
204 SQLiteDatabase db = MLDB.getWritableDatabase();
206 db.execSQL("DELETE from transactions WHERE profile=? and id=?",
207 new Object[]{uuid, tr.getId()});
208 db.execSQL("DELETE from transaction_accounts WHERE profile = ? and transaction_id=?",
209 new Object[]{uuid, tr.getId()});
211 db.execSQL("INSERT INTO transactions(profile, id, date, description, data_hash, keep) " +
212 "values(?,?,?,?,?,1)",
213 new Object[]{uuid, tr.getId(), Globals.formatLedgerDate(tr.getDate()),
214 tr.getDescription(), tr.getDataHash()
217 for (LedgerTransactionAccount item : tr.getAccounts()) {
218 db.execSQL("INSERT INTO transaction_accounts(profile, transaction_id, " +
219 "account_name, amount, currency) values(?, ?, ?, ?, ?)",
220 new Object[]{uuid, tr.getId(), item.getAccountName(), item.getAmount(),
224 Log.d("profile", String.format("Transaction %d stored", tr.getId()));
226 public String getOption(String name, String default_value) {
227 SQLiteDatabase db = MLDB.getReadableDatabase();
228 try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
229 new String[]{uuid, name}))
231 if (cursor.moveToFirst()) {
232 String result = cursor.getString(0);
234 if (result == null) {
235 Log.d("profile", "returning default value for " + name);
236 result = default_value;
238 else Log.d("profile", String.format("option %s=%s", name, result));
242 else return default_value;
244 catch (Exception e) {
245 Log.d("db", "returning default value for " + name, e);
246 return default_value;
249 public long getLongOption(String name, long default_value) {
251 String result = getOption(name, "");
252 if ((result == null) || result.isEmpty()) {
253 Log.d("profile", String.format("Returning default value for option %s", name));
254 longResult = default_value;
258 longResult = Long.parseLong(result);
259 Log.d("profile", String.format("option %s=%s", name, result));
261 catch (Exception e) {
262 Log.d("profile", String.format("Returning default value for option %s", name), e);
263 longResult = default_value;
269 public void setOption(String name, String value) {
270 Log.d("profile", String.format("setting option %s=%s", name, value));
271 SQLiteDatabase db = MLDB.getWritableDatabase();
272 db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);",
273 new String[]{uuid, name, value});
275 public void setLongOption(String name, long value) {
276 setOption(name, String.valueOf(value));
278 public void removeFromDB() {
279 SQLiteDatabase db = MLDB.getWritableDatabase();
280 Log.d("db", String.format("removing progile %s from DB", uuid));
281 db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});
283 public LedgerAccount loadAccount(String name) {
284 SQLiteDatabase db = MLDB.getReadableDatabase();
285 try (Cursor cursor = db.rawQuery("SELECT hidden from accounts where profile=? and name=?",
286 new String[]{uuid, name}))
288 if (cursor.moveToFirst()) {
289 LedgerAccount acc = new LedgerAccount(name);
290 acc.setHidden(cursor.getInt(0) == 1);