]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
33b378450c874652bb5dd7443cb4c8ffbf317779
[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 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.async;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.util.Log;
24
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.utils.MLDB;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public class UpdateTransactionsTask extends AsyncTask<String, Void, List<LedgerTransaction>> {
33     protected List<LedgerTransaction> doInBackground(String[] filterAccName) {
34         Data.backgroundTaskCount.incrementAndGet();
35         String profile_uuid = Data.profile.get().getUuid();
36         try {
37             ArrayList<LedgerTransaction> newList = new ArrayList<>();
38
39             boolean hasFilter = (filterAccName != null) && (filterAccName.length > 0) &&
40                                 (filterAccName[0] != null) && !filterAccName[0].isEmpty();
41
42             String sql;
43             String[] params;
44
45             sql = "SELECT id FROM transactions WHERE profile=? ORDER BY date desc, id desc";
46             params = new String[]{profile_uuid};
47
48             if (hasFilter) {
49                 sql = "SELECT distinct tr.id from transactions tr JOIN transaction_accounts ta " +
50                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
51                       "and ta" + ".account_name LIKE ?||'%' AND ta" +
52                       ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
53                 params = filterAccName;
54             }
55
56             Log.d("UTT", sql);
57             SQLiteDatabase db = MLDB.getReadableDatabase();
58             try (Cursor cursor = db.rawQuery(sql, params)) {
59                 while (cursor.moveToNext()) {
60                     if (isCancelled()) return null;
61
62                     int transaction_id = cursor.getInt(0);
63                     newList.add(new LedgerTransaction(transaction_id));
64 //                    Log.d("UTT", String.format("got transaction %d", transaction_id));
65                 }
66                 Data.transactions.set(newList);
67                 Log.d("UTT", "transaction list value updated");
68             }
69
70             return newList;
71         }
72         finally {
73             Data.backgroundTaskCount.decrementAndGet();
74         }
75     }
76 }