]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java
working transaction list retrieval
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransaction.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.database.sqlite.SQLiteDatabase;
21
22 import net.ktnx.mobileledger.utils.Digest;
23
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;
29
30 public class LedgerTransaction {
31     private static final String DIGEST_TYPE = "SHA-256";
32     public final Comparator<LedgerTransactionItem> comparator =
33             new Comparator<LedgerTransactionItem>() {
34                 @Override
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());
41                 }
42             };
43     private String id;
44     private String date;
45     private String description;
46     private ArrayList<LedgerTransactionItem> items;
47     private String dataHash;
48     public LedgerTransaction(String id, String date, String description) {
49         this.id = id;
50         this.date = date;
51         this.description = description;
52         this.items = new ArrayList<>();
53         this.dataHash = null;
54     }
55     public LedgerTransaction(int id, String date, String description) {
56         this(String.valueOf(id), date, description);
57     }
58     public LedgerTransaction(String date, String description) {
59         this(null, date, description);
60     }
61     public void add_item(LedgerTransactionItem item) {
62         items.add(item);
63         dataHash = null;
64     }
65     public String getDate() {
66         return date;
67     }
68     public void setDate(String date) {
69         this.date = date;
70         dataHash = null;
71     }
72     public String getDescription() {
73         return description;
74     }
75     public void setDescription(String description) {
76         this.description = description;
77         dataHash = null;
78     }
79     public Iterator<LedgerTransactionItem> getItemsIterator() {
80         return new Iterator<LedgerTransactionItem>() {
81             private int pointer = 0;
82             @Override
83             public boolean hasNext() {
84                 return pointer < items.size();
85             }
86
87             @Override
88             public LedgerTransactionItem next() {
89                 return hasNext() ? items.get(pointer++) : null;
90             }
91         };
92     }
93     public String getId() {
94         return id;
95     }
96     public void insertInto(SQLiteDatabase db) {
97         fillDataHash();
98         db.execSQL("INSERT INTO transactions(id, date, description, data_hash) values(?,?,?,?)",
99                 new String[]{id, date, description});
100
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()});
105         }
106     }
107     private void fillDataHash() {
108         if (dataHash != null) return;
109         try {
110             Digest sha = new Digest(DIGEST_TYPE);
111             StringBuilder data = new StringBuilder();
112             data.append(getId());
113             data.append('\0');
114             data.append(getDescription());
115             data.append('\0');
116             for (LedgerTransactionItem item : items) {
117                 data.append(item.getAccountName());
118                 data.append('\0');
119                 data.append(item.getCurrency());
120                 data.append('\0');
121                 data.append(item.getAmount());
122             }
123             sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
124             dataHash = sha.digestToHexString();
125         }
126         catch (NoSuchAlgorithmException e) {
127             throw new RuntimeException(
128                     String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
129         }
130     }
131 }