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.
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.
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/>.
18 package net.ktnx.mobileledger.async;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
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.model.TransactionListItem;
29 import net.ktnx.mobileledger.utils.Globals;
31 import java.text.ParseException;
32 import java.util.ArrayList;
33 import java.util.Calendar;
34 import java.util.Date;
36 import static net.ktnx.mobileledger.utils.Logger.debug;
38 public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
39 protected String doInBackground(String[] filterAccName) {
40 final MobileLedgerProfile profile = Data.profile.getValue();
42 return "Profile not configured";
44 String profile_uuid = profile.getUuid();
45 Data.backgroundTaskStarted();
47 ArrayList<TransactionListItem> newList = new ArrayList<>();
52 if (filterAccName[0] == null) {
53 sql = "SELECT id, date FROM transactions WHERE profile=? ORDER BY date desc, id " +
55 params = new String[]{profile_uuid};
59 sql = "SELECT distinct tr.id, tr.date from transactions tr JOIN " +
60 "transaction_accounts ta " +
61 "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
62 "and ta.account_name LIKE ?||'%' AND ta" +
63 ".amount <> 0 ORDER BY tr.date desc, tr.id desc";
64 params = new String[]{profile_uuid, filterAccName[0]};
68 SQLiteDatabase db = App.getDatabase();
69 String lastDateString = Globals.formatLedgerDate(new Date());
70 Calendar lastDate = Globals.parseLedgerDateAsCalendar(lastDateString);
72 try (Cursor cursor = db.rawQuery(sql, params)) {
73 while (cursor.moveToNext()) {
77 int transaction_id = cursor.getInt(0);
78 String dateString = cursor.getString(1);
79 Calendar date = Globals.parseLedgerDateAsCalendar(dateString);
81 if (!lastDateString.equals(dateString)) {
83 (date.get(Calendar.MONTH) != lastDate.get(Calendar.MONTH)) ||
84 (date.get(Calendar.YEAR) != lastDate.get(Calendar.YEAR));
85 newList.add(new TransactionListItem(date.getTime(), showMonth));
88 new TransactionListItem(new LedgerTransaction(transaction_id), odd));
89 // debug("UTT", String.format("got transaction %d", transaction_id));
92 lastDateString = dateString;
95 Data.transactions.setList(newList);
96 debug("UTT", "transaction list value updated");
101 catch (ParseException e) {
102 return String.format("Error parsing stored date '%s'", e.getMessage());
105 Data.backgroundTaskFinished();