2 * Copyright © 2020 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;
37 public class LedgerTransaction {
38 private static final String DIGEST_TYPE = "SHA-256";
39 public final Comparator<LedgerTransactionAccount> comparator = (o1, o2) -> {
40 int res = o1.getAccountName()
41 .compareTo(o2.getAccountName());
44 res = o1.getCurrency()
45 .compareTo(o2.getCurrency());
49 .compareTo(o2.getComment());
52 return Float.compare(o1.getAmount(), o2.getAmount());
54 private String profile;
56 private SimpleDate date;
57 private String description;
58 private String comment;
59 private ArrayList<LedgerTransactionAccount> accounts;
60 private String dataHash;
61 private boolean dataLoaded;
62 public LedgerTransaction(Integer id, String dateString, String description)
63 throws ParseException {
64 this(id, Globals.parseLedgerDate(dateString), description);
66 public LedgerTransaction(Integer id, SimpleDate date, String description,
67 MobileLedgerProfile profile) {
68 this.profile = profile.getUuid();
71 this.description = description;
72 this.accounts = new ArrayList<>();
76 public LedgerTransaction(Integer id, SimpleDate date, String description) {
77 this(id, date, description, Data.getProfile());
79 public LedgerTransaction(SimpleDate date, String description) {
80 this(null, date, description);
82 public LedgerTransaction(int id) {
83 this(id, (SimpleDate) null, null);
85 public LedgerTransaction(int id, String profileUUID) {
86 this.profile = profileUUID;
89 this.description = null;
90 this.accounts = new ArrayList<>();
92 this.dataLoaded = false;
94 public ArrayList<LedgerTransactionAccount> getAccounts() {
97 public void addAccount(LedgerTransactionAccount item) {
102 public SimpleDate getDateIfAny() {
106 public SimpleDate getDate() {
107 loadData(App.getDatabase());
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 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 boolean existsInDb(SQLiteDatabase db) {
168 try (Cursor c = db.rawQuery("SELECT 1 from transactions where data_hash = ?",
169 new String[]{dataHash}))
171 boolean result = c.moveToFirst();
172 // debug("db", String.format("Transaction %d (%s) %s", id, dataHash,
173 // result ? "already present" : "not present"));
177 public void loadData(SQLiteDatabase db) {
181 try (Cursor cTr = db.rawQuery(
182 "SELECT year, month, day, description, comment from transactions WHERE profile=? " +
183 "AND id=?", new String[]{profile, String.valueOf(id)}))
185 if (cTr.moveToFirst()) {
186 date = new SimpleDate(cTr.getInt(0), cTr.getInt(1), cTr.getInt(2));
187 description = cTr.getString(3);
188 comment = cTr.getString(4);
192 try (Cursor cAcc = db.rawQuery(
193 "SELECT account_name, amount, currency, comment FROM " +
194 "transaction_accounts WHERE profile=? AND transaction_id = ?",
195 new String[]{profile, String.valueOf(id)}))
197 while (cAcc.moveToNext()) {
198 // debug("transactions",
199 // String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
200 // cAcc.getFloat(1), cAcc.getString(2)));
201 addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
202 cAcc.getString(2), cAcc.getString(3)));
211 public String getDataHash() {
215 public void finishLoading() {
219 public boolean equals(@Nullable Object obj) {
223 .equals(this.getClass()))
226 return ((LedgerTransaction) obj).getDataHash()
227 .equals(getDataHash());
230 public boolean hasAccountNamedLike(String name) {
231 name = name.toUpperCase();
232 for (LedgerTransactionAccount acc : accounts) {
233 if (acc.getAccountName()