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.
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.
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/>.
18 package net.ktnx.mobileledger.async;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.util.Log;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.utils.MLDB;
29 import java.util.ArrayList;
30 import java.util.List;
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();
37 ArrayList<LedgerTransaction> newList = new ArrayList<>();
39 boolean hasFilter = (filterAccName != null) && (filterAccName.length > 0) &&
40 (filterAccName[0] != null) && !filterAccName[0].isEmpty();
45 sql = "SELECT id FROM transactions WHERE profile=? ORDER BY date desc, id desc";
46 params = new String[]{profile_uuid};
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;
57 SQLiteDatabase db = MLDB.getReadableDatabase();
58 try (Cursor cursor = db.rawQuery(sql, params)) {
59 while (cursor.moveToNext()) {
60 if (isCancelled()) return null;
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));
66 Data.transactions.set(newList);
67 Log.d("UTT", "transaction list value updated");
73 Data.backgroundTaskCount.decrementAndGet();