]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
9e7c8101b669a91b106fbba811c101ebb11c27f3
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / UpdateTransactionsTask.java
1 /*
2  * Copyright © 2021 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.App;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.model.MobileLedgerProfile;
28 import net.ktnx.mobileledger.ui.MainModel;
29 import net.ktnx.mobileledger.utils.SimpleDate;
30
31 import static net.ktnx.mobileledger.utils.Logger.debug;
32
33 public class UpdateTransactionsTask extends AsyncTask<MainModel, Void, String> {
34     protected String doInBackground(MainModel[] model) {
35         final MobileLedgerProfile profile = Data.getProfile();
36
37         long profile_id = profile.getId();
38         Data.backgroundTaskStarted();
39         try {
40             String sql;
41             String[] params;
42
43             final String accFilter = model[0].getAccountFilter()
44                                              .getValue();
45             if (accFilter == null) {
46                 sql = "SELECT id, year, month, day FROM transactions WHERE profile_id=? ORDER BY " +
47                       "year desc, month desc, day desc, id desc";
48                 params = new String[]{String.valueOf(profile_id)};
49
50             }
51             else {
52                 sql = "SELECT distinct tr.id, tr.year, tr.month, tr.day from transactions tr " +
53                       "JOIN " + "transaction_accounts ta " +
54                       "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile_id=?" +
55                       " " +
56                       "and ta.account_name LIKE ?||'%' AND ta" +
57                       ".amount <> 0 ORDER BY tr.year desc, tr.month desc, tr.day desc, tr.id " +
58                       "desc";
59                 params = new String[]{String.valueOf(profile_id), accFilter};
60             }
61
62             debug("UTT", sql);
63             TransactionAccumulator accumulator = new TransactionAccumulator(model[0]);
64
65             SQLiteDatabase db = App.getDatabase();
66             try (Cursor cursor = db.rawQuery(sql, params)) {
67                 while (cursor.moveToNext()) {
68                     if (isCancelled())
69                         return null;
70
71                     accumulator.put(new LedgerTransaction(cursor.getInt(0)),
72                             new SimpleDate(cursor.getInt(1), cursor.getInt(2), cursor.getInt(3)));
73                 }
74             }
75
76             accumulator.done();
77             debug("UTT", "transaction list value updated");
78
79             return null;
80         }
81         finally {
82             Data.backgroundTaskFinished();
83         }
84     }
85 }