2 * Copyright © 2021 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;
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
26 import net.ktnx.mobileledger.App;
27 import net.ktnx.mobileledger.db.Profile;
28 import net.ktnx.mobileledger.db.Transaction;
29 import net.ktnx.mobileledger.db.TransactionAccount;
30 import net.ktnx.mobileledger.db.TransactionWithAccounts;
31 import net.ktnx.mobileledger.utils.Digest;
32 import net.ktnx.mobileledger.utils.Globals;
33 import net.ktnx.mobileledger.utils.SimpleDate;
35 import java.nio.charset.StandardCharsets;
36 import java.security.NoSuchAlgorithmException;
37 import java.text.ParseException;
38 import java.util.ArrayList;
39 import java.util.Comparator;
40 import java.util.List;
42 public class LedgerTransaction {
43 private static final String DIGEST_TYPE = "SHA-256";
44 public final Comparator<LedgerTransactionAccount> comparator = (o1, o2) -> {
45 int res = o1.getAccountName()
46 .compareTo(o2.getAccountName());
49 res = o1.getCurrency()
50 .compareTo(o2.getCurrency());
54 .compareTo(o2.getComment());
57 return Float.compare(o1.getAmount(), o2.getAmount());
59 private final long profile;
60 private final long ledgerId;
61 private final List<LedgerTransactionAccount> accounts;
63 private SimpleDate date;
64 private String description;
65 private String comment;
66 private String dataHash;
67 private boolean dataLoaded;
68 public LedgerTransaction(long ledgerId, String dateString, String description)
69 throws ParseException {
70 this(ledgerId, Globals.parseLedgerDate(dateString), description);
72 public LedgerTransaction(TransactionWithAccounts dbo) {
73 this(dbo.transaction.getLedgerId(), dbo.transaction.getProfileId());
74 dbId = dbo.transaction.getId();
75 date = new SimpleDate(dbo.transaction.getYear(), dbo.transaction.getMonth(),
76 dbo.transaction.getDay());
77 description = dbo.transaction.getDescription();
78 comment = dbo.transaction.getComment();
79 dataHash = dbo.transaction.getDataHash();
80 if (dbo.accounts != null)
81 for (TransactionAccount acc : dbo.accounts) {
82 accounts.add(new LedgerTransactionAccount(acc));
86 public TransactionWithAccounts toDBO() {
87 TransactionWithAccounts o = new TransactionWithAccounts();
88 o.transaction = new Transaction();
89 o.transaction.setId(dbId);
90 o.transaction.setProfileId(profile);
91 o.transaction.setLedgerId(ledgerId);
92 o.transaction.setYear(date.year);
93 o.transaction.setMonth(date.month);
94 o.transaction.setDay(date.day);
95 o.transaction.setDescription(description);
96 o.transaction.setComment(comment);
98 o.transaction.setDataHash(dataHash);
100 o.accounts = new ArrayList<>();
102 for (LedgerTransactionAccount acc : accounts) {
103 TransactionAccount a = acc.toDBO();
104 a.setOrderNo(orderNo++);
105 a.setTransactionId(dbId);
110 public LedgerTransaction(long ledgerId, SimpleDate date, String description, Profile profile) {
111 this.profile = profile.getId();
112 this.ledgerId = ledgerId;
114 this.description = description;
115 this.accounts = new ArrayList<>();
116 this.dataHash = null;
119 public LedgerTransaction(long ledgerId, SimpleDate date, String description) {
120 this(ledgerId, date, description, Data.getProfile());
122 public LedgerTransaction(SimpleDate date, String description) {
123 this(0, date, description);
125 public LedgerTransaction(int ledgerId) {
126 this(ledgerId, (SimpleDate) null, null);
128 public LedgerTransaction(long ledgerId, long profileId) {
129 this.profile = profileId;
130 this.ledgerId = ledgerId;
132 this.description = null;
133 this.accounts = new ArrayList<>();
134 this.dataHash = null;
135 this.dataLoaded = false;
137 public List<LedgerTransactionAccount> getAccounts() {
140 public void addAccount(LedgerTransactionAccount item) {
145 public SimpleDate getDateIfAny() {
149 public SimpleDate getDate() {
151 throw new IllegalStateException("Transaction has no date");
154 public void setDate(SimpleDate date) {
158 public String getDescription() {
161 public void setDescription(String description) {
162 this.description = description;
165 public String getComment() {
168 public void setComment(String comment) {
169 this.comment = comment;
171 public long getLedgerId() {
174 protected void fillDataHash() {
175 loadData(App.getDatabase());
176 if (dataHash != null)
179 Digest sha = new Digest(DIGEST_TYPE);
180 StringBuilder data = new StringBuilder();
182 data.append(profile);
183 data.append(getLedgerId());
185 data.append(getDescription());
187 data.append(getComment());
189 data.append(Globals.formatLedgerDate(getDate()));
191 for (LedgerTransactionAccount item : accounts) {
192 data.append(item.getAccountName());
194 data.append(item.getCurrency());
196 data.append(item.getAmount());
198 data.append(item.getComment());
200 sha.update(data.toString()
201 .getBytes(StandardCharsets.UTF_8));
202 dataHash = sha.digestToHexString();
204 catch (NoSuchAlgorithmException e) {
205 throw new RuntimeException(
206 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
209 public synchronized void loadData(SQLiteDatabase db) {
213 try (Cursor cTr = db.rawQuery(
214 "SELECT year, month, day, description, comment from transactions WHERE id=?",
215 new String[]{String.valueOf(ledgerId)}))
217 if (cTr.moveToFirst()) {
218 date = new SimpleDate(cTr.getInt(0), cTr.getInt(1), cTr.getInt(2));
219 description = cTr.getString(3);
220 comment = cTr.getString(4);
224 try (Cursor cAcc = db.rawQuery(
225 "SELECT account_name, amount, currency, comment FROM " +
226 "transaction_accounts WHERE transaction_id = ?",
227 new String[]{String.valueOf(ledgerId)}))
229 while (cAcc.moveToNext()) {
230 // debug("transactions",
231 // String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
232 // cAcc.getFloat(1), cAcc.getString(2)));
233 addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
234 cAcc.getString(2), cAcc.getString(3)));
243 public String getDataHash() {
247 public void finishLoading() {
251 public boolean equals(@Nullable Object obj) {
255 .equals(this.getClass()))
258 return ((LedgerTransaction) obj).getDataHash()
259 .equals(getDataHash());
262 public boolean hasAccountNamedLike(String name) {
263 name = name.toUpperCase();
264 for (LedgerTransactionAccount acc : accounts) {
265 if (acc.getAccountName()
273 public void markDataAsLoaded() {