]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java
6a8234f28dc8d7145f8cd578db2353f8f5f41aad
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransaction.java
1 /*
2  * Copyright © 2020 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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25
26 import net.ktnx.mobileledger.App;
27 import net.ktnx.mobileledger.utils.Digest;
28 import net.ktnx.mobileledger.utils.Globals;
29 import net.ktnx.mobileledger.utils.SimpleDate;
30
31 import java.nio.charset.StandardCharsets;
32 import java.security.NoSuchAlgorithmException;
33 import java.text.ParseException;
34 import java.util.ArrayList;
35 import java.util.Comparator;
36
37 public class LedgerTransaction {
38     private static final String DIGEST_TYPE = "SHA-256";
39     public final Comparator<LedgerTransactionAccount> comparator = (o1, o2) -> {
40         int res = o1.getAccountName()
41                     .compareTo(o2.getAccountName());
42         if (res != 0)
43             return res;
44         res = o1.getCurrency()
45                 .compareTo(o2.getCurrency());
46         if (res != 0)
47             return res;
48         res = o1.getComment()
49                 .compareTo(o2.getComment());
50         if (res != 0)
51             return res;
52         return Float.compare(o1.getAmount(), o2.getAmount());
53     };
54     private String profile;
55     private Integer id;
56     private SimpleDate date;
57     private String description;
58     private String comment;
59     private ArrayList<LedgerTransactionAccount> accounts;
60     private String dataHash;
61     private boolean dataLoaded;
62     public LedgerTransaction(Integer id, String dateString, String description)
63             throws ParseException {
64         this(id, Globals.parseLedgerDate(dateString), description);
65     }
66     public LedgerTransaction(Integer id, SimpleDate date, String description,
67                              MobileLedgerProfile profile) {
68         this.profile = profile.getUuid();
69         this.id = id;
70         this.date = date;
71         this.description = description;
72         this.accounts = new ArrayList<>();
73         this.dataHash = null;
74         dataLoaded = false;
75     }
76     public LedgerTransaction(Integer id, SimpleDate date, String description) {
77         this(id, date, description, Data.getProfile());
78     }
79     public LedgerTransaction(SimpleDate date, String description) {
80         this(null, date, description);
81     }
82     public LedgerTransaction(int id) {
83         this(id, (SimpleDate) null, null);
84     }
85     public LedgerTransaction(int id, String profileUUID) {
86         this.profile = profileUUID;
87         this.id = id;
88         this.date = null;
89         this.description = null;
90         this.accounts = new ArrayList<>();
91         this.dataHash = null;
92         this.dataLoaded = false;
93     }
94     public ArrayList<LedgerTransactionAccount> getAccounts() {
95         return accounts;
96     }
97     public void addAccount(LedgerTransactionAccount item) {
98         accounts.add(item);
99         dataHash = null;
100     }
101     @Nullable
102     public SimpleDate getDateIfAny() {
103         return date;
104     }
105     @NonNull
106     public SimpleDate getDate() {
107         loadData(App.getDatabase());
108         if (date == null)
109             throw new IllegalStateException("Transaction has no date");
110         return date;
111     }
112     public void setDate(SimpleDate date) {
113         this.date = date;
114         dataHash = null;
115     }
116     public String getDescription() {
117         return description;
118     }
119     public void setDescription(String description) {
120         this.description = description;
121         dataHash = null;
122     }
123     public String getComment() {
124         return comment;
125     }
126     public void setComment(String comment) {
127         this.comment = comment;
128     }
129     public int getId() {
130         return id;
131     }
132     protected void fillDataHash() {
133         if (dataHash != null)
134             return;
135         try {
136             Digest sha = new Digest(DIGEST_TYPE);
137             StringBuilder data = new StringBuilder();
138             data.append("ver1");
139             data.append(profile);
140             data.append(getId());
141             data.append('\0');
142             data.append(getDescription());
143             data.append('\0');
144             data.append(getComment());
145             data.append('\0');
146             data.append(Globals.formatLedgerDate(getDate()));
147             data.append('\0');
148             for (LedgerTransactionAccount item : accounts) {
149                 data.append(item.getAccountName());
150                 data.append('\0');
151                 data.append(item.getCurrency());
152                 data.append('\0');
153                 data.append(item.getAmount());
154                 data.append('\0');
155                 data.append(item.getComment());
156             }
157             sha.update(data.toString()
158                            .getBytes(StandardCharsets.UTF_8));
159             dataHash = sha.digestToHexString();
160         }
161         catch (NoSuchAlgorithmException e) {
162             throw new RuntimeException(
163                     String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
164         }
165     }
166     public boolean existsInDb(SQLiteDatabase db) {
167         fillDataHash();
168         try (Cursor c = db.rawQuery("SELECT 1 from transactions where data_hash = ?",
169                 new String[]{dataHash}))
170         {
171             boolean result = c.moveToFirst();
172 //            debug("db", String.format("Transaction %d (%s) %s", id, dataHash,
173 //                    result ? "already present" : "not present"));
174             return result;
175         }
176     }
177     public void loadData(SQLiteDatabase db) {
178         if (dataLoaded)
179             return;
180
181         try (Cursor cTr = db.rawQuery(
182                 "SELECT year, month, day, description, comment from transactions WHERE profile=? " +
183                 "AND id=?", new String[]{profile, String.valueOf(id)}))
184         {
185             if (cTr.moveToFirst()) {
186                 date = new SimpleDate(cTr.getInt(0), cTr.getInt(1), cTr.getInt(2));
187                 description = cTr.getString(3);
188                 comment = cTr.getString(4);
189
190                 accounts.clear();
191
192                 try (Cursor cAcc = db.rawQuery(
193                         "SELECT account_name, amount, currency, comment FROM " +
194                         "transaction_accounts WHERE profile=? AND transaction_id = ?",
195                         new String[]{profile, String.valueOf(id)}))
196                 {
197                     while (cAcc.moveToNext()) {
198 //                        debug("transactions",
199 //                                String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
200 //                                        cAcc.getFloat(1), cAcc.getString(2)));
201                         addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
202                                 cAcc.getString(2), cAcc.getString(3)));
203                     }
204
205                     finishLoading();
206                 }
207             }
208         }
209
210     }
211     public String getDataHash() {
212         fillDataHash();
213         return dataHash;
214     }
215     public void finishLoading() {
216         dataLoaded = true;
217     }
218     @Override
219     public boolean equals(@Nullable Object obj) {
220         if (obj == null)
221             return false;
222         if (!obj.getClass()
223                 .equals(this.getClass()))
224             return false;
225
226         return ((LedgerTransaction) obj).getDataHash()
227                                         .equals(getDataHash());
228     }
229
230     public boolean hasAccountNamedLike(String name) {
231         name = name.toUpperCase();
232         for (LedgerTransactionAccount acc : accounts) {
233             if (acc.getAccountName()
234                    .toUpperCase()
235                    .contains(name))
236                 return true;
237         }
238
239         return false;
240     }
241 }