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

OpenSRP / opensrp-client-immunization / #901

pending completion
#901

push

github-actions

web-flow
Merge pull request #191 from opensrp/issue189-2

Migrate core dependencies

5141 of 6670 relevant lines covered (77.08%)

0.77 hits per line

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

86.25
opensrp-immunization/src/main/java/org/smartregister/immunization/service/intent/VaccineIntentService.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.JSONObject;
9
import org.smartregister.immunization.ImmunizationLibrary;
10
import org.smartregister.immunization.domain.Vaccine;
11
import org.smartregister.immunization.domain.jsonmapping.VaccineGroup;
12
import org.smartregister.immunization.repository.VaccineRepository;
13
import org.smartregister.immunization.util.JsonFormUtils;
14
import org.smartregister.immunization.util.VaccinatorUtils;
15

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

20
import timber.log.Timber;
21

22
/**
23
 * Created by keyman on 3/01/2017.
24
 */
25
public class VaccineIntentService extends IntentService {
26
    public static final String EVENT_TYPE = "Vaccination";
27
    public static final String EVENT_TYPE_OUT_OF_CATCHMENT = "Out of Area Service - Vaccination";
28
    public static final String EVENT_TYPE_IS_VOIDED = "Void Event";
29
    public static final String ENTITY_TYPE = "vaccination";
30
    private static final String TAG = VaccineIntentService.class.getCanonicalName();
1✔
31
    private VaccineRepository vaccineRepository;
32
    private List<VaccineGroup> availableVaccines;
33
    private List<org.smartregister.immunization.domain.jsonmapping.Vaccine> specialVaccines;
34
    private ImmunizationLibrary immunizationLibrary;
35

36

37
    public VaccineIntentService() {
38
        super("VaccineService");
1✔
39
    }
1✔
40

41
    private String getParentId(String vaccineName) {
42
        String name = vaccineName;
1✔
43
        String parentEntityId = "";
1✔
44
        if (availableVaccines != null && !availableVaccines.isEmpty()) {
1✔
45
            for (VaccineGroup vaccineGroup : availableVaccines) {
1✔
46
                for (org.smartregister.immunization.domain.jsonmapping.Vaccine vaccine : vaccineGroup.vaccines) {
1✔
47
                    if (StringUtils.containsIgnoreCase(vaccine.name, name)) {
1✔
48
                        return getParentId(vaccine, name);
1✔
49
                    }
50
                }
×
51
            }
×
52
        }
53

54
        if (StringUtils.isBlank(parentEntityId) && specialVaccines != null && !specialVaccines.isEmpty()) {
1✔
55
            for (org.smartregister.immunization.domain.jsonmapping.Vaccine vaccine : specialVaccines) {
1✔
56
                if (StringUtils.containsIgnoreCase(vaccine.name, name)) {
1✔
57
                    return getParentId(vaccine, name);
1✔
58
                }
59
            }
1✔
60
        }
61

62
        if (StringUtils.isBlank(parentEntityId) && StringUtils.containsWhitespace(name)) {
1✔
63
            name = name.split("\\s+")[0];
1✔
64
            return getParentId(name);
1✔
65
        }
66
        return parentEntityId;
1✔
67
    }
68

69
    private String getParentId(org.smartregister.immunization.domain.jsonmapping.Vaccine vaccine, String name) {
70
        String parentEntityId = vaccine.openmrs_date.parent_entity;
1✔
71
        if (parentEntityId.contains("/")) {
1✔
72
            String[] parentEntityArray = parentEntityId.split("/");
1✔
73
            if (StringUtils.containsIgnoreCase(name, "measles")) {
1✔
74
                parentEntityId = parentEntityArray[0];
×
75
            } else if (StringUtils.containsIgnoreCase(name, "mr")) {
1✔
76
                parentEntityId = parentEntityArray[1];
×
77
            }
78
        }
79
        return parentEntityId;
1✔
80
    }
81

82
    @Override
83
    public int onStartCommand(Intent intent, int flags, int startId) {
84
        vaccineRepository = ImmunizationLibrary.getInstance().vaccineRepository();
×
85
        immunizationLibrary = ImmunizationLibrary.getInstance();
×
86
        return super.onStartCommand(intent, flags, startId);
×
87
    }
88

89
    @Override
90
    protected void onHandleIntent(Intent intent) {
91
        if (availableVaccines == null) {
1✔
92
            availableVaccines = VaccinatorUtils.getSupportedVaccines(getBaseContext());
1✔
93
            specialVaccines = VaccinatorUtils.getSpecialVaccines(getBaseContext());
1✔
94
        }
95

96
        String entityId = "1410AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1✔
97
        String calId = "1418AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1✔
98
        String dateDataType = "date";
1✔
99
        String calculationDataType = "calculate";
1✔
100
        String concept = "concept";
1✔
101

102
        try {
103
            List<Vaccine> vaccines = immunizationLibrary.allowSyncImmediately() ?
1✔
104
                    vaccineRepository.findUnSynced() :
×
105
                    vaccineRepository.findUnSyncedBeforeTime((int) ImmunizationLibrary.getInstance().getVaccineSyncTime());
1✔
106
            if (!vaccines.isEmpty()) {
1✔
107
                for (Vaccine vaccine : vaccines) {
1✔
108

109
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
1✔
110
                    String formattedDate = simpleDateFormat.format(vaccine.getDate());
1✔
111

112
                    JSONArray jsonArray = new JSONArray();
1✔
113

114
                    String vaccineName = vaccine.getName().replace(" ", "_");
1✔
115

116
                    JSONObject jsonObject = new JSONObject();
1✔
117
                    jsonObject.put(JsonFormUtils.KEY, vaccineName);
1✔
118
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY, concept);
1✔
119
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY_ID, entityId);
1✔
120
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY_PARENT, getParentId(vaccine.getName()));
1✔
121
                    jsonObject.put(JsonFormUtils.OPENMRS_DATA_TYPE, dateDataType);
1✔
122
                    jsonObject.put(JsonFormUtils.VALUE, formattedDate);
1✔
123
                    jsonArray.put(jsonObject);
1✔
124

125
                    if (vaccine.getCalculation() == null || vaccine.getCalculation() < 0) {
1✔
126
                        vaccine.setCalculation(1);
×
127
                    }
128

129
                    jsonObject = new JSONObject();
1✔
130
                    jsonObject.put(JsonFormUtils.KEY, vaccineName + "_dose");
1✔
131
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY, concept);
1✔
132
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY_ID, calId);
1✔
133
                    jsonObject.put(JsonFormUtils.OPENMRS_ENTITY_PARENT, getParentId(vaccine.getName()));
