• 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

77.93
opensrp-immunization/src/main/java/org/smartregister/immunization/adapter/ImmunizationRowAdapter.java
1
package org.smartregister.immunization.adapter;
2

3
import static org.smartregister.immunization.util.VaccinatorUtils.generateScheduleList;
4
import static org.smartregister.immunization.util.VaccinatorUtils.receivedVaccines;
5
import static org.smartregister.util.Utils.getValue;
6

7
import android.content.Context;
8
import android.os.AsyncTask;
9
import android.view.View;
10
import android.view.ViewGroup;
11
import android.widget.BaseAdapter;
12

13
import org.apache.commons.lang3.StringUtils;
14
import org.joda.time.DateTime;
15
import org.smartregister.commonregistry.CommonPersonObjectClient;
16
import org.smartregister.domain.Alert;
17
import org.smartregister.domain.Photo;
18
import org.smartregister.immunization.db.VaccineRepo;
19
import org.smartregister.immunization.domain.State;
20
import org.smartregister.immunization.domain.Vaccine;
21
import org.smartregister.immunization.domain.VaccineWrapper;
22
import org.smartregister.immunization.repository.VaccineRepository;
23
import org.smartregister.immunization.util.ImageUtils;
24
import org.smartregister.immunization.util.VaccinatorUtils;
25
import org.smartregister.immunization.view.ImmunizationRowCard;
26
import org.smartregister.immunization.view.ImmunizationRowGroup;
27
import org.smartregister.util.Utils;
28

29
import java.util.ArrayList;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.HashMap;
33
import java.util.List;
34
import java.util.Locale;
35
import java.util.Map;
36
import java.util.concurrent.TimeUnit;
37

38
import timber.log.Timber;
39

40
/**
41
 * Created by raihan on 13/03/2017.
42
 */
43
public class ImmunizationRowAdapter extends BaseAdapter {
44
    private static final String TAG = "ImmunizationRowAdapter";
45
    private final Context context;
46
    private final ImmunizationRowGroup vaccineGroup;
47
    public boolean editmode;
48
    private HashMap<String, ImmunizationRowCard> vaccineCards;
49
    private List<Vaccine> vaccineList;
50
    private List<Alert> alertList;
51

52
    public ImmunizationRowAdapter(Context context, ImmunizationRowGroup vaccineGroup,
53
                                  boolean editmode, List<Vaccine> vaccineList, List<Alert> alertList) {
1✔
54
        this.context = context;
1✔
55
        this.editmode = editmode;
1✔
56
        this.vaccineGroup = vaccineGroup;
1✔
57
        this.vaccineList = vaccineList;
1✔
58
        this.alertList = alertList;
1✔
59
        vaccineCards = new HashMap<>();
1✔
60
    }
1✔
61

62
    @Override
63
    public int getCount() {
64
        return vaccineGroup.getVaccineData().vaccines.size();
1✔
65
    }
66

67
    @Override
68
    public Object getItem(int position) {
69
        return vaccineCards.get(position);
1✔
70
    }
71

72
    @Override
73
    public long getItemId(int position) {
74
        return 231231 + position;
1✔
75
    }
76

77
    @Override
78
    public View getView(int position, View convertView, ViewGroup parent) {
79
        try {
80
            org.smartregister.immunization.domain.jsonmapping.Vaccine vaccineData = vaccineGroup.getVaccineData().vaccines
1✔
81
                    .get(position);
1✔
82
            String vaccineName = vaccineData.name;
1✔
83
            if (!vaccineCards.containsKey(vaccineName)) {
1✔
84
                ImmunizationRowCard vaccineCard = new ImmunizationRowCard(context, editmode);
1✔
85
                vaccineCard.setId((int) getItemId(position));
1✔
86
                vaccineCards.put(vaccineName, vaccineCard);
1✔
87
                ImmunizationRowTask immunizationRowTask = new ImmunizationRowTask(vaccineCard, vaccineName,
1✔
88
                        vaccineGroup.getVaccineData().days_after_birth_due,
1✔
89
                        vaccineGroup.getChildDetails());
1✔
90
                Utils.startAsyncTask(immunizationRowTask, null);
1✔
91
            }
92
            return vaccineCards.get(vaccineName);
1✔
93
        } catch (Exception e) {
×
94
            Timber.e(e);
×
95
            return null;
×
96
        }
97
    }
98

99
    public void update(ArrayList<VaccineWrapper> vaccinesToUpdate) {
100
        if (vaccineCards != null) {
1✔
101
            if (vaccinesToUpdate == null) {// Update all vaccines
1✔
102
                for (ImmunizationRowCard curCard : vaccineCards.values()) {
1✔
103
                    if (curCard != null) curCard.updateState();
×
104
                }
1✔
105
            } else {// Update just the vaccines specified
106
                for (VaccineWrapper currWrapper : vaccinesToUpdate) {
1✔
107
                    if (vaccineCards.containsKey(currWrapper.getName())) {
1✔
108
                        vaccineCards.get(currWrapper.getName()).updateState();
×
109
                    }
110
                }
1✔
111
            }
112
        }
113
    }
1✔
114

115
    public ArrayList<VaccineWrapper> getDueVaccines() {
116
        ArrayList<VaccineWrapper> dueVaccines = new ArrayList<>();
1✔
117
        if (vaccineCards != null) {
1✔
118
            for (ImmunizationRowCard curCard : vaccineCards.values()) {
1✔
119
                if (curCard != null && (curCard.getState().equals(State.DUE)
×
120
                        || curCard.getState().equals(State.OVERDUE))) {
×
121
                    dueVaccines.add(curCard.getVaccineWrapper());
×
122
                }
123
            }
×
124
        }
125

126
        return dueVaccines;
1✔
127
    }
128

129
    public void updateWrapper(VaccineWrapper tag) {
130
        List<Vaccine> vaccineList = getVaccineList();
1✔
131

132
        if (!vaccineList.isEmpty()) {
1✔
133
            for (Vaccine vaccine : vaccineList) {
1✔
134
                if (tag.getName().toLowerCase().contains(vaccine.getName().toLowerCase()) && vaccine.getDate() != null) {
1✔
135

136
                    //Add exceptions
137
                    if (VaccinatorUtils.isSkippableVaccine(tag.getName()) && !tag.getName().equalsIgnoreCase(vaccine.getName())) {
1✔
138
                        continue;
×
139
                    }
140

141
                    long diff = vaccine.getUpdatedAt() - vaccine.getDate().getTime();
1✔
142
                    if (diff > 0 && TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS) > 1) {
1✔
143
                        tag.setUpdatedVaccineDate(new DateTime(vaccine.getDate()), false);
×
144
                    } else {
145
                        tag.setUpdatedVaccineDate(new DateTime(vaccine.getDate()), true);
1✔
146
                    }
147
                    tag.setDbKey(vaccine.getId());
1✔
148
                    tag.setSynced(vaccine.getSyncStatus() != null && vaccine.getSyncStatus()
1✔
149
                            .equals(VaccineRepository.TYPE_Synced));
1✔
150
                    if (tag.getName().contains("/")) {
1✔
151
                        String[] array = tag.getName().split("/");
1✔
152
                        if ((array[0]).toLowerCase().contains(vaccine.getName())) {
1✔
153
                            tag.setName(array[0]);
×
154
                        } else if ((array[1]).toLowerCase().contains(vaccine.getName())) {
1✔
155
                            tag.setName(array[1]);
×
156
                        }
157
                    }
158
                    tag.setCreatedAt(vaccine.getCreatedAt());
1✔
159
                }
160
            }
1✔
161
        }
162
    }
