Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/iemr/common/data/users/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
@Entity
@Table(name = "m_user")
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BeneficiaryModel> userExitsCheckWithId(String beneficiaryID, String auth, Boolean is1097) throws Exception;

public List<BeneficiaryModel> userExitsCheckWithHealthId_ABHAId(String healthID, String auth, Boolean is1097)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
*
Expand Down Expand Up @@ -322,6 +323,34 @@ private void setBeneficiaryGender(List<BeneficiaryModel> 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<String, Object> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,4 +70,8 @@ public List<BeneficiariesDTO> getBeneficiaryListByFamilyId(String familyId, Stri

public List<BeneficiariesDTO> getBeneficiaryListByGovId(String identity, String auth, Boolean is1097)
throws IEMRException;

public Map<String, Object> searchBeneficiariesUsingES(String query, Integer userId, String auth, Boolean is1097) throws IEMRException;


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -107,6 +118,75 @@ public List<BeneficiariesDTO> getBeneficiaryListByIDs(HashSet benIdList, String
return listBenDetailForOutboundDTO;
}

/**
Call Identity API's Elasticsearch universal search
*/
@Override
public Map<String, Object> searchBeneficiariesUsingES(String query, Integer userId, String auth, Boolean is1097 ) throws IEMRException {

Map<String, Object> response = new HashMap<>();

try {
HashMap<String, Object> 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<BeneficiariesPartialDTO> getPartialBeneficiaryListByIDs(HashSet benIdList, String auth, Boolean is1097)
throws IEMRException {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading