• 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

78.05
opensrp-immunization/src/main/java/org/smartregister/immunization/adapter/ServiceCardAdapter.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.nextServiceDue;
5
import static org.smartregister.util.Utils.getName;
6
import static org.smartregister.util.Utils.getValue;
7

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

14
import org.apache.commons.lang3.StringUtils;
15
import org.joda.time.DateTime;
16
import org.smartregister.commonregistry.CommonPersonObjectClient;
17
import org.smartregister.domain.Alert;
18
import org.smartregister.domain.Photo;
19
import org.smartregister.immunization.domain.ServiceRecord;
20
import org.smartregister.immunization.domain.ServiceType;
21
import org.smartregister.immunization.domain.ServiceWrapper;
22
import org.smartregister.immunization.repository.RecurringServiceRecordRepository;
23
import org.smartregister.immunization.repository.VaccineRepository;
24
import org.smartregister.immunization.util.ImageUtils;
25
import org.smartregister.immunization.util.VaccinatorUtils;
26
import org.smartregister.immunization.view.ServiceCard;
27
import org.smartregister.immunization.view.ServiceGroup;
28
import org.smartregister.util.Utils;
29

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

39
import timber.log.Timber;
40

41
/**
42
 * Created by keyman on 15/05/2017.
43
 */
44
public class ServiceCardAdapter extends BaseAdapter {
45
    private static final String TAG = "ServiceCardAdapter";
46
    private final Context context;
47
    private final ServiceGroup serviceGroup;
48
    private HashMap<String, ServiceCard> serviceCards;
49
    private List<ServiceRecord> serviceRecordList;
50
    private List<Alert> alertList;
51
    private Map<String, List<ServiceType>> serviceTypeMap;
52

53
    private boolean isChildActive = true;
1✔
54

55
    public ServiceCardAdapter(Context context, ServiceGroup serviceGroup, List<ServiceRecord> serviceRecordList,
56
                              List<Alert> alertList, Map<String, List<ServiceType>> serviceTypeMap) {
1✔
57
        this.context = context;
1✔
58
        this.serviceGroup = serviceGroup;
1✔
59
        this.serviceRecordList = serviceRecordList;
1✔
60
        this.alertList = alertList;
1✔
61
        this.serviceTypeMap = serviceTypeMap;
1✔
62
        serviceCards = new HashMap<>();
1✔
63
    }
1✔
64

65
    public void updateAll() {
66
        if (serviceCards != null) {
1✔
67
            // Update all vaccines
68
            for (ServiceCard curCard : serviceCards.values()) {
1✔
69
                if (curCard != null) curCard.updateState();
1✔
70
            }
1✔
71
        }
72

73
        visibilityCheck();
1✔
74
    }
1✔
75

76
    public void visibilityCheck() {
77
        // if all cards have been updated
78
        if (getCount() == serviceCards.size()) {
1✔
79
            if (atLeastOneVisibleCard()) {
1✔
80
                serviceGroup.post(new Runnable() {
1✔
81
                    @Override
82
                    public void run() {
83
                        serviceGroup.setVisibility(View.VISIBLE);
1✔
84
                    }
1✔
85
                });
86
            } else {
87
                serviceGroup.post(new Runnable() {
×
88
                    @Override
89
                    public void run() {
90
                        serviceGroup.setVisibility(View.VISIBLE);
×
91
                    }
×
92
                });
93
            }
94
        }
95
    }
1✔
96

97
    @Override
98
    public int getCount() {
99
        List<String> types = serviceGroup.getServiceTypeKeys();
1✔
100
        if (types == null || types.isEmpty()) {
1✔
101
            return 0;
×
102
        }
103
        return types.size();
1✔
104
    }
105

106
    @Override
107
    public Object getItem(int position) {
108
        return serviceCards.get(position);
1✔
109
    }
110

111
    @Override
112
    public long getItemId(int position) {
113
        return 231231 + position;
1✔
114
    }
115

116
    @Override
117
    public View getView(int position, View convertView, ViewGroup parent) {
118
        try {
119
            String type = serviceGroup.getServiceTypeKeys().get(position);
1✔
120
            if (!serviceCards.containsKey(type)) {
1✔
121
                ServiceCard serviceCard = new ServiceCard(context);
1✔
122
                serviceCard.setChildActive(isChildActive);
1✔
123
                serviceCard.setId((int) getItemId(position));
1✔
124
                serviceCards.put(type, serviceCard);
1✔
125

126
                ServiceCardTask serviceRowTask = new ServiceCardTask(serviceCard, serviceGroup.getChildDetails(), type);
1✔
127
                Utils.startAsyncTask(serviceRowTask, null);
1✔
128
            }
129

130
            return serviceCards.get(type);
1✔
131
        } catch (Exception e) {
×
132
            Timber.e(e);
×
133
            return null;
×
134
        }
135

136
    }
137

138
    public boolean atLeastOneVisibleCard() {
139
        if (serviceCards != null) {
1✔
140
            for (ServiceCard serviceCard : serviceCards.values()) {
1✔
141
                if (serviceCard.getVisibility() == View.VISIBLE) {
1✔
142
                    return true;
1✔
143
                }
144
            }
×
145
        }
146
        return false;
×
147
    }
148

149
    public void updateChildsActiveStatus() {
150
        if (serviceCards != null) {
1✔
151
            // Update all vaccines
152
            for (ServiceCard curCard : serviceCards.values()) {
1✔
153
                if (curCard != null) {
1✔
154
                    curCard.setChildActive(isChildActive);
1✔
155
                    curCard.updateChildsActiveStatus();
1✔
156
                }
157
            }
1✔
158
        }
159
    }
1✔
160

161
    public void setChildActive(boolean isChildActive) {
162
        this.isChildActive = isChildActive;
1✔
163
    }
1✔
164

165
    public void updateWrapperStatus(ArrayList<ServiceWrapper> tags, CommonPersonObjectClient childDetails) {
166
        if (tags == null) {
1✔
167
            return;
×
168
        }
169

170
        for (ServiceWrapper tag : tags) {
1✔
171
            updateWrapperStatus(tag.getDefaultName(), tag, childDetails);
1✔
172
        }
1✔
173
    }
1✔
174

175
    public void updateWrapperStatus(String type, ServiceWrapper tag, CommonPersonObjectClient childDetails) {
176

177
        List<ServiceType> serviceTypes = getServiceTypeMap().get(type);
1✔
178

179
        List<ServiceRecord> serviceRecordList = new ArrayList<>();
1✔
180
        for (ServiceRecord serviceRecord : getServiceRecordList()) {
1✔
181
            //if (serviceRecord.getRecurringServiceId().equals(tag.getTypeId())) {
182
            //if (serviceRecord.getName().equalsIgnoreCase(tag.getDefaultName())) {
183
            serviceRecordList.add(serviceRecord);
1✔
184
            //}
185
        }
1✔
186

187
        List<Alert> alertList = getAlertList();
1✔
188

189
        Map<String, Date> receivedServices = VaccinatorUtils.receivedServices(serviceRecordList);
1✔
190

191
        String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
192
        List<Map<String, Object>> sch = generateScheduleList(serviceTypes, new DateTime(dobString), receivedServices,
1✔
193
                alertList);
194

195

196
        Map<String, Object> nv = null;
1✔
197
        if (serviceRecordList.isEmpty()) {
1✔
198
            nv = nextServiceDue(sch, serviceTypes);
1✔
199
        } else {
200
            ServiceRecord lastServiceRecord = null;
1✔
201
            for (ServiceRecord serviceRecord : serviceRecordList) {
1✔
202
                if (serviceRecord.getSyncStatus().equalsIgnoreCase(RecurringServiceRecordRepository.TYPE_Unsynced)) {
1✔
203
                    lastServiceRecord = serviceRecord;
×
204
                }
205
            }
1✔
206

207
            if (lastServiceRecord != null) {
1✔
208
                nv = nextServiceDue(sch, lastServiceRecord);
×
209
            }
210
        }
211

212
        if (nv == null) {
1✔
213
            Date lastVaccine = null;
1✔
214
            if (!serviceRecordList.isEmpty()) {
1✔
215
                ServiceRecord serviceRecord = serviceRecordList.get(serviceRecordList.size() - 1);
1✔
216
                lastVaccine = serviceRecord.getDate();
1✔
217
            }
218

219
            nv = nextServiceDue(sch, lastVaccine);
1✔
220
        }
221

222
        if (nv != null) {
1✔
223
            ServiceType nextServiceType = (ServiceType) nv.get("service");
×
224
            tag.setStatus(nv.get("status").toString());
×
225
            tag.setAlert((Alert) nv.get("alert"));
×
226
            if (nv.get("date") != null && nv.get("date") instanceof DateTime) {
×
227
                tag.setVaccineDate((DateTime) nv.get("date"));
×
228
            }
229
            tag.setServiceType(nextServiceType);
×
230
        }
231
    }
1✔
232

233
    public Map<String, List<ServiceType>> getServiceTypeMap() {
234
        if (serviceTypeMap == null) {
1✔
235
            serviceTypeMap = new LinkedHashMap<>();
×
236
        }
237
        return serviceTypeMap;
1✔
238
    }
239

240
    public List<ServiceRecord> getServiceRecordList() {
241
        if (serviceRecordList == null) {
1✔
242
            serviceRecordList = new ArrayList<>();
×
243
        }
244
        return serviceRecordList;
1✔
245
    }
246

247
    public List<Alert> getAlertList() {
248
        return alertList;
1✔
249
    }
250

251
    public void updateAllWrapperStatus(CommonPersonObjectClient childDetails) {
252

253
        List<ServiceWrapper> tags = allWrappers();
×
254
        if (tags == null) {
×
255
            return;
×
256
        }
257

258
        for (ServiceWrapper tag : tags) {
×
259
            updateWrapperStatus(tag.getDefaultName(), tag, childDetails);
×
260
        }
×
261
    }
×
262

263
    public List<ServiceWrapper> allWrappers() {
264
        List<ServiceWrapper> serviceWrappers = new ArrayList<>();
1✔
265
        if (serviceCards != null) {
1✔
266
            for (ServiceCard serviceCard : serviceCards.values()) {
1✔
267
                serviceWrappers.add(serviceCard.getServiceWrapper());
1✔
268
            }
1✔
269
        }
270
        return serviceWrappers;
1✔
271
    }
272

273
    public void updateWrapper(ServiceWrapper tag) {
274
        List<ServiceRecord> serviceRecordList = getServiceRecordList();
1✔
275

276
        if (!serviceRecordList.isEmpty()) {
1✔
277
            for (ServiceRecord serviceRecord : serviceRecordList) {
1✔
278
                if (tag.getName().toLowerCase().contains(serviceRecord.getName().toLowerCase()) && serviceRecord
1✔
279
                        .getDate() != null) {
1✔
280
                    long diff = serviceRecord.getUpdatedAt() - serviceRecord.getDate().getTime();
1✔
281
                    if (diff > 0 && TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS) > 1) {
1✔
282
                        tag.setUpdatedVaccineDate(new DateTime(serviceRecord.getDate()), false);
×
283
                    } else {
284
                        tag.setUpdatedVaccineDate(new DateTime(serviceRecord.getDate()), true);
1✔
285
                    }
286
                    tag.setDbKey(serviceRecord.getId());
1✔
287
                    tag.setSynced(serviceRecord.getSyncStatus() != null && serviceRecord.getSyncStatus()
1✔
288
                            .equals(VaccineRepository.TYPE_Synced));
1✔
289
                }
290
            }
1✔
291
        }
292

293
    }
