2020import android .database .sqlite .SQLiteDatabase ;
2121import android .database .sqlite .SQLiteStatement ;
2222import android .support .annotation .NonNull ;
23+ import android .support .annotation .Nullable ;
2324
2425import org .gnucash .android .app .GnuCashApplication ;
2526import org .gnucash .android .model .PeriodType ;
2627import org .gnucash .android .model .Recurrence ;
2728
2829import java .sql .Timestamp ;
30+ import java .util .ArrayList ;
31+ import java .util .Calendar ;
32+ import java .util .Collections ;
33+ import java .util .List ;
2934
3035import static org .gnucash .android .db .DatabaseSchema .RecurrenceEntry ;
3136
@@ -58,7 +63,7 @@ public Recurrence buildModelInstance(@NonNull Cursor cursor) {
5863 long multiplier = cursor .getLong (cursor .getColumnIndexOrThrow (RecurrenceEntry .COLUMN_MULTIPLIER ));
5964 String periodStart = cursor .getString (cursor .getColumnIndexOrThrow (RecurrenceEntry .COLUMN_PERIOD_START ));
6065 String periodEnd = cursor .getString (cursor .getColumnIndexOrThrow (RecurrenceEntry .COLUMN_PERIOD_END ));
61- String byDay = cursor .getString (cursor .getColumnIndexOrThrow (RecurrenceEntry .COLUMN_BYDAY ));
66+ String byDays = cursor .getString (cursor .getColumnIndexOrThrow (RecurrenceEntry .COLUMN_BYDAY ));
6267
6368 PeriodType periodType = PeriodType .valueOf (type );
6469 periodType .setMultiplier ((int ) multiplier );
@@ -67,7 +72,7 @@ public Recurrence buildModelInstance(@NonNull Cursor cursor) {
6772 recurrence .setPeriodStart (Timestamp .valueOf (periodStart ));
6873 if (periodEnd != null )
6974 recurrence .setPeriodEnd (Timestamp .valueOf (periodEnd ));
70- recurrence .setByDay ( byDay );
75+ recurrence .setByDays ( stringToByDays ( byDays ) );
7176
7277 populateBaseModelAttributes (cursor , recurrence );
7378
@@ -79,8 +84,8 @@ public Recurrence buildModelInstance(@NonNull Cursor cursor) {
7984 stmt .clearBindings ();
8085 stmt .bindLong (1 , recurrence .getPeriodType ().getMultiplier ());
8186 stmt .bindString (2 , recurrence .getPeriodType ().name ());
82- if (recurrence .getByDay () != null )
83- stmt .bindString (3 , recurrence .getByDay ( ));
87+ if (! recurrence .getByDays (). isEmpty () )
88+ stmt .bindString (3 , byDaysToString ( recurrence .getByDays () ));
8489 //recurrence should always have a start date
8590 stmt .bindString (4 , recurrence .getPeriodStart ().toString ());
8691
@@ -90,4 +95,87 @@ public Recurrence buildModelInstance(@NonNull Cursor cursor) {
9095
9196 return stmt ;
9297 }
98+
99+ /**
100+ * Converts a list of days of week as Calendar constants to an String for
101+ * storing in the database.
102+ *
103+ * @param byDays list of days of week constants from Calendar
104+ * @return String of days of the week or null if {@code byDays} was empty
105+ */
106+ private static @ NonNull String byDaysToString (@ NonNull List <Integer > byDays ) {
107+ StringBuilder builder = new StringBuilder ();
108+ for (int day : byDays ) {
109+ switch (day ) {
110+ case Calendar .MONDAY :
111+ builder .append ("MO" );
112+ break ;
113+ case Calendar .TUESDAY :
114+ builder .append ("TU" );
115+ break ;
116+ case Calendar .WEDNESDAY :
117+ builder .append ("WE" );
118+ break ;
119+ case Calendar .THURSDAY :
120+ builder .append ("TH" );
121+ break ;
122+ case Calendar .FRIDAY :
123+ builder .append ("FR" );
124+ break ;
125+ case Calendar .SATURDAY :
126+ builder .append ("SA" );
127+ break ;
128+ case Calendar .SUNDAY :
129+ builder .append ("SU" );
130+ break ;
131+ default :
132+ throw new RuntimeException ("bad day of week: " + day );
133+ }
134+ builder .append ("," );
135+ }
136+ builder .deleteCharAt (builder .length ()-1 );
137+ return builder .toString ();
138+ }
139+
140+ /**
141+ * Converts a String with the comma-separated days of the week into a
142+ * list of Calendar constants.
143+ *
144+ * @param byDaysString String with comma-separated days fo the week
145+ * @return list of days of the week as Calendar constants.
146+ */
147+ private static @ NonNull List <Integer > stringToByDays (@ Nullable String byDaysString ) {
148+ if (byDaysString == null )
149+ return Collections .emptyList ();
150+
151+ List <Integer > byDaysList = new ArrayList <>();
152+ for (String day : byDaysString .split ("," )) {
153+ switch (day ) {
154+ case "MO" :
155+ byDaysList .add (Calendar .MONDAY );
156+ break ;
157+ case "TU" :
158+ byDaysList .add (Calendar .TUESDAY );
159+ break ;
160+ case "WE" :
161+ byDaysList .add (Calendar .WEDNESDAY );
162+ break ;
163+ case "TH" :
164+ byDaysList .add (Calendar .THURSDAY );
165+ break ;
166+ case "FR" :
167+ byDaysList .add (Calendar .FRIDAY );
168+ break ;
169+ case "SA" :
170+ byDaysList .add (Calendar .SATURDAY );
171+ break ;
172+ case "SU" :
173+ byDaysList .add (Calendar .SUNDAY );
174+ break ;
175+ default :
176+ throw new RuntimeException ("bad day of week: " + day );
177+ }
178+ }
179+ return byDaysList ;
180+ }
93181}
0 commit comments