diff --git a/health-services/libraries/health-services-models/pom.xml b/health-services/libraries/health-services-models/pom.xml index 0e75c7fbe97..76d5af42d3c 100644 --- a/health-services/libraries/health-services-models/pom.xml +++ b/health-services/libraries/health-services-models/pom.xml @@ -84,4 +84,4 @@ https://nexus-repo.digit.org/nexus/content/repositories/snapshots/ - + \ No newline at end of file diff --git a/health-services/referralmanagement/pom.xml b/health-services/referralmanagement/pom.xml index 4bf32a9765a..9fedd00ee74 100644 --- a/health-services/referralmanagement/pom.xml +++ b/health-services/referralmanagement/pom.xml @@ -51,7 +51,7 @@ org.egov.common health-services-models - 1.0.23-dev-SNAPSHOT + 1.0.25-dev-SNAPSHOT compile @@ -132,4 +132,4 @@ https://nexus-repo.digit.org/nexus/content/repositories/snapshots/ - \ No newline at end of file + diff --git a/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/repository/HouseholdRepository.java b/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/repository/HouseholdRepository.java new file mode 100644 index 00000000000..082fe188765 --- /dev/null +++ b/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/repository/HouseholdRepository.java @@ -0,0 +1,49 @@ +package org.egov.referralmanagement.repository; + +import org.egov.common.ds.Tuple; +import org.egov.common.models.household.Household; +import org.egov.referralmanagement.repository.rowmapper.HouseholdRowMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Repository; +import org.springframework.util.StringUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Repository +public class HouseholdRepository { + + @Autowired + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + @Autowired + private HouseholdRowMapper householdRowMapper; + + + public Tuple> findByViewCLF(String localityCode, Integer limit, Integer offset, String tenantId, Long lastModifiedTime, String householdId) { + String query = null; + Map paramsMap = new HashMap<>(); + Long totalCount = null; + + query = "select * from household_address_clf_mv where localitycode=:localitycode "; + if (StringUtils.hasLength(householdId)) { + query = query + " and id=:id"; + paramsMap.put("id", householdId); + } else { + paramsMap.put("start", offset); + paramsMap.put("end", offset+limit); + query = query + " and rank between :start and :end "; + + Map paramsMapCount = new HashMap<>(); + paramsMapCount.put("localitycode", localityCode); + paramsMapCount.put("lastModifiedTime", lastModifiedTime); + Integer maxRank = namedParameterJdbcTemplate.queryForObject("select max(rank) from household_address_clf_mv where localitycode=:localitycode and lastModifiedTime>=:lastModifiedTime", paramsMapCount, Integer.class); + totalCount = maxRank == null ? 0L : Long.valueOf(maxRank); + } + paramsMap.put("localitycode", localityCode); + return new Tuple<>(totalCount, this.namedParameterJdbcTemplate.query(query, paramsMap, householdRowMapper)); + + } +} diff --git a/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/repository/rowmapper/HouseholdRowMapper.java b/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/repository/rowmapper/HouseholdRowMapper.java new file mode 100644 index 00000000000..83efd0ed86d --- /dev/null +++ b/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/repository/rowmapper/HouseholdRowMapper.java @@ -0,0 +1,76 @@ +package org.egov.referralmanagement.repository.rowmapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.egov.common.contract.models.AuditDetails; +import org.egov.common.models.core.AdditionalFields; +import org.egov.common.models.household.Address; +import org.egov.common.models.household.AddressType; +import org.egov.common.models.core.Boundary; +import org.egov.common.models.household.Household; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Component; + +@Component +public class HouseholdRowMapper implements RowMapper { + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public Household mapRow(ResultSet resultSet, int i) throws SQLException { + try { + AuditDetails auditDetails = AuditDetails.builder() + .createdBy(resultSet.getString("createdBy")) + .createdTime(resultSet.getLong("createdTime")) + .lastModifiedBy(resultSet.getString("lastModifiedBy")) + .lastModifiedTime(resultSet.getLong("lastModifiedTime")) + .build(); + AuditDetails clientAuditDetails = AuditDetails.builder() + .createdTime(resultSet.getLong("clientCreatedTime")) + .createdBy(resultSet.getString("clientCreatedBy")) + .lastModifiedTime(resultSet.getLong("clientLastModifiedTime")) + .lastModifiedBy(resultSet.getString("clientLastModifiedBy")) + .build(); + Household household = Household.builder() + .id(resultSet.getString("id")) + .rowVersion(resultSet.getInt("rowVersion")) + .isDeleted(resultSet.getBoolean("isDeleted")) + .tenantId(resultSet.getString("tenantId")) + .memberCount(resultSet.getInt("numberOfMembers")) + .clientReferenceId(resultSet.getString("clientReferenceId")) + .auditDetails(auditDetails) + .clientAuditDetails(clientAuditDetails) + .additionalFields(resultSet.getString("additionalDetails") == null ? null : objectMapper.readValue(resultSet + .getString("additionalDetails"), AdditionalFields.class)) + .address(Address.builder() + .id(resultSet.getString("aid")) + .clientReferenceId(resultSet.getString("aclientreferenceid")) + .tenantId(resultSet.getString("atenantid")) + .doorNo(resultSet.getString("doorNo")) + .latitude(resultSet.getDouble("latitude")) + .longitude(resultSet.getDouble("longitude")) + .locationAccuracy(resultSet.getDouble("locationAccuracy")) + .type(AddressType.fromValue(resultSet.getString("type"))) + .addressLine1(resultSet.getString("addressLine1")) + .addressLine2(resultSet.getString("addressLine2")) + .landmark(resultSet.getString("landmark")) + .city(resultSet.getString("city")) + .pincode(resultSet.getString("pinCode")) + .buildingName(resultSet.getString("buildingName")) + .street(resultSet.getString("street")) + .locality(resultSet.getString("localityCode") != null ? + Boundary.builder().code(resultSet.getString("localityCode")).build() : null) + .build()) + .build(); + if (household.getAddress().getId() == null) { + household.setAddress(null); + } + return household; + } catch (JsonProcessingException e) { + throw new SQLException(e); + } + + } +} diff --git a/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/service/DownsyncService.java b/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/service/DownsyncService.java index 7db34924dd4..b4fa32b3780 100644 --- a/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/service/DownsyncService.java +++ b/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/service/DownsyncService.java @@ -10,7 +10,9 @@ import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.tuple.Pair; import org.egov.common.contract.request.RequestInfo; +import org.egov.common.ds.Tuple; import org.egov.common.http.client.ServiceRequestClient; import org.egov.common.models.household.Household; import org.egov.common.models.household.HouseholdBulkResponse; @@ -35,17 +37,17 @@ import org.egov.common.models.referralmanagement.Referral; import org.egov.common.models.referralmanagement.ReferralSearch; import org.egov.common.models.referralmanagement.ReferralSearchRequest; -import org.egov.common.models.referralmanagement.beneficiarydownsync.Downsync; -import org.egov.common.models.referralmanagement.beneficiarydownsync.DownsyncCriteria; -import org.egov.common.models.referralmanagement.beneficiarydownsync.DownsyncRequest; +import org.egov.common.models.referralmanagement.beneficiarydownsync.*; import org.egov.common.models.referralmanagement.sideeffect.SideEffect; import org.egov.common.models.referralmanagement.sideeffect.SideEffectSearch; import org.egov.common.models.referralmanagement.sideeffect.SideEffectSearchRequest; import org.egov.referralmanagement.config.ReferralManagementConfiguration; +import org.egov.referralmanagement.repository.HouseholdRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; @Service @Slf4j @@ -62,14 +64,17 @@ public class DownsyncService { private ReferralManagementService referralService; private MasterDataService masterDataService; + private HouseholdRepository householdRepository; + + private static final Integer SEARCH_MAX_COUNT = 1000; @Autowired - public DownsyncService( ServiceRequestClient serviceRequestClient, - ReferralManagementConfiguration referralManagementConfiguration, - NamedParameterJdbcTemplate jdbcTemplate, - SideEffectService sideEffectService, - ReferralManagementService referralService, - MasterDataService masterDataService ) { + public DownsyncService(ServiceRequestClient serviceRequestClient, + ReferralManagementConfiguration referralManagementConfiguration, + NamedParameterJdbcTemplate jdbcTemplate, + SideEffectService sideEffectService, + ReferralManagementService referralService, + MasterDataService masterDataService, HouseholdRepository householdRepository) { this.restClient = serviceRequestClient; this.configs = referralManagementConfiguration; @@ -77,7 +82,7 @@ public DownsyncService( ServiceRequestClient serviceRequestClient, this.sideEffectService=sideEffectService; this.referralService=referralService; this.masterDataService=masterDataService; - + this.householdRepository = householdRepository; } /** @@ -85,7 +90,7 @@ public DownsyncService( ServiceRequestClient serviceRequestClient, * @param downsyncRequest * @return Downsync */ - public Downsync prepareDownsyncData(DownsyncRequest downsyncRequest) { + public Downsync prepareDownsyncData(DownsyncRequest downsyncRequest, boolean isCLF) { Downsync downsync = new Downsync(); DownsyncCriteria downsyncCriteria = downsyncRequest.getDownsyncCriteria(); @@ -104,7 +109,19 @@ public Downsync prepareDownsyncData(DownsyncRequest downsyncRequest) { LinkedHashMap projectType = masterDataService.getProjectType(downsyncRequest); /* search household */ - householdIds = searchHouseholds(downsyncRequest, downsync); + List households = null; + if (isCLF) { + households = searchHouseholdsCLF(downsyncRequest); + }else { + householdIds = searchHouseholds(downsyncRequest, downsync); + } + if (CollectionUtils.isEmpty(householdIds)) { + householdIds = (households == null) + ? Collections.emptyList() + : households.stream() + .map(Household::getId) + .collect(Collectors.toList()); + } /* search household member using household ids */ if (isSyncTimeAvailable || !CollectionUtils.isEmpty(householdIds)) { @@ -122,8 +139,13 @@ public Downsync prepareDownsyncData(DownsyncRequest downsyncRequest) { beneficiaryClientRefIds = individualClientRefIds; - if("HOUSEHOLD".equalsIgnoreCase(beneficiaryType)) - beneficiaryClientRefIds = downsync.getHouseholds().stream().map(Household::getClientReferenceId).collect(Collectors.toList()); + if("HOUSEHOLD".equalsIgnoreCase(beneficiaryType)) { + if (households == null) + beneficiaryClientRefIds = downsync.getHouseholds().stream().map(Household::getClientReferenceId).collect(Collectors.toList()); + else + beneficiaryClientRefIds = households.stream().map(Household::getClientReferenceId).collect(Collectors.toList()); + } + //fetch beneficiary in the db if (isSyncTimeAvailable || !CollectionUtils.isEmpty(beneficiaryClientRefIds)) { @@ -147,6 +169,28 @@ public Downsync prepareDownsyncData(DownsyncRequest downsyncRequest) { return downsync; } + public DownsyncCLFHousehold prepareDownsyncCLFDataHousehold(DownsyncRequest downsyncRequest) { + DownsyncCLFHousehold downsyncCLFHousehold = new DownsyncCLFHousehold(); + downsyncCLFHousehold.setDownsyncCriteria(downsyncRequest.getDownsyncCriteria()); + + long startTime = System.currentTimeMillis(); + log.info("The household search start time : " + startTime); + List householdList = searchHouseholdsCLF(downsyncRequest); + + Map householdIdMemberCountMap = gethouseholdMemberCountMap(householdList.stream() + .map(Household::getId).collect(Collectors.toList())); + List householdMemberCountMap = new ArrayList<>(); + + for (Household household : householdList) { + Integer memberCount = householdIdMemberCountMap.get(household.getId()) == null ? 0 : householdIdMemberCountMap.get(household.getId()); + HouseholdMemberMap householdMemberMap = HouseholdMemberMap.builder().household(household).numberOfMembers(memberCount).build(); + householdMemberCountMap.add(householdMemberMap); + } + + downsyncCLFHousehold.setHouseholdMemberCountMap(householdMemberCountMap); + log.info("The search time : " + (System.currentTimeMillis() - startTime)/1000); + return downsyncCLFHousehold; + } /** * @@ -167,6 +211,9 @@ private List searchHouseholds(DownsyncRequest downsyncRequest, Downsync .localityCode(criteria.getLocality()) .build(); + if (StringUtils.hasLength(criteria.getHouseholdId())) { + householdSearch.setId(Collections.singletonList(criteria.getHouseholdId())); + } HouseholdSearchRequest searchRequest = HouseholdSearchRequest.builder() .household(householdSearch) .requestInfo(requestInfo) @@ -183,6 +230,21 @@ private List searchHouseholds(DownsyncRequest downsyncRequest, Downsync return households.stream().map(Household::getId).collect(Collectors.toList()); } + private List searchHouseholdsCLF(DownsyncRequest downsyncRequest) { + + DownsyncCriteria criteria = downsyncRequest.getDownsyncCriteria(); + + //HouseholdBulkResponse res = restClient.fetchResult(householdUrl, searchRequest, HouseholdBulkResponse.class); + Tuple> res = null; + + res = householdRepository.findByViewCLF(criteria.getLocality(), criteria.getLimit(), criteria.getOffset(), null, criteria.getLastSyncedTime() != null ? criteria.getLastSyncedTime() : 0L, criteria.getHouseholdId()); + + downsyncRequest.getDownsyncCriteria().setTotalCount(res.getX()); + + List households = res.getY(); + return households; + } + /** * * @param downsyncRequest @@ -196,23 +258,28 @@ private List searchIndividuals(DownsyncRequest downsyncRequest, Downsync DownsyncCriteria criteria = downsyncRequest.getDownsyncCriteria(); RequestInfo requestInfo = downsyncRequest.getRequestInfo(); - StringBuilder url = new StringBuilder(configs.getIndividualHost()) - .append(configs.getIndividualSearchUrl()); + List individuals = new ArrayList<>(); + List> subLists = splitList(individualIds.stream().toList(), SEARCH_MAX_COUNT); - url = appendUrlParams(url, criteria, 0, individualIds.size(),true); + for (List list : subLists) { + StringBuilder url = new StringBuilder(configs.getIndividualHost()) + .append(configs.getIndividualSearchUrl()); + url = appendUrlParams(url, criteria, 0, list.size(), true); - IndividualSearch individualSearch = IndividualSearch.builder() - .build(); + IndividualSearch individualSearch = IndividualSearch.builder() + .build(); - if(!CollectionUtils.isEmpty(individualIds)) - individualSearch.setId(new ArrayList<>(individualIds)); + if(!CollectionUtils.isEmpty(individualIds)) + individualSearch.setId(new ArrayList<>(individualIds)); - IndividualSearchRequest searchRequest = IndividualSearchRequest.builder() - .individual(individualSearch) - .requestInfo(requestInfo) - .build(); + IndividualSearchRequest searchRequest = IndividualSearchRequest.builder() + .individual(individualSearch) + .requestInfo(requestInfo) + .build(); - List individuals = restClient.fetchResult(url, searchRequest, IndividualBulkResponse.class).getIndividual(); + IndividualBulkResponse res = restClient.fetchResult(url, searchRequest, IndividualBulkResponse.class); + individuals.addAll(res.getIndividual()); + } downsync.setIndividuals(individuals); return individuals.stream().map(Individual::getClientReferenceId).collect(Collectors.toList()); @@ -229,26 +296,30 @@ private Set searchMembers(DownsyncRequest downsyncRequest, Downsync down Long lastChangedSince = downsyncRequest.getDownsyncCriteria().getLastSyncedTime(); - List memberids = getPrimaryIds(householdIds, "householdId","HOUSEHOLD_MEMBER",lastChangedSince); + List memberids = getPrimaryIds(householdIds, "householdId","HOUSEHOLD_MEMBER", + lastChangedSince, downsyncRequest.getDownsyncCriteria().getLimit(), downsyncRequest.getDownsyncCriteria().getOffset()); if (CollectionUtils.isEmpty(memberids)) return Collections.emptySet(); - StringBuilder memberUrl = new StringBuilder(configs.getHouseholdHost()) - .append(configs.getHouseholdMemberSearchUrl()); - - appendUrlParams(memberUrl, downsyncRequest.getDownsyncCriteria(), 0, householdIds.size(), false); - - HouseholdMemberSearch memberSearch = HouseholdMemberSearch.builder() - .id(memberids) - .build(); - - HouseholdMemberSearchRequest searchRequest = HouseholdMemberSearchRequest.builder() - .householdMemberSearch(memberSearch) - .requestInfo(downsyncRequest.getRequestInfo()) - .build(); - - List members = restClient.fetchResult(memberUrl, searchRequest, HouseholdMemberBulkResponse.class).getHouseholdMembers(); + List> subLists = splitList(memberids, SEARCH_MAX_COUNT); + List members = new ArrayList<>(); + for (List list : subLists) { + StringBuilder memberUrl = new StringBuilder(configs.getHouseholdHost()) + .append(configs.getHouseholdMemberSearchUrl()); + appendUrlParams(memberUrl, downsyncRequest.getDownsyncCriteria(), 0, list.size(), false); + HouseholdMemberSearch memberSearch = HouseholdMemberSearch.builder() + .id(list) + .build(); + + HouseholdMemberSearchRequest searchRequest = HouseholdMemberSearchRequest.builder() + .householdMemberSearch(memberSearch) + .requestInfo(downsyncRequest.getRequestInfo()) + .build(); + + List membersSublist = restClient.fetchResult(memberUrl, searchRequest, HouseholdMemberBulkResponse.class).getHouseholdMembers(); + members.addAll(membersSublist); + } downsync.setHouseholdMembers(members); return members.stream().map(HouseholdMember::getIndividualId).collect(Collectors.toSet()); @@ -272,28 +343,33 @@ private List searchBeneficiaries(DownsyncRequest downsyncRequest, Downsy beneficiaryClientRefIds, "beneficiaryclientreferenceid", "PROJECT_BENEFICIARY", - lastChangedSince + lastChangedSince, null, null ); if(CollectionUtils.isEmpty(beneficiaryIds)) return Collections.emptyList(); - StringBuilder url = new StringBuilder(configs.getProjectHost()) - .append(configs.getProjectBeneficiarySearchUrl()); + List> subLists = splitList(beneficiaryIds, SEARCH_MAX_COUNT); + List beneficiaries = new ArrayList<>(); - url = appendUrlParams(url, criteria, 0, beneficiaryClientRefIds.size(),false); + for (List list : subLists) { + StringBuilder url = new StringBuilder(configs.getProjectHost()) + .append(configs.getProjectBeneficiarySearchUrl()); + url = appendUrlParams(url, criteria, 0, list.size(), false); - ProjectBeneficiarySearch search = ProjectBeneficiarySearch.builder() - .id(beneficiaryIds) - .projectId(Collections.singletonList(downsyncRequest.getDownsyncCriteria().getProjectId())) - .build(); + ProjectBeneficiarySearch search = ProjectBeneficiarySearch.builder() + .id(list) + .projectId(Collections.singletonList(downsyncRequest.getDownsyncCriteria().getProjectId())) + .build(); - BeneficiarySearchRequest searchRequest = BeneficiarySearchRequest.builder() - .projectBeneficiary(search) - .requestInfo(requestInfo) - .build(); + BeneficiarySearchRequest searchRequest = BeneficiarySearchRequest.builder() + .projectBeneficiary(search) + .requestInfo(requestInfo) + .build(); - List beneficiaries = restClient.fetchResult(url, searchRequest, BeneficiaryBulkResponse.class).getProjectBeneficiaries(); + List beneficiariesSubList = restClient.fetchResult(url, searchRequest, BeneficiaryBulkResponse.class).getProjectBeneficiaries(); + beneficiaries.addAll(beneficiariesSubList); + } downsync.setProjectBeneficiaries(beneficiaries); return beneficiaries.stream().map(ProjectBeneficiary::getClientReferenceId).collect(Collectors.toList()); @@ -315,27 +391,32 @@ private List searchTasks(DownsyncRequest downsyncRequest, Downsync downs DownsyncCriteria criteria = downsyncRequest.getDownsyncCriteria(); RequestInfo requestInfo = downsyncRequest.getRequestInfo(); List taskIds = getPrimaryIds(beneficiaryClientRefIds, "projectBeneficiaryClientReferenceId", "PROJECT_TASK", - criteria.getLastSyncedTime()); + criteria.getLastSyncedTime(), null, null); if(CollectionUtils.isEmpty(taskIds)) return Collections.emptyList(); - StringBuilder url = new StringBuilder(configs.getProjectHost()) - .append(configs.getProjectTaskSearchUrl()); + List> subLists = splitList(taskIds, SEARCH_MAX_COUNT); + List tasks = new ArrayList<>(); - url = appendUrlParams(url, criteria, 0, taskIds.size(), false); + for (List list : subLists) { + StringBuilder url = new StringBuilder(configs.getProjectHost()) + .append(configs.getProjectTaskSearchUrl()); - TaskSearch search = TaskSearch.builder() - .id(taskIds) - .projectId(Collections.singletonList(downsyncRequest.getDownsyncCriteria().getProjectId())) - .build(); + url = appendUrlParams(url, criteria, 0, list.size(), false); - TaskSearchRequest searchRequest = TaskSearchRequest.builder() - .task(search) - .requestInfo(requestInfo) - .build(); + TaskSearch search = TaskSearch.builder() + .id(list) + .projectId(Collections.singletonList(downsyncRequest.getDownsyncCriteria().getProjectId())) + .build(); - List tasks = restClient.fetchResult(url, searchRequest, TaskBulkResponse.class).getTasks(); + TaskSearchRequest searchRequest = TaskSearchRequest.builder() + .task(search) + .requestInfo(requestInfo) + .build(); + List tasksSubList = restClient.fetchResult(url, searchRequest, TaskBulkResponse.class).getTasks(); + tasks.addAll(tasksSubList); + } downsync.setTasks(tasks); return tasks.stream().map(Task::getClientReferenceId).collect(Collectors.toList()); @@ -354,7 +435,8 @@ private void searchSideEffect(DownsyncRequest downsyncRequest, Downsync downsync RequestInfo requestInfo = downsyncRequest.getRequestInfo(); /* FIXME SHOULD BE REMOVED AND TASK SEARCH SHOULD BE enhanced with list of client-ref-beneficiary ids*/ - List SEIds = getPrimaryIds(taskClientRefIds, "taskClientReferenceId", "SIDE_EFFECT", criteria.getLastSyncedTime()); + List SEIds = getPrimaryIds(taskClientRefIds, "taskClientReferenceId", + "SIDE_EFFECT", criteria.getLastSyncedTime(), null, null); if(CollectionUtils.isEmpty(SEIds)) return; @@ -412,6 +494,27 @@ private void referralSearch(DownsyncRequest downsyncRequest, Downsync downsync, downsync.setReferrals(referrals); } + private Map gethouseholdMemberCountMap(List idList) { + + Map memberCountMap = new HashMap<>(); + + if (!CollectionUtils.isEmpty(idList)) { + StringBuilder memberIdsquery = new StringBuilder("SELECT householdId, COUNT(*) AS memberCount " + + "FROM HOUSEHOLD_MEMBER WHERE householdId IN (:householdId) GROUP BY householdId"); + + Map paramMap = new HashMap<>(); + paramMap.put("householdId", idList); + List> memberCountList = jdbcTemplate.queryForList(memberIdsquery.toString(), paramMap); + return memberCountList.stream() + .collect(Collectors.toMap( + row -> (String) row.get("householdId"), + row -> ((Number) row.get("memberCount")).intValue() + )); + + } + return memberCountMap; + } + /** * common method to fetch Ids with list of relation Ids like id of member with householdIds @@ -421,7 +524,8 @@ private void referralSearch(DownsyncRequest downsyncRequest, Downsync downsync, * @param lastChangedSince * @return */ - private List getPrimaryIds(List idList, String idListFieldName, String tableName, Long lastChangedSince) { + private List getPrimaryIds(List idList, String idListFieldName, String tableName, + Long lastChangedSince, Integer limit, Integer offset) { /** * Adding lastShangedSince to id query to avoid load on API search for members @@ -445,6 +549,12 @@ private List getPrimaryIds(List idList, String idListFieldName, paramMap.put("lastChangedSince", lastChangedSince); } + if (tableName.equalsIgnoreCase("HOUSEHOLD_MEMBER")) { + memberIdsquery.append(" ORDER BY lastModifiedTime ASC LIMIT :limit OFFSET :offset"); + paramMap.put("limit", limit); + paramMap.put("offset", offset); + } + String finalQuery = String.format(memberIdsquery.toString(), tableName, idListFieldName, idListFieldName); /* FIXME SHOULD BE REMOVED AND SEARCH SHOULD BE enhanced with list of household ids*/ List memberids = jdbcTemplate.queryForList(finalQuery, paramMap, String.class); @@ -486,4 +596,12 @@ private StringBuilder appendUrlParams(StringBuilder url, DownsyncCriteria criter return url; } + + private List> splitList(List list, int size) { + List> subLists = new ArrayList<>(); + for (int i = 0; i < list.size(); i += size) { + subLists.add(list.subList(i, Math.min(i + size, list.size()))); + } + return subLists; + } } diff --git a/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/web/controllers/BeneficiaryDownsyncController.java b/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/web/controllers/BeneficiaryDownsyncController.java index d780ef1cce0..dfcb22d8203 100644 --- a/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/web/controllers/BeneficiaryDownsyncController.java +++ b/health-services/referralmanagement/src/main/java/org/egov/referralmanagement/web/controllers/BeneficiaryDownsyncController.java @@ -4,9 +4,7 @@ import io.swagger.annotations.ApiParam; import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; -import org.egov.common.models.referralmanagement.beneficiarydownsync.Downsync; -import org.egov.common.models.referralmanagement.beneficiarydownsync.DownsyncRequest; -import org.egov.common.models.referralmanagement.beneficiarydownsync.DownsyncResponse; +import org.egov.common.models.referralmanagement.beneficiarydownsync.*; import org.egov.common.utils.ResponseInfoFactory; import org.egov.referralmanagement.service.DownsyncService; import org.springframework.beans.factory.annotation.Autowired; @@ -42,7 +40,7 @@ public ResponseEntity getBeneficaryData (@ApiParam(value = "Ca Downsync.builder(). downsyncCriteria(request.getDownsyncCriteria()) .build(); - Downsync downsync = downsyncService.prepareDownsyncData(request); + Downsync downsync = downsyncService.prepareDownsyncData(request, false); DownsyncResponse response = DownsyncResponse.builder() .downsync(downsync) .responseInfo(ResponseInfoFactory @@ -51,4 +49,29 @@ public ResponseEntity getBeneficaryData (@ApiParam(value = "Ca return ResponseEntity.status(HttpStatus.ACCEPTED).body(response); } + + @PostMapping(value = "/v1/_get/CLF/household") + public ResponseEntity getBeneficaryDataCFLHousehold (@ApiParam(value = "Capture details of Side Effect", required = true) @Valid @RequestBody DownsyncRequest request) { + // API response only contains household data and number of members present in that household + log.info("UserUUID: {}", request.getRequestInfo().getUserInfo().getUuid()); + log.info("Downsync RequestBody: {}", mapper.valueToTree(request).toString()); + DownsyncCLFHousehold downsyncCLFHousehold = downsyncService.prepareDownsyncCLFDataHousehold(request); + DownsyncCLFHouseholdResponse response = DownsyncCLFHouseholdResponse.builder() + .Households(downsyncCLFHousehold).responseInfo(ResponseInfoFactory.createResponseInfo(request.getRequestInfo(), true)).build(); + return ResponseEntity.status(HttpStatus.ACCEPTED).body(response); + } + + @PostMapping(value = "/v1/_get/CLF/memberData") + public ResponseEntity getBeneficaryDataCFLMember (@ApiParam(value = "Capture details of Side Effect", required = true) @Valid @RequestBody DownsyncRequest request) { + log.info("UserUUID: {}", request.getRequestInfo().getUserInfo().getUuid()); + log.info("Downsync RequestBody: {}", mapper.valueToTree(request).toString()); + Downsync downsyncCLFMember = downsyncService.prepareDownsyncData(request, true); + DownsyncResponse response = DownsyncResponse.builder() + .downsync(downsyncCLFMember).responseInfo(ResponseInfoFactory.createResponseInfo(request.getRequestInfo(), true)).build(); + return ResponseEntity.status(HttpStatus.ACCEPTED).body(response); + } + + } + +