]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
remove unused method
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / MobileLedgerProfile.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.util.Log;
23
24 import net.ktnx.mobileledger.utils.Globals;
25 import net.ktnx.mobileledger.utils.MLDB;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.UUID;
30
31 public final class MobileLedgerProfile {
32     private String uuid;
33     private String name;
34     private String url;
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) {
40         this.uuid = uuid;
41         this.name = name;
42         this.url = url;
43         this.authEnabled = authEnabled;
44         this.authUserName = authUserName;
45         this.authPassword = authPassword;
46     }
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);
55     }
56     // loads all profiles into Data.profiles
57     // returns the profile with the given UUID
58     public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) {
59         MobileLedgerProfile result = null;
60         List<MobileLedgerProfile> list = new ArrayList<>();
61         SQLiteDatabase db = MLDB.getReadableDatabase();
62         try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
63                                          "auth_password FROM profiles order by order_no", null))
64         {
65             while (cursor.moveToNext()) {
66                 MobileLedgerProfile item =
67                         new MobileLedgerProfile(cursor.getString(0), cursor.getString(1),
68                         cursor.getString(2), cursor.getInt(3) == 1, cursor.getString(4),
69                                 cursor.getString(5));
70                 list.add(item);
71                 if (item.getUuid().equals(currentProfileUUID)) result = item;
72             }
73         }
74         Data.profiles.setList(list);
75         return result;
76     }
77     public static void storeProfilesOrder() {
78         SQLiteDatabase db = MLDB.getWritableDatabase();
79         db.beginTransaction();
80         try {
81             int orderNo = 0;
82             for (MobileLedgerProfile p : Data.profiles.getList()) {
83                 db.execSQL("update profiles set order_no=? where uuid=?",
84                         new Object[]{orderNo, p.getUuid()});
85                 orderNo++;
86             }
87             db.setTransactionSuccessful();
88         }
89         finally {
90             db.endTransaction();
91         }
92     }
93
94     public String getUuid() {
95         return uuid;
96     }
97     public String getName() {
98         return name;
99     }
100     public void setName(String name) {
101         this.name = name;
102     }
103     public void setName(CharSequence text) {
104         setName(String.valueOf(text));
105     }
106     public String getUrl() {
107         return url;
108     }
109     public void setUrl(String url) {
110         this.url = url;
111     }
112     public void setUrl(CharSequence text) {
113         setUrl(String.valueOf(text));
114     }
115     public boolean isAuthEnabled() {
116         return authEnabled;
117     }
118     public void setAuthEnabled(boolean authEnabled) {
119         this.authEnabled = authEnabled;
120     }
121     public String getAuthUserName() {
122         return authUserName;
123     }
124     public void setAuthUserName(String authUserName) {
125         this.authUserName = authUserName;
126     }
127     public void setAuthUserName(CharSequence text) {
128         setAuthUserName(String.valueOf(text));
129     }
130     public String getAuthPassword() {
131         return authPassword;
132     }
133     public void setAuthPassword(String authPassword) {
134         this.authPassword = authPassword;
135     }
136     public void setAuthPassword(CharSequence text) {
137         setAuthPassword(String.valueOf(text));
138     }
139     public void storeInDB() {
140         SQLiteDatabase db = MLDB.getWritableDatabase();
141         db.beginTransaction();
142         try {
143             db.execSQL("REPLACE INTO profiles(uuid, name, url, use_authentication, auth_user, " +
144                        "auth_password) VALUES(?, ?, ?, ?, ?, ?)",
145                     new Object[]{uuid, name, url, authEnabled, authEnabled ? authUserName : null,
146                                  authEnabled ? authPassword : null
147                     });
148             db.setTransactionSuccessful();
149         }
150         finally {
151             db.endTransaction();
152         }
153     }
154     public void storeAccount(LedgerAccount acc) {
155         SQLiteDatabase db = MLDB.getWritableDatabase();
156
157         // replace into is a bad idea because it would reset hidden to its default value
158         // we like the default, but for new accounts only
159         db.execSQL("update accounts set level = ?, keep = 1 where profile=? and name = ?",
160                 new Object[]{acc.getLevel(), uuid, acc.getName()});
161         db.execSQL("insert into accounts(profile, name, name_upper, parent_name, level) " +
162                    "select ?,?,?,?,? where (select changes() = 0)",
163                 new Object[]{uuid, acc.getName(), acc.getName().toUpperCase(), acc.getParentName(),
164                              acc.getLevel()
165                 });
166     }
167     public void storeAccountValue(String name, String currency, Float amount) {
168         SQLiteDatabase db = MLDB.getWritableDatabase();
169         db.execSQL("replace into account_values(profile, account, " +
170                    "currency, value, keep) values(?, ?, ?, ?, 1);",
171                 new Object[]{uuid, name, currency, amount});
172     }
173     public void storeTransaction(LedgerTransaction tr) {
174         SQLiteDatabase db = MLDB.getWritableDatabase();
175         tr.fillDataHash();
176         db.execSQL("DELETE from transactions WHERE profile=? and id=?",
177                 new Object[]{uuid, tr.getId()});
178         db.execSQL("DELETE from transaction_accounts WHERE profile = ? and transaction_id=?",
179                 new Object[]{uuid, tr.getId()});
180
181         db.execSQL("INSERT INTO transactions(profile, id, date, description, data_hash, keep) " +
182                    "values(?,?,?,?,?,1)",
183                 new Object[]{uuid, tr.getId(), Globals.formatLedgerDate(tr.getDate()),
184                              tr.getDescription(), tr.getDataHash()
185                 });
186
187         for (LedgerTransactionAccount item : tr.getAccounts()) {
188             db.execSQL("INSERT INTO transaction_accounts(profile, transaction_id, " +
189                        "account_name, amount, currency) values(?, ?, ?, ?, ?)",
190                     new Object[]{uuid, tr.getId(), item.getAccountName(), item.getAmount(),
191                                  item.getCurrency()
192                     });
193         }
194         Log.d("profile", String.format("Transaction %d stored", tr.getId()));
195     }
196     public String getOption(String name, String default_value) {
197         SQLiteDatabase db = MLDB.getReadableDatabase();
198         try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
199                 new String[]{uuid, name}))
200         {
201             if (cursor.moveToFirst()) {
202                 String result = cursor.getString(0);
203
204                 if (result == null) {
205                     Log.d("profile", "returning default value for " + name);
206                     result = default_value;
207                 }
208                 else Log.d("profile", String.format("option %s=%s", name, result));
209
210                 return result;
211             }
212             else return default_value;
213         }
214         catch (Exception e) {
215             Log.d("db", "returning default value for " + name, e);
216             return default_value;
217         }
218     }
219     public long getLongOption(String name, long default_value) {
220         long longResult;
221         String result = getOption(name, "");
222         if ((result == null) || result.isEmpty()) {
223             Log.d("profile", String.format("Returning default value for option %s", name));
224             longResult = default_value;
225         }
226         else {
227             try {
228                 longResult = Long.parseLong(result);
229                 Log.d("profile", String.format("option %s=%s", name, result));
230             }
231             catch (Exception e) {
232                 Log.d("profile", String.format("Returning default value for option %s", name), e);
233                 longResult = default_value;
234             }
235         }
236
237         return longResult;
238     }
239     public void setOption(String name, String value) {
240         Log.d("profile", String.format("setting option %s=%s", name, value));
241         SQLiteDatabase db = MLDB.getWritableDatabase();
242         db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);",
243                 new String[]{uuid, name, value});
244     }
245     public void setLongOption(String name, long value) {
246         setOption(name, String.valueOf(value));
247     }
248     public void removeFromDB() {
249         SQLiteDatabase db = MLDB.getWritableDatabase();
250         Log.d("db", String.format("removing progile %s from DB", uuid));
251         db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});
252     }
253     public LedgerAccount loadAccount(String name) {
254         SQLiteDatabase db = MLDB.getReadableDatabase();
255         try (Cursor cursor = db.rawQuery("SELECT hidden from accounts where profile=? and name=?",
256                 new String[]{uuid, name}))
257         {
258             if (cursor.moveToFirst()) {
259                 LedgerAccount acc = new LedgerAccount(name);
260                 acc.setHidden(cursor.getInt(0) == 1);
261
262                 return acc;
263             }
264         }
265
266         return null;
267     }
268 }