Skip to content

Commit 559af0e

Browse files
committed
Fix imported weekly scheduled actions no having the days of the week set
Currently, days of the week aren't parsed for scheduled actions, so they aren't set either. This ensures scheduled actions are executed at least once per week by setting them to run on the day of the week of the start time. It's a temporary workaround, in the future we should implement parsing of days of the week.
1 parent 5a452d4 commit 559af0e

3 files changed

Lines changed: 415 additions & 0 deletions

File tree

app/src/main/java/org/gnucash/android/importer/GncXmlHandler.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
import java.sql.Timestamp;
6161
import java.text.ParseException;
6262
import java.util.ArrayList;
63+
import java.util.Calendar;
64+
import java.util.Collections;
65+
import java.util.Date;
6366
import java.util.HashMap;
6467
import java.util.List;
6568
import java.util.Map;
@@ -791,6 +794,10 @@ public void endElement(String uri, String localName, String qualifiedName) throw
791794

792795
case GncXmlHelper.TAG_SCHEDULED_ACTION:
793796
if (mScheduledAction.getActionUID() != null && !mIgnoreScheduledAction) {
797+
if (mScheduledAction.getRecurrence().getPeriodType() == PeriodType.WEEK) {
798+
// TODO: implement parsing of by days for scheduled actions
799+
setMinimalScheduledActionByDays();
800+
}
794801
mScheduledActionsList.add(mScheduledAction);
795802
int count = generateMissedScheduledTransactions(mScheduledAction);
796803
Log.i(LOG_TAG, String.format("Generated %d transactions from scheduled action", count));
@@ -1115,4 +1122,17 @@ private int generateMissedScheduledTransactions(ScheduledAction scheduledAction)
11151122
scheduledAction.setLastRun(lastRuntime);
11161123
return generatedTransactionCount;
11171124
}
1125+
1126+
/**
1127+
* Sets the by days of the scheduled action to the day of the week of the start time.
1128+
*
1129+
* <p>Until we implement parsing of days of the week for scheduled actions,
1130+
* this ensures they are executed at least once per week.</p>
1131+
*/
1132+
private void setMinimalScheduledActionByDays() {
1133+
Calendar calendar = Calendar.getInstance();
1134+
calendar.setTime(new Date(mScheduledAction.getStartTime()));
1135+
mScheduledAction.getRecurrence().setByDays(
1136+
Collections.singletonList(calendar.get(Calendar.DAY_OF_WEEK)));
1137+
}
11181138
}

app/src/test/java/org/gnucash/android/test/unit/importer/GncXmlHandlerTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
import org.gnucash.android.db.DatabaseHelper;
2323
import org.gnucash.android.db.adapter.AccountsDbAdapter;
2424
import org.gnucash.android.db.adapter.BooksDbAdapter;
25+
import org.gnucash.android.db.adapter.RecurrenceDbAdapter;
26+
import org.gnucash.android.db.adapter.ScheduledActionDbAdapter;
2527
import org.gnucash.android.db.adapter.SplitsDbAdapter;
2628
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
2729
import org.gnucash.android.export.xml.GncXmlHelper;
2830
import org.gnucash.android.importer.GncXmlHandler;
2931
import org.gnucash.android.model.Account;
3032
import org.gnucash.android.model.AccountType;
3133
import org.gnucash.android.model.Money;
34+
import org.gnucash.android.model.ScheduledAction;
3235
import org.gnucash.android.model.Split;
3336
import org.gnucash.android.model.Transaction;
3437
import org.gnucash.android.model.TransactionType;
@@ -47,6 +50,8 @@
4750
import java.io.IOException;
4851
import java.io.InputStream;
4952
import java.text.ParseException;
53+
import java.util.Calendar;
54+
import java.util.Date;
5055

5156
import javax.xml.parsers.ParserConfigurationException;
5257
import javax.xml.parsers.SAXParser;
@@ -64,6 +69,7 @@ public class GncXmlHandlerTest {
6469
private BooksDbAdapter mBooksDbAdapter;
6570
private TransactionsDbAdapter mTransactionsDbAdapter;
6671
private AccountsDbAdapter mAccountsDbAdapter;
72+
private ScheduledActionDbAdapter mScheduledActionDbAdapter;
6773

6874
@Before
6975
public void setUp() throws Exception {
@@ -95,6 +101,8 @@ private void setUpDbAdapters(String bookUID) {
95101
SQLiteDatabase mainDb = databaseHelper.getReadableDatabase();
96102
mTransactionsDbAdapter = new TransactionsDbAdapter(mainDb, new SplitsDbAdapter(mainDb));
97103
mAccountsDbAdapter = new AccountsDbAdapter(mainDb, mTransactionsDbAdapter);
104+
RecurrenceDbAdapter recurrenceDbAdapter = new RecurrenceDbAdapter(mainDb);
105+
mScheduledActionDbAdapter = new ScheduledActionDbAdapter(mainDb, recurrenceDbAdapter);
98106
}
99107

100108
/**
@@ -334,6 +342,31 @@ public void simpleScheduledTransactionImport() throws ParseException {
334342
assertThat(split2.isPairOf(split1)).isTrue();
335343
}
336344

345+
/**
346+
* Tests that importing a weekly scheduled action sets the days of the
347+
* week of the recursion.
348+
*/
349+
@Test
350+
public void importingScheduledAction_shouldSetByDays() throws ParseException {
351+
String bookUID = importGnuCashXml("importingScheduledAction_shouldSetByDays.xml");
352+
setUpDbAdapters(bookUID);
353+
354+
ScheduledAction scheduledTransaction =
355+
mScheduledActionDbAdapter.getRecord("b5a13acb5a9459ebed10d06b75bbad10");
356+
357+
// There are 3 byDays but, for now, getting one is enough to ensure it is executed
358+
assertThat(scheduledTransaction.getRecurrence().getByDays().size()).isGreaterThanOrEqualTo(1);
359+
360+
// Until we implement parsing of days of the week for scheduled actions,
361+
// we'll just use the day of the week of the start time.
362+
int dayOfWeekFromByDays = scheduledTransaction.getRecurrence().getByDays().get(0);
363+
Date startTime = new Date(scheduledTransaction.getStartTime());
364+
Calendar calendar = Calendar.getInstance();
365+
calendar.setTime(startTime);
366+
int dayOfWeekFromStartTime = calendar.get(Calendar.DAY_OF_WEEK);
367+
assertThat(dayOfWeekFromByDays).isEqualTo(dayOfWeekFromStartTime);
368+
}
369+
337370
/**
338371
* Checks for bug 562 - Scheduled transaction imported with imbalanced splits.
339372
*

0 commit comments

Comments
 (0)