Skip to content

Commit

Permalink
hdss sync count
Browse files Browse the repository at this point in the history
  • Loading branch information
trevlenb2 committed Nov 6, 2024
1 parent 68976ca commit 9bd9cd5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,70 +51,54 @@ public ResponseEntity<HdssCompoundObj> taskSync(@RequestBody HdssSyncRequest hds

long serverVersion = hdssSyncRequest.getServerVersion();

List<HdssCompoundHouseholdIndividualProjection> individuals = compoundsRepository.getAllCompoundsForUserAssignmentAndServerVersionAndBatchSize(
int count = compoundsRepository.getTotalCountOfCompoundsForUserAssignmentAndServerVersionAndBatchSize(
hdssSyncRequest.getUserId());

List<HdssCompoundHouseholdIndividualProjection> individuals = compoundsRepository
.getAllCompoundsForUserAssignmentAndServerVersionAndBatchSize(
hdssSyncRequest.getUserId(), serverVersion, hdssSyncRequest.getBatchSize());

if (!individuals.isEmpty()) {
Optional<Long> maxServerVersion = individuals.stream()
.map(HdssCompoundHouseholdIndividualProjection::getServerVersion)
.reduce(Long::max);
.map(HdssCompoundHouseholdIndividualProjection::getServerVersion).reduce(Long::max);

return ResponseEntity.ok(HdssCompoundObj.builder()
.allCompounds(individuals.stream()
.map(individual -> HdssCompound
.builder()
return ResponseEntity.ok(HdssCompoundObj.builder().allCompounds(individuals.stream().map(
individual -> HdssCompound.builder().serverVersion(individual.getServerVersion())
.compoundId(individual.getCompoundId()).build()).collect(Collectors.toSet()))
.compoundHouseHolds(individuals.stream().map(
individual -> HdssCompoundHousehold.builder().compoundId(individual.getCompoundId())
.serverVersion(individual.getServerVersion())
.householdId(individual.getHouseholdId()).build()).collect(Collectors.toSet()))
.allHouseholdIndividual(individuals.stream().map(
individual -> HdssHouseholdIndividual.builder()
.serverVersion(individual.getServerVersion())
.compoundId(individual.getCompoundId())
.build()).collect(Collectors.toSet()))
.compoundHouseHolds(
individuals.stream()
.map(individual -> HdssCompoundHousehold.builder()
.compoundId(individual.getCompoundId())
.serverVersion(individual.getServerVersion())
.householdId(individual.getHouseholdId())
.build())
.collect(Collectors.toSet()))
.allHouseholdIndividual(
individuals.stream()
.map(individual -> HdssHouseholdIndividual
.builder()
.serverVersion(individual.getServerVersion())
.individualId(individual.getIndividualId())
.householdId(individual.getHouseholdId())
.build()
).collect(Collectors.toSet()))
.allHouseholdStructure(
individuals.stream()
.map(individual -> HdssHouseholdStructure
.builder()
.serverVersion(individual.getServerVersion())
.structureId(individual.getStructureId())
.householdId(individual.getHouseholdId())
.build())
.collect(Collectors.toSet()))
.allIndividuals(individuals.stream()
.map(individual -> HdssIndividual
.builder()
.identifier(individual.getId())
.individualId(individual.getIndividualId())
.householdId(individual.getHouseholdId()).build()).collect(Collectors.toSet()))
.allHouseholdStructure(individuals.stream().map(
individual -> HdssHouseholdStructure.builder()
.serverVersion(individual.getServerVersion())
.dob(individual.getDob().toString())
.gender(individual.getGender())
.build()).collect(Collectors.toSet()))
.structureId(individual.getStructureId()).householdId(individual.getHouseholdId())
.build()).collect(Collectors.toSet())).allIndividuals(individuals.stream().map(
individual -> HdssIndividual.builder().identifier(individual.getId())
.individualId(individual.getIndividualId())
.serverVersion(individual.getServerVersion()).dob(individual.getDob().toString())
.gender(individual.getGender()).build()).collect(Collectors.toSet()))
.serverVersion(maxServerVersion.isPresent() ? maxServerVersion.get() : 0)
.build());
.totalRecords(count).build());
} else {
return ResponseEntity.ok(HdssCompoundObj.builder().isEmpty(true).build());
}
}

@PostMapping(value = "/count", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public int gethdssDBcount(@RequestBody HdssSyncRequest hdssSyncRequest){
public int gethdssDBcount(@RequestBody HdssSyncRequest hdssSyncRequest) {
long serverVersion = hdssSyncRequest.getServerVersion();

return compoundsRepository.getCountOfCompoundsForUserAssignmentAndServerVersionAndBatchSize(hdssSyncRequest.getUserId(),serverVersion);
return compoundsRepository.getCountOfCompoundsForUserAssignmentAndServerVersionAndBatchSize(
hdssSyncRequest.getUserId(), serverVersion);
}


@PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<HdssCompoundHouseholdIndividualObj>> search(
@RequestBody HdssSearchRequest hdssSearchRequest) throws ParseException {
Expand Down Expand Up @@ -144,7 +128,7 @@ public ResponseEntity<List<HdssCompoundHouseholdIndividualObj>> search(

// Construct the JSON array format as a string

log.debug("search date: {}",searchDate);
log.debug("search date: {}", searchDate);

searchDeterminer = searchDeterminer.concat("D");
}
Expand All @@ -155,22 +139,19 @@ public ResponseEntity<List<HdssCompoundHouseholdIndividualObj>> search(
switch (searchDeterminer) {

case "G":
individualProjections = compoundsRepository.searchWithGender(
hdssSearchRequest.getGender());
individualProjections = compoundsRepository.searchWithGender(hdssSearchRequest.getGender());
break;
case "GD":
individualProjections = compoundsRepository.searchWithGenderAndDob(
hdssSearchRequest.getGender(), searchDate);
break;
case "GDS":
log.debug("search date: {}",searchDate);
log.debug("search date: {}", searchDate);
individualProjections = compoundsRepository.searchWithStringGenderAndDob(
hdssSearchRequest.getSearchString(), hdssSearchRequest.getGender(),
searchDate);
hdssSearchRequest.getSearchString(), hdssSearchRequest.getGender(), searchDate);
break;
case "D":
individualProjections = compoundsRepository.searchWithDob(
searchDate);
individualProjections = compoundsRepository.searchWithDob(searchDate);
break;
case "DS":
individualProjections = compoundsRepository.searchWithStringAndDob(
Expand All @@ -186,18 +167,15 @@ public ResponseEntity<List<HdssCompoundHouseholdIndividualObj>> search(
break;

}
if (individualProjections!=null) {
return ResponseEntity.ok(individualProjections.stream()
.map(individualProjection ->
HdssCompoundHouseholdIndividualObj.builder()
.compoundId(individualProjection.getCompoundId())
.householdId(individualProjection.getHouseholdId())
.individualId(individualProjection.getIndividualId())
.gender(individualProjection.getGender())
.dob(individualProjection.getDob().toString())
.id(individualProjection.getId())
.build())
.collect(Collectors.toList()));
if (individualProjections != null) {
return ResponseEntity.ok(individualProjections.stream().map(
individualProjection -> HdssCompoundHouseholdIndividualObj.builder()
.compoundId(individualProjection.getCompoundId())
.householdId(individualProjection.getHouseholdId())
.individualId(individualProjection.getIndividualId())
.gender(individualProjection.getGender())
.dob(individualProjection.getDob().toString()).id(individualProjection.getId())
.build()).collect(Collectors.toList()));
} else {
return ResponseEntity.notFound().build();
}
Expand Down Expand Up @@ -232,12 +210,9 @@ public ResponseEntity<HdssCompoundObj> taskSync(
.serverVersion(hdssCompoundHouseholdIndividualPushObj.getServerVersion())
.individualId(hdssCompoundHouseholdIndividualPushObj.getIndividualId())
.householdId(hdssCompoundHouseholdIndividualPushObj.getHouseholdId())
.structureId(hdssCompoundHouseholdIndividualPushObj.getStructureId())
.fields(Fields.builder()
.gender(hdssCompoundHouseholdIndividualPushObj.getGender())
.dob(parse)
.build())
.build();
.structureId(hdssCompoundHouseholdIndividualPushObj.getStructureId()).fields(
Fields.builder().gender(hdssCompoundHouseholdIndividualPushObj.getGender())
.dob(parse).build()).build();
}

).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class HdssCompoundObj implements Serializable {

long serverVersion;

int totalRecords;

@Data
@Builder
public static class HdssCompound implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,37 @@ List<HdssIndividualProjection> getAllIndividualsByCompoundIdInAndServerVersion(
+ " )as t",nativeQuery = true)
int getCountOfCompoundsForUserAssignmentAndServerVersionAndBatchSize(String username, long serverVersion);

@Query(value = "SELECT count(*) from (\n"
+ "SELECT DISTINCT cast(hc.id as varchar) as id, hc.compound_id as compoundId,hc.household_id as householdId,hc.individual_id as individualId, hc.fields->>'gender' as gender, CAST(hc.fields->>'dob' as date) as dob, hc.server_version as serverVersion, cast(hc.structure_id as varchar) as structureId FROM\n"
+ " (SELECT lr.location_identifier as child_location, arr.ancestor\n"
+ " from location_relationship lr,\n"
+ " unnest(lr.ancestry) with ordinality arr(ancestor, pos)\n"
+ " ) as lr\n"
+ " left join location lp on lr.ancestor = lp.identifier\n"
+ " left join geographic_level pgl on pgl.identifier = lp.geographic_level_identifier\n"
+ " left join location lc on lc.identifier = lr.child_location\n"
+ " left join geographic_level cgl on cgl.identifier = lc.geographic_level_identifier\n"
+ " inner join hdss.hdss_compounds hc on hc.structure_id = lr.child_location\n"
+ "WHERE lp.identifier in (\n"
+ " SELECT\n"
+ " l.identifier\n"
+ " from plan_assignment pa\n"
+ " inner join organization o on pa.organization_identifier = o.identifier\n"
+ " left join plan_locations pl on pl.identifier = pa.plan_locations_identifier\n"
+ " left join location l on l.identifier = pl.location_identifier\n"
+ " left join geographic_level gl on gl.identifier = l.geographic_level_identifier\n"
+ " left join plan p on pl.plan_identifier = p.identifier\n"
+ " left join location_hierarchy lh on p.hierarchy_identifier = lh.identifier\n"
+ " left join plan_target_type ptt on ptt.plan_identifier = p.identifier\n"
+ " left join geographic_level pgl on ptt.geographic_level_identifier = pgl.identifier\n"
+ " left join user_organization uo on o.identifier = uo.organization_identifier\n"
+ " left join users u on u.identifier = uo.user_identifier\n"
+ " WHERE gl.name = lh.node_order[array_position(lh.node_order, pgl.name) - 1]\n"
+ " and u.username = :username\n"
+ ") and hc.server_version>0\n"
+ " )as t",nativeQuery = true)
int getTotalCountOfCompoundsForUserAssignmentAndServerVersionAndBatchSize(String username);



@Query(value = "SELECT\n"
Expand Down

0 comments on commit 9bd9cd5

Please sign in to comment.