]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java
keep the new transaction FAB hidden when the profile doesn't permit new transactions
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransaction.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.Digest;
25 import net.ktnx.mobileledger.utils.Globals;
26
27 import java.nio.charset.Charset;
28 import java.security.NoSuchAlgorithmException;
29 import java.text.ParseException;
30 import java.util.ArrayList;
31 import java.util.Comparator;
32 import java.util.Date;
33
34 public class LedgerTransaction {
35     private static final String DIGEST_TYPE = "SHA-256";
36     public final Comparator<LedgerTransactionAccount> comparator =
37             new Comparator<LedgerTransactionAccount>() {
38                 @Override
39                 public int compare(LedgerTransactionAccount o1, LedgerTransactionAccount o2) {
40                     int res = o1.getAccountName().compareTo(o2.getAccountName());
41                     if (res != 0) return res;
42                     res = o1.getCurrency().compareTo(o2.getCurrency());
43                     if (res != 0) return res;
44                     return Float.compare(o1.getAmount(), o2.getAmount());
45                 }
46             };
47     private String profile;
48     private Integer id;
49     private Date date;
50     private String description;
51     private ArrayList<LedgerTransactionAccount> accounts;
52     private String dataHash;
53     private boolean dataLoaded;
54     public LedgerTransaction(Integer id, String dateString, String description)
55             throws ParseException {
56         this(id, Globals.parseLedgerDate(dateString), description);
57     }
58     public LedgerTransaction(Integer id, Date date, String description) {
59         this.profile = Data.profile.get().getUuid();
60         this.id = id;
61         this.date = date;
62         this.description = description;
63         this.accounts = new ArrayList<>();
64         this.dataHash = null;
65         dataLoaded = false;
66     }
67     public LedgerTransaction(Date date, String description) {
68         this(null, date, description);
69     }
70     public LedgerTransaction(int id) {
71         this(id, (Date) null, null);
72     }
73     public ArrayList<LedgerTransactionAccount> getAccounts() {
74         return accounts;
75     }
76     public void addAccount(LedgerTransactionAccount item) {
77         accounts.add(item);
78         dataHash = null;
79     }
80     public Date getDate() {
81         return date;
82     }
83     public void setDate(Date date) {
84         this.date = date;
85         dataHash = null;
86     }
87     public String getDescription() {
88         return description;
89     }
90     public void setDescription(String description) {
91         this.description = description;
92         dataHash = null;
93     }
94     public int getId() {
95         return id;
96     }
97     protected void fillDataHash() {
98         if (dataHash != null) return;
99         try {
100             Digest sha = new Digest(DIGEST_TYPE);
101             StringBuilder data = new StringBuilder();
102             data.append(profile);
103             data.append(getId());
104             data.append('\0');
105             data.append(getDescription());
106             data.append('\0');
107             data.append(Globals.formatLedgerDate(getDate()));
108             data.append('\0');
109             for (LedgerTransactionAccount item : accounts) {
110                 data.append(item.getAccountName());
111                 data.append('\0');
112                 data.append(item.getCurrency());
113                 data.append('\0');
114                 data.append(item.getAmount());
115             }
116             sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
117             dataHash = sha.digestToHexString();
118         }
119         catch (NoSuchAlgorithmException e) {
120             throw new RuntimeException(
121                     String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
122         }
123     }
124     public boolean existsInDb(SQLiteDatabase db) {
125         fillDataHash();
126         try (Cursor c = db
127                 .rawQuery("SELECT 1 from transactions where data_hash = ?", new String[]{dataHash}))
128         {
129             boolean result = c.moveToFirst();
130             Log.d("db", String.format("Transaction %d (%s) %s", id, dataHash,
131                     result ? "already present" : "not present"));
132             return result;
133         }
134     }
135     public void loadData(SQLiteDatabase db) {
136         if (dataLoaded) return;
137
138         try (Cursor cTr = db
139                 .rawQuery("SELECT date, description from transactions WHERE profile=? AND id=?",
140                         new String[]{profile, String.valueOf(id)}))
141         {
142             if (cTr.moveToFirst()) {
143                 String dateString = cTr.getString(0);
144                 try {
145                     date = Globals.parseLedgerDate(dateString);
146                 }
147                 catch (ParseException e) {
148                     e.printStackTrace();
149                     throw new RuntimeException(
150                             String.format("Error parsing date '%s' from " + "transacion %d",
151                                     dateString, id));
152                 }
153                 description = cTr.getString(1);
154
155                 try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
156                                                "transaction_accounts WHERE " +
157                                                "profile=? AND transaction_id = ?",
158                         new String[]{profile, String.valueOf(id)}))
159                 {
160                     while (cAcc.moveToNext()) {
161 //                        Log.d("transactions",
162 //                                String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
163 //                                        cAcc.getFloat(1), cAcc.getString(2)));
164                         addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
165                                 cAcc.getString(2)));
166                     }
167
168                     finishLoading();
169                 }
170             }
171         }
172
173     }
174     public String getDataHash() {
175         return dataHash;
176     }
177     public void finishLoading() {
178         dataLoaded = true;
179     }
180 }