From 94fc8fb2cc8cfcc374035c0b60d0899123bf022c Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 16 Nov 2022 12:35:55 +0300 Subject: [PATCH 1/4] allow search via post --- .../java/org/opensrp/web/rest/RestUtils.java | 49 ++- .../org/opensrp/web/rest/SearchResource.java | 167 ++++++++--- .../org/opensrp/web/utils/SearchHelper.java | 280 +++++++++++++----- .../opensrp/web/rest/SearchResourceTest.java | 69 +++-- .../opensrp/web/utils/SearchHelperTest.java | 164 +++++++++- 5 files changed, 575 insertions(+), 154 deletions(-) diff --git a/src/main/java/org/opensrp/web/rest/RestUtils.java b/src/main/java/org/opensrp/web/rest/RestUtils.java index 2394967fd..a1afa9333 100644 --- a/src/main/java/org/opensrp/web/rest/RestUtils.java +++ b/src/main/java/org/opensrp/web/rest/RestUtils.java @@ -19,6 +19,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.domain.Multimedia; import org.opensrp.service.multimedia.MultimediaFileManager; import org.opensrp.service.multimedia.ObjectStorageMultimediaFileManager; @@ -53,12 +54,7 @@ public static Integer getIntegerFilter(String filter, HttpServletRequest req) String strval = getStringFilter(filter, req); return strval == null ? null : Integer.parseInt(strval); } - - public static boolean getBooleanFilter(String filter, HttpServletRequest req) { - String stringFilter = getStringFilter(filter, req); - return Boolean.parseBoolean(stringFilter); - } - + public static Float getFloatFilter(String filter, HttpServletRequest req) { String strval = getStringFilter(filter, req); @@ -73,21 +69,42 @@ public static DateTime getDateFilter(String filter, HttpServletRequest req) thro public static DateTime[] getDateRangeFilter(String filter, HttpServletRequest req) throws ParseException { - String strval = getStringFilter(filter, req); - if(strval == null){ - return null; - } - DateTime d1 = new DateTime(strval.substring(0, strval.indexOf(":"))); - DateTime d2 = new DateTime(strval.substring(strval.indexOf(":")+1)); - return new DateTime[]{d1,d2}; + String strval = getStringFilter(filter, req); + if(strval == null){ + 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 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); + } + public static void main(String[] args) { System.out.println(new DateTime("​1458932400000")); } - public static String setDateFilter(Date date) throws ParseException + public static synchronized String setDateFilter(Date date) throws ParseException { return date == null ? null : SDF.format(date); } diff --git a/src/main/java/org/opensrp/web/rest/SearchResource.java b/src/main/java/org/opensrp/web/rest/SearchResource.java index 5c5374dea..5f3742604 100644 --- a/src/main/java/org/opensrp/web/rest/SearchResource.java +++ b/src/main/java/org/opensrp/web/rest/SearchResource.java @@ -1,25 +1,10 @@ package org.opensrp.web.rest; -import static org.opensrp.common.AllConstants.BaseEntity.LAST_UPDATE; -import static org.opensrp.common.AllConstants.Client.BIRTH_DATE; -import static org.opensrp.common.AllConstants.Client.FIRST_NAME; -import static org.opensrp.common.AllConstants.Client.GENDER; -import static org.opensrp.common.AllConstants.Client.LAST_NAME; -import static org.opensrp.common.AllConstants.Client.MIDDLE_NAME; -import static org.opensrp.web.rest.RestUtils.getStringFilter; - -import java.text.ParseException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; - import org.apache.commons.lang3.StringUtils; 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.smartregister.domain.Client; import org.smartregister.domain.Event; @@ -33,9 +18,32 @@ 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; +import javax.servlet.http.HttpServletRequest; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.opensrp.common.AllConstants.BaseEntity.LAST_UPDATE; +import static org.opensrp.common.AllConstants.Client.ALT_NAME; +import static org.opensrp.common.AllConstants.Client.PHONE_NUMBER; +import static org.opensrp.common.AllConstants.Client.FIRST_NAME; +import static org.opensrp.common.AllConstants.Client.LAST_NAME; +import static org.opensrp.common.AllConstants.Client.MIDDLE_NAME; +import static org.opensrp.common.AllConstants.Client.ATTRIBUTE; +import static org.opensrp.common.AllConstants.Client.IDENTIFIER; +import static org.opensrp.common.AllConstants.Client.GENDER; +import static org.opensrp.common.AllConstants.Client.NAME; +import static org.opensrp.common.AllConstants.Client.ALT_PHONE_NUMBER; +import static org.opensrp.common.AllConstants.Client.BIRTH_DATE; +import static org.opensrp.web.rest.RestUtils.getStringFilter; + @Controller @RequestMapping(value = "/rest/search") public class SearchResource extends RestResource { @@ -54,15 +62,25 @@ public SearchResource(SearchService searchService, ClientService clientService, this.clientService = clientService; this.eventService = eventService; } - + + /** + * @param request + * contains search parameter of with attributes and full colon e.g + * 1. search?attributes=phone_number:072700000 + * or search parameter without attribute and without colon e.g + * 2. search?phone_number=072700000 + * @throws ParseException + */ @Override public List search(HttpServletRequest request) throws ParseException {//TODO search should not call different url but only add params String firstName = getStringFilter(FIRST_NAME, request); String middleName = getStringFilter(MIDDLE_NAME, request); String lastName = getStringFilter(LAST_NAME, request); - + Optional phoneNumber = Optional.ofNullable(getStringFilter(PHONE_NUMBER, request)); + Optional altPhoneNumber = Optional.ofNullable(getStringFilter(ALT_PHONE_NUMBER, request)); + Optional alternateName = Optional.ofNullable(getStringFilter(ALT_NAME, request)); ClientSearchBean searchBean = new ClientSearchBean(); - searchBean.setNameLike(getStringFilter("name", request)); + searchBean.setNameLike(getStringFilter(NAME, request)); searchBean.setGender(getStringFilter(GENDER, request)); DateTime[] birthdate = RestUtils.getDateRangeFilter(BIRTH_DATE, request);//TODO add ranges like fhir do http://hl7.org/fhir/search.html @@ -77,19 +95,71 @@ public List search(HttpServletRequest request) throws ParseException {// searchBean.setLastEditFrom(lastEdit[0]); searchBean.setLastEditTo(lastEdit[1]); } - Map attributeMap = null; - String attributes = getStringFilter("attribute", request); + Map attributeMap = new HashMap<>(); + String attributes = getStringFilter(ATTRIBUTE, request); + 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 identifierMap = null; + String identifiers = getStringFilter(IDENTIFIER, request); + 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(); + identifierMap.put(identifierType, identifierValue); + } + + searchBean.setIdentifiers(identifierMap); + return searchService.searchClient(searchBean, firstName, middleName, lastName, null); + } + + @RequestMapping(method = RequestMethod.POST, value = "/search", produces = { MediaType.APPLICATION_JSON_VALUE }) + public List 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 phoneNumber = Optional.ofNullable(jsonObject.optString(PHONE_NUMBER)); + Optional altPhoneNumber = Optional.ofNullable(jsonObject.optString(ALT_PHONE_NUMBER)); + Optional 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 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 = new HashMap(); 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 identifierMap = null; - String identifiers = getStringFilter("identifier", request); + 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]; @@ -104,45 +174,62 @@ public List search(HttpServletRequest request) throws ParseException {// @RequestMapping(method = RequestMethod.GET, value = "/path", produces = { MediaType.APPLICATION_JSON_VALUE }) private List 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 searchPathBy(@RequestBody String jsonRequestBody) throws ParseException { + + 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 searchAndProcess(SearchEntityWrapper childSearchEntity, SearchEntityWrapper motherSearchEntity, + String contactPhoneNumber){ try { //Process clients search via demographics ClientSearchBean searchBean = new ClientSearchBean(); List children = new ArrayList(); - - 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 mothers = new ArrayList(); - + 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 clientBaseEntityIds = getClientBaseEntityIdsByContactPhoneNumber(contactPhoneNumber); - + List eventChildren = clientService.findByFieldValue(BaseEntity.BASE_ENTITY_ID, clientBaseEntityIds); - + children = SearchHelper.intersection(children, eventChildren);// Search conjunction is "AND" find intersection - + List linkedMothers = new ArrayList(); - + String RELATIONSHIP_KEY = "mother"; if (!children.isEmpty()) { List clientIds = new ArrayList(); @@ -152,7 +239,7 @@ private List searchPathBy(HttpServletRequest request) throws ParseE clientIds.add(relationshipId); } } - + linkedMothers = clientService.findByFieldValue(BaseEntity.BASE_ENTITY_ID, clientIds); } diff --git a/src/main/java/org/opensrp/web/utils/SearchHelper.java b/src/main/java/org/opensrp/web/utils/SearchHelper.java index f003f122e..dfae23949 100644 --- a/src/main/java/org/opensrp/web/utils/SearchHelper.java +++ b/src/main/java/org/opensrp/web/utils/SearchHelper.java @@ -8,6 +8,7 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; +import org.json.JSONObject; import org.smartregister.domain.Client; import org.opensrp.search.ClientSearchBean; import org.opensrp.web.rest.RestUtils; @@ -16,26 +17,37 @@ import static org.opensrp.common.AllConstants.Client.GENDER; public class SearchHelper { - + + public static final String ZEIR_ID = "zeir_id"; + public static final String OPENSRP_ID = "opensrp_id"; + + public static final String SIM_PRINT_GUID = "simprints_guid"; + + public static final String FIRST_NAME = "first_name"; + public static final String MIDDLE_NAME = "middle_name"; + public static final String LAST_NAME = "last_name"; + public static final String BIRTH_DATE = "birth_date"; + + //Attributes + public static final String INACTIVE = "inactive"; + public static final String LOST_TO_FOLLOW_UP = "lost_to_follow_up"; + public static final String NFC_CARD_IDENTIFIER = "nfc_card_identifier"; + + // Mother + public static final String MOTHER_GUARDIAN_FIRST_NAME = "mother_first_name"; + public static final String MOTHER_GUARDIAN_LAST_NAME = "mother_last_name"; + public static final String MOTHER_GUARDIAN_NRC_NUMBER = "mother_nrc_number"; + public static final String MOTHER_COMPASS_RELATIONSHIP_ID = "mother_compass_relationship_id"; + + public static final String NRC_NUMBER_KEY = "NRC_Number"; + public static final String COMPASS_RELATIONSHIP_ID = "Compass_Relationship_ID"; + public static SearchEntityWrapper childSearchParamProcessor(HttpServletRequest request) throws ParseException { ClientSearchBean searchBean = new ClientSearchBean(); - - String ZEIR_ID = "zeir_id"; - String OPENSRP_ID = "opensrp_id"; - String SIM_PRINT_GUID = "simprints_guid"; - - String FIRST_NAME = "first_name"; - String MIDDLE_NAME = "middle_name"; - String LAST_NAME = "last_name"; - String BIRTH_DATE = "birth_date"; - - //Attributes - String INACTIVE = "inactive"; - String LOST_TO_FOLLOW_UP = "lost_to_follow_up"; - String NFC_CARD_IDENTIFIER = "nfc_card_identifier"; - + + Integer limit = RestUtils.getIntegerFilter("limit", request); if (limit == null || limit.intValue() == 0) { limit = 100; @@ -46,10 +58,6 @@ public static SearchEntityWrapper childSearchParamProcessor(HttpServletRequest r searchBean.setLastEditFrom(lastEdit[0]); searchBean.setLastEditTo(lastEdit[1]); } - - String zeirId = RestUtils.getStringFilter(ZEIR_ID, request); - String opensrpId = RestUtils.getStringFilter(OPENSRP_ID, request); - String simprintsGuid = RestUtils.getStringFilter(SIM_PRINT_GUID, request); @@ -57,21 +65,155 @@ public static SearchEntityWrapper childSearchParamProcessor(HttpServletRequest r searchBean.setMiddleName(RestUtils.getStringFilter(MIDDLE_NAME, request)); searchBean.setLastName(RestUtils.getStringFilter(LAST_NAME, request)); searchBean.setGender(RestUtils.getStringFilter(GENDER, request)); - - String inActive = RestUtils.getStringFilter(INACTIVE, request); - String lostToFollowUp = RestUtils.getStringFilter(LOST_TO_FOLLOW_UP, request); - String nfcCardIdentifier = RestUtils.getStringFilter(NFC_CARD_IDENTIFIER, request); - - DateTime[] birthdate = RestUtils.getDateRangeFilter(BIRTH_DATE, request);//TODO add ranges like fhir do http://hl7.org/fhir/search.html - + + DateTime[] birthdate = RestUtils + .getDateRangeFilter(BIRTH_DATE, request);//TODO add ranges like fhir do http://hl7.org/fhir/search.html + + //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]); + } + + + Map commonSearchParams = new HashMap<>(); + commonSearchParams.put(ZEIR_ID, RestUtils.getStringFilter(ZEIR_ID, request)); + commonSearchParams.put(OPENSRP_ID, RestUtils.getStringFilter(OPENSRP_ID, request)); + commonSearchParams.put(SIM_PRINT_GUID, RestUtils.getStringFilter(SIM_PRINT_GUID, request)); + commonSearchParams.put(INACTIVE, RestUtils.getStringFilter(INACTIVE, request)); + commonSearchParams.put(LOST_TO_FOLLOW_UP, RestUtils.getStringFilter(LOST_TO_FOLLOW_UP, request)); + commonSearchParams.put(NFC_CARD_IDENTIFIER, RestUtils.getStringFilter(NFC_CARD_IDENTIFIER, request)); + + setIdentifiersAndAttributeToChildSearchBean(commonSearchParams, searchBean); + + boolean isValid = isSearchValid(searchBean); + + return new SearchEntityWrapper(isValid, searchBean, limit); + } + + public static SearchEntityWrapper motherSearchParamProcessor(HttpServletRequest request) throws ParseException { + + ClientSearchBean motherSearchBean = new ClientSearchBean(); + + Integer limit = setCoreFilters(request, motherSearchBean); + + + + String motherGuardianNrc = RestUtils.getStringFilter(MOTHER_GUARDIAN_NRC_NUMBER, request); + String compassRelationshipId = RestUtils.getStringFilter(MOTHER_COMPASS_RELATIONSHIP_ID, request); + + motherSearchBean.setFirstName(RestUtils.getStringFilter(MOTHER_GUARDIAN_FIRST_NAME, request)); + motherSearchBean.setLastName(RestUtils.getStringFilter(MOTHER_GUARDIAN_LAST_NAME, request)); + + setNameLikeAndAtrributesOnMotherSearchBean(motherGuardianNrc,compassRelationshipId, motherSearchBean); + + boolean isValid = isSearchValid(motherSearchBean); + + return new SearchEntityWrapper(isValid, motherSearchBean, limit); + } + + public static SearchEntityWrapper childSearchParamProcessor(JSONObject jsonObject) throws ParseException { + + ClientSearchBean searchBean = new ClientSearchBean(); + + Integer limit = !jsonObject.optString("limit").equals("") ? Integer.parseInt(jsonObject.optString("limit")) + : jsonObject.optInt("limit"); + if (limit == 0) { + limit = 100; + } + + DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, jsonObject);//TODO client by provider id + if (lastEdit != null) { + searchBean.setLastEditFrom(lastEdit[0]); + searchBean.setLastEditTo(lastEdit[1]); + } + + searchBean.setFirstName(jsonObject.optString(FIRST_NAME)); + searchBean.setMiddleName(jsonObject.optString(MIDDLE_NAME)); + searchBean.setLastName(jsonObject.optString(LAST_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 + //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]); } + + Map commonSearchParams = new HashMap<>(); + commonSearchParams.put(ZEIR_ID, jsonObject.optString(ZEIR_ID)); + commonSearchParams.put(OPENSRP_ID, jsonObject.optString(OPENSRP_ID)); + commonSearchParams.put(SIM_PRINT_GUID, jsonObject.optString(SIM_PRINT_GUID)); + commonSearchParams.put(INACTIVE, jsonObject.optString(INACTIVE)); + commonSearchParams.put(LOST_TO_FOLLOW_UP, jsonObject.optString(LOST_TO_FOLLOW_UP)); + commonSearchParams.put(NFC_CARD_IDENTIFIER, jsonObject.optString(NFC_CARD_IDENTIFIER)); + + setIdentifiersAndAttributeToChildSearchBean(commonSearchParams, searchBean); + + boolean isValid = isSearchValid(searchBean); + + return new SearchEntityWrapper(isValid, searchBean, limit); + } + + public static SearchEntityWrapper motherSearchParamProcessor(JSONObject jsonObject) throws ParseException { + + ClientSearchBean motherSearchBean = new ClientSearchBean(); + + Integer limit = setCoreFilters(jsonObject, motherSearchBean); + + String motherGuardianNrc = jsonObject.optString(MOTHER_GUARDIAN_NRC_NUMBER); + String compassRelationshipId = jsonObject.optString(MOTHER_COMPASS_RELATIONSHIP_ID); + + motherSearchBean.setFirstName(jsonObject.optString(MOTHER_GUARDIAN_FIRST_NAME)); + motherSearchBean.setLastName(jsonObject.optString(MOTHER_GUARDIAN_LAST_NAME)); + + setNameLikeAndAtrributesOnMotherSearchBean(motherGuardianNrc,compassRelationshipId, motherSearchBean); + + boolean isValid = isSearchValid(motherSearchBean); + + return new SearchEntityWrapper(isValid, motherSearchBean, limit); + } + + public static void setNameLikeAndAtrributesOnMotherSearchBean(String motherGuardianNrc, + String compassRelationshipId, + ClientSearchBean motherSearchBean){ + Map motherAttributes = new HashMap<>(); + if (!StringUtils.isBlank(motherGuardianNrc)) { + motherAttributes.put(NRC_NUMBER_KEY, motherGuardianNrc); + } + if (!StringUtils.isBlank(compassRelationshipId)) { + motherAttributes.put(COMPASS_RELATIONSHIP_ID, compassRelationshipId); + } + + String nameLike = null; + + if (!StringUtils.isBlank(motherSearchBean.getFirstName()) + && StringUtils.containsWhitespace(motherSearchBean.getFirstName().trim()) + && StringUtils.isBlank(motherSearchBean.getLastName())) { + String[] arr = motherSearchBean.getFirstName().split("\\s+"); + nameLike = arr[0]; + motherSearchBean.setFirstName(null); + } + + motherSearchBean.setNameLike(nameLike); + motherSearchBean.setAttributes(motherAttributes); + + } + + public static void setIdentifiersAndAttributeToChildSearchBean(Map commonSearchParams, ClientSearchBean searchBean){ Map identifiers = new HashMap(); - // + + String zeirId = commonSearchParams.get(ZEIR_ID); + String opensrpId = commonSearchParams.get(OPENSRP_ID); + String simprintsGuid = commonSearchParams.get(SIM_PRINT_GUID); + String lostToFollowUp = commonSearchParams.get(LOST_TO_FOLLOW_UP); + String inActive = commonSearchParams.get(INACTIVE); + String nfcCardIdentifier = commonSearchParams.get(NFC_CARD_IDENTIFIER); + if (!StringUtils.isBlank(zeirId)) { identifiers.put(ZEIR_ID, zeirId); identifiers.put("ZEIR_ID", zeirId); //Maintains backward compatibility with upper case key @@ -104,50 +246,7 @@ public static SearchEntityWrapper childSearchParamProcessor(HttpServletRequest r searchBean.setIdentifiers(identifiers); searchBean.setAttributes(attributes); - - boolean isValid = isSearchValid(searchBean); - - return new SearchEntityWrapper(isValid, searchBean, limit); - } - - public static SearchEntityWrapper motherSearchParamProcessor(HttpServletRequest request) throws ParseException { - - ClientSearchBean motherSearchBean = new ClientSearchBean(); - - Integer limit = setCoreFilters(request, motherSearchBean); - - // Mother - String MOTHER_GUARDIAN_FIRST_NAME = "mother_first_name"; - String MOTHER_GUARDIAN_LAST_NAME = "mother_last_name"; - String MOTHER_GUARDIAN_NRC_NUMBER = "mother_nrc_number"; - - String motherGuardianNrc = RestUtils.getStringFilter(MOTHER_GUARDIAN_NRC_NUMBER, request); - - motherSearchBean.setFirstName(RestUtils.getStringFilter(MOTHER_GUARDIAN_FIRST_NAME, request)); - motherSearchBean.setLastName(RestUtils.getStringFilter(MOTHER_GUARDIAN_LAST_NAME, request)); - - String NRC_NUMBER_KEY = "NRC_Number"; - Map motherAttributes = new HashMap(); - if (!StringUtils.isBlank(motherGuardianNrc)) { - motherAttributes.put(NRC_NUMBER_KEY, motherGuardianNrc); - } - - String nameLike = null; - - if (!StringUtils.isBlank(motherSearchBean.getFirstName()) - && StringUtils.containsWhitespace(motherSearchBean.getFirstName().trim()) - && StringUtils.isBlank(motherSearchBean.getLastName())) { - String[] arr = motherSearchBean.getFirstName().split("\\s+"); - nameLike = arr[0]; - motherSearchBean.setFirstName(null); - } - - motherSearchBean.setNameLike(nameLike); - motherSearchBean.setAttributes(motherAttributes); - - boolean isValid = isSearchValid(motherSearchBean); - - return new SearchEntityWrapper(isValid, motherSearchBean, limit); + } public static Integer setCoreFilters(HttpServletRequest request, ClientSearchBean searchBean) throws ParseException { @@ -162,7 +261,24 @@ public static Integer setCoreFilters(HttpServletRequest request, ClientSearchBea searchBean.setLastEditFrom(lastEdit[0]); searchBean.setLastEditTo(lastEdit[1]); } - + + return limit; + } + + public static Integer setCoreFilters(JSONObject jsonObject, ClientSearchBean searchBean) throws ParseException { + + Integer limit = !jsonObject.optString("limit").equals("") ? Integer.parseInt(jsonObject.optString("limit")) + : jsonObject.optInt("limit"); + if (limit == 0) { + limit = 100; + } + + DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, jsonObject);//TODO client by provider id + if (lastEdit != null) { + searchBean.setLastEditFrom(lastEdit[0]); + searchBean.setLastEditTo(lastEdit[1]); + } + return limit; } @@ -259,9 +375,21 @@ public static String getContactPhoneNumberParam(HttpServletRequest request) { String CONTACT_PHONE_NUMBER = "contact_phone_number"; String motherGuardianPhoneNumber = RestUtils.getStringFilter(MOTHER_GUARDIAN_PHONE_NUMBER, request); motherGuardianPhoneNumber = StringUtils.isBlank(motherGuardianPhoneNumber) - ? RestUtils.getStringFilter(CONTACT_PHONE_NUMBER, request) - : motherGuardianPhoneNumber; - + ? RestUtils.getStringFilter(CONTACT_PHONE_NUMBER, request) + : motherGuardianPhoneNumber; + + return motherGuardianPhoneNumber; + } + + public static String getContactPhoneNumberParam(JSONObject jsonObject) { + //Search by mother contact number + String MOTHER_GUARDIAN_PHONE_NUMBER = "mother_contact_phone_number"; + String CONTACT_PHONE_NUMBER = "contact_phone_number"; + String motherGuardianPhoneNumber = jsonObject.optString(MOTHER_GUARDIAN_PHONE_NUMBER); + motherGuardianPhoneNumber = StringUtils.isBlank(motherGuardianPhoneNumber) + ? jsonObject.optString(CONTACT_PHONE_NUMBER) + : motherGuardianPhoneNumber; + return motherGuardianPhoneNumber; } diff --git a/src/test/java/org/opensrp/web/rest/SearchResourceTest.java b/src/test/java/org/opensrp/web/rest/SearchResourceTest.java index ad386f8a9..c12091911 100644 --- a/src/test/java/org/opensrp/web/rest/SearchResourceTest.java +++ b/src/test/java/org/opensrp/web/rest/SearchResourceTest.java @@ -1,31 +1,31 @@ package org.opensrp.web.rest; -import java.util.Arrays; -import java.util.List; - +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.opensrp.repository.PlanRepository; -import org.opensrp.service.TaskGenerator; +import org.opensrp.service.*; import org.smartregister.domain.Client; import org.opensrp.repository.ClientsRepository; import org.opensrp.repository.EventsRepository; import org.opensrp.repository.SearchRepository; -import org.opensrp.service.ClientService; -import org.opensrp.service.EventService; -import org.opensrp.service.SearchService; import org.opensrp.web.rest.it.TestWebContextLoader; import org.opensrp.web.utils.SearchHelper; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.context.WebApplicationContext; -import junit.framework.Assert; +import java.text.ParseException; +import java.util.Arrays; +import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = TestWebContextLoader.class, locations = { "classpath:test-webmvc-config.xml", }) @@ -40,21 +40,29 @@ public class SearchResourceTest { private ClientService clientService; private EventService eventService; - - private SearchRepository searchRepository; - - private ClientsRepository clientRepository; - - private EventsRepository eventsRepository; + + private ExportEventDataMapper exportEventDataMapper; + private TaskGenerator taskGenerator; private PlanRepository planRepository; - + MockHttpServletRequest mockHttpServletRequest; + String phoneNumber = "0727000000"; + String town = "town"; + + String firstName = "name"; + + String male = "male"; + + DateTime birthDate = new DateTime(0l, DateTimeZone.UTC); + @Before public void setUp() { - - searchRepository = Mockito.mock(SearchRepository.class); + SearchRepository searchRepository = Mockito.mock(SearchRepository.class); + ClientsRepository clientRepository = Mockito.mock(ClientsRepository.class); + EventsRepository eventsRepository = Mockito.mock(EventsRepository.class); + searchService = Mockito.spy(new SearchService(searchRepository)); clientService = Mockito.spy(new ClientService(clientRepository)); eventService = Mockito.spy(new EventService(eventsRepository, clientService,taskGenerator,planRepository,null)); @@ -62,7 +70,7 @@ public void setUp() { } @Test - public void testInstantanceCreatesCorrectly() throws Exception { + public void testInstantanceCreatesCorrectly() { SearchResource searchResource = new SearchResource(searchService, clientService, eventService); Assert.assertNotNull(searchResource); @@ -80,4 +88,29 @@ public void testIntersectionMethodReturnsCorrectResult() throws Exception { Assert.assertEquals(listA, result); } + @Test + public void shouldSearchClient() throws ParseException { + mockHttpServletRequest = new MockHttpServletRequest(); + mockHttpServletRequest.addParameter("ff", "ona"); + mockHttpServletRequest.addParameter("phone_number", phoneNumber); + mockHttpServletRequest.addParameter("alt_phone_number", phoneNumber); + mockHttpServletRequest.addParameter("alt_name", firstName); + mockHttpServletRequest.addParameter("attribute", "next_contact_date:2022-06-15"); + mockHttpServletRequest.addParameter("dob", String.valueOf(birthDate)); + mockHttpServletRequest.addParameter("identifier", "fsdf"+":"+ "sfdf"); + SearchResource searchResource=new SearchResource(searchService,clientService,eventService); + List clients = searchResource.search(mockHttpServletRequest); + Assert.assertNotNull(clients); + } + + @Test + public void shouldSearchClientPost() throws ParseException { + String jsonRequestString = "{\"ff\":\"ona\",\"identifier\":\"fsdf:sfdf\",\"alt_name\":\"name\"," + + "\"alt_phone_number\":\"0727000000\",\"dob\":\"1970-01-01T00:00:00.000Z\",\"phone_number\":\"0727000000\"," + + "\"attribute\":\"next_contact_date:2022-06-15\"}"; + SearchResource searchResource=new SearchResource(searchService,clientService,eventService); + List clients = searchResource.searchByPost(jsonRequestString); + Assert.assertNotNull(clients); + + } } diff --git a/src/test/java/org/opensrp/web/utils/SearchHelperTest.java b/src/test/java/org/opensrp/web/utils/SearchHelperTest.java index 0055f09d6..c37e51860 100644 --- a/src/test/java/org/opensrp/web/utils/SearchHelperTest.java +++ b/src/test/java/org/opensrp/web/utils/SearchHelperTest.java @@ -1,14 +1,14 @@ package org.opensrp.web.utils; import java.text.ParseException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; +import org.json.JSONObject; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.opensrp.common.util.EasyMap; +import org.opensrp.search.ClientSearchBean; import org.smartregister.domain.Client; import org.springframework.mock.web.MockHttpServletRequest; @@ -233,5 +233,161 @@ public void childSearchParamProcessorShouldHaveCorrectIdentifierWhenCreatingBea String result = searchEntityWrapper.getClientSearchBean().getIdentifiers().get("opensrp_id"); Assert.assertEquals("2093980", result); } - + + @Test + public void testProcessSearchResult() { + final String mother = "mother"; + + Client motherClient = new Client("dummy-mother-base-entity-id"); + motherClient.setFirstName("Jane"); + motherClient.setLastName("Doe"); + motherClient.setGender("FEMALE"); + motherClient.setIdentifiers(EasyMap.mapOf("M_ZEIR_ID", "673939_mother")); + List motherList = new ArrayList<>(); + motherList.add(motherClient); + + List list = new ArrayList<>(); + list.add("dummy-mother-base-entity-id"); + + Map> relationships = new HashMap<>(); + relationships.put(mother, list); + + Client child = new Client("dummy-mother-base-entity-id"); + child.setFirstName("John"); + child.setLastName("Doe"); + child.setGender("Male"); + child.setRelationships(relationships); + child.setIdentifiers(EasyMap.mapOf("M_ZEIR_ID", "673939_mother")); + + List childList = new ArrayList<>(); + childList.add(child); + + List childMothers = SearchHelper.processSearchResult(childList, motherList, mother); + Assert.assertEquals(1, childMothers.size()); + Assert.assertEquals("John Doe", childMothers.get(0).getChild().fullName()); + } + + @Test + public void testMotherSearchParamProcessorForHttpServletRequest() throws ParseException { + HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); + Mockito.when(httpServletRequest.getParameter("limit")).thenReturn("0"); + Mockito.when(httpServletRequest.getParameter("mother_first_name")).thenReturn("Jane"); + Mockito.when(httpServletRequest.getParameter("mother_last_name")).thenReturn("Doe"); + Mockito.when(httpServletRequest.getParameter("mother_nrc_number")).thenReturn("2093980"); + Mockito.when(httpServletRequest.getParameter("NRC_Number")).thenReturn("20939801123"); + Mockito.when(httpServletRequest.getParameter("mother_compass_relationship_id")).thenReturn("dab102f71bd"); + SearchEntityWrapper searchEntityWrapper = SearchHelper.motherSearchParamProcessor(httpServletRequest); + Map result = searchEntityWrapper.getClientSearchBean().getAttributes(); + Assert.assertEquals(2, result.size()); + Assert.assertTrue( result.containsKey("NRC_Number")); + Assert.assertTrue( result.containsKey("Compass_Relationship_ID")); + Assert.assertEquals("2093980", result.get("NRC_Number")); + } + + @Test + public void testMotherSearchParamProcessorForJSONObject() throws ParseException { + JSONObject jsonObject = Mockito.mock(JSONObject.class); + Mockito.when(jsonObject.optString("limit")).thenReturn("0"); + Mockito.when(jsonObject.optString("mother_first_name")).thenReturn("Jane"); + Mockito.when(jsonObject.optString("mother_last_name")).thenReturn("Doe"); + Mockito.when(jsonObject.optString("mother_nrc_number")).thenReturn("2093980"); + Mockito.when(jsonObject.optString("NRC_Number")).thenReturn("20939801123"); + Mockito.when(jsonObject.optString("mother_compass_relationship_id")).thenReturn("dab102f71bd"); + Mockito.when(jsonObject.optString("lastEdited")).thenReturn(""); + SearchEntityWrapper searchEntityWrapper = SearchHelper.motherSearchParamProcessor(jsonObject); + Map result = searchEntityWrapper.getClientSearchBean().getAttributes(); + Assert.assertEquals(2, result.size()); + Assert.assertTrue( result.containsKey("NRC_Number")); + Assert.assertTrue( result.containsKey("Compass_Relationship_ID")); + Assert.assertEquals("2093980", result.get("NRC_Number")); + } + + @Test + public void testChildSearchParamProcessorForJSONObject() throws ParseException { + JSONObject jsonObject = Mockito.mock(JSONObject.class); + Mockito.when(jsonObject.optString("limit")).thenReturn("50"); + Mockito.when(jsonObject.optString("lastEdited")).thenReturn(""); + Mockito.when(jsonObject.optString(SearchHelper.BIRTH_DATE)).thenReturn(""); + Mockito.when(jsonObject.optString(SearchHelper.ZEIR_ID)).thenReturn("1234"); + Mockito.when(jsonObject.optString(SearchHelper.OPENSRP_ID)).thenReturn("4567"); + Mockito.when(jsonObject.optString(SearchHelper.SIM_PRINT_GUID)).thenReturn("91011"); + Mockito.when(jsonObject.optString(SearchHelper.INACTIVE)).thenReturn("false"); + Mockito.when(jsonObject.optString(SearchHelper.LOST_TO_FOLLOW_UP)).thenReturn("true"); + Mockito.when(jsonObject.optString(SearchHelper.NFC_CARD_IDENTIFIER)).thenReturn("nfc_card_identifier_1"); + SearchEntityWrapper searchEntityWrapper = SearchHelper.childSearchParamProcessor(jsonObject); + + Map attributes = searchEntityWrapper.getClientSearchBean().getAttributes(); + Assert.assertEquals(3, attributes.size()); + + Map identifiers = searchEntityWrapper.getClientSearchBean().getIdentifiers(); + Assert.assertEquals(4, identifiers.size()); + + Assert.assertTrue(identifiers.containsKey(SearchHelper.ZEIR_ID)); + Assert.assertTrue(identifiers.containsKey("ZEIR_ID")); //check backward compatibility with upper case key + Assert.assertTrue(identifiers.containsKey(SearchHelper.SIM_PRINT_GUID)); + + Assert.assertTrue(attributes.containsKey(SearchHelper.INACTIVE)); + Assert.assertTrue(attributes.containsKey(SearchHelper.LOST_TO_FOLLOW_UP)); + Assert.assertTrue(attributes.containsKey("NFC_Card_Identifier")); + + Assert.assertEquals(identifiers.get(SearchHelper.ZEIR_ID), "1234"); + Assert.assertEquals(attributes.get("NFC_Card_Identifier"), "nfc_card_identifier_1"); + } + + @Test + public void testChildSearchParamProcessorForHttpServletRequest() throws ParseException { + HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); + Mockito.when(httpServletRequest.getParameter("limit")).thenReturn("50"); + Mockito.when(httpServletRequest.getParameter("lastEdited")).thenReturn(""); + Mockito.when(httpServletRequest.getParameter(SearchHelper.BIRTH_DATE)).thenReturn(""); + Mockito.when(httpServletRequest.getParameter(SearchHelper.ZEIR_ID)).thenReturn("1234"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.OPENSRP_ID)).thenReturn("4567"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.SIM_PRINT_GUID)).thenReturn("91011"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.INACTIVE)).thenReturn("false"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.LOST_TO_FOLLOW_UP)).thenReturn("true"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.NFC_CARD_IDENTIFIER)).thenReturn("nfc_card_identifier_1"); + SearchEntityWrapper searchEntityWrapper = SearchHelper.childSearchParamProcessor(httpServletRequest); + + Map attributes = searchEntityWrapper.getClientSearchBean().getAttributes(); + Assert.assertEquals(3, attributes.size()); + + Map identifiers = searchEntityWrapper.getClientSearchBean().getIdentifiers(); + Assert.assertEquals(4, identifiers.size()); + + Assert.assertTrue(identifiers.containsKey(SearchHelper.ZEIR_ID)); + Assert.assertTrue(identifiers.containsKey("ZEIR_ID")); //check backward compatibility with upper case key + Assert.assertTrue(identifiers.containsKey(SearchHelper.SIM_PRINT_GUID)); + + Assert.assertTrue(attributes.containsKey(SearchHelper.INACTIVE)); + Assert.assertTrue(attributes.containsKey(SearchHelper.LOST_TO_FOLLOW_UP)); + Assert.assertTrue(attributes.containsKey("NFC_Card_Identifier")); + + Assert.assertEquals(identifiers.get(SearchHelper.ZEIR_ID), "1234"); + Assert.assertEquals(attributes.get("NFC_Card_Identifier"), "nfc_card_identifier_1"); + } + + @Test + public void testSetCoreFiltersForJSONObjectWithIntegerLimitReturnsValue(){ + JSONObject jsonObject = new JSONObject(); + jsonObject.put("limit", 50); + try { + int result = SearchHelper.setCoreFilters(jsonObject, new ClientSearchBean()); + Assert.assertEquals(50,result); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + @Test + public void testSetCoreFiltersForJSONObjectWithStringLimitReturnsValue(){ + JSONObject jsonObject = new JSONObject(); + jsonObject.put("limit", "50"); + try { + int result = SearchHelper.setCoreFilters(jsonObject, new ClientSearchBean()); + Assert.assertEquals(50,result); + } catch (ParseException e) { + e.printStackTrace(); + } + } + } From 97958251c4e7c295e4ad5e6d0f76bf6857ed7201 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Thu, 17 Nov 2022 16:09:19 +0300 Subject: [PATCH 2/4] run code formatter --- .../java/org/opensrp/web/rest/RestUtils.java | 266 +++---- .../org/opensrp/web/rest/SearchResource.java | 544 +++++++------ .../org/opensrp/web/utils/SearchHelper.java | 714 +++++++++-------- .../opensrp/web/rest/SearchResourceTest.java | 175 ++-- .../opensrp/web/utils/SearchHelperTest.java | 750 +++++++++--------- 5 files changed, 1223 insertions(+), 1226 deletions(-) diff --git a/src/main/java/org/opensrp/web/rest/RestUtils.java b/src/main/java/org/opensrp/web/rest/RestUtils.java index a1afa9333..9130558b3 100644 --- a/src/main/java/org/opensrp/web/rest/RestUtils.java +++ b/src/main/java/org/opensrp/web/rest/RestUtils.java @@ -26,116 +26,108 @@ import org.springframework.http.HttpHeaders; public class RestUtils { - public static final String DATE_FORMAT = "dd-MM-yyyy"; - public static final SimpleDateFormat SDF = new SimpleDateFormat("dd-MM-yyyy"); - public static final String DATETIME_FORMAT = "dd-MM-yyyy HH:mm"; - public static final SimpleDateFormat SDTF = new SimpleDateFormat("dd-MM-yyyy HH:mm"); + public static final String DATE_FORMAT = "dd-MM-yyyy"; + public static final SimpleDateFormat SDF = new SimpleDateFormat("dd-MM-yyyy"); + public static final String DATETIME_FORMAT = "dd-MM-yyyy HH:mm"; + public static final SimpleDateFormat SDTF = new SimpleDateFormat("dd-MM-yyyy HH:mm"); - private static final Logger logger = LogManager.getLogger(RestUtils.class.toString()); + private static final Logger logger = LogManager.getLogger(RestUtils.class.toString()); - public static String getStringFilter(String filter, HttpServletRequest req) - { - return StringUtils.isBlank(req.getParameter(filter)) ? null : req.getParameter(filter); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public static Enum getEnumFilter(String filter, Class cls, HttpServletRequest req) - { - String filterVal = getStringFilter(filter, req); - if (filterVal != null) { - return Enum.valueOf(cls, filterVal); - } - return null; - } - - public static Integer getIntegerFilter(String filter, HttpServletRequest req) - { - String strval = getStringFilter(filter, req); - return strval == null ? null : Integer.parseInt(strval); - } - - public static Float getFloatFilter(String filter, HttpServletRequest req) - { - String strval = getStringFilter(filter, req); - return strval == null ? null : Float.parseFloat(strval); - } - - public static DateTime getDateFilter(String filter, HttpServletRequest req) throws ParseException - { - String strval = getStringFilter(filter, req); - return strval == null ? null : new DateTime(strval); - } - - public static DateTime[] getDateRangeFilter(String filter, HttpServletRequest req) throws ParseException - { - String strval = getStringFilter(filter, req); - if(strval == null){ - 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 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); - } - - public static void main(String[] args) { - System.out.println(new DateTime("​1458932400000")); - } - - public static synchronized String setDateFilter(Date date) throws ParseException - { - return date == null ? null : SDF.format(date); - } - - public static void verifyRequiredProperties(List properties, T entity) { - if(properties != null) - for (String p : properties) { - Field[] aaa = entity.getClass().getDeclaredFields(); - for (Field field : aaa) { - if(field.getName().equals(p)){ - field.setAccessible(true); - try { - if(field.get(entity) == null || field.get(entity).toString().trim().equalsIgnoreCase("")){ - throw new RuntimeException("A required field "+p+" was found empty"); - } - } catch (IllegalArgumentException e) { - e.printStackTrace(); - throw new RuntimeException("A required field "+p+" was not found in resource class"); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - } - } - } + public static String getStringFilter(String filter, HttpServletRequest req) { + return StringUtils.isBlank(req.getParameter(filter)) ? null : req.getParameter(filter); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public static Enum getEnumFilter(String filter, Class cls, HttpServletRequest req) { + String filterVal = getStringFilter(filter, req); + if (filterVal != null) { + return Enum.valueOf(cls, filterVal); + } + return null; + } + + public static Integer getIntegerFilter(String filter, HttpServletRequest req) { + String strval = getStringFilter(filter, req); + return strval == null ? null : Integer.parseInt(strval); + } + + public static Float getFloatFilter(String filter, HttpServletRequest req) { + String strval = getStringFilter(filter, req); + return strval == null ? null : Float.parseFloat(strval); + } - public static HttpHeaders getJSONUTF8Headers() { - HttpHeaders responseHeaders = new HttpHeaders(); - responseHeaders.add("Content-Type", "application/json; charset=utf-8"); - return responseHeaders; - } + public static DateTime getDateFilter(String filter, HttpServletRequest req) throws ParseException { + String strval = getStringFilter(filter, req); + return strval == null ? null : new DateTime(strval); + } + + public static DateTime[] getDateRangeFilter(String filter, HttpServletRequest req) throws ParseException { + String strval = getStringFilter(filter, req); + if (strval == null) { + 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 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); + } + + public static void main(String[] args) { + System.out.println(new DateTime("​1458932400000")); + } + + public static synchronized String setDateFilter(Date date) throws ParseException { + return date == null ? null : SDF.format(date); + } + + public static void verifyRequiredProperties(List properties, T entity) { + if (properties != null) + for (String p : properties) { + Field[] aaa = entity.getClass().getDeclaredFields(); + for (Field field : aaa) { + if (field.getName().equals(p)) { + field.setAccessible(true); + try { + if (field.get(entity) == null || field.get(entity).toString().trim().equalsIgnoreCase("")) { + throw new RuntimeException("A required field " + p + " was found empty"); + } + } catch (IllegalArgumentException e) { + e.printStackTrace(); + throw new RuntimeException("A required field " + p + " was not found in resource class"); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + } + } + + public static HttpHeaders getJSONUTF8Headers() { + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.add("Content-Type", "application/json; charset=utf-8"); + return responseHeaders; + } /** * Zips multimedia files and writes content to {@param zipOutputStream} @@ -144,35 +136,35 @@ public static HttpHeaders getJSONUTF8Headers() { * @param multimediaFiles * @throws IOException */ - public static void zipFiles(ZipOutputStream zipOutputStream, List multimediaFiles, MultimediaFileManager fileManager) throws IOException { - for (Multimedia multiMedia : multimediaFiles) { - FileInputStream inputStream; - File file = fileManager.retrieveFile(multiMedia.getFilePath()); - if (file != null) { - logger.info("Adding " + file.getName()); - zipOutputStream.putNextEntry(new ZipEntry(file.getName())); - try { - inputStream = new FileInputStream(file); - } catch (FileNotFoundException e) { - logger.warn("Could not find file " + file.getAbsolutePath()); - continue; - } - - // Write the contents of the file - BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); - int data; - while ((data = bufferedInputStream.read()) != -1) { - zipOutputStream.write(data); - } - bufferedInputStream.close(); - zipOutputStream.closeEntry(); - logger.info("Done downloading file " + file.getName()); - - // clean up temp files (may want to cache in future) - if (fileManager instanceof ObjectStorageMultimediaFileManager) { - file.delete(); - } - } - } - } + public static void zipFiles(ZipOutputStream zipOutputStream, List multimediaFiles, MultimediaFileManager fileManager) throws IOException { + for (Multimedia multiMedia : multimediaFiles) { + FileInputStream inputStream; + File file = fileManager.retrieveFile(multiMedia.getFilePath()); + if (file != null) { + logger.info("Adding " + file.getName()); + zipOutputStream.putNextEntry(new ZipEntry(file.getName())); + try { + inputStream = new FileInputStream(file); + } catch (FileNotFoundException e) { + logger.warn("Could not find file " + file.getAbsolutePath()); + continue; + } + + // Write the contents of the file + BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); + int data; + while ((data = bufferedInputStream.read()) != -1) { + zipOutputStream.write(data); + } + bufferedInputStream.close(); + zipOutputStream.closeEntry(); + logger.info("Done downloading file " + file.getName()); + + // clean up temp files (may want to cache in future) + if (fileManager instanceof ObjectStorageMultimediaFileManager) { + file.delete(); + } + } + } + } } diff --git a/src/main/java/org/opensrp/web/rest/SearchResource.java b/src/main/java/org/opensrp/web/rest/SearchResource.java index 5f3742604..190cb20b0 100644 --- a/src/main/java/org/opensrp/web/rest/SearchResource.java +++ b/src/main/java/org/opensrp/web/rest/SearchResource.java @@ -47,277 +47,275 @@ @Controller @RequestMapping(value = "/rest/search") public class SearchResource extends RestResource { - - private static Logger logger = LogManager.getLogger(SearchResource.class.toString()); - - private SearchService searchService; - - private ClientService clientService; - - private EventService eventService; - - @Autowired - public SearchResource(SearchService searchService, ClientService clientService, EventService eventService) { - this.searchService = searchService; - this.clientService = clientService; - this.eventService = eventService; - } - - /** - * @param request - * contains search parameter of with attributes and full colon e.g - * 1. search?attributes=phone_number:072700000 - * or search parameter without attribute and without colon e.g - * 2. search?phone_number=072700000 - * @throws ParseException - */ - @Override - public List search(HttpServletRequest request) throws ParseException {//TODO search should not call different url but only add params - String firstName = getStringFilter(FIRST_NAME, request); - String middleName = getStringFilter(MIDDLE_NAME, request); - String lastName = getStringFilter(LAST_NAME, request); - Optional phoneNumber = Optional.ofNullable(getStringFilter(PHONE_NUMBER, request)); - Optional altPhoneNumber = Optional.ofNullable(getStringFilter(ALT_PHONE_NUMBER, request)); - Optional alternateName = Optional.ofNullable(getStringFilter(ALT_NAME, request)); - ClientSearchBean searchBean = new ClientSearchBean(); - searchBean.setNameLike(getStringFilter(NAME, request)); - - searchBean.setGender(getStringFilter(GENDER, request)); - DateTime[] birthdate = RestUtils.getDateRangeFilter(BIRTH_DATE, request);//TODO add ranges like fhir do http://hl7.org/fhir/search.html - DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, request);//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 attributeMap = new HashMap<>(); - String attributes = getStringFilter(ATTRIBUTE, request); - 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 identifierMap = null; - String identifiers = getStringFilter(IDENTIFIER, request); - 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(); - identifierMap.put(identifierType, identifierValue); - } - - searchBean.setIdentifiers(identifierMap); - return searchService.searchClient(searchBean, firstName, middleName, lastName, null); - } - - @RequestMapping(method = RequestMethod.POST, value = "/search", produces = { MediaType.APPLICATION_JSON_VALUE }) - public List 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 phoneNumber = Optional.ofNullable(jsonObject.optString(PHONE_NUMBER)); - Optional altPhoneNumber = Optional.ofNullable(jsonObject.optString(ALT_PHONE_NUMBER)); - Optional 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 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 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(); - 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 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 searchPathBy(@RequestBody String jsonRequestBody) throws ParseException { - - 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 searchAndProcess(SearchEntityWrapper childSearchEntity, SearchEntityWrapper motherSearchEntity, - String contactPhoneNumber){ - try { - - //Process clients search via demographics - - ClientSearchBean searchBean = new ClientSearchBean(); - List children = new ArrayList(); - if (childSearchEntity.isValid()) { - searchBean = childSearchEntity.getClientSearchBean(); - children = searchService.searchClient(searchBean, searchBean.getFirstName(), searchBean.getMiddleName(), - searchBean.getLastName(), childSearchEntity.getLimit()); - } - - //Process mothers search via mother demographics - - ClientSearchBean motherSearchBean = new ClientSearchBean(); - List mothers = new ArrayList(); - - if (motherSearchEntity.isValid()) { - motherSearchBean = motherSearchEntity.getClientSearchBean(); - mothers = searchService.searchClient(motherSearchBean, motherSearchBean.getFirstName(), - motherSearchBean.getMiddleName(), motherSearchBean.getLastName(), motherSearchEntity.getLimit()); - } - - //Process clients search via contact phone number - - - List clientBaseEntityIds = getClientBaseEntityIdsByContactPhoneNumber(contactPhoneNumber); - - List eventChildren = clientService.findByFieldValue(BaseEntity.BASE_ENTITY_ID, clientBaseEntityIds); - - children = SearchHelper.intersection(children, eventChildren);// Search conjunction is "AND" find intersection - - List linkedMothers = new ArrayList(); - - String RELATIONSHIP_KEY = "mother"; - if (!children.isEmpty()) { - List clientIds = new ArrayList(); - for (Client c : children) { - String relationshipId = SearchHelper.getRelationalId(c, RELATIONSHIP_KEY); - if (relationshipId != null && !clientIds.contains(relationshipId)) { - clientIds.add(relationshipId); - } - } - - linkedMothers = clientService.findByFieldValue(BaseEntity.BASE_ENTITY_ID, clientIds); - - } - - List linkedChildren = new ArrayList(); - - 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(); - } - } - - public List getClientBaseEntityIdsByContactPhoneNumber(String motherGuardianPhoneNumber) { - List clientBaseEntityIds = new ArrayList(); - - if (!StringUtils.isBlank(motherGuardianPhoneNumber)) { - - List events = eventService.findEventsByConceptAndValue("159635AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", - motherGuardianPhoneNumber); - if (events != null && !events.isEmpty()) { - for (Event event : events) { - String entityId = event.getBaseEntityId(); - if (entityId != null && !clientBaseEntityIds.contains(entityId)) { - clientBaseEntityIds.add(entityId); - } - } - - } - } - return clientBaseEntityIds; - } - - @Override - public List filter(String query) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Client getByUniqueId(String uniqueId) { - // TODO Auto-generated method stub - return null; - } - - @Override - public List requiredProperties() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Client create(Client entity) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Client update(Client entity) { - // TODO Auto-generated method stub - return null; - } - + + private static Logger logger = LogManager.getLogger(SearchResource.class.toString()); + + private SearchService searchService; + + private ClientService clientService; + + private EventService eventService; + + @Autowired + public SearchResource(SearchService searchService, ClientService clientService, EventService eventService) { + this.searchService = searchService; + this.clientService = clientService; + this.eventService = eventService; + } + + /** + * @param request contains search parameter of with attributes and full colon e.g + * 1. search?attributes=phone_number:072700000 + * or search parameter without attribute and without colon e.g + * 2. search?phone_number=072700000 + * @throws ParseException + */ + @Override + public List search(HttpServletRequest request) throws ParseException {//TODO search should not call different url but only add params + String firstName = getStringFilter(FIRST_NAME, request); + String middleName = getStringFilter(MIDDLE_NAME, request); + String lastName = getStringFilter(LAST_NAME, request); + Optional phoneNumber = Optional.ofNullable(getStringFilter(PHONE_NUMBER, request)); + Optional altPhoneNumber = Optional.ofNullable(getStringFilter(ALT_PHONE_NUMBER, request)); + Optional alternateName = Optional.ofNullable(getStringFilter(ALT_NAME, request)); + ClientSearchBean searchBean = new ClientSearchBean(); + searchBean.setNameLike(getStringFilter(NAME, request)); + + searchBean.setGender(getStringFilter(GENDER, request)); + DateTime[] birthdate = RestUtils.getDateRangeFilter(BIRTH_DATE, request);//TODO add ranges like fhir do http://hl7.org/fhir/search.html + DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, request);//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 attributeMap = new HashMap<>(); + String attributes = getStringFilter(ATTRIBUTE, request); + 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 identifierMap = null; + String identifiers = getStringFilter(IDENTIFIER, request); + 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(); + identifierMap.put(identifierType, identifierValue); + } + + searchBean.setIdentifiers(identifierMap); + return searchService.searchClient(searchBean, firstName, middleName, lastName, null); + } + + @RequestMapping(method = RequestMethod.POST, value = "/search", produces = {MediaType.APPLICATION_JSON_VALUE}) + public List 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 phoneNumber = Optional.ofNullable(jsonObject.optString(PHONE_NUMBER)); + Optional altPhoneNumber = Optional.ofNullable(jsonObject.optString(ALT_PHONE_NUMBER)); + Optional 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 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 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(); + 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 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 searchPathBy(@RequestBody String jsonRequestBody) throws ParseException { + + 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 searchAndProcess(SearchEntityWrapper childSearchEntity, SearchEntityWrapper motherSearchEntity, + String contactPhoneNumber) { + try { + + //Process clients search via demographics + + ClientSearchBean searchBean = new ClientSearchBean(); + List children = new ArrayList(); + if (childSearchEntity.isValid()) { + searchBean = childSearchEntity.getClientSearchBean(); + children = searchService.searchClient(searchBean, searchBean.getFirstName(), searchBean.getMiddleName(), + searchBean.getLastName(), childSearchEntity.getLimit()); + } + + //Process mothers search via mother demographics + + ClientSearchBean motherSearchBean = new ClientSearchBean(); + List mothers = new ArrayList(); + + if (motherSearchEntity.isValid()) { + motherSearchBean = motherSearchEntity.getClientSearchBean(); + mothers = searchService.searchClient(motherSearchBean, motherSearchBean.getFirstName(), + motherSearchBean.getMiddleName(), motherSearchBean.getLastName(), motherSearchEntity.getLimit()); + } + + //Process clients search via contact phone number + + + List clientBaseEntityIds = getClientBaseEntityIdsByContactPhoneNumber(contactPhoneNumber); + + List eventChildren = clientService.findByFieldValue(BaseEntity.BASE_ENTITY_ID, clientBaseEntityIds); + + children = SearchHelper.intersection(children, eventChildren);// Search conjunction is "AND" find intersection + + List linkedMothers = new ArrayList(); + + String RELATIONSHIP_KEY = "mother"; + if (!children.isEmpty()) { + List clientIds = new ArrayList(); + for (Client c : children) { + String relationshipId = SearchHelper.getRelationalId(c, RELATIONSHIP_KEY); + if (relationshipId != null && !clientIds.contains(relationshipId)) { + clientIds.add(relationshipId); + } + } + + linkedMothers = clientService.findByFieldValue(BaseEntity.BASE_ENTITY_ID, clientIds); + + } + + List linkedChildren = new ArrayList(); + + 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(); + } + } + + public List getClientBaseEntityIdsByContactPhoneNumber(String motherGuardianPhoneNumber) { + List clientBaseEntityIds = new ArrayList(); + + if (!StringUtils.isBlank(motherGuardianPhoneNumber)) { + + List events = eventService.findEventsByConceptAndValue("159635AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + motherGuardianPhoneNumber); + if (events != null && !events.isEmpty()) { + for (Event event : events) { + String entityId = event.getBaseEntityId(); + if (entityId != null && !clientBaseEntityIds.contains(entityId)) { + clientBaseEntityIds.add(entityId); + } + } + + } + } + return clientBaseEntityIds; + } + + @Override + public List filter(String query) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Client getByUniqueId(String uniqueId) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List requiredProperties() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Client create(Client entity) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Client update(Client entity) { + // TODO Auto-generated method stub + return null; + } + } diff --git a/src/main/java/org/opensrp/web/utils/SearchHelper.java b/src/main/java/org/opensrp/web/utils/SearchHelper.java index dfae23949..85d3e7e01 100644 --- a/src/main/java/org/opensrp/web/utils/SearchHelper.java +++ b/src/main/java/org/opensrp/web/utils/SearchHelper.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; + import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.json.JSONObject; @@ -18,411 +19,408 @@ public class SearchHelper { - public static final String ZEIR_ID = "zeir_id"; - public static final String OPENSRP_ID = "opensrp_id"; + public static final String ZEIR_ID = "zeir_id"; + public static final String OPENSRP_ID = "opensrp_id"; + + public static final String SIM_PRINT_GUID = "simprints_guid"; + + public static final String FIRST_NAME = "first_name"; + public static final String MIDDLE_NAME = "middle_name"; + public static final String LAST_NAME = "last_name"; + public static final String BIRTH_DATE = "birth_date"; + + //Attributes + public static final String INACTIVE = "inactive"; + public static final String LOST_TO_FOLLOW_UP = "lost_to_follow_up"; + public static final String NFC_CARD_IDENTIFIER = "nfc_card_identifier"; + + // Mother + public static final String MOTHER_GUARDIAN_FIRST_NAME = "mother_first_name"; + public static final String MOTHER_GUARDIAN_LAST_NAME = "mother_last_name"; + public static final String MOTHER_GUARDIAN_NRC_NUMBER = "mother_nrc_number"; + public static final String MOTHER_COMPASS_RELATIONSHIP_ID = "mother_compass_relationship_id"; + + public static final String NRC_NUMBER_KEY = "NRC_Number"; + public static final String COMPASS_RELATIONSHIP_ID = "Compass_Relationship_ID"; + + public static SearchEntityWrapper childSearchParamProcessor(HttpServletRequest request) throws ParseException { + + ClientSearchBean searchBean = new ClientSearchBean(); + + + Integer limit = RestUtils.getIntegerFilter("limit", request); + if (limit == null || limit.intValue() == 0) { + limit = 100; + } + + DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, request);//TODO client by provider id + if (lastEdit != null) { + searchBean.setLastEditFrom(lastEdit[0]); + searchBean.setLastEditTo(lastEdit[1]); + } + + + searchBean.setFirstName(RestUtils.getStringFilter(FIRST_NAME, request)); + searchBean.setMiddleName(RestUtils.getStringFilter(MIDDLE_NAME, request)); + searchBean.setLastName(RestUtils.getStringFilter(LAST_NAME, request)); + searchBean.setGender(RestUtils.getStringFilter(GENDER, request)); + + DateTime[] birthdate = RestUtils + .getDateRangeFilter(BIRTH_DATE, request);//TODO add ranges like fhir do http://hl7.org/fhir/search.html + + //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]); + } + + + Map commonSearchParams = new HashMap<>(); + commonSearchParams.put(ZEIR_ID, RestUtils.getStringFilter(ZEIR_ID, request)); + commonSearchParams.put(OPENSRP_ID, RestUtils.getStringFilter(OPENSRP_ID, request)); + commonSearchParams.put(SIM_PRINT_GUID, RestUtils.getStringFilter(SIM_PRINT_GUID, request)); + commonSearchParams.put(INACTIVE, RestUtils.getStringFilter(INACTIVE, request)); + commonSearchParams.put(LOST_TO_FOLLOW_UP, RestUtils.getStringFilter(LOST_TO_FOLLOW_UP, request)); + commonSearchParams.put(NFC_CARD_IDENTIFIER, RestUtils.getStringFilter(NFC_CARD_IDENTIFIER, request)); + + setIdentifiersAndAttributeToChildSearchBean(commonSearchParams, searchBean); + + boolean isValid = isSearchValid(searchBean); - public static final String SIM_PRINT_GUID = "simprints_guid"; + return new SearchEntityWrapper(isValid, searchBean, limit); + } - public static final String FIRST_NAME = "first_name"; - public static final String MIDDLE_NAME = "middle_name"; - public static final String LAST_NAME = "last_name"; - public static final String BIRTH_DATE = "birth_date"; + public static SearchEntityWrapper motherSearchParamProcessor(HttpServletRequest request) throws ParseException { - //Attributes - public static final String INACTIVE = "inactive"; - public static final String LOST_TO_FOLLOW_UP = "lost_to_follow_up"; - public static final String NFC_CARD_IDENTIFIER = "nfc_card_identifier"; + ClientSearchBean motherSearchBean = new ClientSearchBean(); - // Mother - public static final String MOTHER_GUARDIAN_FIRST_NAME = "mother_first_name"; - public static final String MOTHER_GUARDIAN_LAST_NAME = "mother_last_name"; - public static final String MOTHER_GUARDIAN_NRC_NUMBER = "mother_nrc_number"; - public static final String MOTHER_COMPASS_RELATIONSHIP_ID = "mother_compass_relationship_id"; + Integer limit = setCoreFilters(request, motherSearchBean); - public static final String NRC_NUMBER_KEY = "NRC_Number"; - public static final String COMPASS_RELATIONSHIP_ID = "Compass_Relationship_ID"; - public static SearchEntityWrapper childSearchParamProcessor(HttpServletRequest request) throws ParseException { - - ClientSearchBean searchBean = new ClientSearchBean(); + String motherGuardianNrc = RestUtils.getStringFilter(MOTHER_GUARDIAN_NRC_NUMBER, request); + String compassRelationshipId = RestUtils.getStringFilter(MOTHER_COMPASS_RELATIONSHIP_ID, request); + motherSearchBean.setFirstName(RestUtils.getStringFilter(MOTHER_GUARDIAN_FIRST_NAME, request)); + motherSearchBean.setLastName(RestUtils.getStringFilter(MOTHER_GUARDIAN_LAST_NAME, request)); + setNameLikeAndAtrributesOnMotherSearchBean(motherGuardianNrc, compassRelationshipId, motherSearchBean); - Integer limit = RestUtils.getIntegerFilter("limit", request); - if (limit == null || limit.intValue() == 0) { - limit = 100; - } - - DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, request);//TODO client by provider id - if (lastEdit != null) { - searchBean.setLastEditFrom(lastEdit[0]); - searchBean.setLastEditTo(lastEdit[1]); - } + boolean isValid = isSearchValid(motherSearchBean); + return new SearchEntityWrapper(isValid, motherSearchBean, limit); + } + public static SearchEntityWrapper childSearchParamProcessor(JSONObject jsonObject) throws ParseException { - searchBean.setFirstName(RestUtils.getStringFilter(FIRST_NAME, request)); - searchBean.setMiddleName(RestUtils.getStringFilter(MIDDLE_NAME, request)); - searchBean.setLastName(RestUtils.getStringFilter(LAST_NAME, request)); - searchBean.setGender(RestUtils.getStringFilter(GENDER, request)); + ClientSearchBean searchBean = new ClientSearchBean(); - DateTime[] birthdate = RestUtils - .getDateRangeFilter(BIRTH_DATE, request);//TODO add ranges like fhir do http://hl7.org/fhir/search.html + Integer limit = !jsonObject.optString("limit").equals("") ? Integer.parseInt(jsonObject.optString("limit")) + : jsonObject.optInt("limit"); + if (limit == 0) { + limit = 100; + } - //TODO lookinto Swagger https://slack-files.com/files-pri-safe/T0EPSEJE9-F0TBD0N77/integratingswagger.pdf?c=1458211183-179d2bfd2e974585c5038fba15a86bf83097810a + DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, jsonObject);//TODO client by provider id + if (lastEdit != null) { + searchBean.setLastEditFrom(lastEdit[0]); + searchBean.setLastEditTo(lastEdit[1]); + } - if (birthdate != null) { - searchBean.setBirthdateFrom(birthdate[0]); - searchBean.setBirthdateTo(birthdate[1]); - } + searchBean.setFirstName(jsonObject.optString(FIRST_NAME)); + searchBean.setMiddleName(jsonObject.optString(MIDDLE_NAME)); + searchBean.setLastName(jsonObject.optString(LAST_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 - Map commonSearchParams = new HashMap<>(); - commonSearchParams.put(ZEIR_ID, RestUtils.getStringFilter(ZEIR_ID, request)); - commonSearchParams.put(OPENSRP_ID, RestUtils.getStringFilter(OPENSRP_ID, request)); - commonSearchParams.put(SIM_PRINT_GUID, RestUtils.getStringFilter(SIM_PRINT_GUID, request)); - commonSearchParams.put(INACTIVE, RestUtils.getStringFilter(INACTIVE, request)); - commonSearchParams.put(LOST_TO_FOLLOW_UP, RestUtils.getStringFilter(LOST_TO_FOLLOW_UP, request)); - commonSearchParams.put(NFC_CARD_IDENTIFIER, RestUtils.getStringFilter(NFC_CARD_IDENTIFIER, request)); + //TODO lookinto Swagger https://slack-files.com/files-pri-safe/T0EPSEJE9-F0TBD0N77/integratingswagger.pdf?c=1458211183-179d2bfd2e974585c5038fba15a86bf83097810a - setIdentifiersAndAttributeToChildSearchBean(commonSearchParams, searchBean); + if (birthdate != null) { + searchBean.setBirthdateFrom(birthdate[0]); + searchBean.setBirthdateTo(birthdate[1]); + } - boolean isValid = isSearchValid(searchBean); + Map commonSearchParams = new HashMap<>(); + commonSearchParams.put(ZEIR_ID, jsonObject.optString(ZEIR_ID)); + commonSearchParams.put(OPENSRP_ID, jsonObject.optString(OPENSRP_ID)); + commonSearchParams.put(SIM_PRINT_GUID, jsonObject.optString(SIM_PRINT_GUID)); + commonSearchParams.put(INACTIVE, jsonObject.optString(INACTIVE)); + commonSearchParams.put(LOST_TO_FOLLOW_UP, jsonObject.optString(LOST_TO_FOLLOW_UP)); + commonSearchParams.put(NFC_CARD_IDENTIFIER, jsonObject.optString(NFC_CARD_IDENTIFIER)); - return new SearchEntityWrapper(isValid, searchBean, limit); - } + setIdentifiersAndAttributeToChildSearchBean(commonSearchParams, searchBean); - public static SearchEntityWrapper motherSearchParamProcessor(HttpServletRequest request) throws ParseException { + boolean isValid = isSearchValid(searchBean); - ClientSearchBean motherSearchBean = new ClientSearchBean(); + return new SearchEntityWrapper(isValid, searchBean, limit); + } - Integer limit = setCoreFilters(request, motherSearchBean); + public static SearchEntityWrapper motherSearchParamProcessor(JSONObject jsonObject) throws ParseException { + ClientSearchBean motherSearchBean = new ClientSearchBean(); + Integer limit = setCoreFilters(jsonObject, motherSearchBean); - String motherGuardianNrc = RestUtils.getStringFilter(MOTHER_GUARDIAN_NRC_NUMBER, request); - String compassRelationshipId = RestUtils.getStringFilter(MOTHER_COMPASS_RELATIONSHIP_ID, request); + String motherGuardianNrc = jsonObject.optString(MOTHER_GUARDIAN_NRC_NUMBER); + String compassRelationshipId = jsonObject.optString(MOTHER_COMPASS_RELATIONSHIP_ID); - motherSearchBean.setFirstName(RestUtils.getStringFilter(MOTHER_GUARDIAN_FIRST_NAME, request)); - motherSearchBean.setLastName(RestUtils.getStringFilter(MOTHER_GUARDIAN_LAST_NAME, request)); + motherSearchBean.setFirstName(jsonObject.optString(MOTHER_GUARDIAN_FIRST_NAME)); + motherSearchBean.setLastName(jsonObject.optString(MOTHER_GUARDIAN_LAST_NAME)); - setNameLikeAndAtrributesOnMotherSearchBean(motherGuardianNrc,compassRelationshipId, motherSearchBean); + setNameLikeAndAtrributesOnMotherSearchBean(motherGuardianNrc, compassRelationshipId, motherSearchBean); - boolean isValid = isSearchValid(motherSearchBean); + boolean isValid = isSearchValid(motherSearchBean); - return new SearchEntityWrapper(isValid, motherSearchBean, limit); - } + return new SearchEntityWrapper(isValid, motherSearchBean, limit); + } - public static SearchEntityWrapper childSearchParamProcessor(JSONObject jsonObject) throws ParseException { + public static void setNameLikeAndAtrributesOnMotherSearchBean(String motherGuardianNrc, + String compassRelationshipId, + ClientSearchBean motherSearchBean) { + Map motherAttributes = new HashMap<>(); + if (!StringUtils.isBlank(motherGuardianNrc)) { + motherAttributes.put(NRC_NUMBER_KEY, motherGuardianNrc); + } + if (!StringUtils.isBlank(compassRelationshipId)) { + motherAttributes.put(COMPASS_RELATIONSHIP_ID, compassRelationshipId); + } - ClientSearchBean searchBean = new ClientSearchBean(); + String nameLike = null; - Integer limit = !jsonObject.optString("limit").equals("") ? Integer.parseInt(jsonObject.optString("limit")) - : jsonObject.optInt("limit"); - if (limit == 0) { - limit = 100; - } + if (!StringUtils.isBlank(motherSearchBean.getFirstName()) + && StringUtils.containsWhitespace(motherSearchBean.getFirstName().trim()) + && StringUtils.isBlank(motherSearchBean.getLastName())) { + String[] arr = motherSearchBean.getFirstName().split("\\s+"); + nameLike = arr[0]; + motherSearchBean.setFirstName(null); + } - DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, jsonObject);//TODO client by provider id - if (lastEdit != null) { - searchBean.setLastEditFrom(lastEdit[0]); - searchBean.setLastEditTo(lastEdit[1]); - } + motherSearchBean.setNameLike(nameLike); + motherSearchBean.setAttributes(motherAttributes); - searchBean.setFirstName(jsonObject.optString(FIRST_NAME)); - searchBean.setMiddleName(jsonObject.optString(MIDDLE_NAME)); - searchBean.setLastName(jsonObject.optString(LAST_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 + public static void setIdentifiersAndAttributeToChildSearchBean(Map commonSearchParams, ClientSearchBean searchBean) { + Map identifiers = new HashMap(); - //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]); - } + String zeirId = commonSearchParams.get(ZEIR_ID); + String opensrpId = commonSearchParams.get(OPENSRP_ID); + String simprintsGuid = commonSearchParams.get(SIM_PRINT_GUID); + String lostToFollowUp = commonSearchParams.get(LOST_TO_FOLLOW_UP); + String inActive = commonSearchParams.get(INACTIVE); + String nfcCardIdentifier = commonSearchParams.get(NFC_CARD_IDENTIFIER); + + if (!StringUtils.isBlank(zeirId)) { + identifiers.put(ZEIR_ID, zeirId); + identifiers.put("ZEIR_ID", zeirId); //Maintains backward compatibility with upper case key + } + + if (!StringUtils.isBlank(opensrpId)) { + identifiers.put(OPENSRP_ID, opensrpId); + } + if (!StringUtils.isBlank(simprintsGuid)) { + identifiers.put(SIM_PRINT_GUID, simprintsGuid); + } - Map commonSearchParams = new HashMap<>(); - commonSearchParams.put(ZEIR_ID, jsonObject.optString(ZEIR_ID)); - commonSearchParams.put(OPENSRP_ID, jsonObject.optString(OPENSRP_ID)); - commonSearchParams.put(SIM_PRINT_GUID, jsonObject.optString(SIM_PRINT_GUID)); - commonSearchParams.put(INACTIVE, jsonObject.optString(INACTIVE)); - commonSearchParams.put(LOST_TO_FOLLOW_UP, jsonObject.optString(LOST_TO_FOLLOW_UP)); - commonSearchParams.put(NFC_CARD_IDENTIFIER, jsonObject.optString(NFC_CARD_IDENTIFIER)); - setIdentifiersAndAttributeToChildSearchBean(commonSearchParams, searchBean); + Map attributes = new HashMap(); + if (!StringUtils.isBlank(inActive) || !StringUtils.isBlank(lostToFollowUp) + || !StringUtils.isBlank(nfcCardIdentifier)) { - boolean isValid = isSearchValid(searchBean); + if (!StringUtils.isBlank(inActive)) { + attributes.put(INACTIVE, inActive); + } - return new SearchEntityWrapper(isValid, searchBean, limit); - } + if (!StringUtils.isBlank(lostToFollowUp)) { + attributes.put(LOST_TO_FOLLOW_UP, lostToFollowUp); + } - public static SearchEntityWrapper motherSearchParamProcessor(JSONObject jsonObject) throws ParseException { + if (!StringUtils.isBlank(nfcCardIdentifier)) { + attributes.put("NFC_Card_Identifier", nfcCardIdentifier);//Key different case than constant + } + } - ClientSearchBean motherSearchBean = new ClientSearchBean(); + searchBean.setIdentifiers(identifiers); + searchBean.setAttributes(attributes); - Integer limit = setCoreFilters(jsonObject, motherSearchBean); + } - String motherGuardianNrc = jsonObject.optString(MOTHER_GUARDIAN_NRC_NUMBER); - String compassRelationshipId = jsonObject.optString(MOTHER_COMPASS_RELATIONSHIP_ID); + public static Integer setCoreFilters(HttpServletRequest request, ClientSearchBean searchBean) throws ParseException { - motherSearchBean.setFirstName(jsonObject.optString(MOTHER_GUARDIAN_FIRST_NAME)); - motherSearchBean.setLastName(jsonObject.optString(MOTHER_GUARDIAN_LAST_NAME)); + Integer limit = RestUtils.getIntegerFilter("limit", request); + if (limit == null || limit.intValue() == 0) { + limit = 100; + } - setNameLikeAndAtrributesOnMotherSearchBean(motherGuardianNrc,compassRelationshipId, motherSearchBean); + DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, request);//TODO client by provider id + if (lastEdit != null) { + searchBean.setLastEditFrom(lastEdit[0]); + searchBean.setLastEditTo(lastEdit[1]); + } - boolean isValid = isSearchValid(motherSearchBean); + return limit; + } - return new SearchEntityWrapper(isValid, motherSearchBean, limit); - } - - public static void setNameLikeAndAtrributesOnMotherSearchBean(String motherGuardianNrc, - String compassRelationshipId, - ClientSearchBean motherSearchBean){ - Map motherAttributes = new HashMap<>(); - if (!StringUtils.isBlank(motherGuardianNrc)) { - motherAttributes.put(NRC_NUMBER_KEY, motherGuardianNrc); - } - if (!StringUtils.isBlank(compassRelationshipId)) { - motherAttributes.put(COMPASS_RELATIONSHIP_ID, compassRelationshipId); - } - - String nameLike = null; - - if (!StringUtils.isBlank(motherSearchBean.getFirstName()) - && StringUtils.containsWhitespace(motherSearchBean.getFirstName().trim()) - && StringUtils.isBlank(motherSearchBean.getLastName())) { - String[] arr = motherSearchBean.getFirstName().split("\\s+"); - nameLike = arr[0]; - motherSearchBean.setFirstName(null); - } - - motherSearchBean.setNameLike(nameLike); - motherSearchBean.setAttributes(motherAttributes); - - } - - public static void setIdentifiersAndAttributeToChildSearchBean(Map commonSearchParams, ClientSearchBean searchBean){ - Map identifiers = new HashMap(); - - String zeirId = commonSearchParams.get(ZEIR_ID); - String opensrpId = commonSearchParams.get(OPENSRP_ID); - String simprintsGuid = commonSearchParams.get(SIM_PRINT_GUID); - String lostToFollowUp = commonSearchParams.get(LOST_TO_FOLLOW_UP); - String inActive = commonSearchParams.get(INACTIVE); - String nfcCardIdentifier = commonSearchParams.get(NFC_CARD_IDENTIFIER); - - if (!StringUtils.isBlank(zeirId)) { - identifiers.put(ZEIR_ID, zeirId); - identifiers.put("ZEIR_ID", zeirId); //Maintains backward compatibility with upper case key - } - - if (!StringUtils.isBlank(opensrpId)) { - identifiers.put(OPENSRP_ID, opensrpId); - } - if (!StringUtils.isBlank(simprintsGuid)) { - identifiers.put(SIM_PRINT_GUID, simprintsGuid); - } - - - Map attributes = new HashMap(); - if (!StringUtils.isBlank(inActive) || !StringUtils.isBlank(lostToFollowUp) - || !StringUtils.isBlank(nfcCardIdentifier)) { - - if (!StringUtils.isBlank(inActive)) { - attributes.put(INACTIVE, inActive); - } - - if (!StringUtils.isBlank(lostToFollowUp)) { - attributes.put(LOST_TO_FOLLOW_UP, lostToFollowUp); - } - - if (!StringUtils.isBlank(nfcCardIdentifier)) { - attributes.put("NFC_Card_Identifier", nfcCardIdentifier);//Key different case than constant - } - } - - searchBean.setIdentifiers(identifiers); - searchBean.setAttributes(attributes); - - } - - public static Integer setCoreFilters(HttpServletRequest request, ClientSearchBean searchBean) throws ParseException { - - Integer limit = RestUtils.getIntegerFilter("limit", request); - if (limit == null || limit.intValue() == 0) { - limit = 100; - } - - DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, request);//TODO client by provider id - if (lastEdit != null) { - searchBean.setLastEditFrom(lastEdit[0]); - searchBean.setLastEditTo(lastEdit[1]); - } - - return limit; - } - - public static Integer setCoreFilters(JSONObject jsonObject, ClientSearchBean searchBean) throws ParseException { - - Integer limit = !jsonObject.optString("limit").equals("") ? Integer.parseInt(jsonObject.optString("limit")) - : jsonObject.optInt("limit"); - if (limit == 0) { - limit = 100; - } - - DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, jsonObject);//TODO client by provider id - if (lastEdit != null) { - searchBean.setLastEditFrom(lastEdit[0]); - searchBean.setLastEditTo(lastEdit[1]); - } - - return limit; - } - - /** - * Here we check to see if the search entity param is valid for use in child search Some form of - * reflections and custom annotations might have been better - * - * @param searchBean model with search params - * @return boolean whether the search entity is valid - */ - - public static boolean isSearchValid(ClientSearchBean searchBean) { - - return !StringUtils.isBlank(searchBean.getFirstName()) - || !StringUtils.isBlank(searchBean.getMiddleName()) - || !StringUtils.isBlank(searchBean.getLastName()) - || !StringUtils.isBlank(searchBean.getGender()) - || (searchBean.getAttributes() != null && !searchBean.getAttributes().isEmpty()) - || searchBean.getBirthdateFrom() != null || searchBean.getBirthdateTo() != null - || searchBean.getLastEditFrom() != null || searchBean.getLastEditTo() != null - || (searchBean.getIdentifiers() != null && !searchBean.getIdentifiers().isEmpty()) - || !StringUtils.isBlank(searchBean.getNameLike()); - - } - - /** - * // Method returns the intersection of two lists - * - * @param list1_ - * @param list2_ - * @return merged intersection list - */ - public static List intersection(List list1_, List list2_) { - - List list1 = list1_; - List list2 = list2_; - - list1 = createClientListIfEmpty(list1); - - list2 = createClientListIfEmpty(list2); - - if (list1.isEmpty() && list2.isEmpty()) { - return new ArrayList(); - } - - if (list1.isEmpty() && !list2.isEmpty()) { - return list2; - } - - if (list2.isEmpty() && !list1.isEmpty()) { - return list1; - } - - List list = new ArrayList(); - - for (Client t : list1) { - if (contains(list2, t)) { - list.add(t); - } - } - - return list; - } - - public static List createClientListIfEmpty(List list_) { - List list = list_; - - if (list == null) { - list = new ArrayList(); - } - - return list; - } - - public static boolean contains(List clients, Client c) { - if (clients == null || clients.isEmpty() || c == null) { - return false; - } - for (Client client : clients) { - - if (client != null && client.getBaseEntityId() != null && c.getBaseEntityId() != null - && client.getBaseEntityId().equals(c.getBaseEntityId())) { - - return true; - - } - } - return false; - } - - public static String getContactPhoneNumberParam(HttpServletRequest request) { - //Search by mother contact number - String MOTHER_GUARDIAN_PHONE_NUMBER = "mother_contact_phone_number"; - String CONTACT_PHONE_NUMBER = "contact_phone_number"; - String motherGuardianPhoneNumber = RestUtils.getStringFilter(MOTHER_GUARDIAN_PHONE_NUMBER, request); - motherGuardianPhoneNumber = StringUtils.isBlank(motherGuardianPhoneNumber) - ? RestUtils.getStringFilter(CONTACT_PHONE_NUMBER, request) - : motherGuardianPhoneNumber; - - return motherGuardianPhoneNumber; - } - - public static String getContactPhoneNumberParam(JSONObject jsonObject) { - //Search by mother contact number - String MOTHER_GUARDIAN_PHONE_NUMBER = "mother_contact_phone_number"; - String CONTACT_PHONE_NUMBER = "contact_phone_number"; - String motherGuardianPhoneNumber = jsonObject.optString(MOTHER_GUARDIAN_PHONE_NUMBER); - motherGuardianPhoneNumber = StringUtils.isBlank(motherGuardianPhoneNumber) - ? jsonObject.optString(CONTACT_PHONE_NUMBER) - : motherGuardianPhoneNumber; - - return motherGuardianPhoneNumber; - } - - public static List processSearchResult(List children, List mothers, - String RELATIONSHIP_KEY) { - List childMotherList = new ArrayList(); - for (Client child : children) { - for (Client mother : mothers) { - String relationalId = getRelationalId(child, RELATIONSHIP_KEY); - String motherEntityId = mother.getBaseEntityId(); - if (relationalId != null && motherEntityId != null && relationalId.equalsIgnoreCase(motherEntityId)) { - childMotherList.add(new ChildMother(child, mother)); - } - } - } - - return childMotherList; - } - - public static String getRelationalId(Client c, String relationshipKey) { - Map> relationships = c.getRelationships(); - if (relationships != null) { - for (Map.Entry> entry : relationships.entrySet()) { - String key = entry.getKey(); - if (key.equalsIgnoreCase(relationshipKey)) { - List rList = entry.getValue(); - if (!rList.isEmpty()) { - return rList.get(0); - } - } - } - } - - return null; - } + public static Integer setCoreFilters(JSONObject jsonObject, ClientSearchBean searchBean) throws ParseException { + + Integer limit = !jsonObject.optString("limit").equals("") ? Integer.parseInt(jsonObject.optString("limit")) + : jsonObject.optInt("limit"); + if (limit == 0) { + limit = 100; + } + + DateTime[] lastEdit = RestUtils.getDateRangeFilter(LAST_UPDATE, jsonObject);//TODO client by provider id + if (lastEdit != null) { + searchBean.setLastEditFrom(lastEdit[0]); + searchBean.setLastEditTo(lastEdit[1]); + } + + return limit; + } + + /** + * Here we check to see if the search entity param is valid for use in child search Some form of + * reflections and custom annotations might have been better + * + * @param searchBean model with search params + * @return boolean whether the search entity is valid + */ + + public static boolean isSearchValid(ClientSearchBean searchBean) { + + return !StringUtils.isBlank(searchBean.getFirstName()) + || !StringUtils.isBlank(searchBean.getMiddleName()) + || !StringUtils.isBlank(searchBean.getLastName()) + || !StringUtils.isBlank(searchBean.getGender()) + || (searchBean.getAttributes() != null && !searchBean.getAttributes().isEmpty()) + || searchBean.getBirthdateFrom() != null || searchBean.getBirthdateTo() != null + || searchBean.getLastEditFrom() != null || searchBean.getLastEditTo() != null + || (searchBean.getIdentifiers() != null && !searchBean.getIdentifiers().isEmpty()) + || !StringUtils.isBlank(searchBean.getNameLike()); + + } + + /** + * // Method returns the intersection of two lists + * + * @param list1_ + * @param list2_ + * @return merged intersection list + */ + public static List intersection(List list1_, List list2_) { + + List list1 = list1_; + List list2 = list2_; + + list1 = createClientListIfEmpty(list1); + + list2 = createClientListIfEmpty(list2); + + if (list1.isEmpty() && list2.isEmpty()) { + return new ArrayList(); + } + + if (list1.isEmpty() && !list2.isEmpty()) { + return list2; + } + + if (list2.isEmpty() && !list1.isEmpty()) { + return list1; + } + + List list = new ArrayList(); + + for (Client t : list1) { + if (contains(list2, t)) { + list.add(t); + } + } + + return list; + } + + public static List createClientListIfEmpty(List list_) { + List list = list_; + + if (list == null) { + list = new ArrayList(); + } + + return list; + } + + public static boolean contains(List clients, Client c) { + if (clients == null || clients.isEmpty() || c == null) { + return false; + } + for (Client client : clients) { + + if (client != null && client.getBaseEntityId() != null && c.getBaseEntityId() != null + && client.getBaseEntityId().equals(c.getBaseEntityId())) { + + return true; + + } + } + return false; + } + + public static String getContactPhoneNumberParam(HttpServletRequest request) { + //Search by mother contact number + String MOTHER_GUARDIAN_PHONE_NUMBER = "mother_contact_phone_number"; + String CONTACT_PHONE_NUMBER = "contact_phone_number"; + String motherGuardianPhoneNumber = RestUtils.getStringFilter(MOTHER_GUARDIAN_PHONE_NUMBER, request); + motherGuardianPhoneNumber = StringUtils.isBlank(motherGuardianPhoneNumber) + ? RestUtils.getStringFilter(CONTACT_PHONE_NUMBER, request) + : motherGuardianPhoneNumber; + + return motherGuardianPhoneNumber; + } + + public static String getContactPhoneNumberParam(JSONObject jsonObject) { + //Search by mother contact number + String MOTHER_GUARDIAN_PHONE_NUMBER = "mother_contact_phone_number"; + String CONTACT_PHONE_NUMBER = "contact_phone_number"; + String motherGuardianPhoneNumber = jsonObject.optString(MOTHER_GUARDIAN_PHONE_NUMBER); + motherGuardianPhoneNumber = StringUtils.isBlank(motherGuardianPhoneNumber) + ? jsonObject.optString(CONTACT_PHONE_NUMBER) + : motherGuardianPhoneNumber; + + return motherGuardianPhoneNumber; + } + + public static List processSearchResult(List children, List mothers, + String RELATIONSHIP_KEY) { + List childMotherList = new ArrayList(); + for (Client child : children) { + for (Client mother : mothers) { + String relationalId = getRelationalId(child, RELATIONSHIP_KEY); + String motherEntityId = mother.getBaseEntityId(); + if (relationalId != null && motherEntityId != null && relationalId.equalsIgnoreCase(motherEntityId)) { + childMotherList.add(new ChildMother(child, mother)); + } + } + } + + return childMotherList; + } + + public static String getRelationalId(Client c, String relationshipKey) { + Map> relationships = c.getRelationships(); + if (relationships != null) { + for (Map.Entry> entry : relationships.entrySet()) { + String key = entry.getKey(); + if (key.equalsIgnoreCase(relationshipKey)) { + List rList = entry.getValue(); + if (!rList.isEmpty()) { + return rList.get(0); + } + } + } + } + + return null; + } } diff --git a/src/test/java/org/opensrp/web/rest/SearchResourceTest.java b/src/test/java/org/opensrp/web/rest/SearchResourceTest.java index c12091911..2efbb9522 100644 --- a/src/test/java/org/opensrp/web/rest/SearchResourceTest.java +++ b/src/test/java/org/opensrp/web/rest/SearchResourceTest.java @@ -9,7 +9,11 @@ import org.junit.runner.RunWith; import org.mockito.Mockito; import org.opensrp.repository.PlanRepository; -import org.opensrp.service.*; +import org.opensrp.service.ClientService; +import org.opensrp.service.EventService; +import org.opensrp.service.ExportEventDataMapper; +import org.opensrp.service.SearchService; +import org.opensrp.service.TaskGenerator; import org.smartregister.domain.Client; import org.opensrp.repository.ClientsRepository; import org.opensrp.repository.EventsRepository; @@ -28,89 +32,90 @@ import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(loader = TestWebContextLoader.class, locations = { "classpath:test-webmvc-config.xml", }) -@ActiveProfiles(profiles = { "jedis", "postgres", "basic_auth" }) +@ContextConfiguration(loader = TestWebContextLoader.class, locations = {"classpath:test-webmvc-config.xml",}) +@ActiveProfiles(profiles = {"jedis", "postgres", "basic_auth"}) public class SearchResourceTest { - - @Autowired - protected WebApplicationContext webApplicationContext; - - private SearchService searchService; - - private ClientService clientService; - - private EventService eventService; - - private ExportEventDataMapper exportEventDataMapper; - - - private TaskGenerator taskGenerator; - - private PlanRepository planRepository; - MockHttpServletRequest mockHttpServletRequest; - String phoneNumber = "0727000000"; - String town = "town"; - - String firstName = "name"; - - String male = "male"; - - DateTime birthDate = new DateTime(0l, DateTimeZone.UTC); - - @Before - public void setUp() { - SearchRepository searchRepository = Mockito.mock(SearchRepository.class); - ClientsRepository clientRepository = Mockito.mock(ClientsRepository.class); - EventsRepository eventsRepository = Mockito.mock(EventsRepository.class); - - searchService = Mockito.spy(new SearchService(searchRepository)); - clientService = Mockito.spy(new ClientService(clientRepository)); - eventService = Mockito.spy(new EventService(eventsRepository, clientService,taskGenerator,planRepository,null)); - - } - - @Test - public void testInstantanceCreatesCorrectly() { - - SearchResource searchResource = new SearchResource(searchService, clientService, eventService); - Assert.assertNotNull(searchResource); - - } - - @Test - public void testIntersectionMethodReturnsCorrectResult() throws Exception { - - Client clientA = Mockito.mock(Client.class); - List listA = Arrays.asList(new Client[] { clientA }); - List result = SearchHelper.intersection(null, listA); - - Assert.assertNotNull(result); - Assert.assertEquals(listA, result); - - } - @Test - public void shouldSearchClient() throws ParseException { - mockHttpServletRequest = new MockHttpServletRequest(); - mockHttpServletRequest.addParameter("ff", "ona"); - mockHttpServletRequest.addParameter("phone_number", phoneNumber); - mockHttpServletRequest.addParameter("alt_phone_number", phoneNumber); - mockHttpServletRequest.addParameter("alt_name", firstName); - mockHttpServletRequest.addParameter("attribute", "next_contact_date:2022-06-15"); - mockHttpServletRequest.addParameter("dob", String.valueOf(birthDate)); - mockHttpServletRequest.addParameter("identifier", "fsdf"+":"+ "sfdf"); - SearchResource searchResource=new SearchResource(searchService,clientService,eventService); - List clients = searchResource.search(mockHttpServletRequest); - Assert.assertNotNull(clients); - } - - @Test - public void shouldSearchClientPost() throws ParseException { - String jsonRequestString = "{\"ff\":\"ona\",\"identifier\":\"fsdf:sfdf\",\"alt_name\":\"name\"," + - "\"alt_phone_number\":\"0727000000\",\"dob\":\"1970-01-01T00:00:00.000Z\",\"phone_number\":\"0727000000\"," + - "\"attribute\":\"next_contact_date:2022-06-15\"}"; - SearchResource searchResource=new SearchResource(searchService,clientService,eventService); - List clients = searchResource.searchByPost(jsonRequestString); - Assert.assertNotNull(clients); - - } + + @Autowired + protected WebApplicationContext webApplicationContext; + + private SearchService searchService; + + private ClientService clientService; + + private EventService eventService; + + private ExportEventDataMapper exportEventDataMapper; + + + private TaskGenerator taskGenerator; + + private PlanRepository planRepository; + MockHttpServletRequest mockHttpServletRequest; + String phoneNumber = "0727000000"; + String town = "town"; + + String firstName = "name"; + + String male = "male"; + + DateTime birthDate = new DateTime(0l, DateTimeZone.UTC); + + @Before + public void setUp() { + SearchRepository searchRepository = Mockito.mock(SearchRepository.class); + ClientsRepository clientRepository = Mockito.mock(ClientsRepository.class); + EventsRepository eventsRepository = Mockito.mock(EventsRepository.class); + + searchService = Mockito.spy(new SearchService(searchRepository)); + clientService = Mockito.spy(new ClientService(clientRepository)); + eventService = Mockito.spy(new EventService(eventsRepository, clientService, taskGenerator, planRepository, null)); + + } + + @Test + public void testInstantanceCreatesCorrectly() { + + SearchResource searchResource = new SearchResource(searchService, clientService, eventService); + Assert.assertNotNull(searchResource); + + } + + @Test + public void testIntersectionMethodReturnsCorrectResult() throws Exception { + + Client clientA = Mockito.mock(Client.class); + List listA = Arrays.asList(new Client[]{clientA}); + List result = SearchHelper.intersection(null, listA); + + Assert.assertNotNull(result); + Assert.assertEquals(listA, result); + + } + + @Test + public void shouldSearchClientWithGetRequest() throws ParseException { + mockHttpServletRequest = new MockHttpServletRequest(); + mockHttpServletRequest.addParameter("ff", "ona"); + mockHttpServletRequest.addParameter("phone_number", phoneNumber); + mockHttpServletRequest.addParameter("alt_phone_number", phoneNumber); + mockHttpServletRequest.addParameter("alt_name", firstName); + mockHttpServletRequest.addParameter("attribute", "next_contact_date:2022-06-15"); + mockHttpServletRequest.addParameter("dob", String.valueOf(birthDate)); + mockHttpServletRequest.addParameter("identifier", "fsdf" + ":" + "sfdf"); + SearchResource searchResource = new SearchResource(searchService, clientService, eventService); + List clients = searchResource.search(mockHttpServletRequest); + Assert.assertNotNull(clients); + } + + @Test + public void shouldSearchClientWithPostRequest() throws ParseException { + String jsonRequestString = "{\"ff\":\"ona\",\"identifier\":\"fsdf:sfdf\",\"alt_name\":\"name\"," + + "\"alt_phone_number\":\"0727000000\",\"dob\":\"1970-01-01T00:00:00.000Z\",\"phone_number\":\"0727000000\"," + + "\"attribute\":\"next_contact_date:2022-06-15\"}"; + SearchResource searchResource = new SearchResource(searchService, clientService, eventService); + List clients = searchResource.searchByPost(jsonRequestString); + Assert.assertNotNull(clients); + + } } diff --git a/src/test/java/org/opensrp/web/utils/SearchHelperTest.java b/src/test/java/org/opensrp/web/utils/SearchHelperTest.java index c37e51860..4aac0333e 100644 --- a/src/test/java/org/opensrp/web/utils/SearchHelperTest.java +++ b/src/test/java/org/opensrp/web/utils/SearchHelperTest.java @@ -1,7 +1,11 @@ package org.opensrp.web.utils; import java.text.ParseException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.json.JSONObject; import org.junit.Before; @@ -17,377 +21,377 @@ import javax.servlet.http.HttpServletRequest; public class SearchHelperTest { - - private Client testClient = null; - - @Before - public void setUp() { - - Client motherClient = new Client("dummy-mother-base-entity-id"); - motherClient.setFirstName("Jane"); - motherClient.setLastName("Doe"); - motherClient.setGender("FEMALE"); - motherClient.setIdentifiers(EasyMap.mapOf("M_ZEIR_ID", "673939_mother")); - testClient = motherClient; - } - - @Test - public void testCreateClientListIfEmptyCreatesListOnNull() { - - List list = SearchHelper.createClientListIfEmpty(null); - Assert.assertNotNull(list); - } - - @Test - public void testCreateClientListIfEmptyDoesNotModifyParameterList() { - Client client = new Client("dummy-base-entity-id"); - client.setFirstName("Johnny"); - client.setLastName("Test"); - client.setGender("MALE"); - - List myClientList = Arrays.asList(new Client[] { client }); - - List list = SearchHelper.createClientListIfEmpty(myClientList); - - Assert.assertNotNull(list); - org.springframework.util.Assert.notEmpty(list); - Assert.assertEquals("dummy-base-entity-id", list.get(0).getBaseEntityId()); - Assert.assertEquals("Johnny", list.get(0).getFirstName()); - Assert.assertEquals("Test", list.get(0).getLastName()); - Assert.assertEquals("MALE", list.get(0).getGender()); - } - - @Test - public void testGetContactPhoneNumberParamReturnsCorrectValueForParam() { - - String MOTHER_GUARDIAN_PHONE_NUMBER = "mother_contact_phone_number"; - String CONTACT_PHONE_NUMBER = "contact_phone_number"; - - MockHttpServletRequest req = new MockHttpServletRequest(); - req.addParameter(MOTHER_GUARDIAN_PHONE_NUMBER, "+254738388383"); - req.addParameter(CONTACT_PHONE_NUMBER, "+2547112233445"); - - String motherGuardianNum = SearchHelper.getContactPhoneNumberParam(req); - - Assert.assertNotNull(motherGuardianNum); - Assert.assertEquals("+254738388383", motherGuardianNum); - - req.removeParameter(MOTHER_GUARDIAN_PHONE_NUMBER); - motherGuardianNum = SearchHelper.getContactPhoneNumberParam(req); - - Assert.assertNotNull(motherGuardianNum); - Assert.assertEquals("+2547112233445", motherGuardianNum); - } - - @Test - public void testIntersectionReturnsEmptyListIfBothListsEmptyOrNull() { - - List list = new ArrayList<>(); - - List list2 = new ArrayList<>(); - - List resultList = SearchHelper.intersection(list, list2); - - Assert.assertNotNull(resultList); - Assert.assertEquals(0, resultList.size()); - - resultList = SearchHelper.intersection(new ArrayList(), new ArrayList()); - - Assert.assertNotNull(resultList); - Assert.assertEquals(0, resultList.size()); - - } - - @Test - public void testIntersectionReturnsClientListAIfClientBListIsNullOrEmpty() { - - List list = new ArrayList<>(); - - Client client = new Client("dummy-base-entity-id_1"); - client.setFirstName("Johnny"); - client.setLastName("Test"); - client.setGender("MALE"); - - Client client2 = new Client("dummy-base-entity-id_2"); - client2.setFirstName("Jane"); - client2.setLastName("Test"); - client2.setGender("FEMALE"); - - list.add(client); - list.add(client2); - - List resultList = SearchHelper.intersection(list, null); - - Assert.assertNotNull(resultList); - Assert.assertEquals(2, resultList.size()); - Assert.assertEquals("Johnny", resultList.get(0).getFirstName()); - Assert.assertEquals("Jane", resultList.get(1).getFirstName()); - - resultList = SearchHelper.intersection(list, new ArrayList()); - - Assert.assertNotNull(resultList); - Assert.assertEquals(2, resultList.size()); - Assert.assertEquals("Johnny", resultList.get(0).getFirstName()); - Assert.assertEquals("Jane", resultList.get(1).getFirstName()); - - } - - @Test - public void testIntersectionReturnsClientListBIfClientAListIsNullOrEmpty() { - - List list = new ArrayList<>(); - - Client client3 = new Client("dummy-base-entity-id_3"); - client3.setFirstName("James"); - client3.setLastName("Dean"); - client3.setGender("MALE"); - - Client client4 = new Client("dummy-base-entity-id_1"); - client4.setFirstName("Johnny"); - client4.setLastName("Test"); - client4.setGender("MALE"); - - Client client5 = new Client("dummy-base-entity-id_2"); - client5.setFirstName("Jane"); - client5.setLastName("Test"); - client5.setGender("FEMALE"); - - List list2 = new ArrayList<>(); - - list2.add(client3); - list2.add(client4); - list2.add(client5); - - List resultList = SearchHelper.intersection(list, list2); - - Assert.assertNotNull(resultList); - Assert.assertEquals(3, resultList.size()); - Assert.assertEquals("James", resultList.get(0).getFirstName()); - Assert.assertEquals("Johnny", resultList.get(1).getFirstName()); - Assert.assertEquals("Jane", resultList.get(2).getFirstName()); - - resultList = SearchHelper.intersection(null, list2); - - Assert.assertNotNull(resultList); - Assert.assertEquals(3, resultList.size()); - Assert.assertEquals("James", resultList.get(0).getFirstName()); - Assert.assertEquals("Johnny", resultList.get(1).getFirstName()); - Assert.assertEquals("Jane", resultList.get(2).getFirstName()); - - } - - @Test - public void testIntersectionReturnsAConjunctionOfTwoClientLists() { - - List list = new ArrayList<>(); - - Client client = new Client("dummy-base-entity-id_1"); - client.setFirstName("Johnny"); - client.setLastName("Test"); - client.setGender("MALE"); - - Client client2 = new Client("dummy-base-entity-id_2"); - client2.setFirstName("Jane"); - client2.setLastName("Test"); - client2.setGender("FEMALE"); - - list.add(client); - list.add(client2); - - Client client3 = new Client("dummy-base-entity-id_3"); - client3.setFirstName("James"); - client3.setLastName("Dean"); - client3.setGender("MALE"); - - Client client4 = new Client("dummy-base-entity-id_1"); - client4.setFirstName("Johnny"); - client4.setLastName("Test"); - client4.setGender("MALE"); - - Client client5 = new Client("dummy-base-entity-id_2"); - client5.setFirstName("Jane"); - client5.setLastName("Test"); - client5.setGender("FEMALE"); - - List list2 = new ArrayList<>(); - - list2.add(client3); - list2.add(client4); - list2.add(client5); - - List resultList = SearchHelper.intersection(list, list2); - - Assert.assertNotNull(resultList); - Assert.assertEquals(2, resultList.size()); - Assert.assertEquals("Johnny", resultList.get(0).getFirstName()); - Assert.assertEquals("Jane", resultList.get(1).getFirstName()); - - } - - @Test - public void childSearchParamProcessorShouldHaveCorrectIdentifierWhenCreatingBean() throws ParseException { - HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); - Mockito.when(httpServletRequest.getParameter("limit")).thenReturn("20"); - Mockito.when(httpServletRequest.getParameter("opensrp_id")).thenReturn("2093980"); - SearchEntityWrapper searchEntityWrapper = SearchHelper.childSearchParamProcessor(httpServletRequest); - String result = searchEntityWrapper.getClientSearchBean().getIdentifiers().get("opensrp_id"); - Assert.assertEquals("2093980", result); - } - - @Test - public void testProcessSearchResult() { - final String mother = "mother"; - - Client motherClient = new Client("dummy-mother-base-entity-id"); - motherClient.setFirstName("Jane"); - motherClient.setLastName("Doe"); - motherClient.setGender("FEMALE"); - motherClient.setIdentifiers(EasyMap.mapOf("M_ZEIR_ID", "673939_mother")); - List motherList = new ArrayList<>(); - motherList.add(motherClient); - - List list = new ArrayList<>(); - list.add("dummy-mother-base-entity-id"); - - Map> relationships = new HashMap<>(); - relationships.put(mother, list); - - Client child = new Client("dummy-mother-base-entity-id"); - child.setFirstName("John"); - child.setLastName("Doe"); - child.setGender("Male"); - child.setRelationships(relationships); - child.setIdentifiers(EasyMap.mapOf("M_ZEIR_ID", "673939_mother")); - - List childList = new ArrayList<>(); - childList.add(child); - - List childMothers = SearchHelper.processSearchResult(childList, motherList, mother); - Assert.assertEquals(1, childMothers.size()); - Assert.assertEquals("John Doe", childMothers.get(0).getChild().fullName()); - } - - @Test - public void testMotherSearchParamProcessorForHttpServletRequest() throws ParseException { - HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); - Mockito.when(httpServletRequest.getParameter("limit")).thenReturn("0"); - Mockito.when(httpServletRequest.getParameter("mother_first_name")).thenReturn("Jane"); - Mockito.when(httpServletRequest.getParameter("mother_last_name")).thenReturn("Doe"); - Mockito.when(httpServletRequest.getParameter("mother_nrc_number")).thenReturn("2093980"); - Mockito.when(httpServletRequest.getParameter("NRC_Number")).thenReturn("20939801123"); - Mockito.when(httpServletRequest.getParameter("mother_compass_relationship_id")).thenReturn("dab102f71bd"); - SearchEntityWrapper searchEntityWrapper = SearchHelper.motherSearchParamProcessor(httpServletRequest); - Map result = searchEntityWrapper.getClientSearchBean().getAttributes(); - Assert.assertEquals(2, result.size()); - Assert.assertTrue( result.containsKey("NRC_Number")); - Assert.assertTrue( result.containsKey("Compass_Relationship_ID")); - Assert.assertEquals("2093980", result.get("NRC_Number")); - } - - @Test - public void testMotherSearchParamProcessorForJSONObject() throws ParseException { - JSONObject jsonObject = Mockito.mock(JSONObject.class); - Mockito.when(jsonObject.optString("limit")).thenReturn("0"); - Mockito.when(jsonObject.optString("mother_first_name")).thenReturn("Jane"); - Mockito.when(jsonObject.optString("mother_last_name")).thenReturn("Doe"); - Mockito.when(jsonObject.optString("mother_nrc_number")).thenReturn("2093980"); - Mockito.when(jsonObject.optString("NRC_Number")).thenReturn("20939801123"); - Mockito.when(jsonObject.optString("mother_compass_relationship_id")).thenReturn("dab102f71bd"); - Mockito.when(jsonObject.optString("lastEdited")).thenReturn(""); - SearchEntityWrapper searchEntityWrapper = SearchHelper.motherSearchParamProcessor(jsonObject); - Map result = searchEntityWrapper.getClientSearchBean().getAttributes(); - Assert.assertEquals(2, result.size()); - Assert.assertTrue( result.containsKey("NRC_Number")); - Assert.assertTrue( result.containsKey("Compass_Relationship_ID")); - Assert.assertEquals("2093980", result.get("NRC_Number")); - } - - @Test - public void testChildSearchParamProcessorForJSONObject() throws ParseException { - JSONObject jsonObject = Mockito.mock(JSONObject.class); - Mockito.when(jsonObject.optString("limit")).thenReturn("50"); - Mockito.when(jsonObject.optString("lastEdited")).thenReturn(""); - Mockito.when(jsonObject.optString(SearchHelper.BIRTH_DATE)).thenReturn(""); - Mockito.when(jsonObject.optString(SearchHelper.ZEIR_ID)).thenReturn("1234"); - Mockito.when(jsonObject.optString(SearchHelper.OPENSRP_ID)).thenReturn("4567"); - Mockito.when(jsonObject.optString(SearchHelper.SIM_PRINT_GUID)).thenReturn("91011"); - Mockito.when(jsonObject.optString(SearchHelper.INACTIVE)).thenReturn("false"); - Mockito.when(jsonObject.optString(SearchHelper.LOST_TO_FOLLOW_UP)).thenReturn("true"); - Mockito.when(jsonObject.optString(SearchHelper.NFC_CARD_IDENTIFIER)).thenReturn("nfc_card_identifier_1"); - SearchEntityWrapper searchEntityWrapper = SearchHelper.childSearchParamProcessor(jsonObject); - - Map attributes = searchEntityWrapper.getClientSearchBean().getAttributes(); - Assert.assertEquals(3, attributes.size()); - - Map identifiers = searchEntityWrapper.getClientSearchBean().getIdentifiers(); - Assert.assertEquals(4, identifiers.size()); - - Assert.assertTrue(identifiers.containsKey(SearchHelper.ZEIR_ID)); - Assert.assertTrue(identifiers.containsKey("ZEIR_ID")); //check backward compatibility with upper case key - Assert.assertTrue(identifiers.containsKey(SearchHelper.SIM_PRINT_GUID)); - - Assert.assertTrue(attributes.containsKey(SearchHelper.INACTIVE)); - Assert.assertTrue(attributes.containsKey(SearchHelper.LOST_TO_FOLLOW_UP)); - Assert.assertTrue(attributes.containsKey("NFC_Card_Identifier")); - - Assert.assertEquals(identifiers.get(SearchHelper.ZEIR_ID), "1234"); - Assert.assertEquals(attributes.get("NFC_Card_Identifier"), "nfc_card_identifier_1"); - } - - @Test - public void testChildSearchParamProcessorForHttpServletRequest() throws ParseException { - HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); - Mockito.when(httpServletRequest.getParameter("limit")).thenReturn("50"); - Mockito.when(httpServletRequest.getParameter("lastEdited")).thenReturn(""); - Mockito.when(httpServletRequest.getParameter(SearchHelper.BIRTH_DATE)).thenReturn(""); - Mockito.when(httpServletRequest.getParameter(SearchHelper.ZEIR_ID)).thenReturn("1234"); - Mockito.when(httpServletRequest.getParameter(SearchHelper.OPENSRP_ID)).thenReturn("4567"); - Mockito.when(httpServletRequest.getParameter(SearchHelper.SIM_PRINT_GUID)).thenReturn("91011"); - Mockito.when(httpServletRequest.getParameter(SearchHelper.INACTIVE)).thenReturn("false"); - Mockito.when(httpServletRequest.getParameter(SearchHelper.LOST_TO_FOLLOW_UP)).thenReturn("true"); - Mockito.when(httpServletRequest.getParameter(SearchHelper.NFC_CARD_IDENTIFIER)).thenReturn("nfc_card_identifier_1"); - SearchEntityWrapper searchEntityWrapper = SearchHelper.childSearchParamProcessor(httpServletRequest); - - Map attributes = searchEntityWrapper.getClientSearchBean().getAttributes(); - Assert.assertEquals(3, attributes.size()); - - Map identifiers = searchEntityWrapper.getClientSearchBean().getIdentifiers(); - Assert.assertEquals(4, identifiers.size()); - - Assert.assertTrue(identifiers.containsKey(SearchHelper.ZEIR_ID)); - Assert.assertTrue(identifiers.containsKey("ZEIR_ID")); //check backward compatibility with upper case key - Assert.assertTrue(identifiers.containsKey(SearchHelper.SIM_PRINT_GUID)); - - Assert.assertTrue(attributes.containsKey(SearchHelper.INACTIVE)); - Assert.assertTrue(attributes.containsKey(SearchHelper.LOST_TO_FOLLOW_UP)); - Assert.assertTrue(attributes.containsKey("NFC_Card_Identifier")); - - Assert.assertEquals(identifiers.get(SearchHelper.ZEIR_ID), "1234"); - Assert.assertEquals(attributes.get("NFC_Card_Identifier"), "nfc_card_identifier_1"); - } - - @Test - public void testSetCoreFiltersForJSONObjectWithIntegerLimitReturnsValue(){ - JSONObject jsonObject = new JSONObject(); - jsonObject.put("limit", 50); - try { - int result = SearchHelper.setCoreFilters(jsonObject, new ClientSearchBean()); - Assert.assertEquals(50,result); - } catch (ParseException e) { - e.printStackTrace(); - } - } - - @Test - public void testSetCoreFiltersForJSONObjectWithStringLimitReturnsValue(){ - JSONObject jsonObject = new JSONObject(); - jsonObject.put("limit", "50"); - try { - int result = SearchHelper.setCoreFilters(jsonObject, new ClientSearchBean()); - Assert.assertEquals(50,result); - } catch (ParseException e) { - e.printStackTrace(); - } - } + + private Client testClient = null; + + @Before + public void setUp() { + + Client motherClient = new Client("dummy-mother-base-entity-id"); + motherClient.setFirstName("Jane"); + motherClient.setLastName("Doe"); + motherClient.setGender("FEMALE"); + motherClient.setIdentifiers(EasyMap.mapOf("M_ZEIR_ID", "673939_mother")); + testClient = motherClient; + } + + @Test + public void testCreateClientListIfEmptyCreatesListOnNull() { + + List list = SearchHelper.createClientListIfEmpty(null); + Assert.assertNotNull(list); + } + + @Test + public void testCreateClientListIfEmptyDoesNotModifyParameterList() { + Client client = new Client("dummy-base-entity-id"); + client.setFirstName("Johnny"); + client.setLastName("Test"); + client.setGender("MALE"); + + List myClientList = Arrays.asList(new Client[]{client}); + + List list = SearchHelper.createClientListIfEmpty(myClientList); + + Assert.assertNotNull(list); + org.springframework.util.Assert.notEmpty(list); + Assert.assertEquals("dummy-base-entity-id", list.get(0).getBaseEntityId()); + Assert.assertEquals("Johnny", list.get(0).getFirstName()); + Assert.assertEquals("Test", list.get(0).getLastName()); + Assert.assertEquals("MALE", list.get(0).getGender()); + } + + @Test + public void testGetContactPhoneNumberParamReturnsCorrectValueForParam() { + + String MOTHER_GUARDIAN_PHONE_NUMBER = "mother_contact_phone_number"; + String CONTACT_PHONE_NUMBER = "contact_phone_number"; + + MockHttpServletRequest req = new MockHttpServletRequest(); + req.addParameter(MOTHER_GUARDIAN_PHONE_NUMBER, "+254738388383"); + req.addParameter(CONTACT_PHONE_NUMBER, "+2547112233445"); + + String motherGuardianNum = SearchHelper.getContactPhoneNumberParam(req); + + Assert.assertNotNull(motherGuardianNum); + Assert.assertEquals("+254738388383", motherGuardianNum); + + req.removeParameter(MOTHER_GUARDIAN_PHONE_NUMBER); + motherGuardianNum = SearchHelper.getContactPhoneNumberParam(req); + + Assert.assertNotNull(motherGuardianNum); + Assert.assertEquals("+2547112233445", motherGuardianNum); + } + + @Test + public void testIntersectionReturnsEmptyListIfBothListsEmptyOrNull() { + + List list = new ArrayList<>(); + + List list2 = new ArrayList<>(); + + List resultList = SearchHelper.intersection(list, list2); + + Assert.assertNotNull(resultList); + Assert.assertEquals(0, resultList.size()); + + resultList = SearchHelper.intersection(new ArrayList(), new ArrayList()); + + Assert.assertNotNull(resultList); + Assert.assertEquals(0, resultList.size()); + + } + + @Test + public void testIntersectionReturnsClientListAIfClientBListIsNullOrEmpty() { + + List list = new ArrayList<>(); + + Client client = new Client("dummy-base-entity-id_1"); + client.setFirstName("Johnny"); + client.setLastName("Test"); + client.setGender("MALE"); + + Client client2 = new Client("dummy-base-entity-id_2"); + client2.setFirstName("Jane"); + client2.setLastName("Test"); + client2.setGender("FEMALE"); + + list.add(client); + list.add(client2); + + List resultList = SearchHelper.intersection(list, null); + + Assert.assertNotNull(resultList); + Assert.assertEquals(2, resultList.size()); + Assert.assertEquals("Johnny", resultList.get(0).getFirstName()); + Assert.assertEquals("Jane", resultList.get(1).getFirstName()); + + resultList = SearchHelper.intersection(list, new ArrayList()); + + Assert.assertNotNull(resultList); + Assert.assertEquals(2, resultList.size()); + Assert.assertEquals("Johnny", resultList.get(0).getFirstName()); + Assert.assertEquals("Jane", resultList.get(1).getFirstName()); + + } + + @Test + public void testIntersectionReturnsClientListBIfClientAListIsNullOrEmpty() { + + List list = new ArrayList<>(); + + Client client3 = new Client("dummy-base-entity-id_3"); + client3.setFirstName("James"); + client3.setLastName("Dean"); + client3.setGender("MALE"); + + Client client4 = new Client("dummy-base-entity-id_1"); + client4.setFirstName("Johnny"); + client4.setLastName("Test"); + client4.setGender("MALE"); + + Client client5 = new Client("dummy-base-entity-id_2"); + client5.setFirstName("Jane"); + client5.setLastName("Test"); + client5.setGender("FEMALE"); + + List list2 = new ArrayList<>(); + + list2.add(client3); + list2.add(client4); + list2.add(client5); + + List resultList = SearchHelper.intersection(list, list2); + + Assert.assertNotNull(resultList); + Assert.assertEquals(3, resultList.size()); + Assert.assertEquals("James", resultList.get(0).getFirstName()); + Assert.assertEquals("Johnny", resultList.get(1).getFirstName()); + Assert.assertEquals("Jane", resultList.get(2).getFirstName()); + + resultList = SearchHelper.intersection(null, list2); + + Assert.assertNotNull(resultList); + Assert.assertEquals(3, resultList.size()); + Assert.assertEquals("James", resultList.get(0).getFirstName()); + Assert.assertEquals("Johnny", resultList.get(1).getFirstName()); + Assert.assertEquals("Jane", resultList.get(2).getFirstName()); + + } + + @Test + public void testIntersectionReturnsAConjunctionOfTwoClientLists() { + + List list = new ArrayList<>(); + + Client client = new Client("dummy-base-entity-id_1"); + client.setFirstName("Johnny"); + client.setLastName("Test"); + client.setGender("MALE"); + + Client client2 = new Client("dummy-base-entity-id_2"); + client2.setFirstName("Jane"); + client2.setLastName("Test"); + client2.setGender("FEMALE"); + + list.add(client); + list.add(client2); + + Client client3 = new Client("dummy-base-entity-id_3"); + client3.setFirstName("James"); + client3.setLastName("Dean"); + client3.setGender("MALE"); + + Client client4 = new Client("dummy-base-entity-id_1"); + client4.setFirstName("Johnny"); + client4.setLastName("Test"); + client4.setGender("MALE"); + + Client client5 = new Client("dummy-base-entity-id_2"); + client5.setFirstName("Jane"); + client5.setLastName("Test"); + client5.setGender("FEMALE"); + + List list2 = new ArrayList<>(); + + list2.add(client3); + list2.add(client4); + list2.add(client5); + + List resultList = SearchHelper.intersection(list, list2); + + Assert.assertNotNull(resultList); + Assert.assertEquals(2, resultList.size()); + Assert.assertEquals("Johnny", resultList.get(0).getFirstName()); + Assert.assertEquals("Jane", resultList.get(1).getFirstName()); + + } + + @Test + public void childSearchParamProcessorShouldHaveCorrectIdentifierWhenCreatingBean() throws ParseException { + HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); + Mockito.when(httpServletRequest.getParameter("limit")).thenReturn("20"); + Mockito.when(httpServletRequest.getParameter("opensrp_id")).thenReturn("2093980"); + SearchEntityWrapper searchEntityWrapper = SearchHelper.childSearchParamProcessor(httpServletRequest); + String result = searchEntityWrapper.getClientSearchBean().getIdentifiers().get("opensrp_id"); + Assert.assertEquals("2093980", result); + } + + @Test + public void testProcessSearchResult() { + final String mother = "mother"; + + Client motherClient = new Client("dummy-mother-base-entity-id"); + motherClient.setFirstName("Jane"); + motherClient.setLastName("Doe"); + motherClient.setGender("FEMALE"); + motherClient.setIdentifiers(EasyMap.mapOf("M_ZEIR_ID", "673939_mother")); + List motherList = new ArrayList<>(); + motherList.add(motherClient); + + List list = new ArrayList<>(); + list.add("dummy-mother-base-entity-id"); + + Map> relationships = new HashMap<>(); + relationships.put(mother, list); + + Client child = new Client("dummy-mother-base-entity-id"); + child.setFirstName("John"); + child.setLastName("Doe"); + child.setGender("Male"); + child.setRelationships(relationships); + child.setIdentifiers(EasyMap.mapOf("M_ZEIR_ID", "673939_mother")); + + List childList = new ArrayList<>(); + childList.add(child); + + List childMothers = SearchHelper.processSearchResult(childList, motherList, mother); + Assert.assertEquals(1, childMothers.size()); + Assert.assertEquals("John Doe", childMothers.get(0).getChild().fullName()); + } + + @Test + public void testMotherSearchParamProcessorForHttpServletRequest() throws ParseException { + HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); + Mockito.when(httpServletRequest.getParameter("limit")).thenReturn("0"); + Mockito.when(httpServletRequest.getParameter("mother_first_name")).thenReturn("Jane"); + Mockito.when(httpServletRequest.getParameter("mother_last_name")).thenReturn("Doe"); + Mockito.when(httpServletRequest.getParameter("mother_nrc_number")).thenReturn("2093980"); + Mockito.when(httpServletRequest.getParameter("NRC_Number")).thenReturn("20939801123"); + Mockito.when(httpServletRequest.getParameter("mother_compass_relationship_id")).thenReturn("dab102f71bd"); + SearchEntityWrapper searchEntityWrapper = SearchHelper.motherSearchParamProcessor(httpServletRequest); + Map result = searchEntityWrapper.getClientSearchBean().getAttributes(); + Assert.assertEquals(2, result.size()); + Assert.assertTrue(result.containsKey("NRC_Number")); + Assert.assertTrue(result.containsKey("Compass_Relationship_ID")); + Assert.assertEquals("2093980", result.get("NRC_Number")); + } + + @Test + public void testMotherSearchParamProcessorForJSONObject() throws ParseException { + JSONObject jsonObject = Mockito.mock(JSONObject.class); + Mockito.when(jsonObject.optString("limit")).thenReturn("0"); + Mockito.when(jsonObject.optString("mother_first_name")).thenReturn("Jane"); + Mockito.when(jsonObject.optString("mother_last_name")).thenReturn("Doe"); + Mockito.when(jsonObject.optString("mother_nrc_number")).thenReturn("2093980"); + Mockito.when(jsonObject.optString("NRC_Number")).thenReturn("20939801123"); + Mockito.when(jsonObject.optString("mother_compass_relationship_id")).thenReturn("dab102f71bd"); + Mockito.when(jsonObject.optString("lastEdited")).thenReturn(""); + SearchEntityWrapper searchEntityWrapper = SearchHelper.motherSearchParamProcessor(jsonObject); + Map result = searchEntityWrapper.getClientSearchBean().getAttributes(); + Assert.assertEquals(2, result.size()); + Assert.assertTrue(result.containsKey("NRC_Number")); + Assert.assertTrue(result.containsKey("Compass_Relationship_ID")); + Assert.assertEquals("2093980", result.get("NRC_Number")); + } + + @Test + public void testChildSearchParamProcessorForJSONObject() throws ParseException { + JSONObject jsonObject = Mockito.mock(JSONObject.class); + Mockito.when(jsonObject.optString("limit")).thenReturn("50"); + Mockito.when(jsonObject.optString("lastEdited")).thenReturn(""); + Mockito.when(jsonObject.optString(SearchHelper.BIRTH_DATE)).thenReturn(""); + Mockito.when(jsonObject.optString(SearchHelper.ZEIR_ID)).thenReturn("1234"); + Mockito.when(jsonObject.optString(SearchHelper.OPENSRP_ID)).thenReturn("4567"); + Mockito.when(jsonObject.optString(SearchHelper.SIM_PRINT_GUID)).thenReturn("91011"); + Mockito.when(jsonObject.optString(SearchHelper.INACTIVE)).thenReturn("false"); + Mockito.when(jsonObject.optString(SearchHelper.LOST_TO_FOLLOW_UP)).thenReturn("true"); + Mockito.when(jsonObject.optString(SearchHelper.NFC_CARD_IDENTIFIER)).thenReturn("nfc_card_identifier_1"); + SearchEntityWrapper searchEntityWrapper = SearchHelper.childSearchParamProcessor(jsonObject); + + Map attributes = searchEntityWrapper.getClientSearchBean().getAttributes(); + Assert.assertEquals(3, attributes.size()); + + Map identifiers = searchEntityWrapper.getClientSearchBean().getIdentifiers(); + Assert.assertEquals(4, identifiers.size()); + + Assert.assertTrue(identifiers.containsKey(SearchHelper.ZEIR_ID)); + Assert.assertTrue(identifiers.containsKey("ZEIR_ID")); //check backward compatibility with upper case key + Assert.assertTrue(identifiers.containsKey(SearchHelper.SIM_PRINT_GUID)); + + Assert.assertTrue(attributes.containsKey(SearchHelper.INACTIVE)); + Assert.assertTrue(attributes.containsKey(SearchHelper.LOST_TO_FOLLOW_UP)); + Assert.assertTrue(attributes.containsKey("NFC_Card_Identifier")); + + Assert.assertEquals(identifiers.get(SearchHelper.ZEIR_ID), "1234"); + Assert.assertEquals(attributes.get("NFC_Card_Identifier"), "nfc_card_identifier_1"); + } + + @Test + public void testChildSearchParamProcessorForHttpServletRequest() throws ParseException { + HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); + Mockito.when(httpServletRequest.getParameter("limit")).thenReturn("50"); + Mockito.when(httpServletRequest.getParameter("lastEdited")).thenReturn(""); + Mockito.when(httpServletRequest.getParameter(SearchHelper.BIRTH_DATE)).thenReturn(""); + Mockito.when(httpServletRequest.getParameter(SearchHelper.ZEIR_ID)).thenReturn("1234"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.OPENSRP_ID)).thenReturn("4567"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.SIM_PRINT_GUID)).thenReturn("91011"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.INACTIVE)).thenReturn("false"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.LOST_TO_FOLLOW_UP)).thenReturn("true"); + Mockito.when(httpServletRequest.getParameter(SearchHelper.NFC_CARD_IDENTIFIER)).thenReturn("nfc_card_identifier_1"); + SearchEntityWrapper searchEntityWrapper = SearchHelper.childSearchParamProcessor(httpServletRequest); + + Map attributes = searchEntityWrapper.getClientSearchBean().getAttributes(); + Assert.assertEquals(3, attributes.size()); + + Map identifiers = searchEntityWrapper.getClientSearchBean().getIdentifiers(); + Assert.assertEquals(4, identifiers.size()); + + Assert.assertTrue(identifiers.containsKey(SearchHelper.ZEIR_ID)); + Assert.assertTrue(identifiers.containsKey("ZEIR_ID")); //check backward compatibility with upper case key + Assert.assertTrue(identifiers.containsKey(SearchHelper.SIM_PRINT_GUID)); + + Assert.assertTrue(attributes.containsKey(SearchHelper.INACTIVE)); + Assert.assertTrue(attributes.containsKey(SearchHelper.LOST_TO_FOLLOW_UP)); + Assert.assertTrue(attributes.containsKey("NFC_Card_Identifier")); + + Assert.assertEquals(identifiers.get(SearchHelper.ZEIR_ID), "1234"); + Assert.assertEquals(attributes.get("NFC_Card_Identifier"), "nfc_card_identifier_1"); + } + + @Test + public void testSetCoreFiltersForJSONObjectWithIntegerLimitReturnsValue() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("limit", 50); + try { + int result = SearchHelper.setCoreFilters(jsonObject, new ClientSearchBean()); + Assert.assertEquals(50, result); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + @Test + public void testSetCoreFiltersForJSONObjectWithStringLimitReturnsValue() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("limit", "50"); + try { + int result = SearchHelper.setCoreFilters(jsonObject, new ClientSearchBean()); + Assert.assertEquals(50, result); + } catch (ParseException e) { + e.printStackTrace(); + } + } } From 64daba87db52a3518c729c37b018351de4288ac7 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 23 Nov 2022 09:50:43 +0300 Subject: [PATCH 3/4] rename searchByPath method --- src/main/java/org/opensrp/web/rest/SearchResource.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opensrp/web/rest/SearchResource.java b/src/main/java/org/opensrp/web/rest/SearchResource.java index 190cb20b0..90960394f 100644 --- a/src/main/java/org/opensrp/web/rest/SearchResource.java +++ b/src/main/java/org/opensrp/web/rest/SearchResource.java @@ -112,7 +112,7 @@ public List search(HttpServletRequest request) throws ParseException {// String identifierType = StringUtils.isBlank(identifiers) ? null : identifiers.split(":", -1)[0]; String identifierValue = StringUtils.isBlank(identifiers) ? null : identifiers.split(":", -1)[1]; - identifierMap = new HashMap(); + identifierMap = new HashMap<>(); identifierMap.put(identifierType, identifierValue); } @@ -172,7 +172,7 @@ public List searchByPost(@RequestBody String jsonRequestBody) throws Par } @RequestMapping(method = RequestMethod.GET, value = "/path", produces = {MediaType.APPLICATION_JSON_VALUE}) - private List searchPathBy(HttpServletRequest request) throws ParseException { + private List searchPathByGet(HttpServletRequest request) throws ParseException { String contactPhoneNumber = SearchHelper.getContactPhoneNumberParam(request); SearchEntityWrapper childSearchEntity = SearchHelper.childSearchParamProcessor(request); @@ -182,7 +182,7 @@ private List searchPathBy(HttpServletRequest request) throws ParseE } @RequestMapping(method = RequestMethod.POST, value = "/path", produces = {MediaType.APPLICATION_JSON_VALUE}) - private List searchPathBy(@RequestBody String jsonRequestBody) throws ParseException { + private List searchPathByPost(@RequestBody String jsonRequestBody) throws ParseException { JSONObject jsonRequestBodyObject = new JSONObject(jsonRequestBody); SearchEntityWrapper childSearchEntity = SearchHelper.childSearchParamProcessor(jsonRequestBodyObject); From 1b59e43d9cf2c6d4a392a770e51d290a9c7b8f2c Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 23 Nov 2022 09:53:13 +0300 Subject: [PATCH 4/4] fix codacy issues --- src/test/java/org/opensrp/web/rest/SearchResourceTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/java/org/opensrp/web/rest/SearchResourceTest.java b/src/test/java/org/opensrp/web/rest/SearchResourceTest.java index 2efbb9522..cacd668eb 100644 --- a/src/test/java/org/opensrp/web/rest/SearchResourceTest.java +++ b/src/test/java/org/opensrp/web/rest/SearchResourceTest.java @@ -11,7 +11,6 @@ import org.opensrp.repository.PlanRepository; import org.opensrp.service.ClientService; import org.opensrp.service.EventService; -import org.opensrp.service.ExportEventDataMapper; import org.opensrp.service.SearchService; import org.opensrp.service.TaskGenerator; import org.smartregister.domain.Client; @@ -45,8 +44,6 @@ public class SearchResourceTest { private EventService eventService; - private ExportEventDataMapper exportEventDataMapper; - private TaskGenerator taskGenerator;