-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use post request for old advanced search #327
Open
hilpitome
wants to merge
21
commits into
master
Choose a base branch
from
326-make-search-request-with-post
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e666ed1
use post request for old advanced search
hilpitome 91333e2
modify tests to changed account for changes
hilpitome 415579f
update failing tests
hilpitome 08c1724
fix failing tests
hilpitome 403d19b
remove unused method
hilpitome 6fcbad0
check if R package import is affecting tests
hilpitome c90b880
use eq arguement matcher for failing test
hilpitome a8727ec
try Mockito.anyString
hilpitome cb38044
use Mockito.any
hilpitome 2abc59f
update library version
hilpitome 5e0dd44
Merge branch 'master' into 326-make-search-request-with-post
hilpitome ed9e6ba
merge master
hilpitome cd76b55
Merge branch '326-make-search-request-with-post' of github.com:OpenSR…
hilpitome 74ef760
Update gradle.properties
hilpitome 695973a
remove unnecessary file
hilpitome 3e44941
Merge branch '326-make-search-request-with-post' of github.com:OpenSR…
hilpitome 7e00a9d
Merge branch 'master' into 326-make-search-request-with-post
hilpitome 5fe0951
remove unnecessary method
hilpitome ea1c0d0
Merge branch 'master' into 326-make-search-request-with-post
hilpitome 8c03bb6
merge with master
hilpitome ffcdd25
Merge branch '326-make-search-request-with-post' of github.com:OpenSR…
hilpitome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import org.jetbrains.annotations.Nullable; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.smartregister.CoreLibrary; | ||
import org.smartregister.DristhiConfiguration; | ||
import org.smartregister.child.ChildLibrary; | ||
|
@@ -63,8 +64,12 @@ private Response<String> globalSearch(Map<String, String> searchParameters) { | |
if (ChildLibrary.getInstance().getProperties().isTrue(ChildAppProperties.KEY.USE_NEW_ADVANCE_SEARCH_APPROACH)) { | ||
return retrieveRemoteClients(searchParameters); | ||
} | ||
String paramString = ""; | ||
return searchUsingOldApproachWithPostRequest(searchParameters); | ||
} | ||
|
||
private Response<String> searchUsingOldApproachWithPostRequest(Map<String, String> searchParameters){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See earlier comment |
||
if (!searchParameters.isEmpty()) { | ||
JSONObject jsonObject = new JSONObject(); | ||
for (Map.Entry<String, String> entry : searchParameters.entrySet()) { | ||
String key = entry.getKey(); | ||
String value = entry.getValue(); | ||
|
@@ -74,19 +79,17 @@ private Response<String> globalSearch(Map<String, String> searchParameters) { | |
} | ||
|
||
if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) { | ||
value = urlEncode(value); | ||
String param = key.trim() + "=" + value.trim(); | ||
if (StringUtils.isBlank(paramString)) { | ||
paramString = "?" + param; | ||
} else { | ||
paramString += "&" + param; | ||
try { | ||
jsonObject.put(key, value); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} | ||
String uri = getDristhiConfiguration().dristhiBaseURL() + SEARCH_URL + paramString; | ||
String uri = getDristhiConfiguration().dristhiBaseURL() + SEARCH_URL; | ||
Timber.i("Advance Search URI: %s ", uri); | ||
return getHttpAgent().fetch(uri); | ||
return getHttpAgent().post(uri, jsonObject.toString()); | ||
} | ||
return new Response<>(ResponseStatus.failure, "[]"); | ||
} | ||
|
@@ -99,14 +102,26 @@ private Response<String> globalSearch(Map<String, String> searchParameters) { | |
* @return Payload string of the response of search | ||
*/ | ||
private Response<String> retrieveRemoteClients(Map<String, String> searchParameters) { | ||
SearchMother searchMother = new SearchMother(searchParameters).invoke(); | ||
SearchMother searchMother = null; | ||
try { | ||
searchMother = new SearchMother(searchParameters).invoke(); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
assert searchMother != null; | ||
Response<String> motherSearchResult = searchMother.getMotherSearchResult(); | ||
|
||
String childSearchParameters = generateChildSearchParameters(searchParameters); | ||
if (StringUtils.isNotBlank(childSearchParameters)) { | ||
JSONObject childSearchJSONObject = null; | ||
try { | ||
childSearchJSONObject = generateChildSearchParameters(searchParameters); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
assert childSearchJSONObject != null; | ||
if (childSearchJSONObject.length()>0) { | ||
String searchEndpoint = getDristhiConfiguration().dristhiBaseURL() + NEW_ADVANCE_SEARCH_URL; | ||
Timber.i("Child Search URI: %s%s", searchEndpoint, childSearchParameters); | ||
Response<String> childSearchResults = getHttpAgent().fetch(searchEndpoint + childSearchParameters); | ||
Timber.i("Child Search URI: %s%s", searchEndpoint, childSearchJSONObject.toString()); | ||
Response<String> childSearchResults = getHttpAgent().post(searchEndpoint, childSearchJSONObject.toString()); | ||
if (childSearchResults.status() == ResponseStatus.success && motherSearchResult != null && motherSearchResult.status() == ResponseStatus.success) { | ||
try { | ||
JSONArray mothersJsonArray = new JSONArray(motherSearchResult.payload()); | ||
|
@@ -132,18 +147,16 @@ public String getMotherGuardianPhoneNumber() { | |
return motherGuardianNumber; | ||
} | ||
|
||
private String generateChildSearchParameters(Map<String, String> searchParameters) { | ||
private JSONObject generateChildSearchParameters(Map<String, String> searchParameters) throws JSONException { | ||
removeMotherSearchParameters(searchParameters); | ||
|
||
StringBuilder queryParamStringBuilder = new StringBuilder(); | ||
|
||
String identifier = searchParameters.remove(Constants.KEY.ZEIR_ID); | ||
|
||
//Search by ZEIR id and include mother relationship when identifier is provided | ||
JSONObject jsonObject = new JSONObject(); | ||
if (StringUtils.isNotBlank(identifier)) { | ||
queryParamStringBuilder.append("?identifier=").append(identifier); | ||
queryParamStringBuilder.append("&relationships=mother"); | ||
return queryParamStringBuilder.toString(); | ||
jsonObject.put("identifier", identifier); | ||
jsonObject.put("relationships", "mother"); | ||
return jsonObject; | ||
} | ||
|
||
//Handle name param - use either firs/last name //TODO server does not support full name | ||
|
@@ -152,46 +165,45 @@ private String generateChildSearchParameters(Map<String, String> searchParameter | |
name = StringUtils.isNotBlank(name) ? name : lastname; | ||
|
||
if (StringUtils.isNotBlank(name)) { | ||
queryParamStringBuilder.append("?name=").append(name); | ||
jsonObject.put("name", name); | ||
} | ||
|
||
//Handle birth dates param | ||
String birthDate = getChildBirthDateParameter(searchParameters, name); | ||
String birthDate = getChildBirthDateParameter(searchParameters); | ||
|
||
if (StringUtils.isNotBlank(birthDate)) { | ||
queryParamStringBuilder.append(birthDate); | ||
jsonObject.put("birthdate", birthDate); | ||
} | ||
|
||
//Handle other client attributes | ||
String formattedAttributes = getChildClientAttributes(searchParameters, queryParamStringBuilder, | ||
StringUtils.isNotBlank(name) || StringUtils.isNotBlank(birthDate)); | ||
String formattedAttributes = getChildClientAttributes(searchParameters); | ||
if (StringUtils.isNotBlank(formattedAttributes)) { | ||
jsonObject.put("attribute", formattedAttributes); | ||
} | ||
if (StringUtils.isNotBlank(name) || StringUtils.isNotBlank(birthDate) || StringUtils.isNotBlank(formattedAttributes)) { | ||
queryParamStringBuilder.append("&relationships=mother"); | ||
jsonObject.put("relationships", "mother"); | ||
} | ||
return queryParamStringBuilder.toString(); | ||
return jsonObject; | ||
} | ||
|
||
@NotNull | ||
private String getChildBirthDateParameter(Map<String, String> searchParameters, String name) { | ||
private String getChildBirthDateParameter(Map<String, String> searchParameters) { | ||
String birthDate = ""; | ||
String birthDatesString = searchParameters.remove(Constants.KEY.BIRTH_DATE); | ||
|
||
String[] birthDates = birthDatesString != null ? birthDatesString.split(":") : new String[]{}; | ||
if (birthDates != null && birthDates.length == 2 && StringUtils.isNotBlank(name)) { | ||
birthDate = String.format("&birthdate=%s:%s", birthDates[0], birthDates[1]); | ||
} else if (birthDates != null && birthDates.length == 2 && StringUtils.isBlank(name)) { | ||
birthDate = String.format("?birthdate=%s:%s", birthDates[0], birthDates[1]); | ||
if (birthDates != null && birthDates.length == 2 ) { | ||
birthDate = String.format("%s:%s", birthDates[0], birthDates[1]); | ||
} | ||
|
||
return birthDate; | ||
} | ||
|
||
@Nullable | ||
private String getChildClientAttributes(Map<String, String> searchParameters, StringBuilder queryParamStringBuilder, boolean nameBirthDateAttributesPresent) { | ||
private String getChildClientAttributes(Map<String, String> searchParameters) { | ||
StringBuilder clientAttributes = new StringBuilder(); | ||
for (Map.Entry<String, String> entry : searchParameters.entrySet()) { | ||
String key = entry.getKey(); | ||
String value = urlEncode(entry.getValue()); | ||
String value = entry.getValue(); | ||
if (key.equalsIgnoreCase(Constants.CHILD_STATUS.ACTIVE) && value.equalsIgnoreCase("true") || | ||
key.equalsIgnoreCase(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP) && value.equalsIgnoreCase("false") || | ||
key.equalsIgnoreCase(Constants.CHILD_STATUS.INACTIVE) && value.equalsIgnoreCase("false")) { | ||
|
@@ -204,11 +216,7 @@ private String getChildClientAttributes(Map<String, String> searchParameters, St | |
if (StringUtils.isNotBlank(clientAttributes)) { | ||
formattedAttributes = clientAttributes.toString().replaceAll(",$", "").trim(); | ||
} | ||
if (StringUtils.isNotBlank(formattedAttributes) && nameBirthDateAttributesPresent) { | ||
queryParamStringBuilder.append("&attribute=").append(formattedAttributes); | ||
} else if (StringUtils.isNotBlank(formattedAttributes) && !nameBirthDateAttributesPresent) { | ||
queryParamStringBuilder.append("?attribute=").append(formattedAttributes); | ||
} | ||
|
||
return formattedAttributes; | ||
} | ||
|
||
|
@@ -257,30 +265,29 @@ public Response<String> getMotherSearchResult() { | |
return motherSearchResult; | ||
} | ||
|
||
public SearchMother invoke() { | ||
public SearchMother invoke() throws JSONException { | ||
JSONObject jsonObject = new JSONObject(); | ||
String searchEndpoint = getDristhiConfiguration().dristhiBaseURL() + NEW_ADVANCE_SEARCH_URL; | ||
String motherSearchParameters = ""; | ||
|
||
String name = searchParameters.remove(Constants.KEY.MOTHER_FIRST_NAME); | ||
String lastname = searchParameters.remove(Constants.KEY.MOTHER_LAST_NAME); | ||
name = StringUtils.isNotBlank(name) ? name : lastname; | ||
|
||
if (StringUtils.isNotBlank(name)) { | ||
motherSearchParameters = String.format("?name=%s", name); | ||
jsonObject.put("name", name); | ||
} | ||
|
||
String phoneNumber = searchParameters.remove(getMotherGuardianPhoneNumber()); | ||
if (StringUtils.isNotBlank(motherSearchParameters) && StringUtils.isNotBlank(phoneNumber)) { | ||
motherSearchParameters = String.format("%s&attribute=%s:%s", motherSearchParameters, getMotherGuardianPhoneNumber(), phoneNumber); | ||
} else if (StringUtils.isBlank(motherSearchParameters) && StringUtils.isNotBlank(phoneNumber)) { | ||
motherSearchParameters = String.format("?attribute=%s:%s", getMotherGuardianPhoneNumber(), phoneNumber); | ||
if(StringUtils.isNotBlank(phoneNumber)){ | ||
String attribute = String.format("%s:%s", getMotherGuardianPhoneNumber(), phoneNumber); | ||
jsonObject.put("attribute", attribute); | ||
} | ||
|
||
if (StringUtils.isNotBlank(motherSearchParameters)) { | ||
motherSearchParameters = String.format("%s&searchRelationship=%s", motherSearchParameters, Constants.KEY.MOTHER); | ||
motherSearchResult = getHttpAgent().fetch(searchEndpoint + motherSearchParameters); | ||
Timber.i("Mother Search URI: %s%s", searchEndpoint, motherSearchParameters); | ||
if(jsonObject.length() > 0){ | ||
jsonObject.put("searchRelationship", Constants.KEY.MOTHER); | ||
motherSearchResult = getHttpAgent().post(searchEndpoint, jsonObject.toString()); | ||
Timber.i("Mother Search URI: %s%s", searchEndpoint, jsonObject.toString()); | ||
} | ||
|
||
return this; | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we update this method name to give more meaning?