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

OpenSRP / opensrp-client-immunization / #906

pending completion
#906

Pull #194

github-actions

web-flow
Merge 2525ae181 into ce201bbb0
Pull Request #194: Remove async task

5180 of 6703 relevant lines covered (77.28%)

0.77 hits per line

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

77.48
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.view.View;
9
import android.view.ViewGroup;
10
import android.widget.BaseAdapter;
11

12
import org.apache.commons.lang3.StringUtils;
13
import org.joda.time.DateTime;
14
import org.smartregister.commonregistry.CommonPersonObjectClient;
15
import org.smartregister.domain.Alert;
16
import org.smartregister.domain.Photo;
17
import org.smartregister.immunization.db.VaccineRepo;
18
import org.smartregister.immunization.domain.State;
19
import org.smartregister.immunization.domain.Vaccine;
20
import org.smartregister.immunization.domain.VaccineWrapper;
21
import org.smartregister.immunization.repository.VaccineRepository;
22
import org.smartregister.immunization.util.ImageUtils;
23
import org.smartregister.immunization.util.VaccinatorUtils;
24
import org.smartregister.immunization.view.ImmunizationRowCard;
25
import org.smartregister.immunization.view.ImmunizationRowGroup;
26
import org.smartregister.util.CallableInteractorCallBack;
27
import org.smartregister.util.GenericInteractor;
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.Callable;
37
import java.util.concurrent.TimeUnit;
38

39
import timber.log.Timber;
40

41
import timber.log.Timber;
42

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

56
    public ImmunizationRowAdapter(Context context, ImmunizationRowGroup vaccineGroup,
57
                                  boolean editmode, List<Vaccine> vaccineList, List<Alert> alertList) {
1✔
58
        this.context = context;
1✔
59
        this.editmode = editmode;
1✔
60
        this.vaccineGroup = vaccineGroup;
1✔
61
        this.vaccineList = vaccineList;
1✔
62
        this.alertList = alertList;
1✔
63
        vaccineCards = new HashMap<>();
1✔
64
        mInteractor = new GenericInteractor();
1✔
65
    }
1✔
66

67
    @Override
68
    public int getCount() {
69
        return vaccineGroup.getVaccineData().vaccines.size();
1✔
70
    }
71

72
    @Override
73
    public Object getItem(int position) {
74
        return vaccineCards.get(position);
1✔
75
    }
76

77
    @Override
78
    public long getItemId(int position) {
79
        return 231231 + position;
1✔
80
    }
81

82
    @Override
83
    public View getView(int position, View convertView, ViewGroup parent) {
84
        try {
85
            org.smartregister.immunization.domain.jsonmapping.Vaccine vaccineData = vaccineGroup.getVaccineData().vaccines
1✔
86
                    .get(position);
1✔
87
            String vaccineName = vaccineData.name;
1✔
88
            if (!vaccineCards.containsKey(vaccineName)) {
1✔
89
                ImmunizationRowCard vaccineCard = getImmunizationRowCard();
1✔
90
                vaccineCard.setId((int) getItemId(position));
1✔
91
                vaccineCards.put(vaccineName, vaccineCard);
1✔
92
                Callable<VaccineWrapper> callable = () -> {
1✔
93
                    CommonPersonObjectClient childDetails = vaccineGroup.getChildDetails();
1✔
94
                    int days_after_birth_due = vaccineGroup.getVaccineData().days_after_birth_due;
1✔
95
                    VaccineWrapper vaccineWrapper = new VaccineWrapper();
1✔
96
                    vaccineWrapper.setId(childDetails.entityId());
1✔
97
                    vaccineWrapper.setGender(childDetails.getDetails().get("gender"));
1✔
98
                    vaccineWrapper.setName(vaccineName);
1✔
99

100
                    String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
101
                    if (StringUtils.isNotBlank(dobString)) {
1✔
102
                        Calendar dobCalender = Calendar.getInstance();
1✔
103
                        DateTime dateTime = new DateTime(dobString);
1✔
104
                        dobCalender.setTime(dateTime.toDate());
1✔
105
                        dobCalender.add(Calendar.DATE, days_after_birth_due);
1✔
106
                        vaccineWrapper.setVaccineDate(new DateTime(dobCalender.getTime()));
1✔
107
                    }
108

109

110
                    Photo photo = ImageUtils.profilePhotoByClient(childDetails);
1✔
111
                    vaccineWrapper.setPhoto(photo);
1✔
112

113
                    String zeirId = getValue(childDetails.getColumnmaps(), "zeir_id", false);
1✔
114
                    vaccineWrapper.setPatientNumber(zeirId);
1✔
115
                    vaccineWrapper.setPatientName(
1✔
116
                            getValue(childDetails.getColumnmaps(), "first_name", true) + " " + getValue(childDetails.getColumnmaps(),
1✔
117
                                    "last_name", true));
118

119
                    updateWrapper(vaccineWrapper);
1✔
120
                    updateWrapperStatus(vaccineWrapper, childDetails);
1✔
121
                    return vaccineWrapper;
1✔
122
                };
123
                ImmunizationRowCallableInteractorCallback immunizationRowCallableInteractorCallback = getImmunizationRowCallableInteractor(vaccineCard, vaccineName);
1✔
124
                GenericInteractor interactor = getGenericInteractor();
1✔
125
                interactor.execute(callable, immunizationRowCallableInteractorCallback);
1✔
126
            }
127
            return vaccineCards.get(vaccineName);
1✔
128
        } catch (Exception e) {
×
129
            Timber.e(e);
×
130
            return null;
×
131
        }
132
    }