1✔
294

295
    class ServiceCardTask extends AsyncTask<Void, Void, ServiceWrapper> {
296

297
        private ServiceCard serviceCard;
298

299
        private CommonPersonObjectClient childDetails;
300

301
        private String type;
302

303
        ServiceCardTask(ServiceCard serviceCard, CommonPersonObjectClient childDetails, String type) {
1✔
304
            this.serviceCard = serviceCard;
1✔
305
            this.childDetails = childDetails;
1✔
306
            this.type = type;
1✔
307
        }
1✔
308

309
        @Override
310
        protected ServiceWrapper doInBackground(Void... params) {
311
            ServiceWrapper serviceWrapper = new ServiceWrapper();
1✔
312
            serviceWrapper.setId(childDetails.entityId());
1✔
313
            serviceWrapper.setGender(childDetails.getDetails().get("gender"));
1✔
314
            serviceWrapper.setDefaultName(type);
1✔
315

316
            String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
317
            if (StringUtils.isNotBlank(dobString)) {
1✔
318
                Calendar dobCalender = Calendar.getInstance();
1✔
319
                DateTime dateTime = new DateTime(dobString);
1✔
320
                dobCalender.setTime(dateTime.toDate());
1✔
321
                serviceWrapper.setDob(new DateTime(dobCalender.getTime()));
1✔
322
            }
323

324
            Photo photo = ImageUtils.profilePhotoByClient(childDetails);
1✔
325
            serviceWrapper.setPhoto(photo);
1✔
326

327
            String zeirId = getValue(childDetails.getColumnmaps(), "zeir_id", false);
1✔
328
            serviceWrapper.setPatientNumber(zeirId);
1✔
329

330
            String firstName = getValue(childDetails.getColumnmaps(), "first_name", true);
1✔
331
            String lastName = getValue(childDetails.getColumnmaps(), "last_name", true);
1✔
332
            String childName = getName(firstName, lastName);
1✔
333
            serviceWrapper.setPatientName(childName.trim());
1✔
334

335
            updateWrapperStatus(type, serviceWrapper, childDetails);
1✔
336
            updateWrapper(serviceWrapper);
1✔
337

338
            return serviceWrapper;
1✔
339
        }
340

341
        @Override
342
        protected void onPostExecute(ServiceWrapper serviceWrapper) {
343
            serviceCard.setServiceWrapper(serviceWrapper);
×
344
            visibilityCheck();
×
345
            notifyDataSetChanged();
×
346
        }
×
347
    }
348

349
    public void updateServiceRecordList(List<ServiceRecord> serviceRecordList) {
350
        this.serviceRecordList = serviceRecordList;
×
351
    }
×
352

353
    public void updateAlertList(List<Alert> alertList) {
354
        this.alertList = alertList;
×
355
    }
×
356
}
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