]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java
include comments when comparing transaction accounts
[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 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 net.ktnx.mobileledger.utils.Digest;
24 import net.ktnx.mobileledger.utils.Globals;
25
26 import java.nio.charset.StandardCharsets;
27 import java.security.NoSuchAlgorithmException;
28 import java.text.ParseException;
29 import java.util.ArrayList;
30 import java.util.Comparator;
31 import java.util.Date;
32
33 public class LedgerTransaction {
34     private static final String DIGEST_TYPE = "SHA-256";
35     public final Comparator<LedgerTransactionAccount> comparator =
36             new Comparator<LedgerTransactionAccount>() {
37                 @Override
38                 public int compare(LedgerTransactionAccount o1, LedgerTransactionAccount o2) {
39                     int res = o1.getAccountName()
40                                 .compareTo(o2.getAccountName());
41                     if (res != 0)
42                         return res;
43                     res = o1.getCurrency()
44                             .compareTo(o2.getCurrency());
45                     if (res != 0)
46                         return res;
47                     res = o1.getComment()
48                             .compareTo(o2.getComment());
49                     if (res != 0)
50                         return res;
51                     return Float.compare(o1.getAmount(), o2.getAmount());
52                 }
53             };
54     private String profile;
55     private Integer id;
56     private Date 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, Date 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, Date date, String description) {
77         this(id, date, description, Data.profile.getValue());
78     }
79     public LedgerTransaction(Date date, String description) {
80         this(null, date, description);
81     }
82     public LedgerTransaction(int id) {
83         this(id, (Date) 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     public Date getDate() {
102         return date;
103     }
104     public void setDate(Date date) {
105         this.date = date;
106         dataHash = null;
107     }
108     public String getDescription() {
109         return description;
110     }
111     public void setDescription(String description) {
112         this.description = description;
113         dataHash = null;
114     }
115     public String getComment() {
116         return comment;
117     }
118     public void setComment(String comment) {
119         this.comment = comment;
120     }
121     public int getId() {
122         return id;
123     }
124     protected void fillDataHash() {
125         if (dataHash != null)
126             return;
127         try {
128             Digest sha = new Digest(DIGEST_TYPE);
129             StringBuilder data = new StringBuilder();
130             data.append(profile);
131             data.append(getId());
132             data.append('\0');
133             data.append(getDescription());
134             data.append('\0');
135             data.append(Globals.formatLedgerDate(getDate()));
136             data.append('\0');
137             for (LedgerTransactionAccount item : accounts) {
138                 data.append(item.getAccountName());
139                 data.append('\0');
140                 data.append(item.getCurrency());
141                 data.append('\0');
142                 data.append(item.getAmount());
143                 data.append('\0');
144                 data.append(item.getComment());
145             }
146             sha.update(data.toString()
147                            .getBytes(StandardCharsets.UTF_8));
148             dataHash = sha.digestToHexString();
149         }
150         catch (NoSuchAlgorithmException e) {
151             throw new RuntimeException(
152                     String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
153         }
154     }
155     public boolean existsInDb(SQLiteDatabase db) {
156         fillDataHash();
157         try (Cursor c = db.rawQuery("SELECT 1 from transactions where data_hash = ?",
158                 new String[]{dataHash}))
159         {
160             boolean result = c.moveToFirst();
161 //            debug("db", String.format("Transaction %d (%s) %s", id, dataHash,
162 //                    result ? "already present" : "not present"));
163             return result;
164         }
165     }
166     public void loadData(SQLiteDatabase db) {
167         if (dataLoaded)
168             return;
169
170         try (Cursor cTr = db.rawQuery(
171                 "SELECT date, description from transactions WHERE profile=? AND id=?",
172                 new String[]{profile, String.valueOf(id)}))
173         {
174             if (cTr.moveToFirst()) {
175                 String dateString = cTr.getString(0);
176                 try {
177                     date = Globals.parseLedgerDate(dateString);
178                 }
179                 catch (ParseException e) {
180                     e.printStackTrace();
181                     throw new RuntimeException(
182                             String.format("Error parsing date '%s' from " + "transaction %d",
183                                     dateString, id));
184                 }
185                 description = cTr.getString(1);
186
187                 try (Cursor cAcc = db.rawQuery(
188                         "SELECT account_name, amount, currency, comment FROM " +
189                         "transaction_accounts WHERE profile=? AND transaction_id = ?",
190                         new String[]{profile, String.valueOf(id)}))
191                 {
192                     while (cAcc.moveToNext()) {
193 //                        debug("transactions",
194 //                                String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
195 //                                        cAcc.getFloat(1), cAcc.getString(2)));
196                         addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
197                                 cAcc.getString(2), cAcc.getString(3)));
198                     }
199
200                     finishLoading();
201                 }
202             }
203         }
204
205     }
206     public String getDataHash() {
207         return dataHash;
208     }
209     public void finishLoading() {
210         dataLoaded = true;
211     }
212 }