2 * Copyright © 2018 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;
23 import net.ktnx.mobileledger.utils.Digest;
25 import java.nio.charset.Charset;
26 import java.security.NoSuchAlgorithmException;
27 import java.util.ArrayList;
28 import java.util.Comparator;
29 import java.util.Iterator;
31 public class LedgerTransaction {
32 private static final String DIGEST_TYPE = "SHA-256";
33 public final Comparator<LedgerTransactionItem> comparator =
34 new Comparator<LedgerTransactionItem>() {
36 public int compare(LedgerTransactionItem o1, LedgerTransactionItem o2) {
37 int res = o1.getAccountName().compareTo(o2.getAccountName());
38 if (res != 0) return res;
39 res = o1.getCurrency().compareTo(o2.getCurrency());
40 if (res != 0) return res;
41 return Float.compare(o1.getAmount(), o2.getAmount());
46 private String description;
47 private ArrayList<LedgerTransactionItem> items;
48 private String dataHash;
49 private boolean dataLoaded;
50 public LedgerTransaction(Integer id, String date, String description) {
53 this.description = description;
54 this.items = new ArrayList<>();
58 public LedgerTransaction(String date, String description) {
59 this(null, date, description);
61 public LedgerTransaction(int id) {
64 public void add_item(LedgerTransactionItem item) {
68 public String getDate() {
71 public void setDate(String date) {
75 public String getDescription() {
78 public void setDescription(String description) {
79 this.description = description;
82 public Iterator<LedgerTransactionItem> getItemsIterator() {
83 return new Iterator<LedgerTransactionItem>() {
84 private int pointer = 0;
86 public boolean hasNext() {
87 return pointer < items.size();
91 public LedgerTransactionItem next() {
92 return hasNext() ? items.get(pointer++) : null;
99 public void insertInto(SQLiteDatabase db) {
101 db.execSQL("INSERT INTO transactions(id, date, description, data_hash) values(?,?,?,?)",
102 new Object[]{id, date, description, dataHash});
104 for (LedgerTransactionItem item : items) {
105 db.execSQL("INSERT INTO transaction_accounts(transaction_id, account_name, amount, " +
106 "currency) values(?, ?, ?, ?)",
107 new Object[]{id, item.getAccountName(), item.getAmount(), item.getCurrency()});
110 private void fillDataHash() {
111 if (dataHash != null) return;
113 Digest sha = new Digest(DIGEST_TYPE);
114 StringBuilder data = new StringBuilder();
115 data.append(getId());
117 data.append(getDescription());
119 for (LedgerTransactionItem item : items) {
120 data.append(item.getAccountName());
122 data.append(item.getCurrency());
124 data.append(item.getAmount());
126 sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
127 dataHash = sha.digestToHexString();
129 catch (NoSuchAlgorithmException e) {
130 throw new RuntimeException(
131 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
134 public void loadData(SQLiteDatabase db) {
135 if (dataLoaded) return;
137 try (Cursor cTr = db.rawQuery("SELECT date, description from transactions WHERE id=?",
138 new String[]{String.valueOf(id)}))
140 if (cTr.moveToFirst()) {
141 date = cTr.getString(0);
142 description = cTr.getString(1);
144 try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
145 "transaction_accounts WHERE transaction_id = ?",
146 new String[]{String.valueOf(id)}))
148 while (cAcc.moveToNext()) {
149 add_item(new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1),