Skip to content
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 @@ -25,6 +25,8 @@
import java.util.ArrayList;
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 @@ -453,7 +455,7 @@ public int updateLabTechnicianFlag(@Param("lab_technician_flag") Short lab_techn
public ArrayList<BeneficiaryFlowStatus> getVisitByLocationAndLastModifDate(@Param("villageID") Integer villageID, @Param("lastModDate") Timestamp lastModDate);

//get ben flow status records based on villageId and last sync date to sync to app local dB
@Query("SELECT t from BeneficiaryFlowStatus t WHERE t.villageID IN :villageIDs AND t.modified_date > :lastModDate ")
ArrayList<BeneficiaryFlowStatus> getFlowRecordsToSync(@Param("villageIDs") List<Integer> villageID, @Param("lastModDate") Timestamp lastModDate);
@Query("SELECT t from BeneficiaryFlowStatus t WHERE t.villageID IN :villageIDs AND t.modified_date > :lastModDate ORDER BY t.modified_date ASC ")
Page<BeneficiaryFlowStatus> getPaginatedFlowRecordsToSync(@Param("villageIDs") List<Integer> villageID, @Param("lastModDate") Timestamp lastModDate, Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.iemr.hwc.service.choApp;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.gson.*;
import com.iemr.hwc.data.benFlowStatus.BeneficiaryFlowStatus;
import com.iemr.hwc.data.choApp.UserActivityLogs;
Expand All @@ -21,7 +23,6 @@
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand All @@ -30,6 +31,9 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
Expand All @@ -40,6 +44,7 @@
import org.springframework.web.client.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
Expand Down Expand Up @@ -203,7 +208,8 @@ public ResponseEntity<String> getBeneficiaryByVillageIDAndLastModifiedDate(SyncS
try {

if(villageIDAndLastSyncDate.getVillageID() !=null && !villageIDAndLastSyncDate.getVillageID().isEmpty()
&& villageIDAndLastSyncDate.getLastSyncDate() != null) {
&& villageIDAndLastSyncDate.getLastSyncDate() != null && villageIDAndLastSyncDate.getPageNo() != null
&& villageIDAndLastSyncDate.getPageSize() != null) {

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(villageIDAndLastSyncDate.getLastSyncDate());
Expand All @@ -217,14 +223,14 @@ public ResponseEntity<String> getBeneficiaryByVillageIDAndLastModifiedDate(SyncS

if (response.hasBody()) {

JSONObject responseJSON = new JSONObject(response.getBody());
JSONArray jsonArray = new JSONArray(responseJSON.getJSONObject("response").getString("data"));
JSONObject responseBodyJSON = new JSONObject(response.getBody());
JSONObject resultJSON = new JSONObject(responseBodyJSON.getJSONObject("response").getString("data"));

outputResponse.setResponse(jsonArray.toString());
outputResponse.setResponse(resultJSON.toString());
}
}else{
logger.error("Unable to search beneficiaries to sync based on villageIDs and lastSyncDate. Incomplete request body - Either villageIDs or lastSyncDate missing.");
outputResponse.setError(400,"Bad request. Incomplete request body - Either villageIDs or lastSyncDate missing.");
logger.error("Unable to search beneficiaries to sync based on villageIDs and lastSyncDate. Incomplete request body - Any of villageIDs, lastSyncDate, pageNo or pageSize missing.");
outputResponse.setError(400,"Bad request. Incomplete request body - Any of villageIDs, lastSyncDate, pageNo or pageSize missing.");
statusCode = HttpStatus.BAD_REQUEST;
}
}
Expand Down Expand Up @@ -257,23 +263,34 @@ public ResponseEntity<String> getFlowRecordsByVillageIDAndLastModifiedDate(SyncS

HttpStatus statusCode = HttpStatus.OK;
OutputResponse outputResponse = new OutputResponse();
ArrayList<BeneficiaryFlowStatus> benFlowList;

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);

Pageable pageable;
Page<BeneficiaryFlowStatus> paginatedFlowRecords;

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");

try {
if (villageIDAndLastSyncDate.getVillageID() !=null && !villageIDAndLastSyncDate.getVillageID().isEmpty()
&& villageIDAndLastSyncDate.getLastSyncDate() != null) {
&& villageIDAndLastSyncDate.getLastSyncDate() != null && villageIDAndLastSyncDate.getPageNo() != null
&& villageIDAndLastSyncDate.getPageSize() != null) {

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(villageIDAndLastSyncDate.getLastSyncDate());

benFlowList = beneficiaryFlowStatusRepo.getFlowRecordsToSync(villageIDAndLastSyncDate.getVillageID(),
new Timestamp(dt.toDate().getTime()));
outputResponse.setResponse(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create().toJson(benFlowList));
pageable = new PageRequest(villageIDAndLastSyncDate.getPageNo(), villageIDAndLastSyncDate.getPageSize());
paginatedFlowRecords = beneficiaryFlowStatusRepo.getPaginatedFlowRecordsToSync(villageIDAndLastSyncDate.getVillageID(),
new Timestamp(dt.toDate().getTime()),pageable);

outputResponse.setResponse(obj.writeValueAsString(paginatedFlowRecords));
}else{
logger.error("Unable to search beneficiaries to sync based on villageIDs and lastSyncDate. Incomplete request body - Either villageIDs or lastSyncDate missing.");
outputResponse.setError(400,"Bad request. Incomplete request body - Either villageIDs or lastSyncDate missing.");
logger.error("Unable to search beneficiaries to sync based on villageIDs and lastSyncDate. Incomplete request body - Any of villageIDs, lastSyncDate, pageNo or pageSize missing.");
outputResponse.setError(400,"Bad request. Incomplete request body - Any of villageIDs, lastSyncDate, pageNo or pageSize missing.");
statusCode = HttpStatus.BAD_REQUEST;
}
} catch (IllegalArgumentException e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public class SyncSearchRequest {
private String lastSyncDate;
private Long lastModifiedDate;
private List<Integer> villageID;
private Integer pageNo;
private Integer pageSize;
}