• 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

82.65
opensrp-immunization/src/main/java/org/smartregister/immunization/adapter/ServiceRowAdapter.java
1
package org.smartregister.immunization.adapter;
2

3
import static org.smartregister.immunization.util.VaccinatorUtils.generateScheduleList;
4
import static org.smartregister.util.Utils.getName;
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.domain.ServiceRecord;
19
import org.smartregister.immunization.domain.ServiceType;
20
import org.smartregister.immunization.domain.ServiceWrapper;
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.ServiceRowCard;
25
import org.smartregister.immunization.view.ServiceRowGroup;
26
import org.smartregister.util.Utils;
27

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

37
import timber.log.Timber;
38

39
/**
40
 * Created by keyman on 15/05/2017.
41
 */
42
public class ServiceRowAdapter extends BaseAdapter {
43
    private static final String TAG = "ServiceRowAdapter";
44
    private final Context context;
45
    private final ServiceRowGroup serviceRowGroup;
46
    public boolean editmode;
47
    private HashMap<String, ServiceRowCard> serviceRowCards;
48
    private List<ServiceType> serviceTypeList;
49
    private List<ServiceRecord> serviceRecordList;
50
    private List<Alert> alertList;
51

52
    public ServiceRowAdapter(Context context, ServiceRowGroup serviceRowGroup, boolean editmode,
53
                             List<ServiceType> serviceTypeList, List<ServiceRecord> serviceRecordList,
54
                             List<Alert> alertList) {
1✔
55
        this.context = context;
1✔
56
        this.editmode = editmode;
1✔
57
        this.serviceRowGroup = serviceRowGroup;
1✔
58
        this.serviceTypeList = serviceTypeList;
1✔
59
        this.serviceRecordList = serviceRecordList;
1✔
60
        this.alertList = alertList;
1✔
61
        serviceRowCards = new LinkedHashMap<>();
1✔
62
    }
1✔
63

64
    @Override
65
    public int getCount() {
66
        List<ServiceType> types = serviceRowGroup.getServiceTypes();
1✔
67
        if (types == null || types.isEmpty()) {
1✔
68
            return 0;
×
69
        }
70
        return types.size();
1✔
71
    }
72

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

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

83
    @Override
84
    public View getView(int position, View convertView, ViewGroup parent) {
85
        try {
86
            ServiceType serviceType = serviceRowGroup.getServiceTypes().get(position);
1✔
87
            if (!serviceRowCards.containsKey(serviceType.getName())) {
1✔
88
                ServiceRowCard serviceRowCard = new ServiceRowCard(context, editmode);
1✔
89
                serviceRowCard.setId((int) getItemId(position));
1✔
90
                serviceRowCards.put(serviceType.getName(), serviceRowCard);
1✔
91

92
                ServiceRowTask serviceRowTask = new ServiceRowTask(serviceRowGroup.getChildDetails()
1✔
93
                        , serviceType, serviceRowCard);
94
                Utils.startAsyncTask(serviceRowTask, null);
1✔
95
            }
96

97
            return serviceRowCards.get(serviceType.getName());
1✔
98
        } catch (Exception e) {
×
99
            Timber.e(e);
×
100
            return null;
×
101
        }
102
    }
103

104
    public void update(ArrayList<ServiceWrapper> servicesToUpdate) {
105
        if (serviceRowCards != null) {
1✔
106
            if (servicesToUpdate == null) {// Update all vaccines
1✔
107
                for (ServiceRowCard curCard : serviceRowCards.values()) {
1✔
108
                    if (curCard != null) curCard.updateState();
×
109
                }
1✔
110
            } else {// Update just the vaccines specified
111
                for (ServiceWrapper currWrapper : servicesToUpdate) {
×
112
                    if (serviceRowCards.containsKey(currWrapper.getName())) {
×
113
                        serviceRowCards.get(currWrapper.getName()).updateState();
×
114
                    }
115
                }
×
116
            }
117
        }
118
    }
1✔
119

120
    public void updateWrapperStatus(ServiceWrapper tag, CommonPersonObjectClient childDetails) {
121

122
        List<ServiceType> serviceTypes = getServiceTypes();
1✔
123

124
        List<ServiceRecord> serviceRecordList = getServiceRecordList();
1✔
125

126
        List<Alert> alertList = getAlertList();
1✔
127

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

130
        String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
131
        List<Map<String, Object>> sch = generateScheduleList(serviceTypes, new DateTime(dobString), receivedServices,
1✔
132
                alertList);
133

134

135
        for (Map<String, Object> m : sch) {
1✔
136
            ServiceType serviceType = (ServiceType) m.get("service");
1✔
137
            if (tag.getName().equalsIgnoreCase(serviceType.getName())) {
1✔
138
                tag.setStatus(m.get("status").toString());
×
139
                tag.setAlert((Alert) m.get("alert"));
×
140
                tag.setServiceType(serviceType);
×
141
                tag.setVaccineDate((DateTime) m.get("date"));
×
142
            }
143
        }
1✔
144
    }
1✔
145

146
    public List<ServiceType> getServiceTypes() {
147
        return serviceTypeList;
1✔
148
    }
149

150
    public List<ServiceRecord> getServiceRecordList() {
151
        return serviceRecordList;
1✔
152
    }
153

154
    public List<Alert> getAlertList() {
155
        return alertList;
1✔
156
    }
157

158
    public void updateWrapper(ServiceWrapper tag) {
159
        List<ServiceRecord> serviceRecordList = getServiceRecordList();
1✔
160

161
        if (!serviceRecordList.isEmpty()) {
1✔
162
            for (ServiceRecord serviceRecord : serviceRecordList) {
1✔
163
                if (tag.getName().equalsIgnoreCase(serviceRecord.getName()) && serviceRecord
1✔
164
                        .getDate() != null) {
1✔
165
                    long diff = serviceRecord.getUpdatedAt() - serviceRecord.getDate().getTime();
1✔
166
                    if (diff > 0 && TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS) > 1) {
1✔
167
                        tag.setUpdatedVaccineDate(new DateTime(serviceRecord.getDate()), false);
×
168
                    } else {
169
                        tag.setUpdatedVaccineDate(new DateTime(serviceRecord.getDate()), true);
1✔
170
                    }
171
                    tag.setDbKey(serviceRecord.getId());
1✔
172
                    tag.setSynced(serviceRecord.getSyncStatus() != null && serviceRecord.getSyncStatus()
1✔
173
                            .equals(VaccineRepository.TYPE_Synced));
1✔
174
                    tag.setCreatedAt(serviceRecord.getCreatedAt());
1✔
175
                }
176
            }
1✔
177
        }
178

179
    }
