1
1
package org .smartregister .chw .repository ;
2
2
3
+ import static org .smartregister .repository .BaseRepository .TYPE_Synced ;
4
+ import static org .smartregister .repository .BaseRepository .TYPE_Valid ;
5
+
3
6
import android .content .Context ;
4
7
8
+ import net .sqlcipher .Cursor ;
5
9
import net .sqlcipher .database .SQLiteDatabase ;
6
10
11
+ import org .apache .commons .lang3 .StringUtils ;
12
+ import org .json .JSONObject ;
7
13
import org .smartregister .chw .anc .repository .VisitRepository ;
14
+ import org .smartregister .chw .application .ChwApplication ;
8
15
import org .smartregister .chw .util .RepositoryUtils ;
16
+ import org .smartregister .domain .Event ;
9
17
import org .smartregister .domain .db .Column ;
10
18
import org .smartregister .immunization .repository .RecurringServiceRecordRepository ;
11
19
import org .smartregister .immunization .repository .VaccineRepository ;
12
20
import org .smartregister .immunization .util .IMDatabaseUtils ;
13
21
import org .smartregister .reporting .ReportingLibrary ;
14
22
import org .smartregister .repository .AlertRepository ;
15
23
import org .smartregister .repository .EventClientRepository ;
24
+ import org .smartregister .sync .helper .ECSyncHelper ;
16
25
26
+ import java .util .ArrayList ;
17
27
import java .util .Arrays ;
18
28
import java .util .Collections ;
29
+ import java .util .List ;
19
30
20
31
import timber .log .Timber ;
21
32
22
33
public class ChwRepositoryFlv {
34
+ private static final String EVENT_ID = "id" ;
35
+ private static final String _ID = "_id" ;
23
36
24
37
public static void onUpgrade (Context context , SQLiteDatabase db , int oldVersion , int newVersion ) {
25
38
Timber .w (ChwRepository .class .getName (),
@@ -46,6 +59,9 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion,
46
59
case 7 :
47
60
upgradeToVersion7 (db );
48
61
break ;
62
+ case 8 :
63
+ upgradeToVersion8 (db );
64
+ break ;
49
65
default :
50
66
break ;
51
67
}
@@ -147,4 +163,73 @@ private static void upgradeToVersion7(SQLiteDatabase db) {
147
163
Timber .e (e , "upgradeToVersion7" );
148
164
}
149
165
}
166
+
167
+ private static void upgradeToVersion8 (SQLiteDatabase db ) {
168
+ List <Event > events = new ArrayList <>();
169
+ String eventTableName = EventClientRepository .Table .event .name ();
170
+ String eventIdCol = EventClientRepository .event_column .eventId .name ();
171
+ String eventSyncStatusCol = EventClientRepository .event_column .syncStatus .name ();
172
+ String eventValidCol = EventClientRepository .event_column .validationStatus .name ();
173
+ String jsonCol = EventClientRepository .event_column .json .name ();
174
+ String formSubmissionCol = EventClientRepository .event_column .formSubmissionId .name ();
175
+
176
+ Cursor cursor ;
177
+ String selection = eventIdCol + " IS NULL AND " + eventValidCol + " = ?" ;
178
+ try {
179
+ cursor = db .query (eventTableName , new String []{jsonCol },
180
+ selection , new String []{TYPE_Valid }, null , null , null );
181
+ events = readEvents (cursor );
182
+ } catch (Exception ex ) {
183
+ Timber .e (ex );
184
+ }
185
+ String updateSQL ;
186
+ for (Event event : events ) {
187
+ updateSQL = String .format ("UPDATE %s SET %s = '%s', %s = '%s' WHERE %s = '%s';" , eventTableName ,
188
+ eventIdCol , event .getEventId (), eventSyncStatusCol , TYPE_Synced , formSubmissionCol , event .getFormSubmissionId ());
189
+ try {
190
+ db .execSQL (updateSQL );
191
+ } catch (Exception e ) {
192
+ Timber .e (e , "upgradeToVersion21 " );
193
+ }
194
+ }
195
+ }
196
+
197
+ private static List <Event > readEvents (Cursor cursor ) {
198
+ List <Event > events = new ArrayList <>();
199
+ ECSyncHelper syncHelper = ChwApplication .getInstance ().getEcSyncHelper ();
200
+ try {
201
+ if (cursor != null && cursor .getCount () > 0 && cursor .moveToFirst ()) {
202
+ while (!cursor .isAfterLast ()) {
203
+ String json = cursor .getString (cursor .getColumnIndex ("json" ));
204
+ Event event = syncHelper .convert (new JSONObject (json ), Event .class );
205
+ event .setEventId (getEventId (json ));
206
+ events .add (event );
207
+ cursor .moveToNext ();
208
+ }
209
+ }
210
+ } catch (Exception e ) {
211
+ Timber .e (e );
212
+ } finally {
213
+ cursor .close ();
214
+ }
215
+ return events ;
216
+ }
217
+
218
+ private static String getEventId (String jsonString ) {
219
+ JSONObject jsonObject ;
220
+ String eventId = null ;
221
+ if (StringUtils .isNotEmpty (jsonString )) {
222
+ try {
223
+ jsonObject = new JSONObject (jsonString );
224
+ if (jsonObject .has (EVENT_ID )) {
225
+ eventId = jsonObject .getString (EVENT_ID );
226
+ } else if (jsonObject .has (_ID )) {
227
+ eventId = jsonObject .getString (_ID );
228
+ }
229
+ } catch (Exception ex ) {
230
+ Timber .e (ex );
231
+ }
232
+ }
233
+ return eventId ;
234
+ }
150
235
}
0 commit comments