]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
dc6536a167c58c74b5792dd0a4aee5d8b760f88e
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / UpdateTransactionsTask.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.async;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23
24 import net.ktnx.mobileledger.model.Data;
25 import net.ktnx.mobileledger.model.LedgerTransaction;
26 import net.ktnx.mobileledger.model.MobileLedgerProfile;
27 import net.ktnx.mobileledger.model.TransactionListItem;
28 import net.ktnx.mobileledger.utils.Globals;
29 import net.ktnx.mobileledger.utils.MLDB;
30
31 import java.text.ParseException;
32 import java.util.ArrayList;
33 import java.util.Date;
34
35 import static net.ktnx.mobileledger.utils.Logger.debug;
36
37 public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
38     protected String doInBackground(String[] filterAccName) {
39         final MobileLedgerProfile profile = Data.profile.get();
40         if (profile == null) return "Profile not configured";
41
42         String profile_uuid = profile.getUuid();
43         Data.backgroundTaskStarted();
44         try {
45             ArrayList<TransactionListItem> newList = new ArrayList<>();
46
47             String sql;
48             String[] params;
49
50             if (filterAccName[0] == null) {
51                 sql = "SELECT id, date FROM transactions WHERE profile=? ORDER BY date desc, id " +
52                       "desc";
53                 params = new String[]{profile_uuid};
54
55             }
56             else {
57                 sql = "SELECT distinct tr.id, tr.date from transactions tr JOIN " +
58                       "transaction_accounts ta " +
59                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
60                       "and ta.account_name LIKE ?||'%' AND ta" +
61                       ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
62                 params = new String[]{profile_uuid, filterAccName[0]};
63             }
64
65             debug("UTT", sql);
66             SQLiteDatabase db = MLDB.getDatabase();
67             String lastDateString = Globals.formatLedgerDate(new Date());
68             Date lastDate = Globals.parseLedgerDate(lastDateString);
69             boolean odd = true;
70             try (Cursor cursor = db.rawQuery(sql, params)) {
71                 while (cursor.moveToNext()) {
72                     if (isCancelled()) return null;
73
74                     int transaction_id = cursor.getInt(0);
75                     String dateString = cursor.getString(1);
76                     Date date = Globals.parseLedgerDate(dateString);
77
78                     if (!lastDateString.equals(dateString)) {
79                         boolean showMonth = (date.getMonth() != lastDate.getMonth() ||
80                                              date.getYear() != lastDate.getYear());
81                         newList.add(new TransactionListItem(date, showMonth));
82                     }
83                     newList.add(
84                             new TransactionListItem(new LedgerTransaction(transaction_id), odd));
85 //                    debug("UTT", String.format("got transaction %d", transaction_id));
86
87                     lastDate = date;
88                     lastDateString = dateString;
89                     odd = !odd;
90                 }
91                 Data.transactions.setList(newList);
92                 debug("UTT", "transaction list value updated");
93             }
94
95             return null;
96         }
97         catch (ParseException e) {
98             return String.format("Error parsing stored date '%s'", e.getMessage());
99         }
100         finally {
101             Data.backgroundTaskFinished();
102         }
103     }
104 }