133

134
    public GenericInteractor getGenericInteractor() {
135
        return mInteractor;
1✔
136
    }
137

138
    public ImmunizationRowCallableInteractorCallback getImmunizationRowCallableInteractor(ImmunizationRowCard vaccineCard, String vaccineName) {
139
        return new ImmunizationRowCallableInteractorCallback(vaccineCard, vaccineName);
1✔
140
    }
141

142

143
    public ImmunizationRowCard getImmunizationRowCard() {
144
        return new ImmunizationRowCard(context, editmode);
1✔
145
    }
146

147
    public void update(ArrayList<VaccineWrapper> vaccinesToUpdate) {
148
        if (vaccineCards != null) {
1✔
149
            if (vaccinesToUpdate == null) {// Update all vaccines
1✔
150
                for (ImmunizationRowCard curCard : vaccineCards.values()) {
1✔
151
                    if (curCard != null) curCard.updateState();
×
152
                }
1✔
153
            } else {// Update just the vaccines specified
154
                for (VaccineWrapper currWrapper : vaccinesToUpdate) {
1✔
155
                    if (vaccineCards.containsKey(currWrapper.getName())) {
1✔
156
                        vaccineCards.get(currWrapper.getName()).updateState();
×
157
                    }
158
                }
1✔
159
            }
160
        }
161
    }
1✔
162

163
    public ArrayList<VaccineWrapper> getDueVaccines() {
164
        ArrayList<VaccineWrapper> dueVaccines = new ArrayList<>();
1✔
165
        if (vaccineCards != null) {
1✔
166
            for (ImmunizationRowCard curCard : vaccineCards.values()) {
1✔
167
                if (curCard != null && (curCard.getState().equals(State.DUE)
×
168
                        || curCard.getState().equals(State.OVERDUE))) {
×
169
                    dueVaccines.add(curCard.getVaccineWrapper());
×
170
                }
171
            }
×
172
        }
173

174
        return dueVaccines;
1✔
175
    }
176

177
    public void updateWrapper(VaccineWrapper tag) {
178
        List<Vaccine> vaccineList = getVaccineList();
1✔
179

180
        if (!vaccineList.isEmpty()) {
1✔
181
            for (Vaccine vaccine : vaccineList) {
1✔
182
                if (tag.getName().toLowerCase().contains(vaccine.getName().toLowerCase()) && vaccine.getDate() != null) {
1✔
183

184
                    //Add exceptions
185
                    if (VaccinatorUtils.isSkippableVaccine(tag.getName()) && !tag.getName().equalsIgnoreCase(vaccine.getName())) {
1✔
186
                        continue;
×
187
                    }
188

189
                    long diff = vaccine.getUpdatedAt() - vaccine.getDate().getTime();
1✔
190
                    if (diff > 0 && TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS) > 1) {
1✔
191
                        tag.setUpdatedVaccineDate(new DateTime(vaccine.getDate()), false);
×
192
                    } else {
193
                        tag.setUpdatedVaccineDate(new DateTime(vaccine.getDate()), true);
1✔
194
                    }
195
                    tag.setDbKey(vaccine.getId());
1✔
196
                    tag.setSynced(vaccine.getSyncStatus() != null && vaccine.getSyncStatus()
1✔
197
                            .equals(VaccineRepository.TYPE_Synced));
1✔
198
                    if (tag.getName().contains("/")) {
1✔
199
                        String[] array = tag.getName().split("/");
1✔
200
                        if ((array[0]).toLowerCase().contains(vaccine.getName())) {
1✔
201
                            tag.setName(array[0]);
×
202
                        } else if ((array[1]).toLowerCase().contains(vaccine.getName())) {
1✔
203
                            tag.setName(array[1]);
×
204
                        }
205
                    }
206
                    tag.setCreatedAt(vaccine.getCreatedAt());
1✔
207
                }
208
            }
1✔
209
        }
210
    }
1✔
211

212
    public List<Vaccine> getVaccineList() {
213
        return vaccineList;
1✔
214
    }
215

