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

OpenSRP / opensrp-client-child / #797

pending completion
#797

Pull #330

github-actions

web-flow
Merge 30c622bb3 into c099d4f8b
Pull Request #330: Fix offline search results count

5228 of 9051 relevant lines covered (57.76%)

0.58 hits per line

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

95.69
opensrp-child/src/main/java/org/smartregister/child/model/BaseChildAdvancedSearchModel.java
1
package org.smartregister.child.model;
2

3
import org.apache.commons.lang3.StringUtils;
4
import org.smartregister.child.contract.ChildAdvancedSearchContract;
5
import org.smartregister.child.util.ChildJsonFormUtils;
6
import org.smartregister.child.util.Constants;
7
import org.smartregister.child.util.Utils;
8
import org.smartregister.domain.Response;
9

10
import java.text.ParseException;
11
import java.text.SimpleDateFormat;
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.Date;
15
import java.util.List;
16
import java.util.Locale;
17
import java.util.Map;
18

19
import timber.log.Timber;
20

21
public abstract class BaseChildAdvancedSearchModel extends BaseChildRegisterFragmentModel
×
22
        implements ChildAdvancedSearchContract.Model {
23

24

25
    private static final String START_DATE = "start_date";
26
    private static final String END_DATE = "end_date";
27

28

29
    @Override
30
    public String createSearchString(Map<String, String> searchMap) {
31
        String searchCriteria = "";
1✔
32

33
        if (searchMap == null || searchMap.isEmpty()) {
1✔
34
            return searchCriteria;
1✔
35
        }
36

37
        for (Map.Entry<String, String> entry : searchMap.entrySet()) {
1✔
38
            String key = entry.getKey();
1✔
39
            key = key.contains(".") ? key.substring(key.indexOf('.') + 1) : key;
1✔
40
            String value = entry.getValue();
1✔
41

42
            if (!StringUtils.isBlank(searchCriteria)) {
1✔
43

44
                searchCriteria += " ;";
1✔
45
            }
46

47
            searchCriteria += " " + Utils.getTranslatedIdentifier(key) + " " + Utils.bold(value) + "";
1✔
48

49
        }
1✔
50
        return removeLastSemiColon(searchCriteria);
1✔
51
    }
52

53
    @Override
54
    public String getMainConditionString(Map<String, String> editMap) {
55
        convertDateToDesiredFormat(editMap);
1✔
56
        final String table = Utils.metadata().getRegisterQueryProvider().getDemographicTable();
1✔
57
        final String childDetailsTable = Utils.metadata().getRegisterQueryProvider().getChildDetailsTable();
1✔
58

59
        final String motherFirstNameKey = Constants.KEY.MOTHER_FIRST_NAME;
1✔
60
        final String motherFirstNameSelect = Constants.KEY.MOTHER + "." + Constants.KEY.FIRST_NAME;
1✔
61
        final String motherLastNameKey = Constants.KEY.MOTHER_LAST_NAME;
1✔
62
        final String motherLastNameSelect = Constants.KEY.MOTHER + "." + Constants.KEY.LAST_NAME;
1✔
63

64
        final String startDateKey = START_DATE;
1✔
65
        final String endDateKey = END_DATE;
1✔
66
        ArrayList<String> demographicTableColumns = new ArrayList<>(Arrays.asList(new String[]{"first_name", "last_name", "zeir_id"}));
1✔
67

68
        String mainConditionString = "";
1✔
69
        for (Map.Entry<String, String> entry : editMap.entrySet()) {
1✔
70
            String key = entry.getKey();
1✔
71
            if (demographicTableColumns.contains(key))
1✔
72
                key = table + "." + key;
1✔
73
            String value = entry.getValue();
1✔
74
            if (!key.contains(startDateKey) && !key.contains(endDateKey) && !key.contains(Constants.CHILD_STATUS.ACTIVE) && !key
1✔
75
                    .contains(Constants.CHILD_STATUS.INACTIVE) && !key.contains(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP) && !key.contains(motherFirstNameKey) && !key
1✔
76
                    .contains(motherLastNameKey)) {
1✔
77
                if (StringUtils.isBlank(mainConditionString)) {
1✔
78
                    mainConditionString += " " + key + " Like '%" + value + "%'";
1✔
79
                } else {
80
                    mainConditionString += " AND " + key + " Like '%" + value + "%'";
1✔
81

82
                }
83
            }
84
        }
1✔
85

86
        if (StringUtils.isBlank(mainConditionString)) {
1✔
87
            if (editMap.containsKey(startDateKey) && editMap.containsKey(endDateKey)) {
1✔
88
                mainConditionString += " " + table + ".dob BETWEEN '" + editMap
1✔
89
                        .get(startDateKey) + "' AND '" + editMap.get(endDateKey) + "'";
1✔
90
            } else if (editMap.containsKey(startDateKey)) {
1✔
91
                mainConditionString += " " + table + ".dob >= '" + editMap.get(startDateKey) + "'";
1✔
92

93
            } else if (editMap.containsKey(endDateKey)) {
1✔
94
                mainConditionString += " " + table + ".dob <= '" + editMap.get(endDateKey) + "'";
1✔
95
            }
96
        } else {
97
            if (editMap.containsKey(startDateKey) && editMap.containsKey(endDateKey)) {
1✔
98
                mainConditionString += " AND " + table + ".dob BETWEEN '" + editMap
1✔
99
                        .get(startDateKey) + "' AND '" + editMap.get(endDateKey) + "'";
1✔
100
            } else if (editMap.containsKey(startDateKey)) {
1✔
101
                mainConditionString += " AND " + table + ".dob >= '" + editMap.get(startDateKey) + "'";
1✔
102

103
            } else if (editMap.containsKey(endDateKey)) {
1✔
104
                mainConditionString += " AND " + table + ".dob <= '" + editMap.get(endDateKey) + "'";
1✔
105
            }
106
        }
107

108
        if (editMap.containsKey(motherFirstNameKey) && editMap.containsKey(motherLastNameKey)) {
1✔
109
            if (StringUtils.isBlank(mainConditionString)) {
1✔
110
                mainConditionString += motherFirstNameSelect + " Like '%" + editMap
1✔
111
                        .get(motherFirstNameKey) + "%' AND " + motherLastNameSelect + " Like '%" + editMap
1✔
112
                        .get(motherLastNameKey) + "%'";
1✔
113
            } else {
114
                mainConditionString += " AND  (" + motherFirstNameSelect + " Like '%" + editMap
1✔
115
                        .get(motherFirstNameKey) + "%' AND " + motherLastNameSelect + " Like '%" + editMap
1✔
116
                        .get(motherLastNameKey) + "%' ) ";
1✔
117
            }
118
        } else if (editMap.containsKey(motherFirstNameKey) && !editMap.containsKey(motherLastNameKey)) {
1✔
119
            if (StringUtils.isBlank(mainConditionString)) {
1✔
120
                mainConditionString += " " + motherFirstNameSelect + " Like '%" + editMap
1✔
121
                        .get(motherFirstNameKey) + "%'";
1✔
122
            } else {
123
                mainConditionString += " AND  (" + motherFirstNameSelect + " Like '%" + editMap
1✔
124
                        .get(motherFirstNameKey) + "%') ";
1✔
125
            }
126
        } else if (!editMap.containsKey(motherFirstNameKey) && editMap.containsKey(motherLastNameKey)) {
1✔
127
            if (StringUtils.isBlank(mainConditionString)) {
1✔
128
                mainConditionString += " " + motherLastNameSelect + " Like '%" + editMap
1✔
129
                        .get(motherLastNameKey) + "%'";
1✔
130
            } else {
131
                mainConditionString += " AND  (" + motherLastNameSelect + " Like '%" + editMap
1✔
132
                        .get(motherLastNameKey) + "%' ) ";
1✔
133
            }
134
        }
135

136
        String statusConditionString = "";
1✔
137
        for (Map.Entry<String, String> entry : editMap.entrySet()) {
1✔
138
            String key = entry.getKey();
1✔
139
            if (demographicTableColumns.contains(key))
1✔
140
                key = table + "." + key;
1✔
141
            String value = entry.getValue();
1✔
142
            if (key.contains(Constants.CHILD_STATUS.ACTIVE) || key.contains(Constants.CHILD_STATUS.INACTIVE) || key.contains(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP)) {
1✔
143

144
                boolean isActive = key.contains(Constants.CHILD_STATUS.ACTIVE) && !key.contains(Constants.CHILD_STATUS.INACTIVE);
1✔
145
                if (StringUtils.isBlank(statusConditionString)) {
1✔
146
                    if (isActive) {
1✔
147
                        statusConditionString += " ( ( " + childDetailsTable + "." + Constants.CHILD_STATUS.INACTIVE + " IS NULL OR " + childDetailsTable + "." + Constants.CHILD_STATUS.INACTIVE + " != '" + Boolean.TRUE
1✔
148
                                .toString() + "' ) " +
1✔
149
                                " AND ( " + childDetailsTable + "." + Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP + " IS NULL OR " + childDetailsTable + "." + Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP + " != '" + Boolean.TRUE
150
                                .toString() + "' ) ) ";
1✔
151
                    } else {
152
                        statusConditionString += " " + key + " = '" + value + "'";
1✔
153
                    }
154
                } else {
155
                    if (isActive) {
1✔
156
                        statusConditionString += " OR ( ( " + childDetailsTable + "." + Constants.CHILD_STATUS.INACTIVE + " IS NULL OR " + childDetailsTable + "." + Constants.CHILD_STATUS.INACTIVE + " != '" + Boolean.TRUE
1✔
157
                                .toString() + "' ) " +
1✔
158
                                " AND ( " + childDetailsTable + "." + Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP + " IS NULL OR " + childDetailsTable + "." + Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP + " != '" + Boolean.TRUE
159
                                .toString() + "' ) ) ";
1✔
160

161
                    } else {
162
                        statusConditionString += " OR " + key + " = '" + value + "'";
1✔
163
                    }
164

165
                }
166
            }
167
        }
1✔
168

169
        if (!statusConditionString.isEmpty()) {
1✔
170
            if (StringUtils.isBlank(mainConditionString)) {
1✔
171
                mainConditionString += statusConditionString;
1✔
172
            } else {
173
                mainConditionString += " AND (" + statusConditionString + ")";
1✔
174
            }
175
        }
176

177
        return String.format("%s AND (%s is null AND %s == '0')", mainConditionString,
1✔
178
                Utils.metadata().getRegisterQueryProvider().getDemographicTable() + "." + Constants.KEY.DATE_REMOVED,
1✔
179
                Utils.metadata().getRegisterQueryProvider().getDemographicTable() + "." + Constants.KEY.IS_CLOSED);
1✔
180
    }
181

182
    private void convertDateToDesiredFormat(Map<String, String> editMap) {
183
        try {
184
            if(editMap.containsKey(START_DATE) && editMap.containsKey(END_DATE)) {
1✔
185
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
1✔
186
                SimpleDateFormat desiredDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
1✔
187
                Date parsedStartDate = simpleDateFormat.parse(editMap.get(START_DATE));
1✔
188
                Date parsedEndDate = simpleDateFormat.parse(editMap.get(END_DATE));
1✔
189
                editMap.put(START_DATE, desiredDateFormat.format(parsedStartDate));
1✔
190
                editMap.put(END_DATE, desiredDateFormat.format(parsedEndDate));
1✔
191
            }
192
        } catch (ParseException e) {
×
193
            Timber.e(e, "Error converting dates to right format");
×
194
        }
1✔
195
    }
1✔
196

197
    private String removeLastSemiColon(String str) {
198
        if (StringUtils.isBlank(str)) {
1✔
199
            return str;
×
200
        }
201
        String s = str.trim();
1✔
202
        if (s.charAt(s.length() - 1) == ';') {
1✔
203
            return s.substring(0, s.length() - 1);
1✔
204
        }
205
        return s;
1✔
206
    }
207

208
    @Override
209
    public List<ChildMotherDetailModel> getChildMotherDetailModels(Response<String> response) {
210
        return ChildJsonFormUtils.processReturnedAdvanceSearchResults(response);
×
211
    }
212

213
}
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

© 2026 Coveralls, Inc