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 long 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(long id, String dateString, String description) throws ParseException {
64 this(id, Globals.parseLedgerDate(dateString), description);
66 public LedgerTransaction(long id, SimpleDate date, String description,
67 MobileLedgerProfile profile) {
68 this.profile = profile.getId();
71 this.description = description;
72 this.accounts = new ArrayList<>();
76 public LedgerTransaction(long id, SimpleDate date, String description) {
77 this(id, date, description, Data.getProfile());
79 public LedgerTransaction(SimpleDate date, String description) {
80 this(0, date, description);
82 public LedgerTransaction(int id) {
83 this(id, (SimpleDate) null, null);
85 public LedgerTransaction(int id, long profileId) {
86 this.profile = profileId;
89 this.description = null;
90 this.accounts = new ArrayList<>();
92 this.dataLoaded = false;
94 public List<LedgerTransactionAccount> getAccounts() {
97 public void addAccount(LedgerTransactionAccount item) {
102 public SimpleDate getDateIfAny() {
106 public SimpleDate getDate() {
108 throw new IllegalStateException("Transaction has no date");
111 public void setDate(SimpleDate date) {
115 public String getDescription() {
118 public void setDescription(String description) {
119 this.description = description;
122 public String getComment() {
125 public void setComment(String comment) {
126 this.comment = comment;
128 public long getId() {
131 protected void fillDataHash() {
132 loadData(App.getDatabase());
133 if (dataHash != null)
136 Digest sha = new Digest(DIGEST_TYPE);
137 StringBuilder data = new StringBuilder();
139 data.append(profile);
140 data.append(getId());
142 data.append(getDescription());
144 data.append(getComment());
146 data.append(Globals.formatLedgerDate(getDate()));
148 for (LedgerTransactionAccount item : accounts) {
149 data.append(item.getAccountName());
151 data.append(item.getCurrency());
153 data.append(item.getAmount());
155 data.append(item.getComment());
157 sha.update(data.toString()
158 .getBytes(StandardCharsets.UTF_8));
159 dataHash = sha.digestToHexString();
161 catch (NoSuchAlgorithmException e) {
162 throw new RuntimeException(
163 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
166 public synchronized void loadData(SQLiteDatabase db) {
170 try (Cursor cTr = db.rawQuery(
171 "SELECT year, month, day, description, comment from transactions WHERE id=?",
172 new String[]{String.valueOf(id)}))
174 if (cTr.moveToFirst()) {
175 date = new SimpleDate(cTr.getInt(0), cTr.getInt(1), cTr.getInt(2));
176 description = cTr.getString(3);
177 comment = cTr.getString(4);
181 try (Cursor cAcc = db.rawQuery(
182 "SELECT account_name, amount, currency, comment FROM " +
183 "transaction_accounts WHERE transaction_id = ?",
184 new String[]{String.valueOf(id)}))
186 while (cAcc.moveToNext()) {
187 // debug("transactions",
188 // String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
189 // cAcc.getFloat(1), cAcc.getString(2)));
190 addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
191 cAcc.getString(2), cAcc.getString(3)));
200 public String getDataHash() {
204 public void finishLoading() {
208 public boolean equals(@Nullable Object obj) {
212 .equals(this.getClass()))
215 return ((LedgerTransaction) obj).getDataHash()
216 .equals(getDataHash());
219 public boolean hasAccountNamedLike(String name) {
220 name = name.toUpperCase();
221 for (LedgerTransactionAccount acc : accounts) {
222 if (acc.getAccountName()
230 public void markDataAsLoaded() {