Skip to content

Beneficiary down sync API pagination #6

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

Open
wants to merge 1 commit into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -32,10 +33,12 @@
import javax.persistence.NoResultException;
import javax.persistence.QueryTimeoutException;

import com.fasterxml.jackson.databind.SerializationFeature;
import com.iemr.common.identity.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -328,9 +331,9 @@ public class IdentityController {
JsonElement json = new JsonParser().parse(object);

SearchSyncDTO search = InputMapper.getInstance().gson().fromJson(json, SearchSyncDTO.class);
List<BeneficiariesDTO> list = svc.searchBeneficiaryByVillageIdAndLastModifyDate(search.getVillageID(), new Timestamp(search.getLastModifiedDate()));
Page<BeneficiariesDTO> beneficiariesDTOPage = svc.searchBeneficiaryByVillageIdAndLastModifyDate(search.getVillageID(), new Timestamp(search.getLastModifiedDate()), search.getPageNo(), search.getPageSize());

response = getSuccessResponseString(list, 200, "success", "getIdentityByVillageAndLastSyncTime");
response = getSuccessPaginatedResponseString(beneficiariesDTOPage, 200, "success", "getIdentityByVillageAndLastSyncTime");

logger.info("IdentityController.getBeneficiary - end");
} catch (Exception e) {
Expand Down Expand Up @@ -800,6 +803,21 @@ private String getSuccessResponseString(List<BeneficiariesDTO> list, Integer sta
return response.toString();
}

private String getSuccessPaginatedResponseString(Page<BeneficiariesDTO> beneficiariesDTOS, Integer statusCode, String statusMsg,
String methodName) throws JsonProcessingException {
ObjectMapper obj = new ObjectMapper();
obj.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
obj.setDateFormat(dateFormat);

String data = obj.writeValueAsString(beneficiariesDTOS);
logger.info("Total results data response size:" + (!beneficiariesDTOS.getContent().isEmpty() ? beneficiariesDTOS.getTotalElements() : "No Beneficiary Found"));
OutputResponse response = new OutputResponse.Builder().setDataJsonType("JsonObject.class")
.setStatusCode(statusCode).setStatusMessage(statusMsg)
.setDataObjectType(this.getClass().getSimpleName()).setMethodName(methodName).setData(data).build();
return response.toString();
}

/**
*
* @param map
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/iemr/common/identity/dto/SearchSyncDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ public class SearchSyncDTO {

private Long lastModifiedDate;
private List<Integer> villageID;
private Integer pageNo;
private Integer pageSize;

}
12 changes: 7 additions & 5 deletions src/main/java/com/iemr/common/identity/repo/BenMappingRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.sql.Timestamp;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
Expand Down Expand Up @@ -126,9 +128,9 @@ public List<Object[]> getBenMappingByBenDetailsIds(@Param("benDetailsIds") List<
public List<Object[]> getBenMappingByVanSerialNo(@Param("benMapIds") BigInteger benMapIds,
@Param("vanID") Integer vanID);

@Query(value = "select m from MBeneficiarymapping m where m.mBeneficiaryaddress.permVillageId IN :villageIDs and "
+ "(m.mBeneficiaryaddress.lastModDate > :lastModDate or m.mBeneficiarycontact.lastModDate > :lastModDate "
+ "or m.mBeneficiarydetail.lastModDate > :lastModDate ) order by m.benMapId Desc")
List<MBeneficiarymapping> findByBeneficiaryDetailsByVillageIDAndLastModifyDate(@Param("villageIDs") List<Integer> villageID, @Param("lastModDate") Timestamp lastModifiedDate);

@Query("SELECT m FROM MBeneficiarymapping m where m.mBeneficiaryaddress.permVillageId IN :villageIDs AND" +
" GREATEST(m.mBeneficiaryaddress.lastModDate, m.mBeneficiarycontact.lastModDate, m.mBeneficiarydetail.lastModDate) > :lastModDate " +
"ORDER BY GREATEST(m.mBeneficiaryaddress.lastModDate, m.mBeneficiarycontact.lastModDate, m.mBeneficiarydetail.lastModDate) ASC ")
Page<MBeneficiarymapping> findBeneficiaryByVillageIDAndLastModifiedDatePaginated(@Param("villageIDs") List<Integer> villageID,
@Param("lastModDate") Timestamp lastModifiedDate, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -507,23 +511,31 @@ public List<BeneficiariesDTO> searhBeneficiaryByFamilyId(String familyId)
return beneficiaryList;
}

public List<BeneficiariesDTO> searchBeneficiaryByVillageIdAndLastModifyDate(List<Integer> villageIDs, Timestamp lastModifiedDate) {
public Page<BeneficiariesDTO> searchBeneficiaryByVillageIdAndLastModifyDate(List<Integer> villageIDs, Timestamp lastModifiedDate, Integer pageNo , Integer pageSize) {

Pageable pageable;
Page<MBeneficiarymapping> beneficiarymappingPage;
List<BeneficiariesDTO> beneficiaryList = new ArrayList<BeneficiariesDTO>();
Page<BeneficiariesDTO> beneficiariesDTOPage = null;
pageable = new PageRequest(pageNo,pageSize);
try {
// find benmap ids
List<MBeneficiarymapping> benMappingsList = mappingRepo.findByBeneficiaryDetailsByVillageIDAndLastModifyDate(villageIDs, lastModifiedDate);
if (benMappingsList != null && !benMappingsList.isEmpty()){
beneficiarymappingPage = mappingRepo.findBeneficiaryByVillageIDAndLastModifiedDatePaginated(villageIDs, lastModifiedDate,pageable);

for (MBeneficiarymapping benMapOBJ : benMappingsList) {
long totalElements = beneficiarymappingPage.getTotalElements();

if (!beneficiarymappingPage.getContent().isEmpty()){

for (MBeneficiarymapping benMapOBJ : beneficiarymappingPage.getContent()) {
beneficiaryList.add(this.getBeneficiariesDTO(benMapOBJ));
}
}
beneficiariesDTOPage = new PageImpl<>(beneficiaryList,pageable,totalElements);
} catch (Exception e) {
logger.error(
"error in beneficiary search to sync to CHO App with villageIDs: " + villageIDs + " error : " + e.getLocalizedMessage());
}
return beneficiaryList;
return beneficiariesDTOPage;
}

public List<BeneficiariesDTO> searhBeneficiaryByGovIdentity(String identity)
Expand Down