• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenSRP / opensrp-client-immunization / #913

pending completion
#913

Pull #201

github-actions

web-flow
Merge 8c9d2d55f into 180f8905e
Pull Request #201: Index sync status column

5182 of 6704 relevant lines covered (77.3%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.21
opensrp-immunization/src/main/java/org/smartregister/immunization/repository/VaccineRepository.java
1
package org.smartregister.immunization.repository;
2

3
import android.content.ContentValues;
4
import android.database.Cursor;
5

6
import net.sqlcipher.database.SQLiteDatabase;
7

8
import org.apache.commons.lang3.StringUtils;
9
import org.ei.drishti.dto.AlertStatus;
10
import org.smartregister.commonregistry.CommonFtsObject;
11
import org.smartregister.domain.Alert;
12
import org.smartregister.immunization.ImmunizationLibrary;
13
import org.smartregister.immunization.domain.Vaccine;
14
import org.smartregister.repository.BaseRepository;
15
import org.smartregister.repository.EventClientRepository;
16
import org.smartregister.service.AlertService;
17

18
import java.text.ParseException;
19
import java.util.ArrayList;
20
import java.util.Calendar;
21
import java.util.Date;
22
import java.util.List;
23

24
import timber.log.Timber;
25

26
public class VaccineRepository extends BaseRepository {
27
    public static final String VACCINE_TABLE_NAME = "vaccines";
28
    public static final String ID_COLUMN = "_id";
29
    public static final String BASE_ENTITY_ID = "base_entity_id";
30
    public static final String EVENT_ID = "event_id";
31
    public static final String FORMSUBMISSION_ID = "formSubmissionId";
32
    public static final String PROGRAM_CLIENT_ID = "program_client_id";
33
    public static final String NAME = "name";
34
    public static final String CALCULATION = "calculation";
35
    public static final String DATE = "date";
36
    public static final String ANMID = "anmid";
37
    public static final String LOCATION_ID = "location_id";
38
    public static final String CHILD_LOCATION_ID = "child_location_id";
39
    public static final String SYNC_STATUS = "sync_status";
40
    public static final String HIA2_STATUS = "hia2_status";
41
    public static final String UPDATED_AT_COLUMN = "updated_at";
42
    public static final String OUT_OF_AREA = "out_of_area";
43
    public static final String IS_VOIDED = "is_voided";
44
    public static final String OUTREACH = "outreach";
45
    public static final String CREATED_AT = "created_at";
46
    public static final String TEAM_ID = "team_id";
47
    public static final String TEAM = "team";
48
    public static final String[] VACCINE_TABLE_COLUMNS = {ID_COLUMN, BASE_ENTITY_ID, PROGRAM_CLIENT_ID, NAME, CALCULATION, DATE, ANMID, LOCATION_ID, CHILD_LOCATION_ID, TEAM, TEAM_ID, SYNC_STATUS, HIA2_STATUS, UPDATED_AT_COLUMN, EVENT_ID, FORMSUBMISSION_ID, OUT_OF_AREA, IS_VOIDED, CREATED_AT, OUTREACH};
1✔
49
    public static final String UPDATE_TABLE_ADD_EVENT_ID_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + EVENT_ID + " VARCHAR;";
50
    public static final String EVENT_ID_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + EVENT_ID + "_index ON " + VACCINE_TABLE_NAME + "(" + EVENT_ID + " COLLATE NOCASE);";
51
    public static final String UPDATE_TABLE_ADD_FORMSUBMISSION_ID_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + FORMSUBMISSION_ID + " VARCHAR;";
52
    public static final String FORMSUBMISSION_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + FORMSUBMISSION_ID + "_index ON " + VACCINE_TABLE_NAME + "(" + FORMSUBMISSION_ID + " COLLATE NOCASE);";
53
    public static final String UPDATE_TABLE_ADD_OUT_OF_AREA_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + OUT_OF_AREA + " INTEGER;";
54
    public static final String UPDATE_TABLE_ADD_IS_VOIDED_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + IS_VOIDED + " INTEGER;";
55
    public static final String UPDATE_TABLE_ADD_OUT_OF_AREA_COL_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + OUT_OF_AREA + "_index ON " + VACCINE_TABLE_NAME + "(" + OUT_OF_AREA + " COLLATE NOCASE);";
56
    public static final String UPDATE_TABLE_ADD_IS_VOIDED_COL_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + IS_VOIDED + "_index ON " + VACCINE_TABLE_NAME + "(" + IS_VOIDED + " COLLATE NOCASE);";
57
    public static final String UPDATE_TABLE_ADD_OUTREACH_COL_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + OUTREACH + "_index ON " + VACCINE_TABLE_NAME + "(" + OUTREACH + " COLLATE NOCASE);";
58
    public static final String UPDATE_TABLE_ADD_HIA2_STATUS_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + HIA2_STATUS + " VARCHAR;";
59
    public static final String ALTER_ADD_CREATED_AT_COLUMN = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + CREATED_AT + " DATETIME NULL ";
60
    public static final String UPDATE_TABLE_ADD_TEAM_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + TEAM + " VARCHAR;";
61
    public static final String UPDATE_TABLE_ADD_TEAM_ID_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + TEAM_ID + " VARCHAR;";
62
    public static final String UPDATE_TABLE_ADD_CHILD_LOCATION_ID_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + CHILD_LOCATION_ID + " VARCHAR;";
63
    public static final String UPDATE_TABLE_VACCINES_ADD_OUTREACH_COL = "ALTER TABLE " + VACCINE_TABLE_NAME + " ADD COLUMN " + OUTREACH + " INTEGER DEFAULT 0;";
64
    public static final String UPDATE_OUTREACH_QUERRY = "UPDATE " + VACCINE_TABLE_NAME + " SET " + OUTREACH + " = 1 WHERE location_id != ?;";
65
    private static final String TAG = VaccineRepository.class.getCanonicalName();
1✔
66
    private static final String VACCINE_SQL = "CREATE TABLE vaccines (_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,base_entity_id VARCHAR NOT NULL,program_client_id VARCHAR NULL,name VARCHAR NOT NULL,calculation INTEGER,date DATETIME NOT NULL,anmid VARCHAR NULL,location_id VARCHAR NULL,sync_status VARCHAR, updated_at INTEGER NULL, UNIQUE(base_entity_id, program_client_id, name) ON CONFLICT IGNORE)";
67
    private static final String BASE_ENTITY_ID_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + BASE_ENTITY_ID + "_index ON " + VACCINE_TABLE_NAME + "(" + BASE_ENTITY_ID + " COLLATE NOCASE);";
68
    private static final String UPDATED_AT_INDEX = "CREATE INDEX " + VACCINE_TABLE_NAME + "_" + UPDATED_AT_COLUMN + "_index ON " + VACCINE_TABLE_NAME + "(" + UPDATED_AT_COLUMN + ");";
69
    public static final String SYNC_STATUS_INDEX = "CREATE INDEX IF NOT EXISTS " + VACCINE_TABLE_NAME + "_" + SYNC_STATUS + "_index ON " + VACCINE_TABLE_NAME + "(" + SYNC_STATUS + ");";
70
    public static String HIA2_Within = "Within";
1✔
71
    public static String HIA2_Overdue = "Overdue";
1✔
72

73
    private CommonFtsObject commonFtsObject;
74
    private AlertService alertService;
75

76
    public VaccineRepository(CommonFtsObject commonFtsObject, AlertService alertService) {
1✔
77
        this.commonFtsObject = commonFtsObject;
1✔
78
        this.alertService = alertService;
1✔
79
    }
1✔
80

81
    public static void createTable(SQLiteDatabase database) {
82
        database.execSQL(VACCINE_SQL);
1✔
83
        database.execSQL(BASE_ENTITY_ID_INDEX);
1✔
84
        database.execSQL(UPDATED_AT_INDEX);
1✔
85
        database.execSQL(SYNC_STATUS_INDEX);
1✔
86
    }
1✔
87

88
    public static void migrateCreatedAt(SQLiteDatabase database) {
89
        try {
90
            String sql = "UPDATE " + VACCINE_TABLE_NAME +
×
91
                    " SET " + CREATED_AT + " = " +
92
                    " ( SELECT " + EventClientRepository.event_column.dateCreated.name() +
×
93
                    "   FROM " + EventClientRepository.Table.event.name() +
×
94
                    "   WHERE " + EventClientRepository.event_column.eventId
95
                    .name() + " = " + VACCINE_TABLE_NAME + "." + EVENT_ID +
×
96
                    "   OR " + EventClientRepository.event_column.formSubmissionId
97
                    .name() + " = " + VACCINE_TABLE_NAME + "." + FORMSUBMISSION_ID +
×
98
                    " ) " +
99
                    " WHERE " + CREATED_AT + " is null ";
100
            database.execSQL(sql);
×
101
        } catch (Exception e) {
×
102
            Timber.e(e);
×
103
        }
×
104
    }
×
105

106
    public void add(Vaccine vaccine) {
107
        if (vaccine == null) {
1✔
108
            return;
1✔
109
        }
110

111
        try {
112
            vaccine.setHia2Status(null);
1✔
113

114
            if (StringUtils.isBlank(vaccine.getSyncStatus())) {
1✔
115
                vaccine.setSyncStatus(TYPE_Unsynced);
1✔
116
            }
117

118
            if (StringUtils.isBlank(vaccine.getFormSubmissionId())) {
1✔
119
                vaccine.setFormSubmissionId(generateRandomUUIDString());
1✔
120
            }
121

122
            if (vaccine.getUpdatedAt() == null) {
1✔
123
                vaccine.setUpdatedAt(Calendar.getInstance().getTimeInMillis());
1✔
124
            }
125

126
            SQLiteDatabase database = getWritableDatabase();
1✔
127
            if (vaccine.getId() == null) {
1✔
128
                Vaccine sameVaccine = findUnique(database, vaccine);
1✔
129
                if (sameVaccine != null) {
1✔
130
                    vaccine.setUpdatedAt(sameVaccine.getUpdatedAt());
×
131
                    vaccine.setId(sameVaccine.getId());
×
132
                    update(database, vaccine);
×
133
                } else {
134
                    if (vaccine.getCreatedAt() == null) {
1✔
135
                        vaccine.setCreatedAt(new Date());
1✔
136
                    }
137
                    vaccine.setId(database.insert(VACCINE_TABLE_NAME, null, createValuesFor(vaccine)));
1✔
138
                }
139
            } else {
1✔
140
                //mark the vaccine as unsynced for processing as an updated event
141
                vaccine.setSyncStatus(TYPE_Unsynced);
1✔
142
                update(database, vaccine);
1✔
143
            }
144
        } catch (Exception e) {
×
145
            Timber.e(e);
×
146
        }
1✔
147
        updateFtsSearch(vaccine);
1✔
148
    }
1✔
149

150
    public void updateHia2Status(Vaccine vaccine, String hai2Status) {
151
        if (vaccine == null || vaccine.getId() == null) {
1✔
152
            return;
1✔
153
        }
154

155
        vaccine.setHia2Status(hai2Status);
1✔
156
        SQLiteDatabase database = getWritableDatabase();
1✔
157
        update(database, vaccine);
1✔
158
    }
1✔
159

160
    public void update(SQLiteDatabase database, Vaccine vaccine) {
161
        if (vaccine == null || vaccine.getId() == null) {
1✔
162
            return;
1✔
163
        }
164

165
        try {
166
            String idSelection = ID_COLUMN + " = ?";
1✔
167
            database.update(VACCINE_TABLE_NAME, createValuesFor(vaccine), idSelection,
1✔
168
                    new String[]{vaccine.getId().toString()});
1✔
169
        } catch (Exception e) {
1✔
170
            Timber.e(e);
1✔
171
        }
1✔
172
    }
1✔
173

174
    private ContentValues createValuesFor(Vaccine vaccine) {
175
        ContentValues values = new ContentValues();
1✔
176
        values.put(ID_COLUMN, vaccine.getId());
1✔
177
        values.put(BASE_ENTITY_ID, vaccine.getBaseEntityId());
1✔
178
        values.put(PROGRAM_CLIENT_ID, vaccine.getProgramClientId());
1✔
179
        values.put(NAME, vaccine.getName() != null ? addHyphen(vaccine.getName().toLowerCase()) : null);
1✔
180
        values.put(CALCULATION, vaccine.getCalculation());
1✔
181
        values.put(DATE, vaccine.getDate() != null ? vaccine.getDate().getTime() : null);
1✔
182
        values.put(ANMID, vaccine.getAnmId());
1✔
183
        values.put(LOCATION_ID, vaccine.getLocationId());
1✔
184
        values.put(TEAM, vaccine.getTeam());
1✔
185
        values.put(TEAM_ID, vaccine.getTeamId());
1✔
186
        values.put(CHILD_LOCATION_ID, vaccine.getChildLocationId());
1✔
187
        values.put(SYNC_STATUS, vaccine.getSyncStatus());
1✔
188
        values.put(HIA2_STATUS, vaccine.getHia2Status());
1✔
189
        values.put(UPDATED_AT_COLUMN, vaccine.getUpdatedAt());
1✔
190
        values.put(EVENT_ID, vaccine.getEventId());
1✔
191
        values.put(FORMSUBMISSION_ID, vaccine.getFormSubmissionId());
1✔
192
        values.put(OUT_OF_AREA, vaccine.getOutOfCatchment());
1✔
193
        values.put(IS_VOIDED, vaccine.getIsVoided());
1✔
194
        values.put(OUTREACH, vaccine.getOutreach());
1✔
195
        values.put(CREATED_AT,
1✔
196
                vaccine.getCreatedAt() != null ? EventClientRepository.dateFormat.format(vaccine.getCreatedAt()) : null);
1✔
197
        return values;
1✔
198
    }
199

200
    public static String addHyphen(String s) {
201
        if (StringUtils.isNotBlank(s)) {
1✔
202
            return s.replace(" ", "_");
1✔
203
        }
204
        return s;
1✔
205
    }
206

207
    public List<Vaccine> findUnSyncedBeforeTime(int minutes) {
208
        List<Vaccine> vaccines = new ArrayList<>();
1✔
209
        Cursor cursor = null;
1✔
210
        try {
211
            Calendar calendar = Calendar.getInstance();
1✔
212
            calendar.add(Calendar.MINUTE, -minutes);
1✔
213

214
            Long time = calendar.getTimeInMillis();
1✔
215

216
            cursor = getReadableDatabase().query(VACCINE_TABLE_NAME, VACCINE_TABLE_COLUMNS,
1✔
217
                    UPDATED_AT_COLUMN + " < ? AND " + SYNC_STATUS + " = ? ", new String[]{time.toString(), TYPE_Unsynced},
1✔
218
                    null, null, null, null);
219
            vaccines = readAllVaccines(cursor);
1✔
220
        } catch (Exception e) {
1✔
221
            Timber.e(e);
1✔
222
        } finally {
223
            if (cursor != null) {
1✔
224
                cursor.close();
1✔
225
            }
226
        }
227
        return vaccines;
1✔
228
    }
229

230
    public List<Vaccine> findUnSynced() {
231
        List<Vaccine> vaccines = new ArrayList<>();
×
232
        Cursor cursor = null;
×
233
        try {
234

235
            cursor = getReadableDatabase().query(VACCINE_TABLE_NAME, VACCINE_TABLE_COLUMNS,
×
236
                    SYNC_STATUS + " = ? ", new String[]{TYPE_Unsynced},
237
                    null, null, null, null);
238
            vaccines = readAllVaccines(cursor);
×
239
        } catch (Exception e) {
×
240
            Timber.e(e);
×
241
        } finally {
242
            if (cursor != null) {
×
243
                cursor.close();
×
244
            }
245
        }
246
        return vaccines;
×
247
    }
248

249
    public List<Vaccine> readAllVaccines(Cursor cursor) {
250
        List<Vaccine> vaccines = new ArrayList<>();
1✔
251

252
        try {
253

254
            if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
1✔
255
                while (!cursor.isAfterLast()) {
1✔
256
                    String vaccineName = cursor.getString(cursor.getColumnIndex(NAME));
1✔
257
                    if (vaccineName != null) {
1✔
258
                        vaccineName = removeHyphen(vaccineName);
1✔
259
                    }
260

261
                    Date createdAt = null;
1✔
262
                    String dateCreatedString = cursor.getString(cursor.getColumnIndex(CREATED_AT));
1✔
263
                    if (StringUtils.isNotBlank(dateCreatedString)) {
1✔
264
                        try {
265
                            createdAt = EventClientRepository.dateFormat.parse(dateCreatedString);
1✔
266
                        } catch (ParseException e) {
1✔
267
                            Timber.e(e);
1✔
268
                        }
1✔
269
                    }
270
                    Vaccine vaccine = new Vaccine(cursor.getLong(cursor.getColumnIndex(ID_COLUMN)),
1✔
271
                            cursor.getString(cursor.getColumnIndex(BASE_ENTITY_ID)),
1✔
272
                            cursor.getString(cursor.getColumnIndex(PROGRAM_CLIENT_ID)),
1✔
273
                            vaccineName,
274
                            cursor.getInt(cursor.getColumnIndex(CALCULATION)),
1✔
275
                            new Date(cursor.getLong(cursor.getColumnIndex(DATE))),
1✔
276
                            cursor.getString(cursor.getColumnIndex(ANMID)),
1✔
277
                            cursor.getString(cursor.getColumnIndex(LOCATION_ID)),
1✔
278
                            cursor.getString(cursor.getColumnIndex(SYNC_STATUS)),
1✔
279
                            cursor.getString(cursor.getColumnIndex(HIA2_STATUS)),
1✔
280
                            cursor.getLong(cursor.getColumnIndex(UPDATED_AT_COLUMN)),
1✔
281
                            cursor.getString(cursor.getColumnIndex(EVENT_ID)),
1✔
282
                            cursor.getString(cursor.getColumnIndex(FORMSUBMISSION_ID)),
1✔
283
                            cursor.getInt(cursor.getColumnIndex(OUT_OF_AREA)),
1✔
284
                            createdAt,
285
                            cursor.getInt(cursor.getColumnIndex(IS_VOIDED)),
1✔
286
                            cursor.getInt(cursor.getColumnIndex(OUTREACH))
1✔
287
                    );
288

289
                    vaccine.setTeam(cursor.getString(cursor.getColumnIndex(TEAM)));
1✔
290
                    vaccine.setTeamId(cursor.getString(cursor.getColumnIndex(TEAM_ID)));
1✔
291
                    vaccine.setChildLocationId(cursor.getString(cursor.getColumnIndex(CHILD_LOCATION_ID)));
1✔
292

293
                    vaccines.add(vaccine);
1✔
294

295
                    cursor.moveToNext();
1✔
296
                }
1✔
297
            }
298
        } catch (Exception e) {
1✔
299

300
        } finally {
301
            cursor.close();
1✔
302
        }
303
        return vaccines;
1✔
304
    }
305

306
    public static String removeHyphen(String s) {
307
        if (StringUtils.isNotBlank(s)) {
1✔
308
            return s.replace("_", " ");
1✔
309
        }
310
        return s;
1✔
311
    }
312

313
    public List<Vaccine> findByEntityId(String entityId) {
314
        SQLiteDatabase database = getReadableDatabase();
1✔
315
        Cursor cursor = database.query(VACCINE_TABLE_NAME, VACCINE_TABLE_COLUMNS,
1✔
316
                BASE_ENTITY_ID + " = ? " + COLLATE_NOCASE + " ORDER BY " + UPDATED_AT_COLUMN, new String[]{entityId}, null,
317
                null, null, null);
318
        return readAllVaccines(cursor);
1✔
319
    }
320

321
    public List<Vaccine> findLatestTwentyFourHoursByEntityId(String entityId) {
322
        SQLiteDatabase database = getReadableDatabase();
1✔
323
        Cursor cursor = database.query(VACCINE_TABLE_NAME, VACCINE_TABLE_COLUMNS,
1✔
324
                BASE_ENTITY_ID + " = ? and (" + UPDATED_AT_COLUMN + "/1000 < strftime('%s',datetime('now','-1 day')))",
325
                new String[]{entityId}, null, null, null, null);
326
        return readAllVaccines(cursor);
1✔
327
    }
328

329
    public Vaccine findUnique(SQLiteDatabase database, Vaccine vaccine) {
330
        if (vaccine == null || (StringUtils.isBlank(vaccine.getFormSubmissionId()) &&
1✔
331
                StringUtils.isBlank(vaccine.getEventId()))) {
×
332

333
            return null;
1✔
334
        }
335

336
        try {
337
            if (database == null) {
1✔
338
                database = getReadableDatabase();
1✔
339
            }
340

341
            String selection = null;
1✔
342
            String[] selectionArgs = null;
1✔
343
            if (StringUtils.isNotBlank(vaccine.getFormSubmissionId()) && StringUtils.isNotBlank(vaccine.getEventId())) {
1✔
344
                selection = FORMSUBMISSION_ID + " = ? " + COLLATE_NOCASE + " OR " + EVENT_ID + " = ? " + COLLATE_NOCASE;
1✔
345
                selectionArgs = new String[]{vaccine.getFormSubmissionId(), vaccine.getEventId()};
1✔
346
            } else if (StringUtils.isNotBlank(vaccine.getEventId())) {
1✔
347
                selection = EVENT_ID + " = ? " + COLLATE_NOCASE;
×
348
                selectionArgs = new String[]{vaccine.getEventId()};
×
349
            } else if (StringUtils.isNotBlank(vaccine.getFormSubmissionId())) {
1✔
350
                selection = FORMSUBMISSION_ID + " = ? " + COLLATE_NOCASE;
1✔
351
                selectionArgs = new String[]{vaccine.getFormSubmissionId()};
1✔
352
            }
353

354
            Cursor cursor = database.query(VACCINE_TABLE_NAME, VACCINE_TABLE_COLUMNS, selection, selectionArgs, null, null,
1✔
355
                    ID_COLUMN + " DESC ", null);
356
            List<Vaccine> vaccines = readAllVaccines(cursor);
1✔
357
            if (!vaccines.isEmpty()) {
1✔
358
                return vaccines.get(0);
×
359
            }
360
        } catch (Exception e) {
1✔
361
            Timber.e(e);
1✔
362
        }
1✔
363
        return null;
1✔
364

365
    }
366

367
    public List<Vaccine> findWithNullHia2Status() {
368
        List<Vaccine> vaccines = new ArrayList<>();
1✔
369
        Cursor cursor = null;
1✔
370
        try {
371

372
            cursor = getReadableDatabase()
1✔
373
                    .query(VACCINE_TABLE_NAME, VACCINE_TABLE_COLUMNS, HIA2_STATUS + " is null", null, null, null, null,
1✔
374
                            null);
375
            vaccines = readAllVaccines(cursor);
×
376
        } catch (Exception e) {
1✔
377
            Timber.e(e);
1✔
378
        } finally {
379
            if (cursor != null) {
1✔
380
                cursor.close();
×
381
            }
382
        }
383
        return vaccines;
1✔
384
    }
385

386
    public void deleteVaccine(String baseEntityId, String vaccineName) {
387
        try {
388
            getWritableDatabase().delete(VACCINE_TABLE_NAME, BASE_ENTITY_ID + " = ? AND " + NAME + " = ? ", new String[]{baseEntityId, vaccineName});
1✔
389
            updateFtsSearch(baseEntityId, vaccineName);
1✔
390
        } catch (Exception e) {
×
391
            Timber.e(e);
×
392
        }
1✔
393
    }
1✔
394

395
    public void deleteVaccine(Long caseId) {
396
        try {
397
            Vaccine vaccine = find(caseId);
1✔
398
            if (vaccine != null) {
1✔
399
                getWritableDatabase().delete(VACCINE_TABLE_NAME, ID_COLUMN + "= ?", new String[]{caseId.toString()});
1✔
400

401
                updateFtsSearch(vaccine.getBaseEntityId(), vaccine.getName());
1✔
402
            }
403
        } catch (Exception e) {
×
404
            Timber.e(e);
×
405
        }
1✔
406
    }
1✔
407

408
    public Vaccine find(Long caseId) {
409
        Vaccine vaccine = null;
1✔
410
        Cursor cursor = null;
1✔
411
        try {
412
            cursor = getReadableDatabase()
1✔
413
                    .query(VACCINE_TABLE_NAME, VACCINE_TABLE_COLUMNS, ID_COLUMN + " = ?", new String[]{caseId.toString()},
1✔
414
                            null, null, null, null);
415
            List<Vaccine> vaccines = readAllVaccines(cursor);
1✔
416
            if (!vaccines.isEmpty()) {
1✔
417
                vaccine = vaccines.get(0);
×
418
            }
419
        } catch (Exception e) {
1✔
420
            Timber.e(e);
1✔
421
        } finally {
422
            if (cursor != null) {
1✔
423
                cursor.close();
1✔
424
            }
425
        }
426
        return vaccine;
1✔
427
    }
428

429
    public Vaccine findByBaseEntityIdAndVaccineName(String baseEntityId, String vaccineName) {
430
        Vaccine vaccine = null;
1✔
431
        Cursor cursor = null;
1✔
432

433
        try {
434
            cursor = getReadableDatabase()
1✔
435
                    .query(VACCINE_TABLE_NAME, VACCINE_TABLE_COLUMNS,
1✔
436
                            BASE_ENTITY_ID + " = ? AND " + NAME + " = ?",
437
                            new String[]{baseEntityId, vaccineName},
438
                            null, null, null, null);
439
            List<Vaccine> vaccines = readAllVaccines(cursor);
1✔
440
            if (!vaccines.isEmpty()) {
1✔
441
                vaccine = vaccines.get(0);
1✔
442
            }
443
        } catch (Exception e) {
1✔
444
            Timber.e(e);
1✔
445
        } finally {
446
            if (cursor != null) {
1✔
447
                cursor.close();
1✔
448
            }
449
        }
450
        return vaccine;
1✔
451
    }
452

453
    public void updateFtsSearch(String entityId, String vaccineName_) {
454
        String vaccineName = vaccineName_;
1✔
455
        try {
456
            if (commonFtsObject != null && alertService() != null) {
1✔
457
                if (vaccineName != null) {
1✔
458
                    vaccineName = removeHyphen(vaccineName);
1✔
459
                }
460

461
                String scheduleName = commonFtsObject.getAlertScheduleName(vaccineName);
1✔
462
                if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(scheduleName)) {
1✔
463
                    Alert alert = alertService().findByEntityIdAndScheduleName(entityId, scheduleName);
×
464
                    alertService().updateFtsSearch(alert, true);
×
465
                }
466
            }
467
        } catch (Exception e) {
×
468
            Timber.e(e);
×
469
        }
1✔
470
    }
