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

OpenSRP / opensrp-client-immunization / #898

pending completion
#898

push

github-actions

web-flow
Merge pull request #195 from opensrp/check-vaccine-duplicates

Add Duplicate Vaccines & Recurring Service Record Checks

5076 of 6673 relevant lines covered (76.07%)

0.76 hits per line

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

83.15
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 String HIA2_Within = "Within";
1✔
70
    public static String HIA2_Overdue = "Overdue";
1✔
71

72
    private CommonFtsObject commonFtsObject;
73
    private AlertService alertService;
74

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

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

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

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

109
        try {
110
            vaccine.setHia2Status(null);
1✔
111

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

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

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

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

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

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

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

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

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

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

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

212
            Long time = calendar.getTimeInMillis();
1✔
213

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

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

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

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

250
        try {
251

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

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

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

291
                    vaccines.add(vaccine);
1✔
292

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

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

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

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

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

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

331
            return null;
1✔
332
        }
333

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

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

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

363
    }
364

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

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

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

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

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

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

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

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

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

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

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

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

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

499
                String bindType = commonFtsObject.getAlertBindType(scheduleName);
1✔
500

501
                if (StringUtils.isNotBlank(bindType) && StringUtils.isNotBlank(scheduleName) && StringUtils
1✔
502
                        .isNotBlank(entityId)) {
×
503
                    String field = addHyphen(scheduleName);
×
504
                    // update vaccine status
505
                    alertService().updateFtsSearchInACR(bindType, entityId, field, AlertStatus.complete.value());
×
506
                }
507
            }
508
        } catch (Exception e) {
×
509
            Timber.e(e);
×
510
        }
1✔
511
    }
1✔
512
}
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