]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
0509f3531cd887236c7c61778d692635ed4709f5
[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         try {
36             ArrayList<LedgerTransaction> newList = new ArrayList<>();
37
38             boolean hasFilter = (filterAccName != null) && (filterAccName.length > 0) &&
39                                 (filterAccName[0] != null) && !filterAccName[0].isEmpty();
40
41             String sql;
42             String[] params;
43
44             sql = "SELECT id FROM transactions  ORDER BY date desc, id desc";
45             params = null;
46
47             if (hasFilter) {
48                 sql = "SELECT distinct tr.id from transactions tr JOIN transaction_accounts ta " +
49                       "ON ta.transaction_id=tr.id WHERE ta.account_name LIKE ?||'%' AND ta" +
50                       ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
51                 params = filterAccName;
52             }
53
54             Log.d("tmp", sql);
55             try (SQLiteDatabase db = MLDB.getReadableDatabase()) {
56                 try (Cursor cursor = db.rawQuery(sql, params)) {
57                     while (cursor.moveToNext()) {
58                         if (isCancelled()) return null;
59
60                         newList.add(new LedgerTransaction(cursor.getInt(0)));
61                     }
62                     Data.transactions.set(newList);
63                     Log.d("transactions", "transaction value updated");
64                 }
65             }
66
67             return newList;
68         }
69         finally {
70             Data.backgroundTaskCount.decrementAndGet();
71         }
72     }
73 }