2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger 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 * Mobile-Ledger 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 Mobile-Ledger. 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;
22 import android.util.Log;
24 import net.ktnx.mobileledger.utils.Digest;
25 import net.ktnx.mobileledger.utils.Globals;
27 import java.nio.charset.Charset;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.ArrayList;
30 import java.util.Comparator;
31 import java.util.Date;
33 public class LedgerTransaction {
34 private static final String DIGEST_TYPE = "SHA-256";
35 public final Comparator<LedgerTransactionAccount> comparator =
36 new Comparator<LedgerTransactionAccount>() {
38 public int compare(LedgerTransactionAccount o1, LedgerTransactionAccount o2) {
39 int res = o1.getAccountName().compareTo(o2.getAccountName());
40 if (res != 0) return res;
41 res = o1.getCurrency().compareTo(o2.getCurrency());
42 if (res != 0) return res;
43 return Float.compare(o1.getAmount(), o2.getAmount());
46 private String profile;
49 private String description;
50 private ArrayList<LedgerTransactionAccount> accounts;
51 private String dataHash;
52 private boolean dataLoaded;
53 public LedgerTransaction(Integer id, String dateString, String description) {
54 this(id, Globals.parseLedgerDate(dateString), description);
56 public LedgerTransaction(Integer id, Date date, String description) {
57 this.profile = Data.profile.get().getUuid();
60 this.description = description;
61 this.accounts = new ArrayList<>();
65 public LedgerTransaction(Date date, String description) {
66 this(null, date, description);
68 public LedgerTransaction(int id) {
69 this(id, (Date) null, null);
71 public ArrayList<LedgerTransactionAccount> getAccounts() {
74 public void addAccount(LedgerTransactionAccount item) {
78 public Date getDate() {
81 public void setDate(Date date) {
85 public String getDescription() {
88 public void setDescription(String description) {
89 this.description = description;
95 protected void fillDataHash() {
96 if (dataHash != null) return;
98 Digest sha = new Digest(DIGEST_TYPE);
99 StringBuilder data = new StringBuilder();
100 data.append(profile);
101 data.append(getId());
103 data.append(getDescription());
105 for (LedgerTransactionAccount item : accounts) {
106 data.append(item.getAccountName());
108 data.append(item.getCurrency());
110 data.append(item.getAmount());
112 sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
113 dataHash = sha.digestToHexString();
115 catch (NoSuchAlgorithmException e) {
116 throw new RuntimeException(
117 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
120 public boolean existsInDb(SQLiteDatabase db) {
123 .rawQuery("SELECT 1 from transactions where data_hash = ?", new String[]{dataHash}))
125 boolean result = c.moveToFirst();
126 Log.d("db", String.format("Transaction %d (%s) %s", id, dataHash,
127 result ? "already present" : "not present"));
131 public void loadData(SQLiteDatabase db) {
132 if (dataLoaded) return;
135 .rawQuery("SELECT date, description from transactions WHERE profile=? AND id=?",
136 new String[]{profile, String.valueOf(id)}))
138 if (cTr.moveToFirst()) {
139 String dateString = cTr.getString(0);
140 date = Globals.parseLedgerDate(dateString);
141 description = cTr.getString(1);
143 try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
144 "transaction_accounts WHERE " +
145 "profile=? AND transaction_id = ?",
146 new String[]{profile, String.valueOf(id)}))
148 while (cAcc.moveToNext()) {
149 // Log.d("transactions",
150 // String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
151 // cAcc.getFloat(1), cAcc.getString(2)));
152 addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
162 public String getDataHash() {
165 public void finishLoading() {