2 * Copyright © 2019 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;
22 import android.util.Log;
24 import net.ktnx.mobileledger.json.ParsedLedgerTransaction;
25 import net.ktnx.mobileledger.json.ParsedPosting;
26 import net.ktnx.mobileledger.utils.Digest;
27 import net.ktnx.mobileledger.utils.Globals;
29 import java.nio.charset.Charset;
30 import java.security.NoSuchAlgorithmException;
31 import java.text.ParseException;
32 import java.util.ArrayList;
33 import java.util.Comparator;
34 import java.util.Date;
36 public class LedgerTransaction {
37 private static final String DIGEST_TYPE = "SHA-256";
38 public final Comparator<LedgerTransactionAccount> comparator =
39 new Comparator<LedgerTransactionAccount>() {
41 public int compare(LedgerTransactionAccount o1, LedgerTransactionAccount o2) {
42 int res = o1.getAccountName().compareTo(o2.getAccountName());
43 if (res != 0) return res;
44 res = o1.getCurrency().compareTo(o2.getCurrency());
45 if (res != 0) return res;
46 return Float.compare(o1.getAmount(), o2.getAmount());
49 private String profile;
52 private String description;
53 private ArrayList<LedgerTransactionAccount> accounts;
54 private String dataHash;
55 private boolean dataLoaded;
56 public LedgerTransaction(Integer id, String dateString, String description)
57 throws ParseException {
58 this(id, Globals.parseLedgerDate(dateString), description);
60 public LedgerTransaction(Integer id, Date date, String description, MobileLedgerProfile profile) {
61 this.profile = profile.getUuid();
64 this.description = description;
65 this.accounts = new ArrayList<>();
69 public LedgerTransaction(Integer id, Date date, String description) {
70 this(id, date, description, Data.profile.get());
72 public LedgerTransaction(Date date, String description) {
73 this(null, date, description);
75 public LedgerTransaction(int id) {
76 this(id, (Date) null, null);
78 public LedgerTransaction(int id, String profileUUID) {
79 this.profile = profileUUID;
82 this.description = null;
83 this.accounts = new ArrayList<>();
85 this.dataLoaded = false;
87 public ArrayList<LedgerTransactionAccount> getAccounts() {
90 public void addAccount(LedgerTransactionAccount item) {
94 public Date getDate() {
97 public void setDate(Date date) {
101 public String getDescription() {
104 public void setDescription(String description) {
105 this.description = description;
111 protected void fillDataHash() {
112 if (dataHash != null) return;
114 Digest sha = new Digest(DIGEST_TYPE);
115 StringBuilder data = new StringBuilder();
116 data.append(profile);
117 data.append(getId());
119 data.append(getDescription());
121 data.append(Globals.formatLedgerDate(getDate()));
123 for (LedgerTransactionAccount item : accounts) {
124 data.append(item.getAccountName());
126 data.append(item.getCurrency());
128 data.append(item.getAmount());
130 sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
131 dataHash = sha.digestToHexString();
133 catch (NoSuchAlgorithmException e) {
134 throw new RuntimeException(
135 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
138 public boolean existsInDb(SQLiteDatabase db) {
141 .rawQuery("SELECT 1 from transactions where data_hash = ?", new String[]{dataHash}))
143 boolean result = c.moveToFirst();
144 Log.d("db", String.format("Transaction %d (%s) %s", id, dataHash,
145 result ? "already present" : "not present"));
149 public void loadData(SQLiteDatabase db) {
150 if (dataLoaded) return;
153 .rawQuery("SELECT date, description from transactions WHERE profile=? AND id=?",
154 new String[]{profile, String.valueOf(id)}))
156 if (cTr.moveToFirst()) {
157 String dateString = cTr.getString(0);
159 date = Globals.parseLedgerDate(dateString);
161 catch (ParseException e) {
163 throw new RuntimeException(
164 String.format("Error parsing date '%s' from " + "transacion %d",
167 description = cTr.getString(1);
169 try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
170 "transaction_accounts WHERE " +
171 "profile=? AND transaction_id = ?",
172 new String[]{profile, String.valueOf(id)}))
174 while (cAcc.moveToNext()) {
175 // Log.d("transactions",
176 // String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
177 // cAcc.getFloat(1), cAcc.getString(2)));
178 addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
188 public String getDataHash() {
191 public void finishLoading() {
194 public ParsedLedgerTransaction toParsedLedgerTransaction() {
195 ParsedLedgerTransaction result = new ParsedLedgerTransaction();
196 result.setTcomment("");
197 result.setTprecedingcomment("");
199 ArrayList<ParsedPosting> postings = new ArrayList<>();
200 for (LedgerTransactionAccount acc : accounts) {
201 if (!acc.getAccountName().isEmpty()) postings.add(acc.asParsedPosting());
204 result.setTpostings(postings);
205 result.setTdate(Globals.formatIsoDate(date));
206 result.setTdate2(null);
208 result.setTdescription(description);