on-demand loading of transaction details
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransaction.java
1 /*
2  * Copyright © 2018 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
23 import net.ktnx.mobileledger.utils.Digest;
24
25 import java.nio.charset.Charset;
26 import java.security.NoSuchAlgorithmException;
27 import java.util.ArrayList;
28 import java.util.Comparator;
29 import java.util.Iterator;
30
31 public class LedgerTransaction {
32     private static final String DIGEST_TYPE = "SHA-256";
33     public final Comparator<LedgerTransactionItem> comparator =
34             new Comparator<LedgerTransactionItem>() {
35                 @Override
36                 public int compare(LedgerTransactionItem o1, LedgerTransactionItem o2) {
37                     int res = o1.getAccountName().compareTo(o2.getAccountName());
38                     if (res != 0) return res;
39                     res = o1.getCurrency().compareTo(o2.getCurrency());
40                     if (res != 0) return res;
41                     return Float.compare(o1.getAmount(), o2.getAmount());
42                 }
43             };
44     private String id;
45     private String date;
46     private String description;
47     private ArrayList<LedgerTransactionItem> items;
48     private String dataHash;
49     private boolean dataLoaded;
50     public LedgerTransaction(String id, String date, String description) {
51         this.id = id;
52         this.date = date;
53         this.description = description;
54         this.items = new ArrayList<>();
55         this.dataHash = null;
56         dataLoaded = false;
57     }
58     public LedgerTransaction(int id, String date, String description) {
59         this(String.valueOf(id), date, description);
60     }
61     public LedgerTransaction(String date, String description) {
62         this(null, date, description);
63     }
64     public LedgerTransaction(int id) {
65         this(id, null, null);
66     }
67     public void add_item(LedgerTransactionItem item) {
68         items.add(item);
69         dataHash = null;
70     }
71     public String getDate() {
72         return date;
73     }
74     public void setDate(String date) {
75         this.date = date;
76         dataHash = null;
77     }
78     public String getDescription() {
79         return description;
80     }
81     public void setDescription(String description) {
82         this.description = description;
83         dataHash = null;
84     }
85     public Iterator<LedgerTransactionItem> getItemsIterator() {
86         return new Iterator<LedgerTransactionItem>() {
87             private int pointer = 0;
88             @Override
89             public boolean hasNext() {
90                 return pointer < items.size();
91             }
92
93             @Override
94             public LedgerTransactionItem next() {
95                 return hasNext() ? items.get(pointer++) : null;
96             }
97         };
98     }
99     public String getId() {
100         return id;
101     }
102     public void insertInto(SQLiteDatabase db) {
103         fillDataHash();
104         db.execSQL("INSERT INTO transactions(id, date, description, data_hash) values(?,?,?,?)",
105                 new String[]{id, date, description});
106
107         for (LedgerTransactionItem item : items) {
108             db.execSQL("INSERT INTO transaction_accounts(transaction_id, account_name, amount, " +
109                        "currency) values(?, ?, ?, ?)",
110                     new Object[]{id, item.getAccountName(), item.getAmount(), item.getCurrency()});
111         }
112     }
113     private void fillDataHash() {
114         if (dataHash != null) return;
115         try {
116             Digest sha = new Digest(DIGEST_TYPE);
117             StringBuilder data = new StringBuilder();
118             data.append(getId());
119             data.append('\0');
120             data.append(getDescription());
121             data.append('\0');
122             for (LedgerTransactionItem item : items) {
123                 data.append(item.getAccountName());
124                 data.append('\0');
125                 data.append(item.getCurrency());
126                 data.append('\0');
127                 data.append(item.getAmount());
128             }
129             sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
130             dataHash = sha.digestToHexString();
131         }
132         catch (NoSuchAlgorithmException e) {
133             throw new RuntimeException(
134                     String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
135         }
136     }
137     public void loadData(SQLiteDatabase db) {
138         if (dataLoaded) return;
139
140         try (Cursor cTr = db.rawQuery("SELECT date, description from transactions WHERE " +
141                                          "id=?",new String[]{id})) {
142             if (cTr.moveToFirst()) {
143                 date = cTr.getString(0);
144                 description = cTr.getString(1);
145
146                 try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
147                                                "transaction_accounts WHERE transaction_id = ?",
148                         new String[]{id}))
149                 {
150                     while (cAcc.moveToNext()) {
151                         add_item(
152                                 new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1),
153                                         cAcc.getString(2)));
154                     }
155                 }
156             }
157         }
158
159     }
160 }