This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #900 from egovernments/develop
Develop
- Loading branch information
Showing
82 changed files
with
3,863 additions
and
752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ss-services/egov-hrms/src/main/java/org/egov/hrms/repository/ElasticSearchRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.egov.hrms.repository; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.egov.hrms.config.PropertiesManager; | ||
import org.egov.hrms.web.contract.EmployeeSearchCriteria; | ||
import org.egov.tracer.model.CustomException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@Repository | ||
public class ElasticSearchRepository { | ||
private PropertiesManager config; | ||
|
||
private FuzzySearchQueryBuilder queryBuilder; | ||
|
||
private RestTemplate restTemplate; | ||
|
||
private ObjectMapper mapper; | ||
|
||
@Autowired | ||
public ElasticSearchRepository(PropertiesManager config, FuzzySearchQueryBuilder queryBuilder, RestTemplate restTemplate, ObjectMapper mapper) { | ||
this.config = config; | ||
this.queryBuilder = queryBuilder; | ||
this.restTemplate = restTemplate; | ||
this.mapper = mapper; | ||
} | ||
|
||
public Object fuzzySearchEmployees(EmployeeSearchCriteria criteria, List<String> uuids) { | ||
|
||
|
||
String url = getESURL(); | ||
|
||
String searchQuery = queryBuilder.getFuzzySearchQuery(criteria, uuids); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Authorization", getESEncodedCredentials()); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
log.info("Headers: " + headers.toString()); | ||
HttpEntity<String> requestEntity = new HttpEntity<>(searchQuery, headers); | ||
ResponseEntity response = null; | ||
try { | ||
response = restTemplate.postForEntity(url, requestEntity, Object.class); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new CustomException("ES_ERROR","Failed to fetch data from ES"); | ||
} | ||
|
||
return response.getBody(); | ||
|
||
} | ||
|
||
|
||
/** | ||
* Generates elasticsearch search url from application properties | ||
* | ||
* @return | ||
*/ | ||
private String getESURL() { | ||
|
||
StringBuilder builder = new StringBuilder(config.getEsHost()); | ||
builder.append(config.getEsPTIndex()); | ||
builder.append(config.getEsSearchEndpoint()); | ||
|
||
return builder.toString(); | ||
} | ||
|
||
public String getESEncodedCredentials() { | ||
String credentials = config.getUserName() + ":" + config.getPassword(); | ||
byte[] credentialsBytes = credentials.getBytes(); | ||
byte[] base64CredentialsBytes = Base64.getEncoder().encode(credentialsBytes); | ||
return "Basic " + new String(base64CredentialsBytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.