Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hcmpre 469 pagination all #891

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions health-services/facility/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
<dependency>
<groupId>org.egov.common</groupId>
<artifactId>health-services-common</artifactId>
<version>1.0.17-SNAPSHOT</version>
<version>1.0.20-dev-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.egov.common</groupId>
<artifactId>health-services-models</artifactId>
<version>1.0.20-SNAPSHOT</version>
<version>1.0.23-dev-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.egov.common.data.query.builder.SelectQueryBuilder;
import org.egov.common.data.query.exception.QueryBuilderException;
import org.egov.common.data.repository.GenericRepository;
import org.egov.common.models.core.SearchResponse;
import org.egov.common.models.facility.Facility;
import org.egov.common.models.facility.FacilitySearch;
import org.egov.common.producer.Producer;
Expand All @@ -23,6 +24,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static org.egov.common.utils.CommonUtils.constructTotalCountCTEAndReturnResult;
import static org.egov.common.utils.CommonUtils.getIdMethod;

@Repository
Expand All @@ -35,7 +37,7 @@ public FacilityRepository(Producer producer, NamedParameterJdbcTemplate namedPar
facilityRowMapper, Optional.of("facility"));
}

public List<Facility> findById(List<String> ids, String columnName, Boolean includeDeleted) {
public SearchResponse<Facility> findById(List<String> ids, String columnName, Boolean includeDeleted) {
List<Facility> objFound = findInCache(ids);
if (!includeDeleted) {
objFound = objFound.stream()
Expand All @@ -48,7 +50,7 @@ public List<Facility> findById(List<String> ids, String columnName, Boolean incl
.map(obj -> (String) ReflectionUtils.invokeMethod(idMethod, obj))
.collect(Collectors.toList()));
if (ids.isEmpty()) {
return objFound;
return SearchResponse.<Facility>builder().response(objFound).build();
}
}

Expand All @@ -61,10 +63,10 @@ public List<Facility> findById(List<String> ids, String columnName, Boolean incl

objFound.addAll(this.namedParameterJdbcTemplate.query(query, paramMap, this.rowMapper));
putInCache(objFound);
return objFound;
return SearchResponse.<Facility>builder().response(objFound).build();
}

public List<Facility> find(FacilitySearch searchObject, Integer limit, Integer offset, String tenantId, Long lastChangedSince, Boolean includeDeleted) throws QueryBuilderException {
public SearchResponse<Facility> find(FacilitySearch searchObject, Integer limit, Integer offset, String tenantId, Long lastChangedSince, Boolean includeDeleted) throws QueryBuilderException {
String query = "SELECT *, a.id as aid,a.tenantid as atenantid, a.clientreferenceid as aclientreferenceid FROM facility f LEFT JOIN address a ON f.addressid = a.id";
Map<String, Object> paramsMap = new HashMap<>();
List<String> whereFields = GenericQueryBuilder.getFieldsWithCondition(searchObject, QueryFieldChecker.isNotNull, paramsMap);
Expand All @@ -83,12 +85,18 @@ public List<Facility> find(FacilitySearch searchObject, Integer limit, Integer o
if (lastChangedSince != null) {
query = query + "and lastModifiedTime>=:lastModifiedTime ";
}
query = query + "ORDER BY f.id ASC LIMIT :limit OFFSET :offset";
query = query + "ORDER BY f.id ASC";
paramsMap.put("tenantId", tenantId);
paramsMap.put("isDeleted", includeDeleted);
paramsMap.put("lastModifiedTime", lastChangedSince);

Long totalCount = constructTotalCountCTEAndReturnResult(query, paramsMap, this.namedParameterJdbcTemplate);

query = query + " LIMIT :limit OFFSET :offset";
paramsMap.put("limit", limit);
paramsMap.put("offset", offset);
return this.namedParameterJdbcTemplate.query(query, paramsMap, this.rowMapper);
List<Facility> facilities = this.namedParameterJdbcTemplate.query(query, paramsMap, this.rowMapper);

return SearchResponse.<Facility>builder().response(facilities).totalCount(totalCount).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import digit.models.coremodels.AuditDetails;
import org.egov.common.contract.models.AuditDetails;
import org.egov.common.models.core.AdditionalFields;
import org.egov.common.models.facility.Address;
import org.egov.common.models.facility.AddressType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.egov.facility.service;

import io.micrometer.core.instrument.search.Search;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.egov.common.ds.Tuple;
import org.egov.common.models.ErrorDetails;
import org.egov.common.models.core.SearchResponse;
import org.egov.common.models.facility.Facility;
import org.egov.common.models.facility.FacilityBulkRequest;
import org.egov.common.models.facility.FacilityRequest;
import org.egov.common.models.facility.FacilitySearchRequest;
import org.egov.common.models.project.ProjectBeneficiary;
import org.egov.common.validator.Validator;
import org.egov.facility.config.FacilityConfiguration;
import org.egov.facility.repository.FacilityRepository;
Expand Down Expand Up @@ -173,23 +176,26 @@ public List<Facility> delete(FacilityBulkRequest request, boolean isBulk) {
return validEntities;
}

public List<Facility> search(FacilitySearchRequest facilitySearchRequest,
Integer limit,
Integer offset,
String tenantId,
Long lastChangedSince,
Boolean includeDeleted) throws Exception {
public SearchResponse<Facility> search(FacilitySearchRequest facilitySearchRequest,
Integer limit,
Integer offset,
String tenantId,
Long lastChangedSince,
Boolean includeDeleted) throws Exception {
log.info("starting search method for facility");
String idFieldName = getIdFieldName(facilitySearchRequest.getFacility());
if (isSearchByIdOnly(facilitySearchRequest.getFacility(), idFieldName)) {
List<String> ids = (List<String>) ReflectionUtils.invokeMethod(getIdMethod(Collections
.singletonList(facilitySearchRequest.getFacility())),
facilitySearchRequest.getFacility());
return facilityRepository.findById(ids, idFieldName, includeDeleted).stream()
SearchResponse<Facility> searchResponse = facilityRepository.findById(ids, idFieldName, includeDeleted);
List<Facility> facilities = searchResponse.getResponse().stream()
.filter(lastChangedSince(lastChangedSince))
.filter(havingTenantId(tenantId))
.filter(includeDeleted(includeDeleted))
.collect(Collectors.toList());
searchResponse.setResponse(facilities);
return searchResponse;
}

log.info("completed search method for facility");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Map<Facility, List<Error>> validate(FacilityBulkRequest request) {
Map<String, Facility> eMap = getIdToObjMap(validEntities, idMethod);
if (!eMap.isEmpty()) {
List<String> entityIds = new ArrayList<>(eMap.keySet());
List<Facility> existingEntities = facilityRepository.findById(entityIds, getIdFieldName(idMethod), false);
List<Facility> existingEntities = facilityRepository.findById(entityIds, getIdFieldName(idMethod), false).getResponse();
List<Facility> nonExistentEntities = checkNonExistentEntities(eMap,
existingEntities, idMethod);
nonExistentEntities.forEach(facility -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Map<Facility, List<Error>> validate(FacilityBulkRequest request) {
Map<String, Facility> eMap = getIdToObjMap(validEntities, idMethod);
if (!eMap.isEmpty()) {
List<String> entityIds = new ArrayList<>(eMap.keySet());
List<Facility> existingEntities = facilityRepository.findById(entityIds, getIdFieldName(idMethod), false);
List<Facility> existingEntities = facilityRepository.findById(entityIds, getIdFieldName(idMethod), false).getResponse();
List<Facility> entitiesWithMismatchedRowVersion =
getEntitiesWithMismatchedRowVersion(eMap, existingEntities, idMethod);
entitiesWithMismatchedRowVersion.forEach(facility -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.egov.common.contract.response.ResponseInfo;
import org.egov.common.models.core.SearchResponse;
import org.egov.common.models.core.URLParams;
import org.egov.common.models.facility.Facility;
import org.egov.common.models.facility.FacilityBulkRequest;
Expand Down Expand Up @@ -111,15 +112,17 @@ public ResponseEntity<FacilityBulkResponse> facilityV1SearchPost(
@Valid @ModelAttribute URLParams urlParams,
@ApiParam(value = "Details for existing facility.", required = true) @Valid @RequestBody FacilitySearchRequest request
) throws Exception {
List<Facility> facilities = facilityService.search(
SearchResponse<Facility> searchResponse = facilityService.search(
request,
urlParams.getLimit(),
urlParams.getOffset(),
urlParams.getTenantId(),
urlParams.getLastChangedSince(),
urlParams.getIncludeDeleted());
FacilityBulkResponse response = FacilityBulkResponse.builder().responseInfo(ResponseInfoFactory
.createResponseInfo(request.getRequestInfo(), true)).facilities(facilities).build();
.createResponseInfo(request.getRequestInfo(), true))
.facilities(searchResponse.getResponse())
.totalCount(searchResponse.getTotalCount()).build();

return ResponseEntity.status(HttpStatus.OK).body(response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.egov.facility.validator;

import org.egov.common.models.Error;
import org.egov.common.models.core.SearchResponse;
import org.egov.common.models.facility.Facility;
import org.egov.common.models.facility.FacilityBulkRequest;
import org.egov.common.models.household.Household;
import org.egov.facility.helper.FacilityBulkRequestTestBuilder;
import org.egov.facility.helper.FacilityTestBuilder;
import org.egov.facility.repository.FacilityRepository;
Expand Down Expand Up @@ -37,7 +39,7 @@ class NonExistentEntityValidatorTest {
void shouldAddToErrorDetailsMapIfEntityNotFound() {
FacilityBulkRequest request = FacilityBulkRequestTestBuilder.builder().withFacilityId("some-id").withRequestInfo().build();
when(facilityRepository.findById(anyList(), anyString(), anyBoolean()))
.thenReturn(Collections.emptyList());
.thenReturn(SearchResponse.<Facility>builder().build());

Map<Facility, List<Error>> errorDetailsMap = fNonExistentValidator.validate(request);

Expand All @@ -49,7 +51,9 @@ void shouldAddToErrorDetailsMapIfEntityNotFound() {
void shouldNotAddToErrorDetailsMapIfEntityFound() {
FacilityBulkRequest request = FacilityBulkRequestTestBuilder.builder().withFacilityId("some-id").withRequestInfo().build();
when(facilityRepository.findById(anyList(), anyString(), anyBoolean()))
.thenReturn(Collections.singletonList(FacilityTestBuilder.builder().withFacility().withId("some-id").build()));
.thenReturn(SearchResponse.<Facility>builder().
response(Collections.singletonList(FacilityTestBuilder.builder().withFacility().withId("some-id").build()))
.build());

Map<Facility, List<Error>> errorDetailsMap = fNonExistentValidator.validate(request);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.egov.facility.validator;

import org.egov.common.models.Error;
import org.egov.common.models.core.SearchResponse;
import org.egov.common.models.facility.Facility;
import org.egov.common.models.facility.FacilityBulkRequest;
import org.egov.facility.helper.FacilityBulkRequestTestBuilder;
Expand Down Expand Up @@ -39,7 +40,9 @@ void shouldAddToErrorDetailsIfRowVersionMismatchFound() {
FacilityBulkRequest request = FacilityBulkRequestTestBuilder.builder().withFacilityId("some-id").withRequestInfo().build();
request.getFacilities().get(0).setRowVersion(2);
when(facilityRepository.findById(anyList(), anyString(), anyBoolean()))
.thenReturn(Collections.singletonList(FacilityTestBuilder.builder().withFacility().withId("some-id").build()));
.thenReturn(SearchResponse.<Facility>builder()
.response(Collections.singletonList(FacilityTestBuilder.builder().withFacility().withId("some-id").build()))
.build());

Map<Facility, List<Error>> errorDetailsMap = fRowVersionValidator.validate(request);

Expand All @@ -51,7 +54,8 @@ void shouldAddToErrorDetailsIfRowVersionMismatchFound() {
void shouldNotAddToErrorDetailsIfRowVersionSimilar() {
FacilityBulkRequest request = FacilityBulkRequestTestBuilder.builder().withFacilityId("some-id").withRequestInfo().build();
when(facilityRepository.findById(anyList(), anyString(), anyBoolean()))
.thenReturn(Collections.singletonList(FacilityTestBuilder.builder().withFacility().withId("some-id").build()));
.thenReturn(SearchResponse.<Facility>builder()
.response(Collections.singletonList(FacilityTestBuilder.builder().withFacility().withId("some-id").build())).build());

Map<Facility, List<Error>> errorDetailsMap = fRowVersionValidator.validate(request);

Expand Down
10 changes: 8 additions & 2 deletions health-services/libraries/health-services-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<artifactId>health-services-common</artifactId>
<packaging>jar</packaging>
<name>health-services-common</name>
<version>1.0.19-SNAPSHOT</version>
<version>1.0.20-dev-SNAPSHOT</version>
<description>Shared classes among services</description>

<parent>
Expand Down Expand Up @@ -115,7 +115,13 @@
<dependency>
<groupId>org.egov.common</groupId>
<artifactId>health-services-models</artifactId>
<version>1.0.22-SNAPSHOT</version>
<version>1.0.23-dev-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.egov</groupId>
<artifactId>mdms-client</artifactId>
<version>2.9.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.egov.common.data.query.builder.SelectQueryBuilder;
import org.egov.common.data.query.exception.QueryBuilderException;
import org.egov.common.models.core.SearchResponse;
import org.egov.common.producer.Producer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
Expand All @@ -23,6 +24,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.egov.common.utils.CommonUtils.constructTotalCountCTEAndReturnResult;
import static org.egov.common.utils.CommonUtils.getIdMethod;
import static org.egov.common.utils.CommonUtils.getMethod;
import static org.egov.common.utils.CommonUtils.getObjClass;
Expand Down Expand Up @@ -236,6 +238,52 @@ public void putInCache(List<T> objects, String key) {
cacheByKey(objects, key);
}

/**
* Finds entities based on search criteria, also returns the count of entities found.
*
* @param searchObject The object containing search criteria.
* @param limit The maximum number of entities to return.
* @param offset The offset for pagination.
* @param tenantId The tenant ID to filter entities.
* @param lastChangedSince The timestamp for last modified entities.
* @param includeDeleted Flag to include deleted entities in the search result.
* @return A list of entities found based on the search criteria with total count.
* @throws QueryBuilderException If an error occurs while building the query.
*/
public SearchResponse<T> findWithCount(Object searchObject,
Integer limit,
Integer offset,
String tenantId,
Long lastChangedSince,
Boolean includeDeleted) throws QueryBuilderException {
String query = selectQueryBuilder.build(searchObject, tableName);
query += " AND tenantId=:tenantId ";
if (query.contains(tableName + " AND")) {
query = query.replace(tableName + " AND", tableName + " WHERE");
}
if (Boolean.FALSE.equals(includeDeleted)) {
query += "AND isDeleted=:isDeleted ";
}
if (lastChangedSince != null) {
query += "AND lastModifiedTime>=:lastModifiedTime ";
}
query += "ORDER BY id ASC";
Map<String, Object> paramsMap = selectQueryBuilder.getParamsMap();
paramsMap.put("tenantId", tenantId);
paramsMap.put("isDeleted", includeDeleted);
paramsMap.put("lastModifiedTime", lastChangedSince);

Long totalCount = constructTotalCountCTEAndReturnResult(query, paramsMap, namedParameterJdbcTemplate);

query += " LIMIT :limit OFFSET :offset";
paramsMap.put("limit", limit);
paramsMap.put("offset", offset);

List<T> resultantList = namedParameterJdbcTemplate.query(query, paramsMap, rowMapper);

return SearchResponse.<T>builder().response(resultantList).totalCount(totalCount).build();
}

/**
* Finds entities based on search criteria.
*
Expand Down
2 changes: 1 addition & 1 deletion health-services/libraries/health-services-models/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.egov.common</groupId>
<artifactId>health-services-models</artifactId>
<version>1.0.22-SNAPSHOT</version>
<version>1.0.23-dev-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
Expand Down
Loading
Loading