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 void storeProfilesOrder() {
69 SQLiteDatabase db = MLDB.getWritableDatabase();
70 db.beginTransaction();
73 for (MobileLedgerProfile p : Data.profiles.getList()) {
74 db.execSQL("update profiles set order_no=? where uuid=?",
75 new Object[]{orderNo, p.getUuid()});
78 db.setTransactionSuccessful();
84 public static List<MobileLedgerProfile> createInitialProfileList() {
85 List<MobileLedgerProfile> result = new ArrayList<>();
86 MobileLedgerProfile first =
87 new MobileLedgerProfile(UUID.randomUUID().toString(), "default", "", false, "", "");
93 public static MobileLedgerProfile loadUUIDFromDB(String profileUUID) {
94 SQLiteDatabase db = MLDB.getReadableDatabase();
99 Boolean useAuthentication;
100 try (Cursor cursor = db.rawQuery("SELECT name, url, use_authentication, auth_user, " +
101 "auth_password FROM profiles WHERE uuid=?",
102 new String[]{profileUUID}))
104 if (cursor.moveToNext()) {
105 name = cursor.getString(0);
106 url = cursor.getString(1);
107 useAuthentication = cursor.getInt(2) == 1;
108 authUser = useAuthentication ? cursor.getString(3) : null;
109 authPassword = useAuthentication ? cursor.getString(4) : null;
112 name = "Unknown profile";
113 url = "Https://server/url";
114 useAuthentication = false;
115 authUser = authPassword = null;
119 return new MobileLedgerProfile(profileUUID, name, url, useAuthentication, authUser,
122 public String getUuid() {
125 public String getName() {
128 public void setName(CharSequence text) {
129 setName(String.valueOf(text));
131 public void setName(String name) {
134 public String getUrl() {
137 public void setUrl(CharSequence text) {
138 setUrl(String.valueOf(text));
140 public void setUrl(String url) {
143 public boolean isAuthEnabled() {
146 public void setAuthEnabled(boolean authEnabled) {
147 this.authEnabled = authEnabled;
149 public String getAuthUserName() {
152 public void setAuthUserName(CharSequence text) {
153 setAuthUserName(String.valueOf(text));
155 public void setAuthUserName(String authUserName) {
156 this.authUserName = authUserName;
158 public String getAuthPassword() {
161 public void setAuthPassword(CharSequence text) {
162 setAuthPassword(String.valueOf(text));
164 public void setAuthPassword(String authPassword) {
165 this.authPassword = authPassword;
167 public void storeInDB() {
168 SQLiteDatabase db = MLDB.getWritableDatabase();
169 db.beginTransaction();
171 db.execSQL("REPLACE INTO profiles(uuid, name, url, use_authentication, auth_user, " +
172 "auth_password) VALUES(?, ?, ?, ?, ?, ?)",
173 new Object[]{uuid, name, url, authEnabled, authEnabled ? authUserName : null,
174 authEnabled ? authPassword : null
176 db.setTransactionSuccessful();
182 public void storeAccount(String name) {
183 SQLiteDatabase db = MLDB.getWritableDatabase();
186 LedgerAccount acc = new LedgerAccount(name);
187 db.execSQL("replace into accounts(profile, name, name_upper, level, keep) values(?, " +
189 new Object[]{this.uuid, name, name.toUpperCase(), acc.getLevel()});
190 name = acc.getParentName();
191 } while (name != null);
193 public void storeAccountValue(String name, String currency, Float amount) {
194 SQLiteDatabase db = MLDB.getWritableDatabase();
195 db.execSQL("replace into account_values(profile, account, " +
196 "currency, value, keep) values(?, ?, ?, ?, 1);",
197 new Object[]{uuid, name, currency, amount});
199 public void storeTransaction(LedgerTransaction tr) {
200 SQLiteDatabase db = MLDB.getWritableDatabase();
202 db.execSQL("DELETE from transactions WHERE profile=? and id=?",
203 new Object[]{uuid, tr.getId()});
204 db.execSQL("DELETE from transaction_accounts WHERE profile = ? and transaction_id=?",
205 new Object[]{uuid, tr.getId()});
207 db.execSQL("INSERT INTO transactions(profile, id, date, description, data_hash, keep) " +
208 "values(?,?,?,?,?,1)",
209 new Object[]{uuid, tr.getId(), tr.getDate(), tr.getDescription(), tr.getDataHash()
212 for (LedgerTransactionAccount item : tr.getAccounts()) {
213 db.execSQL("INSERT INTO transaction_accounts(profile, transaction_id, " +
214 "account_name, amount, currency) values(?, ?, ?, ?, ?)",
215 new Object[]{uuid, tr.getId(), item.getAccountName(), item.getAmount(),
219 Log.d("profile", String.format("Transaction %d stored", tr.getId()));
221 public String get_option_value(String name, String default_value) {
222 SQLiteDatabase db = MLDB.getReadableDatabase();
223 try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
224 new String[]{uuid, name}))
226 if (cursor.moveToFirst()) {
227 String result = cursor.getString(0);
229 if (result == null) {
230 Log.d("profile", "returning default value for " + name);
231 result = default_value;
233 else Log.d("profile", String.format("option %s=%s", name, result));
237 else return default_value;
239 catch (Exception e) {
240 Log.d("db", "returning default value for " + name, e);
241 return default_value;
244 public long get_option_value(String name, long default_value) {
246 String result = get_option_value(name, "");
247 if ((result == null) || result.isEmpty()) {
248 Log.d("profile", String.format("Returning default value for option %s", name));
249 longResult = default_value;
253 longResult = Long.parseLong(result);
254 Log.d("profile", String.format("option %s=%s", name, result));
256 catch (Exception e) {
257 Log.d("profile", String.format("Returning default value for option %s", name), e);
258 longResult = default_value;
264 public void set_option_value(String name, String value) {
265 Log.d("profile", String.format("setting option %s=%s", name, value));
266 SQLiteDatabase db = MLDB.getWritableDatabase();
267 db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);",
268 new String[]{uuid, name, value});
270 public void set_option_value(String name, long value) {
271 set_option_value(name, String.valueOf(value));
273 public void removeFromDB() {
274 SQLiteDatabase db = MLDB.getWritableDatabase();
275 Log.d("db", String.format("removinf progile %s from DB", uuid));
276 db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});