20
20
import android .database .sqlite .SQLiteDatabase ;
21
21
import android .database .sqlite .SQLiteStatement ;
22
22
import android .support .annotation .NonNull ;
23
+ import android .support .annotation .Nullable ;
23
24
24
25
import org .gnucash .android .app .GnuCashApplication ;
25
26
import org .gnucash .android .model .PeriodType ;
26
27
import org .gnucash .android .model .Recurrence ;
27
28
28
29
import java .sql .Timestamp ;
30
+ import java .util .ArrayList ;
31
+ import java .util .Calendar ;
32
+ import java .util .Collections ;
33
+ import java .util .List ;
29
34
30
35
import static org .gnucash .android .db .DatabaseSchema .RecurrenceEntry ;
31
36
@@ -58,7 +63,7 @@ public Recurrence buildModelInstance(@NonNull Cursor cursor) {
58
63
long multiplier = cursor .getLong (cursor .getColumnIndexOrThrow (RecurrenceEntry .COLUMN_MULTIPLIER ));
59
64
String periodStart = cursor .getString (cursor .getColumnIndexOrThrow (RecurrenceEntry .COLUMN_PERIOD_START ));
60
65
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 ));
62
67
63
68
PeriodType periodType = PeriodType .valueOf (type );
64
69
periodType .setMultiplier ((int ) multiplier );
@@ -67,7 +72,7 @@ public Recurrence buildModelInstance(@NonNull Cursor cursor) {
67
72
recurrence .setPeriodStart (Timestamp .valueOf (periodStart ));
68
73
if (periodEnd != null )
69
74
recurrence .setPeriodEnd (Timestamp .valueOf (periodEnd ));
70
- recurrence .setByDay ( byDay );
75
+ recurrence .setByDays ( stringToByDays ( byDays ) );
71
76
72
77
populateBaseModelAttributes (cursor , recurrence );
73
78
@@ -79,8 +84,8 @@ public Recurrence buildModelInstance(@NonNull Cursor cursor) {
79
84
stmt .clearBindings ();
80
85
stmt .bindLong (1 , recurrence .getPeriodType ().getMultiplier ());
81
86
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 () ));
84
89
//recurrence should always have a start date
85
90
stmt .bindString (4 , recurrence .getPeriodStart ().toString ());
86
91
@@ -90,4 +95,87 @@ public Recurrence buildModelInstance(@NonNull Cursor cursor) {
90
95
91
96
return stmt ;
92
97
}
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
+ }
93
181
}
0 commit comments