1✔
163

164
    public List<Vaccine> getVaccineList() {
165
        return vaccineList;
1✔
166
    }
167

168
    public void setVaccineList(List<Vaccine> vaccineList) {
169
        this.vaccineList = vaccineList;
×
170
    }
×
171

172
    public void updateWrapperStatus(ArrayList<VaccineWrapper> tags, CommonPersonObjectClient childDetails) {
173
        if (tags == null) {
1✔
174
            return;
×
175
        }
176

177
        for (VaccineWrapper tag : tags) {
1✔
178
            updateWrapperStatus(tag, childDetails);
1✔
179
        }
1✔
180
    }
1✔
181

182
    public void updateWrapperStatus(VaccineWrapper tag, CommonPersonObjectClient childDetails) {
183
        List<Vaccine> vaccineList = getVaccineList();
1✔
184

185
        List<Alert> alertList = getAlertList();
1✔
186
        Map<String, Date> recievedVaccines = receivedVaccines(vaccineList);
1✔
187
        String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
188
        DateTime dateTime = !dobString.isEmpty() ? new DateTime(dobString) : new DateTime();
1✔
189
        List<Map<String, Object>> sch = generateScheduleList("child", dateTime, recievedVaccines, alertList);
1✔
190

191
        for (Map<String, Object> m : sch) {
1✔
192
            VaccineRepo.Vaccine vaccine = (VaccineRepo.Vaccine) m.get("vaccine");
×
193
            if (tag.getName().toLowerCase().contains(vaccine.display().toLowerCase())) {
×
194

195
                //Add exceptions
196
                if (VaccinatorUtils.isSkippableVaccine(tag.getName()) && !tag.getName().equalsIgnoreCase(vaccine.display())) {
×
197
                    continue;
×
198
                }
199

200
                tag.setStatus(m.get("status").toString());
×
201
                tag.setAlert((Alert) m.get("alert"));
×
202

203
                updateVaccineDate(m, vaccine, tag, recievedVaccines);
×
204
            }
205
        }
×
206
    }
