diff --git a/src/main/java/com/iemr/common/controller/beneficiary/BeneficiaryRegistrationController.java b/src/main/java/com/iemr/common/controller/beneficiary/BeneficiaryRegistrationController.java index d9b0f06f..0876c294 100644 --- a/src/main/java/com/iemr/common/controller/beneficiary/BeneficiaryRegistrationController.java +++ b/src/main/java/com/iemr/common/controller/beneficiary/BeneficiaryRegistrationController.java @@ -36,6 +36,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @@ -70,6 +71,8 @@ import com.iemr.common.service.userbeneficiarydata.MaritalStatusService; import com.iemr.common.service.userbeneficiarydata.StatusService; import com.iemr.common.service.userbeneficiarydata.TitleService; +import com.iemr.common.utils.CookieUtil; +import com.iemr.common.utils.JwtUtil; import com.iemr.common.utils.mapper.InputMapper; import com.iemr.common.utils.mapper.OutputMapper; import com.iemr.common.utils.response.OutputResponse; @@ -103,6 +106,10 @@ public class BeneficiaryRegistrationController { private BeneficiaryOccupationService beneficiaryOccupationService; private GovtIdentityTypeService govtIdentityTypeService; + + @Autowired + private JwtUtil jwtUtil; + @Autowired public void setBenRelationshipTypeService(BenRelationshipTypeService benRelationshipTypeService) { this.benRelationshipTypeService = benRelationshipTypeService; @@ -341,6 +348,57 @@ public String searchUserByPhone( return response.toString(); } + @Operation(summary = "Provide the list of beneficiaries using Elasticsearch") + @RequestMapping(value = "/searchUser", method = RequestMethod.POST, headers = "Authorization") + public String searchUser( + @RequestBody String request, + @RequestHeader(value = "Authorization", required = false) String auth, HttpServletRequest httpRequest) { + + OutputResponse response = new OutputResponse(); + + try { + logger.info("Universal search request received"); + + JsonParser parser = new JsonParser(); + JsonObject requestObj = parser.parse(request).getAsJsonObject(); + + String searchQuery = null; + if (requestObj.has("search") && !requestObj.get("search").isJsonNull()) { + searchQuery = requestObj.get("search").getAsString(); + } + + if (searchQuery == null || searchQuery.trim().isEmpty()) { + response.setError(5000, "Search query is required"); + return response.toString(); + } + + String jwtToken = CookieUtil.getJwtTokenFromCookie(httpRequest); + String userId = jwtUtil.getUserIdFromToken(jwtToken); + int userID=Integer.parseInt(userId); + + Boolean is1097 = false; + if (requestObj.has("is1097") && !requestObj.get("is1097").isJsonNull()) { + is1097 = requestObj.get("is1097").getAsBoolean(); + } + + logger.info("Searching with query: {}, userId: {}, is1097: {}", searchQuery, userID, is1097); + + String result = iemrSearchUserService.searchUser(searchQuery, userID, auth, is1097); + + if (result == null || result.trim().isEmpty()) { + response.setError(5000, "No beneficiaries found"); + return response.toString(); + } + + return result; + + } catch (Exception e) { + logger.error("Error in universal search: {}", e.getMessage(), e); + response.setError(5000, "Error searching beneficiaries: " + e.getMessage()); + return response.toString(); + } + } + @Operation(summary = "Provide the list of beneficiaries based on search criteria") @RequestMapping(value = "/searchBeneficiary", method = RequestMethod.POST, headers = "Authorization") public String searchBeneficiary( diff --git a/src/main/java/com/iemr/common/data/users/User.java b/src/main/java/com/iemr/common/data/users/User.java index e6184e60..7f284b11 100644 --- a/src/main/java/com/iemr/common/data/users/User.java +++ b/src/main/java/com/iemr/common/data/users/User.java @@ -55,6 +55,7 @@ @Entity @Table(name = "m_user") @Data +@JsonIgnoreProperties(ignoreUnknown = true) public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/iemr/common/service/beneficiary/IEMRSearchUserService.java b/src/main/java/com/iemr/common/service/beneficiary/IEMRSearchUserService.java index 6e7848cd..ab4c40fd 100644 --- a/src/main/java/com/iemr/common/service/beneficiary/IEMRSearchUserService.java +++ b/src/main/java/com/iemr/common/service/beneficiary/IEMRSearchUserService.java @@ -38,6 +38,8 @@ String findByBeneficiaryPhoneNo(BenPhoneMap benPhoneMap, Integer pageNo, Integer String findBeneficiary(BeneficiaryModel request, String auth) throws Exception; + String searchUser(String searchQuery, Integer userId, String auth, Boolean is1097) throws Exception; + List userExitsCheckWithId(String beneficiaryID, String auth, Boolean is1097) throws Exception; public List userExitsCheckWithHealthId_ABHAId(String healthID, String auth, Boolean is1097) diff --git a/src/main/java/com/iemr/common/service/beneficiary/IEMRSearchUserServiceImpl.java b/src/main/java/com/iemr/common/service/beneficiary/IEMRSearchUserServiceImpl.java index f67d7815..7c9bfc2d 100644 --- a/src/main/java/com/iemr/common/service/beneficiary/IEMRSearchUserServiceImpl.java +++ b/src/main/java/com/iemr/common/service/beneficiary/IEMRSearchUserServiceImpl.java @@ -25,6 +25,7 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Objects; import org.slf4j.Logger; @@ -76,7 +77,7 @@ import com.iemr.common.repository.userbeneficiarydata.MaritalStatusRepository; import com.iemr.common.repository.userbeneficiarydata.SexualOrientationRepository; import com.iemr.common.repository.userbeneficiarydata.TitleRepository; -import com.iemr.common.utils.mapper.OutputMapper; +import com.iemr.common.utils.exception.IEMRException; /** * @@ -322,6 +323,34 @@ private void setBeneficiaryGender(List iBeneficiary) { } + /** + Universal search using Elasticsearch + */ + @Override + public String searchUser(String searchQuery, Integer userId, String auth, Boolean is1097) throws Exception { + + try { + if (searchQuery == null || searchQuery.trim().isEmpty()) { + throw new IEMRException("Search query is required"); + } + + logger.info("Universal search with query: {}, userId: {}", searchQuery, userId); + + Map response = + identityBeneficiaryService.searchBeneficiariesUsingES( + searchQuery, userId, auth, is1097 + ); + + ObjectMapper mapper = new ObjectMapper(); + return mapper.writeValueAsString(response); + + } catch (Exception e) { + logger.error("Error in universal search", e); + throw new Exception("Error searching beneficiaries: " + e.getMessage(), e); + } +} + + // Advance search @Override public String findBeneficiary(BeneficiaryModel i_beneficiary, String auth) throws Exception { diff --git a/src/main/java/com/iemr/common/service/beneficiary/IdentityBeneficiaryService.java b/src/main/java/com/iemr/common/service/beneficiary/IdentityBeneficiaryService.java index 8b84bc8a..5ebe3b24 100644 --- a/src/main/java/com/iemr/common/service/beneficiary/IdentityBeneficiaryService.java +++ b/src/main/java/com/iemr/common/service/beneficiary/IdentityBeneficiaryService.java @@ -23,6 +23,7 @@ import java.util.HashSet; import java.util.List; +import java.util.Map; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; @@ -69,4 +70,8 @@ public List getBeneficiaryListByFamilyId(String familyId, Stri public List getBeneficiaryListByGovId(String identity, String auth, Boolean is1097) throws IEMRException; + + public Map searchBeneficiariesUsingES(String query, Integer userId, String auth, Boolean is1097) throws IEMRException; + + } diff --git a/src/main/java/com/iemr/common/service/beneficiary/IdentityBeneficiaryServiceImpl.java b/src/main/java/com/iemr/common/service/beneficiary/IdentityBeneficiaryServiceImpl.java index 2e6ac1d4..dd717504 100644 --- a/src/main/java/com/iemr/common/service/beneficiary/IdentityBeneficiaryServiceImpl.java +++ b/src/main/java/com/iemr/common/service/beneficiary/IdentityBeneficiaryServiceImpl.java @@ -21,15 +21,20 @@ */ package com.iemr.common.service.beneficiary; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import com.google.gson.*; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.iemr.common.dto.identity.BeneficiariesDTO; @@ -43,6 +48,12 @@ import com.iemr.common.utils.mapper.OutputMapper; import com.iemr.common.utils.response.OutputResponse; +import org.springframework.beans.factory.annotation.Value; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + @Service public class IdentityBeneficiaryServiceImpl implements IdentityBeneficiaryService { @@ -107,6 +118,75 @@ public List getBeneficiaryListByIDs(HashSet benIdList, String return listBenDetailForOutboundDTO; } + /** + Call Identity API's Elasticsearch universal search + */ + @Override + public Map searchBeneficiariesUsingES(String query, Integer userId, String auth, Boolean is1097 ) throws IEMRException { + + Map response = new HashMap<>(); + + try { + HashMap headers = new HashMap<>(); + if (auth != null && !auth.isEmpty()) { + headers.put("Authorization", auth); + } + + String baseUrl = ConfigProperties + .getPropertyByName("identity-api-url-searchByES") + .replace( + IDENTITY_BASE_URL, + (Boolean.TRUE.equals(is1097)) ? identity1097BaseURL : identityBaseURL + ); + + StringBuilder url = new StringBuilder(baseUrl) + .append("?query=").append(URLEncoder.encode(query, StandardCharsets.UTF_8)); + + if (userId != null) { + url.append("&userId=").append(userId); + } + + logger.info("Calling Identity ES search URL: {}", url); + + String result = httpUtils.get(url.toString(), headers); + + if (result == null || result.isEmpty()) { + response.put("data", Collections.emptyList()); + response.put("statusCode", 200); + response.put("status", "Success"); + response.put("errorMessage", "Success"); + return response; + } + + ObjectMapper mapper = new ObjectMapper(); + + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + + JsonNode rootNode = mapper.readTree(result); + + if (rootNode.has("statusCode") && rootNode.get("statusCode").asInt() != 200) { + String errMsg = rootNode.has("errorMessage") + ? rootNode.get("errorMessage").asText() + : "Identity ES search failed"; + throw new IEMRException(errMsg); + } + + response.put("data", rootNode.path("data")); + response.put("statusCode", 200); + response.put("status", "Success"); + response.put("errorMessage", "Success"); + + return response; + + } catch (IEMRException e) { + throw e; + } catch (Exception e) { + logger.error("Error calling Identity ES search API", e); + throw new IEMRException("Error calling Identity ES search API"); + } +} + @Override public List getPartialBeneficiaryListByIDs(HashSet benIdList, String auth, Boolean is1097) throws IEMRException { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 427754c5..b52dfc0d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -118,6 +118,7 @@ identity-api-url-getByBenRegId =IDENTITY_BASE_URL/id/getByBenRegId?benRegId= identity-api-url-benCreate =IDENTITY_BASE_URL/id/create identity-api-url-benEdit =IDENTITY_BASE_URL/id/edit identity-api-url-benEditEducationCommunity=IDENTITY_BASE_URL/id/editEducationOrCommunity +identity-api-url-searchByES=IDENTITY_BASE_URL/beneficiary/search identity-api-url-getByFamilyId=IDENTITY_BASE_URL/id/searchByFamilyId?familyId= identity-api-url-getByGovIdentity=IDENTITY_BASE_URL/id/searchByGovIdentity?identity=