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

OpenSRP / opensrp-client-immunization / #910

pending completion
#910

Pull #201

github-actions

web-flow
Merge cacc15235 into 180f8905e
Pull Request #201: Index sync status column

5170 of 6704 relevant lines covered (77.12%)

0.77 hits per line

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

73.2
opensrp-immunization/src/main/java/org/smartregister/immunization/domain/ServiceSchedule.java
1
package org.smartregister.immunization.domain;
2

3
import org.joda.time.DateTime;
4
import org.json.JSONException;
5
import org.json.JSONObject;
6
import org.smartregister.clientandeventmodel.DateUtil;
7
import org.smartregister.domain.Alert;
8
import org.smartregister.domain.AlertStatus;
9
import org.smartregister.immunization.ImmunizationLibrary;
10
import org.smartregister.immunization.repository.RecurringServiceRecordRepository;
11
import org.smartregister.immunization.repository.RecurringServiceTypeRepository;
12
import org.smartregister.immunization.util.VaccinateActionUtils;
13
import org.smartregister.immunization.util.VaccinatorUtils;
14
import org.smartregister.service.AlertService;
15

16
import java.util.ArrayList;
17
import java.util.Calendar;
18
import java.util.Date;
19
import java.util.List;
20
import java.util.Locale;
21

22
import timber.log.Timber;
23

24
/**
25
 * Created by Keyman on 26/05/2017.
26
 */
27

