]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java
3e5a45fd7ff6dd939b8eade386ddc7353549be5e
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransaction.java
1 /*
2  * Copyright © 2019 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.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.util.Log;
23
24 import net.ktnx.mobileledger.utils.Digest;
25
26 import java.nio.charset.Charset;
27 import java.security.NoSuchAlgorithmException;
28 import java.util.ArrayList;
29 import java.util.Comparator;
30
31 public class LedgerTransaction {
32     private static final String DIGEST_TYPE = "SHA-256";
33     public final Comparator<LedgerTransactionAccount> comparator =
34             new Comparator<LedgerTransactionAccount>() {
35                 @Override
36                 public int compare(LedgerTransactionAccount o1, LedgerTransactionAccount 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());
42                 }
43             };
44     private String profile;
45     private Integer id;
46     private String date;
47     private String description;
48     private ArrayList<LedgerTransactionAccount> accounts;
49     private String dataHash;
50     private boolean dataLoaded;
51     public LedgerTransaction(Integer id, String date, String description) {
52         this.profile = Data.profile.get().getUuid();
53         this.id = id;
54         this.date = date;
55         this.description = description;
56         this.accounts = new ArrayList<>();
57         this.dataHash = null;
58         dataLoaded = false;
59     }
60     public LedgerTransaction(String date, String description) {
61         this(null, date, description);
62     }
63     public LedgerTransaction(int id) {
64         this(id, null, null);
65     }
66     public ArrayList<LedgerTransactionAccount> getAccounts() {
67         return accounts;
68     }
69     public void addAccount(LedgerTransactionAccount item) {
70         accounts.add(item);
71         dataHash = null;
72     }
73     public String getDate() {
74         return date;
75     }
76     public void setDate(String date) {
77         this.date = date;
78         dataHash = null;
79     }
80     public String getDescription() {
81         return description;
82     }
83     public void setDescription(String description) {
84         this.description = description;
85         dataHash = null;
86     }
87     public int getId() {
88         return id;
89     }
90     protected void fillDataHash() {
91         if (dataHash != null) return;
92         try {
93             Digest sha = new Digest(DIGEST_TYPE);
94             StringBuilder data = new StringBuilder();
95             data.append(profile);
96             data.append(getId());
97             data.append('\0');
98             data.append(getDescription());
99             data.append('\0');
100             for (LedgerTransactionAccount item : accounts) {
101                 data.append(item.getAccountName());
102                 data.append('\0');
103                 data.append(item.getCurrency());
104                 data.append('\0');
105                 data.append(item.getAmount());
106             }
107             sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
108             dataHash = sha.digestToHexString();
109         }
110         catch (NoSuchAlgorithmException e) {
111             throw new RuntimeException(
112                     String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
113         }
114     }
115     public boolean existsInDb(SQLiteDatabase db) {
116         fillDataHash();
117         try (Cursor c = db
118                 .rawQuery("SELECT 1 from transactions where data_hash = ?", new String[]{dataHash}))
119         {
120             boolean result = c.moveToFirst();
121             Log.d("db", String.format("Transaction %d (%s) %s", id, dataHash,
122                     result ? "already present" : "not present"));
123             return result;
124         }
125     }
126     public void loadData(SQLiteDatabase db) {
127         if (dataLoaded) return;
128
129         try (Cursor cTr = db
130                 .rawQuery("SELECT date, description from transactions WHERE profile=? AND id=?",
131                         new String[]{profile, String.valueOf(id)}))
132         {
133             if (cTr.moveToFirst()) {
134                 date = cTr.getString(0);
135                 description = cTr.getString(1);
136
137                 try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
138                                                "transaction_accounts WHERE " +
139                                                "profile=? AND transaction_id = ?",
140                         new String[]{profile, String.valueOf(id)}))
141                 {
142                     while (cAcc.moveToNext()) {
143 //                        Log.d("transactions",
144 //                                String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
145 //                                        cAcc.getFloat(1), cAcc.getString(2)));
146                         addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
147                                 cAcc.getString(2)));
148                     }
149
150                     finishLoading();
151                 }
152             }
153         }
154
155     }
156     public String getDataHash() {
157         return dataHash;
158     }
159     public void finishLoading() {
160         dataLoaded = true;
161     }
162 }