1✔
134
                    jsonObject.put(JsonFormUtils.OPENMRS_DATA_TYPE, calculationDataType);
1✔
135
                    jsonObject.put(JsonFormUtils.VALUE, vaccine.getCalculation());
1✔
136
                    jsonArray.put(jsonObject);
1✔
137

138
                    JsonFormUtils.createVaccineEvent(vaccine, getEventType(), getEntityType(), jsonArray, ImmunizationLibrary.getInstance().context());
1✔
139
                    //log out of catchment service since this is required in some of the hia2 report indicators
140
                    if (vaccine.getBaseEntityId() == null || vaccine.getBaseEntityId().isEmpty() || new Integer(1).equals(vaccine.getOutOfCatchment())) {
1✔
141
                        JsonFormUtils.createVaccineEvent(vaccine, getEventTypeOutOfCatchment(), getEntityType(), jsonArray, ImmunizationLibrary.getInstance().context());
×
142
                    }
143

144
                    vaccineRepository.close(vaccine.getId());
1✔
145
                }
1✔
146
            }
147
        } catch (Exception e) {
1✔
148
            Timber.e(e);
1✔
149
        }
1✔
150
    }
1✔
151

152
    protected String getEventType() {
153
        return EVENT_TYPE;
1✔
154
    }
155

156
    protected String getEventTypeOutOfCatchment() {
157
        return EVENT_TYPE_OUT_OF_CATCHMENT;
1✔
158
    }
159

160
    protected String getEventTypeIsVoided() {
161
        return EVENT_TYPE_IS_VOIDED;
×
162
    }
163

164
    protected String getEntityType() {
165
        return ENTITY_TYPE;
1✔
166
    }
167
}
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