• 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

1.43
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.JSONObject;
8
import org.smartregister.clientandeventmodel.Event;
9
import org.smartregister.immunization.ImmunizationLibrary;
10
import org.smartregister.immunization.domain.ServiceRecord;
11
import org.smartregister.immunization.domain.Vaccine;
12
import org.smartregister.repository.EventClientRepository;
13

14
import java.util.Date;
15

16
import timber.log.Timber;
17

18
/**
19
 * Created by keyman on 31/07/2017.
20
 */
21
public class JsonFormUtils extends org.smartregister.util.JsonFormUtils {
1✔
22

23
    public static void createVaccineEvent(Context context, Vaccine vaccine, String eventType, String entityType,
24
                                          JSONArray fields) {
25
        try {
26
            EventClientRepository db = ImmunizationLibrary.getInstance().eventClientRepository();
×
27

28
            Event event = (Event) new Event()
×
29
                    .withBaseEntityId(vaccine.getBaseEntityId())
×
30
                    .withIdentifiers(vaccine.getIdentifiers())
×
31
                    .withEventDate(vaccine.getDate())
×
32
                    .withEventType(eventType)
×
33
                    .withLocationId(vaccine.getLocationId())
×
34
                    .withProviderId(vaccine.getAnmId())
×
35
                    .withEntityType(entityType)
×
36
                    .withFormSubmissionId(vaccine.getFormSubmissionId() == null ? generateRandomUUIDString() : vaccine
×
37
                            .getFormSubmissionId())
×
38
                    .withDateCreated(new Date());
×
39

40
            event.setTeam(vaccine.getTeam());
×
41
            event.setTeamId(vaccine.getTeamId());
×
42
            event.setChildLocationId(vaccine.getChildLocationId());
×
43
            event.addDetails(IMConstants.VaccineEvent.PROGRAM_CLIENT_ID, vaccine.getProgramClientId());
×
44

45
            event.setClientApplicationVersion(ImmunizationLibrary.getInstance().getApplicationVersion());
×
46
            event.setClientDatabaseVersion(ImmunizationLibrary.getInstance().getDatabaseVersion());
×
47

48
            if (fields != null && fields.length() != 0)
×
49
                for (int i = 0; i < fields.length(); i++) {
×
50
                    JSONObject jsonObject = getJSONObject(fields, i);
×
51
                    String value = getString(jsonObject, VALUE);
×
52
                    if (StringUtils.isNotBlank(value)) {
×
53
                        addObservation(event, jsonObject);
×
54
                    }
55
                }
56

57

58
            if (event != null) {
×
59

60
                JSONObject eventJson = new JSONObject(JsonFormUtils.gson.toJson(event));
×
61

62
                //check if an event already exists and update instead
63
                if (vaccine.getEventId() != null) {
×
64
                    JSONObject existingEvent = db.getEventsByEventId(vaccine.getEventId());
×
65
                    eventJson = merge(existingEvent, eventJson);
×
66
                }
67

68
                //merge if event exists
69
                db.addEvent(event.getBaseEntityId(), eventJson);
×
70
            }
71
        } catch (Exception e) {
×
72
            Timber.e(e);
×
73
        }
×
74
    }
×
75

76
    public static void createServiceEvent(Context context, ServiceRecord serviceRecord, String eventType, String entityType,
77
                                          JSONArray fields) {
78
        try {
79
            EventClientRepository db = ImmunizationLibrary.getInstance().eventClientRepository();
×
80

81
            Event event = (Event) new Event()
×
82
                    .withBaseEntityId(serviceRecord.getBaseEntityId())
×
83
                    .withIdentifiers(serviceRecord.getIdentifiers())
×
84
                    .withEventDate(serviceRecord.getDate())
×
85
                    .withEventType(eventType)
×
86
                    .withLocationId(serviceRecord.getLocationId())
×
87
                    .withProviderId(serviceRecord.getAnmId())
×
88
                    .withEntityType(entityType)
×
89
                    .withFormSubmissionId(
×
90
                            serviceRecord.getFormSubmissionId() == null ? generateRandomUUIDString() : serviceRecord
×
91
                                    .getFormSubmissionId())
×
92
                    .withDateCreated(new Date());
×
93

94
            event.setTeam(serviceRecord.getTeam());
×
95
            event.setTeamId(serviceRecord.getTeamId());
×
96
            event.setChildLocationId(serviceRecord.getChildLocationId());
×
97
            event.addDetails(IMConstants.VaccineEvent.PROGRAM_CLIENT_ID, serviceRecord.getProgramClientId());
×
98

99
            event.setClientApplicationVersion(ImmunizationLibrary.getInstance().getApplicationVersion());
×
100
            event.setClientDatabaseVersion(ImmunizationLibrary.getInstance().getDatabaseVersion());
×
101

102
            if (fields != null && fields.length() != 0)
×
103
                for (int i = 0; i < fields.length(); i++) {
×
104
                    JSONObject jsonObject = getJSONObject(fields, i);
×
105
                    String value = getString(jsonObject, VALUE);
×
106
                    if (StringUtils.isNotBlank(value)) {
×
107
                        addObservation(event, jsonObject);
×
108
                    }
109
                }
110

111

112
            if (event != null) {
×
113

114
                JSONObject eventJson = new JSONObject(JsonFormUtils.gson.toJson(event));
×
115

116
                //check if an event already exists and update instead
117
                if (serviceRecord.getEventId() != null) {
×
118
                    JSONObject existingEvent = db.getEventsByEventId(serviceRecord.getEventId());
×
119
                    eventJson = merge(existingEvent, eventJson);
×
120
                }
121

122
                //merge if event exists
123
                db.addEvent(event.getBaseEntityId(), eventJson);
×
124
            }
125
        } catch (Exception e) {
×
126
            Timber.e(e);
×
127
        }
×
128
    }
×
129

130
}
131

132

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