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;
23 import net.ktnx.mobileledger.json.ParsedLedgerTransaction;
24 import net.ktnx.mobileledger.json.ParsedPosting;
25 import net.ktnx.mobileledger.utils.Digest;
26 import net.ktnx.mobileledger.utils.Globals;
28 import java.nio.charset.Charset;
29 import java.security.NoSuchAlgorithmException;
30 import java.text.ParseException;
31 import java.util.ArrayList;
32 import java.util.Comparator;
33 import java.util.Date;
35 import static net.ktnx.mobileledger.utils.Logger.debug;
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().compareTo(o2.getAccountName());
44 if (res != 0) return res;
45 res = o1.getCurrency().compareTo(o2.getCurrency());
46 if (res != 0) return res;
47 return Float.compare(o1.getAmount(), o2.getAmount());
50 private String profile;
53 private String description;
54 private ArrayList<LedgerTransactionAccount> accounts;
55 private String dataHash;
56 private boolean dataLoaded;
57 public LedgerTransaction(Integer id, String dateString, String description)
58 throws ParseException {
59 this(id, Globals.parseLedgerDate(dateString), description);
61 public LedgerTransaction(Integer id, Date date, String description, MobileLedgerProfile profile) {
62 this.profile = profile.getUuid();
65 this.description = description;
66 this.accounts = new ArrayList<>();
70 public LedgerTransaction(Integer id, Date date, String description) {
71 this(id, date, description, Data.profile.get());
73 public LedgerTransaction(Date date, String description) {
74 this(null, date, description);
76 public LedgerTransaction(int id) {
77 this(id, (Date) null, null);
79 public LedgerTransaction(int id, String profileUUID) {
80 this.profile = profileUUID;
83 this.description = null;
84 this.accounts = new ArrayList<>();
86 this.dataLoaded = false;
88 public ArrayList<LedgerTransactionAccount> getAccounts() {
91 public void addAccount(LedgerTransactionAccount item) {
95 public Date getDate() {
98 public void setDate(Date date) {
102 public String getDescription() {
105 public void setDescription(String description) {
106 this.description = description;
112 protected void fillDataHash() {
113 if (dataHash != null) return;
115 Digest sha = new Digest(DIGEST_TYPE);
116 StringBuilder data = new StringBuilder();
117 data.append(profile);
118 data.append(getId());
120 data.append(getDescription());
122 data.append(Globals.formatLedgerDate(getDate()));
124 for (LedgerTransactionAccount item : accounts) {
125 data.append(item.getAccountName());
127 data.append(item.getCurrency());
129 data.append(item.getAmount());
131 sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
132 dataHash = sha.digestToHexString();
134 catch (NoSuchAlgorithmException e) {
135 throw new RuntimeException(
136 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
139 public boolean existsInDb(SQLiteDatabase db) {
142 .rawQuery("SELECT 1 from transactions where data_hash = ?", new String[]{dataHash}))
144 boolean result = c.moveToFirst();
145 debug("db", String.format("Transaction %d (%s) %s", id, dataHash,
146 result ? "already present" : "not present"));
150 public void loadData(SQLiteDatabase db) {
151 if (dataLoaded) return;
154 .rawQuery("SELECT date, description from transactions WHERE profile=? AND id=?",
155 new String[]{profile, String.valueOf(id)}))
157 if (cTr.moveToFirst()) {
158 String dateString = cTr.getString(0);
160 date = Globals.parseLedgerDate(dateString);
162 catch (ParseException e) {
164 throw new RuntimeException(
165 String.format("Error parsing date '%s' from " + "transacion %d",
168 description = cTr.getString(1);
170 try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
171 "transaction_accounts WHERE " +
172 "profile=? AND transaction_id = ?",
173 new String[]{profile, String.valueOf(id)}))
175 while (cAcc.moveToNext()) {
176 // debug("transactions",
177 // String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
178 // cAcc.getFloat(1), cAcc.getString(2)));
179 addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
189 public String getDataHash() {
192 public void finishLoading() {
195 public ParsedLedgerTransaction toParsedLedgerTransaction() {
196 ParsedLedgerTransaction result = new ParsedLedgerTransaction();
197 result.setTcomment("");
198 result.setTprecedingcomment("");
200 ArrayList<ParsedPosting> postings = new ArrayList<>();
201 for (LedgerTransactionAccount acc : accounts) {
202 if (!acc.getAccountName().isEmpty()) postings.add(acc.asParsedPosting());
205 result.setTpostings(postings);
206 result.setTdate(Globals.formatIsoDate(date));
207 result.setTdate2(null);
209 result.setTdescription(description);