• 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

77.27
opensrp-immunization/src/main/java/org/smartregister/immunization/util/JsonFormUtils.java
1
package org.smartregister.immunization.util;
2

3
import android.content.Context;
4

5
import org.apache.commons.lang3.StringUtils;
6
import org.json.JSONArray;
7
import org.json.JSONException;
8
import org.json.JSONObject;
9
import org.smartregister.AllConstants;
10
import org.smartregister.clientandeventmodel.Event;
11
import org.smartregister.domain.Observation;
12
import org.smartregister.immunization.ImmunizationLibrary;
13
import org.smartregister.immunization.domain.ServiceRecord;
14
import org.smartregister.immunization.domain.Vaccine;
15
import org.smartregister.repository.EventClientRepository;
16

17
import java.util.Date;
18

19
import timber.log.Timber;
20

21
/**
22
 * Created by keyman on 31/07/2017.
23
 */
24
public class JsonFormUtils extends org.smartregister.util.JsonFormUtils {
1✔
25

26
    /**
27
     * This createVaccineEvent method is deprecated, use {@link #createVaccineEvent(Vaccine vaccine, String eventType, String entityType, JSONArray fields, org.smartregister.Context context)} instead which adds application version name.
28
     */
29
    @Deprecated
30
    public static void createVaccineEvent(Context context, Vaccine vaccine, String eventType, String entityType, JSONArray fields) {
31

32
        org.smartregister.Context openSRPContext = ImmunizationLibrary.getInstance().context();
×
33
        createVaccineEvent(vaccine, eventType, entityType, fields, openSRPContext);
×
34
    }
×
35

36
    public static void createVaccineEvent(Vaccine vaccine, String eventType, String entityType, JSONArray fields, org.smartregister.Context context) {
37
        try {
38
            EventClientRepository db = context.getEventClientRepository();
1✔
39

40
            Event event = (Event) new Event()
1✔
41
                    .withBaseEntityId(vaccine.getBaseEntityId())
1✔
42
                    .withIdentifiers(vaccine.getIdentifiers())
1✔
43
                    .withEventDate(vaccine.getDate())
1✔
44
                    .withEventType(eventType)
1✔
45
                    .withLocationId(vaccine.getLocationId())
1✔
46
                    .withProviderId(vaccine.getAnmId())
1✔
47
                    .withEntityType(entityType)
1✔
48
                    .withFormSubmissionId(vaccine.getFormSubmissionId() == null ? generateRandomUUIDString() : vaccine
1✔
49
                            .getFormSubmissionId())
×
50
                    .withDateCreated(new Date());
1✔
51

52
            event.setTeam(vaccine.getTeam());
1✔
53
            event.setTeamId(vaccine.getTeamId());
1✔
54
            event.setChildLocationId(vaccine.getChildLocationId());
1✔
55
            event.addDetails(IMConstants.VaccineEvent.PROGRAM_CLIENT_ID, vaccine.getProgramClientId());
1✔
56
            event.addDetails(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy());
1✔
57

58
            try {
59
                addFormSubmissionFieldObservation(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy(), Observation.TYPE.TEXT, event);
1✔
60
            } catch (JSONException jsonException) {
×
61
                Timber.e(jsonException);
×
62
            }
1✔
63

64
            event.setClientApplicationVersion(ImmunizationLibrary.getInstance().getApplicationVersion());
1✔
65
            event.setClientApplicationVersionName(ImmunizationLibrary.getInstance().getApplicationVersionName());
1✔
66
            event.setClientDatabaseVersion(ImmunizationLibrary.getInstance().getDatabaseVersion());
1✔
67

68
            if (fields != null && fields.length() != 0)
1✔
69
                for (int i = 0; i < fields.length(); i++) {
1✔
70
                    JSONObject jsonObject = getJSONObject(fields, i);
1✔
71
                    String value = getString(jsonObject, VALUE);
1✔
72
                    if (StringUtils.isNotBlank(value)) {
1✔
73
                        addObservation(event, jsonObject);
1✔
74
                    }
75
                }
76

77

78
            if (event != null) {
1✔
79

80
                JSONObject eventJson = new JSONObject(JsonFormUtils.gson.toJson(event));
1✔
81

82
                //check if an event already exists and update instead
83
                if (vaccine.getEventId() != null) {
1✔
84
                    JSONObject existingEvent = db.getEventsByEventId(vaccine.getEventId());
×
85
                    eventJson = merge(existingEvent, eventJson);
×
86
                }
87

88
                //merge if event exists
89
                db.addEvent(event.getBaseEntityId(), eventJson);
1✔
90
            }
91
        } catch (Exception e) {
×
92
            Timber.e(e);
×
93
        }
1✔
94
    }
1✔
95

96
    /**
97
     * This createServiceEvent method is deprecated, use {@link #createServiceEvent(ServiceRecord serviceRecord, String eventType, String entityType, JSONArray fields, org.smartregister.Context context)} instead which adds application version name.
98
     */
99
    @Deprecated
100
    public static void createServiceEvent(Context context, ServiceRecord serviceRecord, String eventType, String entityType, JSONArray fields) {
101

102
        org.smartregister.Context openSRPContext = ImmunizationLibrary.getInstance().context();
×
103
        createServiceEvent(serviceRecord, eventType, entityType, fields, openSRPContext);
×
104
    }
×
105

106
    public static void createServiceEvent(ServiceRecord serviceRecord, String eventType, String entityType, JSONArray fields, org.smartregister.Context context) {
107
        try {
108
            EventClientRepository db = context.getEventClientRepository();
1✔
109

110
            Event event = (Event) new Event()
1✔
111
                    .withBaseEntityId(serviceRecord.getBaseEntityId())
1✔
112
                    .withIdentifiers(serviceRecord.getIdentifiers())
1✔
113
                    .withEventDate(serviceRecord.getDate())
1✔
114
                    .withEventType(eventType)
1✔
115
                    .withLocationId(serviceRecord.getLocationId())
1✔
116
                    .withProviderId(serviceRecord.getAnmId())
1✔
117
                    .withEntityType(entityType)
1✔
118
                    .withFormSubmissionId(
1✔
119
                            serviceRecord.getFormSubmissionId() == null ? generateRandomUUIDString() : serviceRecord
1✔
120
                                    .getFormSubmissionId())
×
121
                    .withDateCreated(new Date());
1✔
122

123
            event.setTeam(serviceRecord.getTeam());
1✔
124
            event.setTeamId(serviceRecord.getTeamId());
1✔
125
            event.setChildLocationId(serviceRecord.getChildLocationId());
1✔
126
            event.addDetails(IMConstants.VaccineEvent.PROGRAM_CLIENT_ID, serviceRecord.getProgramClientId());
1✔
127
            event.addDetails(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy());
1✔
128

129
            try {
130
                addFormSubmissionFieldObservation(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy(), Observation.TYPE.TEXT, event);
1✔
131
            } catch (JSONException jsonException) {
×
132
                Timber.e(jsonException);
×
133
            }
1✔
134

135
            event.setClientApplicationVersion(ImmunizationLibrary.getInstance().getApplicationVersion());
1✔
136
            event.setClientApplicationVersionName(ImmunizationLibrary.getInstance().getApplicationVersionName());
1✔
137
            event.setClientDatabaseVersion(ImmunizationLibrary.getInstance().getDatabaseVersion());
1✔
138

139
            if (fields != null && fields.length() != 0)
1✔
140
                for (int i = 0; i < fields.length(); i++) {
1✔
141
                    JSONObject jsonObject = getJSONObject(fields, i);
1✔
142
                    String value = getString(jsonObject, VALUE);
1✔
143
                    if (StringUtils.isNotBlank(value)) {
1✔
144
                        addObservation(event, jsonObject);
1✔
145
                    }
146
                }
147

148
            if (event != null) {
1✔
149

150
                JSONObject eventJson = new JSONObject(JsonFormUtils.gson.toJson(event));
1✔
151

152
                //check if an event already exists and update instead
153
                if (serviceRecord.getEventId() != null) {
1✔
154
                    JSONObject existingEvent = db.getEventsByEventId(serviceRecord.getEventId());
×
155
                    eventJson = merge(existingEvent, eventJson);
×
156
                }
157

158
                //merge if event exists
159
                db.addEvent(event.getBaseEntityId(), eventJson);
1✔
160
            }
161
        } catch (Exception e) {
×
162
            Timber.e(e);
×
163
        }
1✔
164
    }
1✔
165

166
}
167

168

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