2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe 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 * MoLe 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 MoLe. 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 {
34 private boolean permitPosting;
36 private boolean authEnabled;
37 private String authUserName;
38 private String authPassword;
39 public MobileLedgerProfile(String uuid, String name, boolean permitPosting, String url,
40 boolean authEnabled, String authUserName, String authPassword) {
43 this.permitPosting = permitPosting;
45 this.authEnabled = authEnabled;
46 this.authUserName = authUserName;
47 this.authPassword = authPassword;
49 public MobileLedgerProfile(CharSequence name, boolean permitPosting, CharSequence url,
50 boolean authEnabled, CharSequence authUserName,
51 CharSequence authPassword) {
52 this.uuid = String.valueOf(UUID.randomUUID());
53 this.name = String.valueOf(name);
54 this.permitPosting = permitPosting;
55 this.url = String.valueOf(url);
56 this.authEnabled = authEnabled;
57 this.authUserName = String.valueOf(authUserName);
58 this.authPassword = String.valueOf(authPassword);
60 // loads all profiles into Data.profiles
61 // returns the profile with the given UUID
62 public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) {
63 MobileLedgerProfile result = null;
64 List<MobileLedgerProfile> list = new ArrayList<>();
65 SQLiteDatabase db = MLDB.getReadableDatabase();
66 try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
67 "auth_password, permit_posting FROM profiles order by " +
70 while (cursor.moveToNext()) {
71 MobileLedgerProfile item =
72 new MobileLedgerProfile(cursor.getString(0), cursor.getString(1),
73 cursor.getInt(6) == 1, cursor.getString(2), cursor.getInt(3) == 1,
74 cursor.getString(4), cursor.getString(5));
76 if (item.getUuid().equals(currentProfileUUID)) result = item;
79 Data.profiles.setList(list);
82 public static void storeProfilesOrder() {
83 SQLiteDatabase db = MLDB.getWritableDatabase();
84 db.beginTransaction();
87 for (MobileLedgerProfile p : Data.profiles.getList()) {
88 db.execSQL("update profiles set order_no=? where uuid=?",
89 new Object[]{orderNo, p.getUuid()});
92 db.setTransactionSuccessful();
98 public boolean isPostingPermitted() {
101 public void setPostingPermitted(boolean permitPosting) {
102 this.permitPosting = permitPosting;
104 public String getUuid() {
107 public String getName() {
110 public void setName(String name) {
113 public void setName(CharSequence text) {
114 setName(String.valueOf(text));
116 public String getUrl() {
119 public void setUrl(String url) {
122 public void setUrl(CharSequence text) {
123 setUrl(String.valueOf(text));
125 public boolean isAuthEnabled() {
128 public void setAuthEnabled(boolean authEnabled) {
129 this.authEnabled = authEnabled;
131 public String getAuthUserName() {
134 public void setAuthUserName(String authUserName) {
135 this.authUserName = authUserName;
137 public void setAuthUserName(CharSequence text) {
138 setAuthUserName(String.valueOf(text));
140 public String getAuthPassword() {
143 public void setAuthPassword(String authPassword) {
144 this.authPassword = authPassword;
146 public void setAuthPassword(CharSequence text) {
147 setAuthPassword(String.valueOf(text));
149 public void storeInDB() {
150 SQLiteDatabase db = MLDB.getWritableDatabase();
151 db.beginTransaction();
153 db.execSQL("REPLACE INTO profiles(uuid, name, permit_posting, url, " +
154 "use_authentication, auth_user, " +
155 "auth_password) VALUES(?, ?, ?, ?, ?, ?, ?)",
156 new Object[]{uuid, name, permitPosting, url, authEnabled,
157 authEnabled ? authUserName : null,
158 authEnabled ? authPassword : null
160 db.setTransactionSuccessful();
166 public void storeAccount(LedgerAccount acc) {
167 SQLiteDatabase db = MLDB.getWritableDatabase();
169 // replace into is a bad idea because it would reset hidden to its default value
170 // we like the default, but for new accounts only
171 db.execSQL("update accounts set level = ?, keep = 1 where profile=? and name = ?",
172 new Object[]{acc.getLevel(), uuid, acc.getName()});
173 db.execSQL("insert into accounts(profile, name, name_upper, parent_name, level) " +
174 "select ?,?,?,?,? where (select changes() = 0)",
175 new Object[]{uuid, acc.getName(), acc.getName().toUpperCase(), acc.getParentName(),
179 public void storeAccountValue(String name, String currency, Float amount) {
180 SQLiteDatabase db = MLDB.getWritableDatabase();
181 db.execSQL("replace into account_values(profile, account, " +
182 "currency, value, keep) values(?, ?, ?, ?, 1);",
183 new Object[]{uuid, name, currency, amount});
185 public void storeTransaction(LedgerTransaction tr) {
186 SQLiteDatabase db = MLDB.getWritableDatabase();
188 db.execSQL("DELETE from transactions WHERE profile=? and id=?",
189 new Object[]{uuid, tr.getId()});
190 db.execSQL("DELETE from transaction_accounts WHERE profile = ? and transaction_id=?",
191 new Object[]{uuid, tr.getId()});
193 db.execSQL("INSERT INTO transactions(profile, id, date, description, data_hash, keep) " +
194 "values(?,?,?,?,?,1)",
195 new Object[]{uuid, tr.getId(), Globals.formatLedgerDate(tr.getDate()),
196 tr.getDescription(), tr.getDataHash()
199 for (LedgerTransactionAccount item : tr.getAccounts()) {
200 db.execSQL("INSERT INTO transaction_accounts(profile, transaction_id, " +
201 "account_name, amount, currency) values(?, ?, ?, ?, ?)",
202 new Object[]{uuid, tr.getId(), item.getAccountName(), item.getAmount(),
206 Log.d("profile", String.format("Transaction %d stored", tr.getId()));
208 public String getOption(String name, String default_value) {
209 SQLiteDatabase db = MLDB.getReadableDatabase();
210 try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
211 new String[]{uuid, name}))
213 if (cursor.moveToFirst()) {
214 String result = cursor.getString(0);
216 if (result == null) {
217 Log.d("profile", "returning default value for " + name);
218 result = default_value;
220 else Log.d("profile", String.format("option %s=%s", name, result));
224 else return default_value;
226 catch (Exception e) {
227 Log.d("db", "returning default value for " + name, e);
228 return default_value;
231 public long getLongOption(String name, long default_value) {
233 String result = getOption(name, "");
234 if ((result == null) || result.isEmpty()) {
235 Log.d("profile", String.format("Returning default value for option %s", name));
236 longResult = default_value;
240 longResult = Long.parseLong(result);
241 Log.d("profile", String.format("option %s=%s", name, result));
243 catch (Exception e) {
244 Log.d("profile", String.format("Returning default value for option %s", name), e);
245 longResult = default_value;
251 public void setOption(String name, String value) {
252 Log.d("profile", String.format("setting option %s=%s", name, value));
253 SQLiteDatabase db = MLDB.getWritableDatabase();
254 db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);",
255 new String[]{uuid, name, value});
257 public void setLongOption(String name, long value) {
258 setOption(name, String.valueOf(value));
260 public void removeFromDB() {
261 SQLiteDatabase db = MLDB.getWritableDatabase();
262 Log.d("db", String.format("removing progile %s from DB", uuid));
263 db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});
265 public LedgerAccount loadAccount(String name) {
266 SQLiteDatabase db = MLDB.getReadableDatabase();
267 try (Cursor cursor = db.rawQuery("SELECT hidden from accounts where profile=? and name=?",
268 new String[]{uuid, name}))
270 if (cursor.moveToFirst()) {
271 LedgerAccount acc = new LedgerAccount(name);
272 acc.setHidden(cursor.getInt(0) == 1);
280 public LedgerTransaction loadTransaction(int transactionId) {
281 LedgerTransaction tr = new LedgerTransaction(transactionId, this.uuid);
282 tr.loadData(MLDB.getReadableDatabase());