1✔
180

181
    class ServiceRowTask extends AsyncTask<Void, Void, ServiceWrapper> {
182

183
        private CommonPersonObjectClient childDetails;
184

185
        private ServiceType serviceType;
186

187
        private ServiceRowCard serviceRowCard;
188

189
        ServiceRowTask(CommonPersonObjectClient childDetails, ServiceType serviceType, ServiceRowCard serviceRowCard) {
1✔
190
            this.childDetails = childDetails;
1✔
191
            this.serviceType = serviceType;
1✔
192
            this.serviceRowCard = serviceRowCard;
1✔
193
        }
1✔
194

195
        @Override
196
        protected ServiceWrapper doInBackground(Void... params) {
197
            ServiceWrapper serviceWrapper = new ServiceWrapper();
1✔
198
            serviceWrapper.setId(childDetails.entityId());
1✔
199
            serviceWrapper.setGender(childDetails.getDetails().get("gender"));
1✔
200
            serviceWrapper.setDefaultName(serviceType.getName());
1✔
201

202
            String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
203
            if (StringUtils.isNotBlank(dobString)) {
1✔
204
                Calendar dobCalender = Calendar.getInstance();
1✔
205
                DateTime dateTime = new DateTime(dobString);
1✔
206
                dobCalender.setTime(dateTime.toDate());
1✔
207
                serviceWrapper.setDob(new DateTime(dobCalender.getTime()));
1✔
208
            }
209

210
            Photo photo = ImageUtils.profilePhotoByClient(childDetails);
1✔
211
            serviceWrapper.setPhoto(photo);
1✔
212

213
            String zeirId = getValue(childDetails.getColumnmaps(), "zeir_id", false);
1✔
214
            serviceWrapper.setPatientNumber(zeirId);
1✔
215

216
            String firstName = getValue(childDetails.getColumnmaps(), "first_name", true);
1✔
217
            String lastName = getValue(childDetails.getColumnmaps(), "last_name", true);
1✔
218
            String childName = getName(firstName, lastName);
1✔
219
            serviceWrapper.setPatientName(childName.trim());
1✔
220

221
            updateWrapperStatus(serviceWrapper, childDetails);
1✔
222
            updateWrapper(serviceWrapper);
1✔
223

224
            return serviceWrapper;
1✔
225
        }
226

227
        @Override
228
        protected void onPostExecute(ServiceWrapper serviceWrapper) {
229
            serviceRowCard.setServiceWrapper(serviceWrapper);
×
230
            notifyDataSetChanged();
×
231
        }
×
232
    }
233

234
}
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