216
    public void setVaccineList(List<Vaccine> vaccineList) {
217
        this.vaccineList = vaccineList;
×
218
    }
×
219

220
    public void updateWrapperStatus(ArrayList<VaccineWrapper> tags, CommonPersonObjectClient childDetails) {
221
        if (tags == null) {
1✔
222
            return;
×
223
        }
224

225
        for (VaccineWrapper tag : tags) {
1✔
226
            updateWrapperStatus(tag, childDetails);
1✔
227
        }
1✔
228
    }
1✔
229

230
    public void updateWrapperStatus(VaccineWrapper tag, CommonPersonObjectClient childDetails) {
231
        List<Vaccine> vaccineList = getVaccineList();
1✔
232

233
        List<Alert> alertList = getAlertList();
1✔
234
        Map<String, Date> recievedVaccines = receivedVaccines(vaccineList);
1✔
235
        String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
236
        DateTime dateTime = !dobString.isEmpty() ? new DateTime(dobString) : new DateTime();
1✔
237
        List<Map<String, Object>> sch = generateScheduleList("child", dateTime, recievedVaccines, alertList);
1✔
238

239
        for (Map<String, Object> m : sch) {
1✔
240
            VaccineRepo.Vaccine vaccine = (VaccineRepo.Vaccine) m.get("vaccine");
×
241
            if (tag.getName().toLowerCase().contains(vaccine.display().toLowerCase())) {
×
242

243
                //Add exceptions
244
                if (VaccinatorUtils.isSkippableVaccine(tag.getName()) && !tag.getName().equalsIgnoreCase(vaccine.display())) {
×
245
                    continue;
×
246
                }
247

248
                tag.setStatus(m.get("status").toString());
×
249
                tag.setAlert((Alert) m.get("alert"));
×
250

251
                updateVaccineDate(m, vaccine, tag, recievedVaccines);
×
252
            }
253
        }
×
254
    }
1✔
255

256
    protected void updateVaccineDate(Map<String, Object> m, VaccineRepo.Vaccine vaccine, VaccineWrapper tag, Map<String, Date> recievedVaccines) {
257
        if (m.get("status") != null
1✔
258
                && ((String) m.get("status")).equalsIgnoreCase("due")
1✔
259
                && vaccine.prerequisite() != null) {
1✔
260
            Date preReq = recievedVaccines.get(vaccine.prerequisite().display().toLowerCase(Locale.ENGLISH));
1✔
261
            if (preReq != null) {
1✔
262
                DateTime preReqDateTime = new DateTime(preReq);
1✔
263
                DateTime vaccineDate = preReqDateTime.plusDays(vaccine.prerequisiteGapDays());
1✔
264
                tag.setVaccineDate(vaccineDate);
1✔
265
            }
266
        }
267
    }
1✔
268

269
    public List<Alert> getAlertList() {
270
        return alertList;
1✔
271
    }
272

273
    public void setAlertList(List<Alert> alertList) {
274
        this.alertList = alertList;
×
275
    }
×
276

277
    class ImmunizationRowCallableInteractorCallback implements CallableInteractorCallBack<VaccineWrapper> {
278

279
        private ImmunizationRowCard vaccineCard;
280
        private String vaccineName;
281

282
        ImmunizationRowCallableInteractorCallback(ImmunizationRowCard vaccineCard, String vaccineName) {
1✔
283
            this.vaccineCard = vaccineCard;
1✔
284
            this.vaccineName = vaccineName;
1✔
285
        }
1✔
286

287
        @Override
288
        public void onResult(VaccineWrapper vaccineWrapper) {
289
            vaccineCard.setVaccineWrapper(vaccineWrapper);
×
290
            vaccineGroup.toggleRecordAllTV();
×
291
            if (vaccineWrapper.getStatus() == null) {
×
292
                removeVaccine(vaccineName);
×
293
            } else {
294
                notifyDataSetChanged();
×
295
            }
296
        }
×
297

298
        @Override
299
        public void onError(Exception ex) {
300
            Timber.e(ex);
×
301
        }
×
302
    }
303

304
    private void removeVaccine(String vaccineName) {
305
        vaccineCards.remove(vaccineName);
1✔
306
        for (int i = 0; i < vaccineGroup.getVaccineData().vaccines.size(); i++) {
1✔
307
            org.smartregister.immunization.domain.jsonmapping.Vaccine vaccine = vaccineGroup.getVaccineData().vaccines
1✔
308
                    .get(i);
1✔
309
            if (vaccine.getName().equalsIgnoreCase(vaccineName)) {
1✔
310
                vaccineGroup.getVaccineData().vaccines
1✔
311
                        .remove(i);
1✔
312
            }
313
        }
314
        notifyDataSetChanged();
1✔
315
        vaccineGroup.getVaccinesGV().invalidateViews();
1✔
316
    }
1✔
317
}
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