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 =
40 new Comparator<LedgerTransactionAccount>() {
42 public int compare(LedgerTransactionAccount o1, LedgerTransactionAccount o2) {
43 int res = o1.getAccountName()
44 .compareTo(o2.getAccountName());
47 res = o1.getCurrency()
48 .compareTo(o2.getCurrency());
52 .compareTo(o2.getComment());
55 return Float.compare(o1.getAmount(), o2.getAmount());
58 private String profile;
60 private SimpleDate date;
61 private String description;
62 private String comment;
63 private ArrayList<LedgerTransactionAccount> accounts;
64 private String dataHash;
65 private boolean dataLoaded;
66 public LedgerTransaction(Integer id, String dateString, String description)
67 throws ParseException {
68 this(id, Globals.parseLedgerDate(dateString), description);
70 public LedgerTransaction(Integer id, SimpleDate date, String description,
71 MobileLedgerProfile profile) {
72 this.profile = profile.getUuid();
75 this.description = description;
76 this.accounts = new ArrayList<>();
80 public LedgerTransaction(Integer id, SimpleDate date, String description) {
81 this(id, date, description, Data.profile.getValue());
83 public LedgerTransaction(SimpleDate date, String description) {
84 this(null, date, description);
86 public LedgerTransaction(int id) {
87 this(id, (SimpleDate) null, null);
89 public LedgerTransaction(int id, String profileUUID) {
90 this.profile = profileUUID;
93 this.description = null;
94 this.accounts = new ArrayList<>();
96 this.dataLoaded = false;
98 public ArrayList<LedgerTransactionAccount> getAccounts() {
101 public void addAccount(LedgerTransactionAccount item) {
106 public SimpleDate getDateIfAny() {
110 public SimpleDate getDate() {
111 loadData(App.getDatabase());
113 throw new IllegalStateException("Transaction has no date");
116 public void setDate(SimpleDate date) {
120 public String getDescription() {
123 public void setDescription(String description) {
124 this.description = description;
127 public String getComment() {
130 public void setComment(String comment) {
131 this.comment = comment;
136 protected void fillDataHash() {
137 if (dataHash != null)
140 Digest sha = new Digest(DIGEST_TYPE);
141 StringBuilder data = new StringBuilder();
143 data.append(profile);
144 data.append(getId());
146 data.append(getDescription());
148 data.append(getComment());
150 data.append(Globals.formatLedgerDate(getDate()));
152 for (LedgerTransactionAccount item : accounts) {
153 data.append(item.getAccountName());
155 data.append(item.getCurrency());
157 data.append(item.getAmount());
159 data.append(item.getComment());
161 sha.update(data.toString()
162 .getBytes(StandardCharsets.UTF_8));
163 dataHash = sha.digestToHexString();
165 catch (NoSuchAlgorithmException e) {
166 throw new RuntimeException(
167 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
170 public boolean existsInDb(SQLiteDatabase db) {
172 try (Cursor c = db.rawQuery("SELECT 1 from transactions where data_hash = ?",
173 new String[]{dataHash}))
175 boolean result = c.moveToFirst();
176 // debug("db", String.format("Transaction %d (%s) %s", id, dataHash,
177 // result ? "already present" : "not present"));
181 public void loadData(SQLiteDatabase db) {
185 try (Cursor cTr = db.rawQuery(
186 "SELECT year, month, day, description, comment from transactions WHERE profile=? " +
187 "AND id=?", new String[]{profile, String.valueOf(id)}))
189 if (cTr.moveToFirst()) {
190 date = new SimpleDate(cTr.getInt(0), cTr.getInt(1), cTr.getInt(2));
191 description = cTr.getString(3);
192 comment = cTr.getString(4);
196 try (Cursor cAcc = db.rawQuery(
197 "SELECT account_name, amount, currency, comment FROM " +
198 "transaction_accounts WHERE profile=? AND transaction_id = ?",
199 new String[]{profile, String.valueOf(id)}))
201 while (cAcc.moveToNext()) {
202 // debug("transactions",
203 // String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
204 // cAcc.getFloat(1), cAcc.getString(2)));
205 addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
206 cAcc.getString(2), cAcc.getString(3)));
215 public String getDataHash() {
219 public void finishLoading() {