-
Notifications
You must be signed in to change notification settings - Fork 22
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
allow search via post [v2.2] #1173
Open
hilpitome
wants to merge
9
commits into
v2.2
Choose a base branch
from
1172-v2-allow-search-via-post
base: v2.2
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 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7d609ac
allow search via post
hilpitome 6cfa748
init refactor to share code
hilpitome 59060c1
share common code in SearchHelper
hilpitome 6e79ed2
use opt to get JSONObject fields
hilpitome b128e88
added SearchHelper tests
hilpitome 1e0830b
search client by post
hilpitome a8a1416
fix codacy issues
hilpitome 06ba41c
apply formating
hilpitome e59baa5
rename searchByPath methods
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.joda.time.DateTime; | ||
import org.json.JSONObject; | ||
import org.opensrp.common.AllConstants.BaseEntity; | ||
import org.opensrp.search.ClientSearchBean; | ||
import org.opensrp.service.ClientService; | ||
|
@@ -17,6 +18,7 @@ | |
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
|
@@ -118,48 +120,115 @@ public List<Client> search(HttpServletRequest request) throws ParseException {// | |
searchBean.setIdentifiers(identifierMap); | ||
return searchService.searchClient(searchBean, firstName, middleName, lastName, null); | ||
} | ||
|
||
|
||
@RequestMapping(method = RequestMethod.POST, value = "/search", produces = { MediaType.APPLICATION_JSON_VALUE }) | ||
public List<Client> searchByPost(@RequestBody String jsonRequestBody) throws ParseException {//TODO search should not call different url but only add params | ||
JSONObject jsonObject = new JSONObject(jsonRequestBody); | ||
String firstName = jsonObject.optString(FIRST_NAME); | ||
String middleName = jsonObject.optString(MIDDLE_NAME); | ||
String lastName = jsonObject.optString(LAST_NAME); | ||
Optional<String> phoneNumber = Optional.ofNullable(jsonObject.optString(PHONE_NUMBER)); | ||
Optional<String> altPhoneNumber = Optional.ofNullable(jsonObject.optString(ALT_PHONE_NUMBER)); | ||
Optional<String> alternateName = Optional.ofNullable(jsonObject.optString(ALT_NAME)); | ||
ClientSearchBean searchBean = new ClientSearchBean(); | ||
searchBean.setNameLike(jsonObject.optString(NAME)); | ||
|
||
searchBean.setGender(jsonObject.optString(GENDER)); | ||
DateTime[] birthdate = RestUtils.getDateRangeFilter(BIRTH_DATE, jsonObject);//TODO add ranges like fhir do http://hl7.org/fhir/search.html | ||
DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, jsonObject);//TODO client by provider id | ||
//TODO lookinto Swagger https://slack-files.com/files-pri-safe/T0EPSEJE9-F0TBD0N77/integratingswagger.pdf?c=1458211183-179d2bfd2e974585c5038fba15a86bf83097810a | ||
|
||
if (birthdate != null) { | ||
searchBean.setBirthdateFrom(birthdate[0]); | ||
searchBean.setBirthdateTo(birthdate[1]); | ||
} | ||
if (lastEdit != null) { | ||
searchBean.setLastEditFrom(lastEdit[0]); | ||
searchBean.setLastEditTo(lastEdit[1]); | ||
} | ||
Map<String, String> attributeMap = new HashMap<>(); | ||
String attributes = jsonObject.optString(ATTRIBUTE); | ||
if (!StringUtils.isBlank(attributes)) { | ||
String attributeType = StringUtils.isBlank(attributes) ? null : attributes.split(":", -1)[0]; | ||
String attributeValue = StringUtils.isBlank(attributes) ? null : attributes.split(":", -1)[1]; | ||
attributeMap.put(attributeType, attributeValue); | ||
} | ||
phoneNumber.ifPresent(phoneValue -> attributeMap.put(PHONE_NUMBER, phoneValue)); | ||
altPhoneNumber.ifPresent(altPhoneValue -> attributeMap.put(ALT_PHONE_NUMBER, altPhoneValue)); | ||
alternateName.ifPresent(altNameValue -> attributeMap.put(ALT_NAME, altNameValue)); | ||
searchBean.setAttributes(attributeMap); | ||
|
||
Map<String, String> identifierMap = null; | ||
String identifiers = jsonObject.optString(IDENTIFIER); | ||
if (!StringUtils.isBlank(identifiers)) { | ||
String identifierType = StringUtils.isBlank(identifiers) ? null : identifiers.split(":", -1)[0]; | ||
String identifierValue = StringUtils.isBlank(identifiers) ? null : identifiers.split(":", -1)[1]; | ||
|
||
identifierMap = new HashMap<String, String>(); | ||
identifierMap.put(identifierType, identifierValue); | ||
} | ||
|
||
searchBean.setIdentifiers(identifierMap); | ||
return searchService.searchClient(searchBean, firstName, middleName, lastName, null); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.GET, value = "/path", produces = { MediaType.APPLICATION_JSON_VALUE }) | ||
private List<ChildMother> searchPathBy(HttpServletRequest request) throws ParseException { | ||
|
||
String contactPhoneNumber = SearchHelper.getContactPhoneNumberParam(request); | ||
SearchEntityWrapper childSearchEntity = SearchHelper.childSearchParamProcessor(request); | ||
SearchEntityWrapper motherSearchEntity = SearchHelper.motherSearchParamProcessor(request); | ||
|
||
return searchAndProcess(childSearchEntity, motherSearchEntity, contactPhoneNumber); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.POST, value = "/path", produces = { MediaType.APPLICATION_JSON_VALUE }) | ||
private List<ChildMother> searchPathBy(@RequestBody String jsonRequestBody) throws ParseException { | ||
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. Consider renaming method name |
||
|
||
JSONObject jsonRequestBodyObject = new JSONObject(jsonRequestBody); | ||
SearchEntityWrapper childSearchEntity = SearchHelper.childSearchParamProcessor(jsonRequestBodyObject); | ||
SearchEntityWrapper motherSearchEntity = SearchHelper.motherSearchParamProcessor(jsonRequestBodyObject); | ||
String contactPhoneNumber = SearchHelper.getContactPhoneNumberParam(jsonRequestBodyObject); | ||
|
||
return searchAndProcess(childSearchEntity, motherSearchEntity,contactPhoneNumber); | ||
|
||
} | ||
|
||
private List<ChildMother> searchAndProcess(SearchEntityWrapper childSearchEntity, SearchEntityWrapper motherSearchEntity, | ||
String contactPhoneNumber){ | ||
try { | ||
|
||
//Process clients search via demographics | ||
|
||
ClientSearchBean searchBean = new ClientSearchBean(); | ||
List<Client> children = new ArrayList<Client>(); | ||
|
||
SearchEntityWrapper childSearchEntity = SearchHelper.childSearchParamProcessor(request); | ||
|
||
if (childSearchEntity.isValid()) { | ||
searchBean = childSearchEntity.getClientSearchBean(); | ||
children = searchService.searchClient(searchBean, searchBean.getFirstName(), searchBean.getMiddleName(), | ||
searchBean.getLastName(), childSearchEntity.getLimit()); | ||
searchBean.getLastName(), childSearchEntity.getLimit()); | ||
} | ||
|
||
//Process mothers search via mother demographics | ||
|
||
SearchEntityWrapper motherSearchEntity = SearchHelper.motherSearchParamProcessor(request); | ||
|
||
ClientSearchBean motherSearchBean = new ClientSearchBean(); | ||
List<Client> mothers = new ArrayList<Client>(); | ||
|
||
if (motherSearchEntity.isValid()) { | ||
motherSearchBean = motherSearchEntity.getClientSearchBean(); | ||
mothers = searchService.searchClient(motherSearchBean, motherSearchBean.getFirstName(), | ||
motherSearchBean.getMiddleName(), motherSearchBean.getLastName(), motherSearchEntity.getLimit()); | ||
motherSearchBean.getMiddleName(), motherSearchBean.getLastName(), motherSearchEntity.getLimit()); | ||
} | ||
|
||
//Process clients search via contact phone number | ||
|
||
String contactPhoneNumber = SearchHelper.getContactPhoneNumberParam(request); | ||
|
||
|
||
|
||
List<String> clientBaseEntityIds = getClientBaseEntityIdsByContactPhoneNumber(contactPhoneNumber); | ||
|
||
List<Client> eventChildren = clientService.findByFieldValue(BaseEntity.BASE_ENTITY_ID, clientBaseEntityIds); | ||
|
||
children = SearchHelper.intersection(children, eventChildren);// Search conjunction is "AND" find intersection | ||
|
||
List<Client> linkedMothers = new ArrayList<Client>(); | ||
|
||
String RELATIONSHIP_KEY = "mother"; | ||
if (!children.isEmpty()) { | ||
List<String> clientIds = new ArrayList<String>(); | ||
|
@@ -169,37 +238,37 @@ private List<ChildMother> searchPathBy(HttpServletRequest request) throws ParseE | |
clientIds.add(relationshipId); | ||
} | ||
} | ||
|
||
linkedMothers = clientService.findByFieldValue(BaseEntity.BASE_ENTITY_ID, clientIds); | ||
|
||
} | ||
|
||
List<Client> linkedChildren = new ArrayList<Client>(); | ||
|
||
if (!mothers.isEmpty()) { | ||
for (Client client : mothers) { | ||
linkedChildren.addAll(clientService.findByRelationship(client.getBaseEntityId())); | ||
} | ||
} | ||
|
||
children = SearchHelper.intersection(children, linkedChildren);// Search conjunction is "AND" find intersection | ||
|
||
for (Client linkedMother : linkedMothers) { | ||
if (!SearchHelper.contains(mothers, linkedMother)) { | ||
mothers.add(linkedMother); | ||
} | ||
} | ||
|
||
return SearchHelper.processSearchResult(children, mothers, RELATIONSHIP_KEY); | ||
|
||
} | ||
catch (Exception e) { | ||
|
||
logger.error("", e); | ||
return new ArrayList<ChildMother>(); | ||
} | ||
} | ||
|
||
public List<String> getClientBaseEntityIdsByContactPhoneNumber(String motherGuardianPhoneNumber) { | ||
List<String> clientBaseEntityIds = new ArrayList<String>(); | ||
|
||
|
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.
Consider renaming method name