• 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

76.47
opensrp-immunization/src/main/java/org/smartregister/immunization/service/intent/RecurringIntentService.java
1
package org.smartregister.immunization.service.intent;
2

3
import android.app.IntentService;
4
import android.content.Intent;
5

6
import org.apache.commons.lang3.StringUtils;
7
import org.json.JSONArray;
8
import org.json.JSONException;
9
import org.json.JSONObject;
10
import org.smartregister.immunization.ImmunizationLibrary;
11
import org.smartregister.immunization.domain.ServiceRecord;
12
import org.smartregister.immunization.domain.ServiceType;
13
import org.smartregister.immunization.repository.RecurringServiceRecordRepository;
14
import org.smartregister.immunization.repository.RecurringServiceTypeRepository;
15
import org.smartregister.immunization.util.JsonFormUtils;
16

17
import java.text.SimpleDateFormat;
18
import java.util.List;
19

20
import timber.log.Timber;
21

22

23
/**
24
 * Created by keyman on 3/01/2017.
25
 */
26
public class RecurringIntentService extends IntentService {
27
    public static final String ITN_PROVIDED = "ITN_Provided";
28
    public static final String CHILD_HAS_NET = "Child_Has_Net";
29
    public static final String EVENT_TYPE = "Recurring Service";
30
    public static final String ENTITY_TYPE = "recurring_service";
31
    private static final String TAG = RecurringIntentService.class.getCanonicalName();
1✔
32
    private static final String EXCLUSIVE_BREASTFEEDING = "Exclusive breastfeeding";
33
    protected final String YES = "yes";
1✔
34
    protected final String NO = "no";
1✔
35
    protected final String VALUES = "values";
1✔
36
    protected final String OPENMRS_CHOICES_IDS = "openmrs_choice_ids";
1✔
37
    protected final String OPENMRS_YES = "1065AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1✔
38
    protected final String OPENMRS_NO = "1066AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1✔
39
    private RecurringServiceTypeRepository recurringServiceTypeRepository;
40
    private RecurringServiceRecordRepository recurringServiceRecordRepository;
41
    private ImmunizationLibrary immunizationLibrary;
42

43

44
    public RecurringIntentService() {
45
        super("RecurringService");
1✔
46
    }
1✔
47

48
    @Override
49
    public int onStartCommand(Intent intent, int flags, int startId) {
50
        recurringServiceTypeRepository = ImmunizationLibrary.getInstance().recurringServiceTypeRepository();
×
51
        recurringServiceRecordRepository = ImmunizationLibrary.getInstance().recurringServiceRecordRepository();
×
52
        immunizationLibrary = ImmunizationLibrary.getInstance();
×
53
        return super.onStartCommand(intent, flags, startId);
×
54
    }
55

56
    @Override
57
    protected void onHandleIntent(Intent intent) {
58

59
        String SELECT_DATA_TYPE = "coded";
1✔
60

61
        String CALC_ID = "1639AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1✔
62
        String CALCULATION_DATA_TYPE = "numeric";
1✔
63

64
        String CONCEPT = "concept";
1✔
65
        String ENCOUNTER = "encounter";
1✔
66

67
        String ENCOUNTER_DATE = "encounter_date";
1✔
68
        String DATE_DATA_TYPE = "date";
1✔
69

70
        try {
71
            List<ServiceRecord> serviceRecordList = immunizationLibrary.allowSyncImmediately() ?
1✔
72
                    recurringServiceRecordRepository.findUnSynced() :
1✔
73
                    recurringServiceRecordRepository.findUnSyncedBeforeTime((int) ImmunizationLibrary.getInstance().getVaccineSyncTime());
1✔
74
            if (!serviceRecordList.isEmpty()) {
1✔
75
                for (ServiceRecord serviceRecord : serviceRecordList) {
1✔
76

77
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
1✔
78
                    String formattedDate = simpleDateFormat.format(serviceRecord.getDate());
1✔
79

80
                    ServiceType serviceType = recurringServiceTypeRepository.find(serviceRecord.getRecurringServiceId());
1✔
81
                    if (serviceType == null) {
1✔
82
                        continue;
×
83
                    }
84

85
                    Integer calculation = null;
1✔
86
                    String calculationString = serviceType.getName().replace(serviceType.getType(), "");
1✔
87
                    if (StringUtils.isNotBlank(calculationString)) {
1✔
88
                        if (StringUtils.isNumeric(calculationString.trim())) {
×
89
                            calculation = Integer.valueOf(calculationString.trim());
×
90
                        } else if (StringUtils.containsIgnoreCase(calculationString, "ifc")) {
×
91
                            calculation = 0;
×
92
                        }
93
                    } else {
94
                        calculation = -1;
1✔
95
                    }
96

97
                    boolean itnHasNet = false;
1✔
98

99
                    String serviceRecordName = serviceType.getName().replace(" ", "_").toLowerCase();
1✔
100
                    JSONArray jsonArray = new JSONArray();
1✔
101

102

103
                    JSONObject jsonObject = new JSONObject();
1✔
104
                    jsonObject.put(JsonFormUtils.KEY, serviceRecordName);
1✔
105
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY, serviceType.getServiceNameEntity());
1✔
106
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY_ID, serviceType.getServiceNameEntityId());
1✔
107
                    jsonObject.put(JsonFormUtils.OPENMRS_DATA_TYPE, SELECT_DATA_TYPE);
1✔
108
                    jsonObject.put(JsonFormUtils.VALUE, YES);
1✔
109
                    if (serviceType.getType().equalsIgnoreCase("ITN") && StringUtils
1✔
110
                            .isNotBlank(serviceRecord.getValue()) && serviceRecord.getValue()
1✔
111
                            .equalsIgnoreCase(CHILD_HAS_NET)) {
1✔
112
                        jsonObject.put(JsonFormUtils.VALUE, NO);
×
113
                        itnHasNet = true;
×
114
                    }
115
                    if (serviceType.getType().equalsIgnoreCase(EXCLUSIVE_BREASTFEEDING) && StringUtils
1✔
116
                            .isNotBlank(serviceRecord.getValue()) && serviceRecord.getValue().equalsIgnoreCase(NO)) {
×
117
                        jsonObject.put(JsonFormUtils.VALUE, NO);
×
118
                    }
119
                    addYesNoChoices(jsonObject);
1✔
120
                    jsonArray.put(jsonObject);
1✔
121

122
                    jsonObject = new JSONObject();
1✔
123
                    jsonObject.put(JsonFormUtils.KEY, serviceRecordName + "_dose");
1✔
124
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY, CONCEPT);
1✔
125
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY_ID, CALC_ID);
1✔
126
                    jsonObject.put(JsonFormUtils.OPENMRS_DATA_TYPE, CALCULATION_DATA_TYPE);
1✔
127
                    jsonObject.put(JsonFormUtils.VALUE, calculation);
1✔
128
                    jsonArray.put(jsonObject);
1✔
129

130
                    if (!(serviceType.getDateEntity().equalsIgnoreCase(ENCOUNTER) && serviceType.getDateEntityId()
1✔
131
                            .equalsIgnoreCase(ENCOUNTER_DATE))) {
×
132
                        jsonObject = new JSONObject();
1✔
133
                        jsonObject.put(JsonFormUtils.KEY, serviceRecordName + "_date");
1✔
134
                        jsonObject.put(JsonFormUtils.OPENMRS_ENTITY, CONCEPT);
1✔
135
                        jsonObject.put(JsonFormUtils.OPENMRS_ENTITY_ID, serviceType.getDateEntityId());
1✔
136
                        jsonObject.put(JsonFormUtils.OPENMRS_DATA_TYPE, DATE_DATA_TYPE);
1✔
137
                        jsonObject.put(JsonFormUtils.VALUE, formattedDate);
1✔
138
                        jsonArray.put(jsonObject);
1✔
139
                    }
140

141
                    if (itnHasNet) {
1✔
142
                        jsonObject = new JSONObject();
×
143
                        jsonObject.put(JsonFormUtils.KEY, CHILD_HAS_NET);
×
144
                        jsonObject.put(JsonFormUtils.OPENMRS_ENTITY, "");
×
145
                        jsonObject.put(JsonFormUtils.OPENMRS_ENTITY_ID, "");
×
146
                        jsonObject.put(JsonFormUtils.OPENMRS_DATA_TYPE, SELECT_DATA_TYPE);
×
147
                        jsonObject.put(JsonFormUtils.VALUE, YES);
×
148
                        addYesNoChoices(jsonObject);
×
149
                        jsonArray.put(jsonObject);
×
150
                    }
151

152
                    JsonFormUtils
1✔
153
                            .createServiceEvent(getApplicationContext(), serviceRecord, EVENT_TYPE, ENTITY_TYPE, jsonArray);
1✔
154
                    recurringServiceRecordRepository.close(serviceRecord.getId());
1✔
155
                }
1✔
156
            }
157
        } catch (Exception e) {
1✔
158
            Timber.e(e);
1✔
159
        }
1✔
160
    }
1✔
161

162
    private void addYesNoChoices(JSONObject jsonObject) {
163
        try {
164
            JSONArray valuesArray = new JSONArray();
1✔
165
            valuesArray.put(YES);
1✔
166
            valuesArray.put(NO);
1✔
167

168
            jsonObject.put(VALUES, valuesArray);
1✔
169

170
            JSONObject choices = new JSONObject();
1✔
171
            choices.put(YES, OPENMRS_YES);
1✔
172
            choices.put(NO, OPENMRS_NO);
1✔
173

174
            jsonObject.put(OPENMRS_CHOICES_IDS, choices);
1✔
175

176
        } catch (JSONException e) {
×
177
            Timber.e(e);
×
178
        }
1✔
179
    }
1✔
180
}
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