1✔
471

472
    public AlertService alertService() {
473
        if (alertService == null) {
1✔
474
            alertService = ImmunizationLibrary.getInstance().context().alertService();
×
475
        }
476
        return alertService;
1✔
477
    }
478

479
    public void close(Long caseId) {
480
        try {
481
            ContentValues values = new ContentValues();
1✔
482
            values.put(SYNC_STATUS, TYPE_Synced);
1✔
483
            getWritableDatabase().update(VACCINE_TABLE_NAME, values, ID_COLUMN + " = ?", new String[]{caseId.toString()});
1✔
484
        } catch (Exception e) {
1✔
485
            Timber.e(e);
1✔
486
        }
1✔
487
    }
1✔
488

489
    //-----------------------
490
    // FTS methods
491
    public void updateFtsSearch(Vaccine vaccine) {
492
        try {
493
            if (commonFtsObject != null && alertService() != null) {
1✔
494
                String entityId = vaccine.getBaseEntityId();
1✔
495
                String vaccineName = vaccine.getName();
1✔
496
                if (vaccineName != null) {
1✔
497
                    vaccineName = removeHyphen(vaccineName);
1✔
498
                }
499
                String scheduleName = commonFtsObject.getAlertScheduleName(vaccineName);
1✔
500

501
                String bindType = commonFtsObject.getAlertBindType(scheduleName);
1✔
502

503
                if (StringUtils.isNotBlank(bindType) && StringUtils.isNotBlank(scheduleName) && StringUtils
1✔
504
                        .isNotBlank(entityId)) {
×
505
                    String field = addHyphen(scheduleName);
×
506
                    // update vaccine status
507
                    alertService().updateFtsSearchInACR(bindType, entityId, field, AlertStatus.complete.value());
×
508
                }
509
            }
510
        } catch (Exception e) {
×
511
            Timber.e(e);
×
512
        }
1✔
513
    }
1✔
514
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc