Skip to content
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
wants to merge 9 commits into
base: v2.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/org/opensrp/web/rest/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTime;
import org.json.JSONObject;
import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.AccessToken;
Expand Down Expand Up @@ -87,6 +88,20 @@ public static DateTime[] getDateRangeFilter(String filter, HttpServletRequest re
return new DateTime[]{d1,d2};
}

public static DateTime[] getDateRangeFilter(String filter, JSONObject jsonObject) throws ParseException
{
String strval = jsonObject.optString(filter);
if(strval.equals("")){
return null;
}
if (!strval.contains(":")) {
return new DateTime[] { new DateTime(strval), new DateTime(strval) };
}
DateTime d1 = new DateTime(strval.substring(0, strval.indexOf(":")));
DateTime d2 = new DateTime(strval.substring(strval.indexOf(":")+1));
return new DateTime[]{d1,d2};
}

public static boolean getBooleanFilter(String filter, HttpServletRequest req) {
String stringFilter = getStringFilter(filter, req);
return Boolean.parseBoolean(stringFilter);
Expand Down
129 changes: 99 additions & 30 deletions src/main/java/org/opensrp/web/rest/SearchResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming method name


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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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>();
Expand All @@ -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>();

Expand Down
Loading