1✔
207

208
    protected void updateVaccineDate(Map<String, Object> m, VaccineRepo.Vaccine vaccine, VaccineWrapper tag, Map<String, Date> recievedVaccines) {
209
        if (m.get("status") != null
1✔
210
                && ((String) m.get("status")).equalsIgnoreCase("due")
1✔
211
                && vaccine.prerequisite() != null) {
1✔
212
            Date preReq = recievedVaccines.get(vaccine.prerequisite().display().toLowerCase(Locale.ENGLISH));
1✔
213
            if (preReq != null) {
1✔
214
                DateTime preReqDateTime = new DateTime(preReq);
1✔
215
                DateTime vaccineDate = preReqDateTime.plusDays(vaccine.prerequisiteGapDays());
1✔
216
                tag.setVaccineDate(vaccineDate);
1✔
217
            }
218
        }
219
    }
1✔
220

221
    public List<Alert> getAlertList() {
222
        return alertList;
1✔
223
    }
224

225
    public void setAlertList(List<Alert> alertList) {
226
        this.alertList = alertList;
×
227
    }
×
228

229
    class ImmunizationRowTask extends AsyncTask<Void, Void, VaccineWrapper> {
230

231
        private ImmunizationRowCard vaccineCard;
232

233
        private String vaccineName;
234

235
        private int days_after_birth_due;
236

237
        private CommonPersonObjectClient childDetails;
238

239
        ImmunizationRowTask(ImmunizationRowCard vaccineCard, String vaccineName, int days_after_birth_due,
240
                            CommonPersonObjectClient childDetails) {
1✔
241
            this.vaccineCard = vaccineCard;
1✔
242
            this.vaccineName = vaccineName;
1✔
243
            this.days_after_birth_due = days_after_birth_due;
1✔
244
            this.childDetails = childDetails;
1✔
245
        }
1✔
246

247
        @Override
248
        protected VaccineWrapper doInBackground(Void... params) {
249

250
            VaccineWrapper vaccineWrapper = new VaccineWrapper();
1✔
251
            vaccineWrapper.setId(childDetails.entityId());
1✔
252
            vaccineWrapper.setGender(childDetails.getDetails().get("gender"));
1✔
253
            vaccineWrapper.setName(vaccineName);
1✔
254

255
            String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
256
            if (StringUtils.isNotBlank(dobString)) {
1✔
257
                Calendar dobCalender = Calendar.getInstance();
1✔
258
                DateTime dateTime = new DateTime(dobString);
1✔
259
                dobCalender.setTime(dateTime.toDate());
1✔
260
                dobCalender.add(Calendar.DATE, days_after_birth_due);
1✔
261
                vaccineWrapper.setVaccineDate(new DateTime(dobCalender.getTime()));
1✔
262
            }
263

264

265
            Photo photo = ImageUtils.profilePhotoByClient(childDetails);
1✔
266
            vaccineWrapper.setPhoto(photo);
1✔
267

268
            String zeirId = getValue(childDetails.getColumnmaps(), "zeir_id", false);
1✔
269
            vaccineWrapper.setPatientNumber(zeirId);
1✔
270
            vaccineWrapper.setPatientName(
1✔
271
                    getValue(childDetails.getColumnmaps(), "first_name", true) + " " + getValue(childDetails.getColumnmaps(),
1✔
272
                            "last_name", true));
273

274
            updateWrapper(vaccineWrapper);
1✔
275
            updateWrapperStatus(vaccineWrapper, childDetails);
1✔
276
            return vaccineWrapper;
1✔
277
        }
278

279
        @Override
280
        protected void onPostExecute(VaccineWrapper vaccineWrapper) {
281
            vaccineCard.setVaccineWrapper(vaccineWrapper);
×
282
            vaccineGroup.toggleRecordAllTV();
×
283
            if (vaccineWrapper.getStatus() == null) {
×
284
                removeVaccine(vaccineName);
×
285
            } else {
286
                notifyDataSetChanged();
×
287
            }
288
        }
×
289
    }
290

291
    private void removeVaccine(String vaccineName) {
292
        vaccineCards.remove(vaccineName);
1✔
293
        for (int i = 0; i < vaccineGroup.getVaccineData().vaccines.size(); i++) {
1✔
294
            org.smartregister.immunization.domain.jsonmapping.Vaccine vaccine = vaccineGroup.getVaccineData().vaccines
1✔
295
                    .get(i);
1✔
296
            if (vaccine.getName().equalsIgnoreCase(vaccineName)) {
1✔
297
                vaccineGroup.getVaccineData().vaccines
1✔
298
                        .remove(i);
1✔
299
            }
300
        }
301
        notifyDataSetChanged();
1✔
302
        vaccineGroup.getVaccinesGV().invalidateViews();
1✔
303
    }
1✔
304
}
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