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.utils.Digest;
28 import net.ktnx.mobileledger.utils.Globals;
29 import net.ktnx.mobileledger.utils.SimpleDate;
31 import java.nio.charset.StandardCharsets;
32 import java.security.NoSuchAlgorithmException;
33 import java.text.ParseException;
34 import java.util.ArrayList;
35 import java.util.Comparator;
36 import java.util.List;
38 public class LedgerTransaction {
39 private static final String DIGEST_TYPE = "SHA-256";
40 public final Comparator<LedgerTransactionAccount> comparator = (o1, o2) -> {
41 int res = o1.getAccountName()
42 .compareTo(o2.getAccountName());
45 res = o1.getCurrency()
46 .compareTo(o2.getCurrency());
50 .compareTo(o2.getComment());
53 return Float.compare(o1.getAmount(), o2.getAmount());
55 private final long profile;
56 private final Integer id;
57 private final List<LedgerTransactionAccount> accounts;
58 private SimpleDate date;
59 private String description;
60 private String comment;
61 private String dataHash;
62 private boolean dataLoaded;
63 public LedgerTransaction(Integer id, String dateString, String description)
64 throws ParseException {
65 this(id, Globals.parseLedgerDate(dateString), description);
67 public LedgerTransaction(Integer id, SimpleDate date, String description,
68 MobileLedgerProfile profile) {
69 this.profile = profile.getId();
72 this.description = description;
73 this.accounts = new ArrayList<>();
77 public LedgerTransaction(Integer id, SimpleDate date, String description) {
78 this(id, date, description, Data.getProfile());
80 public LedgerTransaction(SimpleDate date, String description) {
81 this(null, date, description);
83 public LedgerTransaction(int id) {
84 this(id, (SimpleDate) null, null);
86 public LedgerTransaction(int id, long profileId) {
87 this.profile = profileId;
90 this.description = null;
91 this.accounts = new ArrayList<>();
93 this.dataLoaded = false;
95 public List<LedgerTransactionAccount> getAccounts() {
98 public void addAccount(LedgerTransactionAccount item) {
103 public SimpleDate getDateIfAny() {
107 public SimpleDate getDate() {
109 throw new IllegalStateException("Transaction has no date");
112 public void setDate(SimpleDate date) {
116 public String getDescription() {
119 public void setDescription(String description) {
120 this.description = description;
123 public String getComment() {
126 public void setComment(String comment) {
127 this.comment = comment;
132 protected void fillDataHash() {
133 loadData(App.getDatabase());
134 if (dataHash != null)
137 Digest sha = new Digest(DIGEST_TYPE);
138 StringBuilder data = new StringBuilder();
140 data.append(profile);
141 data.append(getId());
143 data.append(getDescription());
145 data.append(getComment());
147 data.append(Globals.formatLedgerDate(getDate()));
149 for (LedgerTransactionAccount item : accounts) {
150 data.append(item.getAccountName());
152 data.append(item.getCurrency());
154 data.append(item.getAmount());
156 data.append(item.getComment());
158 sha.update(data.toString()
159 .getBytes(StandardCharsets.UTF_8));
160 dataHash = sha.digestToHexString();
162 catch (NoSuchAlgorithmException e) {
163 throw new RuntimeException(
164 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
167 public synchronized void loadData(SQLiteDatabase db) {
171 try (Cursor cTr = db.rawQuery(
172 "SELECT year, month, day, description, comment from transactions WHERE profile=? " +
173 "AND id=?", new String[]{String.valueOf(profile), String.valueOf(id)}))
175 if (cTr.moveToFirst()) {
176 date = new SimpleDate(cTr.getInt(0), cTr.getInt(1), cTr.getInt(2));
177 description = cTr.getString(3);
178 comment = cTr.getString(4);
182 try (Cursor cAcc = db.rawQuery(
183 "SELECT account_name, amount, currency, comment FROM " +
184 "transaction_accounts WHERE profile=? AND transaction_id = ?",
185 new String[]{String.valueOf(profile), String.valueOf(id)}))
187 while (cAcc.moveToNext()) {
188 // debug("transactions",
189 // String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
190 // cAcc.getFloat(1), cAcc.getString(2)));
191 addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
192 cAcc.getString(2), cAcc.getString(3)));
201 public String getDataHash() {
205 public void finishLoading() {
209 public boolean equals(@Nullable Object obj) {
213 .equals(this.getClass()))
216 return ((LedgerTransaction) obj).getDataHash()
217 .equals(getDataHash());
220 public boolean hasAccountNamedLike(String name) {
221 name = name.toUpperCase();
222 for (LedgerTransactionAccount acc : accounts) {
223 if (acc.getAccountName()
231 public void markDataAsLoaded() {