28
public class ServiceSchedule {
29

30
    private final ServiceTrigger dueTrigger;
31
    private final ServiceTrigger expiryTrigger;
32

33

34
    public ServiceSchedule(ServiceTrigger dueTrigger, ServiceTrigger expiryTrigger) {
1✔
35
        this.dueTrigger = dueTrigger;
1✔
36
        this.expiryTrigger = expiryTrigger;
1✔
37
    }
1✔
38

39
    public static ServiceSchedule getServiceSchedule(JSONObject schedule)
40
            throws JSONException {
41
        ServiceTrigger dueTrigger = ServiceTrigger.init(schedule.getJSONObject("due"));
1✔
42
        ServiceTrigger expiryTrigger = ServiceTrigger.init(schedule.optJSONObject("expiry"));
1✔
43
        return new ServiceSchedule(dueTrigger, expiryTrigger);
1✔
44
    }
45

46
    public static void updateOfflineAlerts(String baseEntityId, DateTime dob) {
47
        RecurringServiceTypeRepository recurringServiceTypeRepository = ImmunizationLibrary.getInstance()
1✔
48
                .recurringServiceTypeRepository();
1✔
49
        List<String> types = recurringServiceTypeRepository.fetchTypes();
1✔
50
        for (String type : types) {
1✔
51
            updateOfflineAlerts(type, baseEntityId, dob);
1✔
52
        }
1✔
53
    }
1✔
54

55
    public static void updateOfflineAlerts(String type, String baseEntityId, DateTime dob) {
56
        try {
57
            if (baseEntityId == null || dob == null) {
1✔
58
                return;
1✔
59
            }
60

61
            RecurringServiceTypeRepository recurringServiceTypeRepository = ImmunizationLibrary.getInstance()
1✔
62
                    .recurringServiceTypeRepository();
1✔
63
            RecurringServiceRecordRepository recurringServiceRecordRepository = ImmunizationLibrary.getInstance()
1✔
64
                    .recurringServiceRecordRepository();
1✔
65
            AlertService alertService = ImmunizationLibrary.getInstance().context().alertService();
1✔
66

67
            List<ServiceType> serviceTypes = recurringServiceTypeRepository.findByType(type);
1✔
68

69
            String[] alertArray = VaccinateActionUtils.allAlertNames(serviceTypes);
1✔
70

71
            List<Alert> newAlerts = new ArrayList<>();
1✔
72

73
            // Get all the administered services
74
            List<ServiceRecord> issuedServices = recurringServiceRecordRepository.findByEntityId(baseEntityId);
1✔
75
            alertService.deleteOfflineAlerts(baseEntityId, alertArray);
1✔
76

77
            for (ServiceType serviceType : serviceTypes) {
1✔
78
                Alert curAlert = getOfflineAlert(serviceType, issuedServices, baseEntityId, dob);
1✔
79

80
                if (curAlert == null) {
1✔
81
                    break;
×
82
                } else {
83

84
                    boolean exists = false;
1✔
85

86
                    // Check if service is already given
87
                    for (ServiceRecord serviceRecord : issuedServices) {
1✔
88
                        if (curAlert.scheduleName().equalsIgnoreCase(serviceRecord.getName()) || curAlert.visitCode().equalsIgnoreCase(serviceRecord.getName())) {
×
89
                            exists = true;
×
90
                            break;
×
91
                        }
92
                    }
×
93

94
                    if (!exists && !AlertStatus.complete.equals(curAlert.status())) {
1✔
95
                        // Insert alert into table
96
                        newAlerts.add(curAlert);
1✔
97
                    }
98

99
                }
100
            }
1✔
101

102
            alertService.create(newAlerts);
1✔
103

104
        } catch (Exception e) {
×
105
            Timber.e(e);
×
106
        }
1✔
107

108

109
    }
1✔
110

111

112
    public static Alert getOfflineAlert(ServiceType serviceType, List<ServiceRecord> issuedServices,
113
                                        String baseEntityId, DateTime dateOfBirth) {
114

115
        try {
116
            DateTime dueDateTime = VaccinatorUtils.getServiceDueDate(serviceType, dateOfBirth, issuedServices);
1✔
117
            DateTime expiryDateTime = VaccinatorUtils.getServiceExpiryDate(serviceType, dateOfBirth);
1✔
118

119
            AlertStatus alertStatus = null;
1✔
120
            alertStatus = expiryDateTime != null && expiryDateTime.isBeforeNow() ? AlertStatus.expired : null; //Check if expired first
1✔
121

122
            if (alertStatus == null) {
1✔
123
                alertStatus = isServiceIssued(serviceType.getName(), issuedServices) ? AlertStatus.complete : calculateAlertStatus(dueDateTime);
×
124
            }
125

126
            if (alertStatus != null) {
1✔
127
                Date startDate = dueDateTime == null ? dateOfBirth.toDate() : dueDateTime.toDate();
1✔
128
                Date expiryDate = expiryDateTime == null ? null : expiryDateTime.toDate();
1✔
129
                return new Alert(baseEntityId, serviceType.getName(), serviceType.getName().toLowerCase(Locale.ENGLISH).replace(" ", ""),
1✔
130
                        alertStatus, startDate == null ? null : DateUtil.yyyyMMdd.format(startDate),
1✔
131
                        expiryDate == null ? null : DateUtil.yyyyMMdd.format(expiryDate), true);
1✔
132
            }
133
            return null;
×
134
        } catch (Exception e) {
×
135
            Timber.e(e);
×
136
            return null;
×
137
        }
138
    }
139

140
    protected static boolean isServiceIssued(String currentVaccine, List<ServiceRecord> serviceRecords) {
141

142
        for (ServiceRecord serviceRecord : serviceRecords) {
1✔
143
            if (currentVaccine.equalsIgnoreCase(serviceRecord.getName())) {
1✔
144
                return true;
1✔
145
            }
146
        }
×
147

148
        return false;
×
149
    }
150

151
    /**
152
     * Use the trigger date as a reference, since that is what is mostly used
153
     *
154
     * @param referenceDate trigger date
155
     * @return evaluated alert status
156
     */
157
    private static AlertStatus calculateAlertStatus(DateTime referenceDate) {
158
        if (referenceDate != null) {
×
159
            Calendar refCalendarDate = Calendar.getInstance();
×
160
            refCalendarDate.setTime(referenceDate.toDate());
×
161
            standardiseCalendarDate(refCalendarDate);
×
162

163
            Calendar today = Calendar.getInstance();
×
164
            standardiseCalendarDate(today);
×
165

166
            if (refCalendarDate.getTimeInMillis() <= today.getTimeInMillis()) {// Due
×
167
                return AlertStatus.normal;
×
168
            }
169
        }
170

171
        return null;
×
172
    }
173

174
    public static void standardiseCalendarDate(Calendar calendarDate) {
175
        calendarDate.set(Calendar.HOUR_OF_DAY, 0);
1✔
176
        calendarDate.set(Calendar.MINUTE, 0);
1✔
177
        calendarDate.set(Calendar.SECOND, 0);
1✔
178
        calendarDate.set(Calendar.MILLISECOND, 0);
1✔
179
    }
1✔
180

181
    public static DateTime standardiseDateTime(DateTime dateTime) {
182
        if (dateTime != null) {
1✔
183
            return dateTime.withTime(0, 0, 0, 0);
1✔
184
        }
185
        return null;
1✔
186
    }
187

188
    public static DateTime addOffsetToDateTime(DateTime dateTime, List<String> offsets) {
189
        DateTime afterOffset = dateTime;
1✔
190
        if (dateTime != null && offsets != null && !offsets.isEmpty()) {
1✔
191
            for (String offset : offsets) {
1✔
192
                afterOffset = addOffsetToDateTime(afterOffset, offset);
1✔
193
            }
1✔
194
        }
195
        return afterOffset;
1✔
196
    }
197

198
    public static DateTime addOffsetToDateTime(DateTime dateTime, String offset) {
199
        try {
200
            DateTime afterOffset = dateTime;
1✔
201
            if (dateTime != null && offset != null) {
1✔
202
                afterOffset = VaccinatorUtils.processConfigDateTimeOffset(afterOffset, offset);
1✔
203
            }
204

205
            return afterOffset;
1✔
206
        } catch (Exception e) {
×
207
            Timber.e(e);
×
208
            return dateTime;
×
209
        }
210
    }
211

212

213
    public ServiceTrigger getDueTrigger() {
214
        return dueTrigger;
1✔
215
    }
216

217
    public ServiceTrigger getExpiryTrigger() {
218
        return expiryTrigger;
1✔
219
    }
220
}
221

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