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.sqlite.SQLiteDatabase;
22 import net.ktnx.mobileledger.utils.Digest;
24 import java.nio.charset.Charset;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.ArrayList;
27 import java.util.Comparator;
28 import java.util.Iterator;
30 public class LedgerTransaction {
31 private static final String DIGEST_TYPE = "SHA-256";
32 public final Comparator<LedgerTransactionItem> comparator =
33 new Comparator<LedgerTransactionItem>() {
35 public int compare(LedgerTransactionItem o1, LedgerTransactionItem o2) {
36 int res = o1.getAccountName().compareTo(o2.getAccountName());
37 if (res != 0) return res;
38 res = o1.getCurrency().compareTo(o2.getCurrency());
39 if (res != 0) return res;
40 return Float.compare(o1.getAmount(), o2.getAmount());
45 private String description;
46 private ArrayList<LedgerTransactionItem> items;
47 private String dataHash;
48 public LedgerTransaction(String id, String date, String description) {
51 this.description = description;
52 this.items = new ArrayList<>();
55 public LedgerTransaction(int id, String date, String description) {
56 this(String.valueOf(id), date, description);
58 public LedgerTransaction(String date, String description) {
59 this(null, date, description);
61 public void add_item(LedgerTransactionItem item) {
65 public String getDate() {
68 public void setDate(String date) {
72 public String getDescription() {
75 public void setDescription(String description) {
76 this.description = description;
79 public Iterator<LedgerTransactionItem> getItemsIterator() {
80 return new Iterator<LedgerTransactionItem>() {
81 private int pointer = 0;
83 public boolean hasNext() {
84 return pointer < items.size();
88 public LedgerTransactionItem next() {
89 return hasNext() ? items.get(pointer++) : null;
93 public String getId() {
96 public void insertInto(SQLiteDatabase db) {
98 db.execSQL("INSERT INTO transactions(id, date, description, data_hash) values(?,?,?,?)",
99 new String[]{id, date, description});
101 for (LedgerTransactionItem item : items) {
102 db.execSQL("INSERT INTO transaction_accounts(transaction_id, account_name, amount, " +
103 "currency) values(?, ?, ?, ?)",
104 new Object[]{id, item.getAccountName(), item.getAmount(), item.getCurrency()});
107 private void fillDataHash() {
108 if (dataHash != null) return;
110 Digest sha = new Digest(DIGEST_TYPE);
111 StringBuilder data = new StringBuilder();
112 data.append(getId());
114 data.append(getDescription());
116 for (LedgerTransactionItem item : items) {
117 data.append(item.getAccountName());
119 data.append(item.getCurrency());
121 data.append(item.getAmount());
123 sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
124 dataHash = sha.digestToHexString();
126 catch (NoSuchAlgorithmException e) {
127 throw new RuntimeException(
128 String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);