From f91f63014990f67b6a338b9e16bf791b5769aa7c Mon Sep 17 00:00:00 2001 From: Josephat Mwakyusa Date: Wed, 31 Jul 2024 14:36:53 +0300 Subject: [PATCH] Add controller for HDU APIs --- .../icare/Controllers/HDUAPIController.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 backend/src/main/java/com/Adapter/icare/Controllers/HDUAPIController.java diff --git a/backend/src/main/java/com/Adapter/icare/Controllers/HDUAPIController.java b/backend/src/main/java/com/Adapter/icare/Controllers/HDUAPIController.java new file mode 100644 index 00000000..885513b5 --- /dev/null +++ b/backend/src/main/java/com/Adapter/icare/Controllers/HDUAPIController.java @@ -0,0 +1,101 @@ +package com.Adapter.icare.Controllers; + +import com.Adapter.icare.Domains.Datastore; +import com.Adapter.icare.Services.DatastoreService; +import com.google.common.collect.Maps; +import org.springframework.web.bind.annotation.*; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; + +@RestController +@RequestMapping("/api/v1/hduApi") +public class HDUAPIController { + + private final DatastoreService datastoreService; + + public HDUAPIController(DatastoreService datastoreService) { + this.datastoreService = datastoreService; + } + + + @DeleteMapping("datastore/{uuid}") + public void deleteDatastore(@PathVariable("uuid") String uuid) throws Exception { + datastoreService.deleteDatastore(uuid); + } + + // CUSTOM implementation for supporting HDU API temporarily + @GetMapping("clientsVisitsByNamespace/{namespace}") + public List getClientsVisitsDataByNamespace(@PathVariable("namespace") String namespace) throws Exception { + return datastoreService.getClientsVisitsDataByNameSpace(namespace); + } + + @GetMapping("clientsVisitsByKey/{key}") + public List getClientsVisitsDataByKey(@PathVariable("key") String key) throws Exception { + return datastoreService.getClientsVisitsDataByKey(key); + } + + @GetMapping("clientsVisits") + public List getClientsVisits(@RequestParam(value = "key") String key, + @RequestParam(value = "ageType") String ageType, + @RequestParam(value = "startAge") Integer startAge, + @RequestParam(value = "endAge") Integer endAge, + @RequestParam(value = "gender", required = false) String gender, + @RequestParam(value = "diagnosis", required = false) String diagnosis) throws Exception { + return datastoreService.getClientsVisits(key, ageType, startAge, endAge, gender, diagnosis); + } + + @PostMapping(value = "generateAggregateData",produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE) + public Map getAggregateVisits(@RequestBody Map requestParams) throws Exception { + Map results = new HashMap<>(); + List> data = new ArrayList<>(); + String pattern = "yyyy-MM-dd"; + Map mappings = (Map) requestParams.get("mappings"); + Map orgUnit = (Map) requestParams.get("orgUnit"); + String mappingsNamespace = mappings.get("namespace").toString(); + String mappingsKey = mappings.get("key").toString(); + List storedToolMappings = datastoreService.getDatastoreNamespaceDetails(mappingsNamespace); + SimpleDateFormat formatter = new SimpleDateFormat(pattern); + for (Datastore storedToolMapping: storedToolMappings) { + if (storedToolMapping.getValue().get("type").equals("diagnosisDetails")) { + for(Map requestParam: (List>) storedToolMapping.getValue().get("params")) { + Map dataValue = new HashMap<>(); + List> requestedData = datastoreService.getAggregatedData( + requestParams.get("startDate").toString(), + requestParams.get("endDate").toString(), + requestParam.get("ageType").toString(), + (Integer) requestParam.get("startAge"), + (Integer) requestParam.get("endAge"), + requestParam.get("gender").toString(), + mappingsNamespace, + mappingsKey, + orgUnit.get("code").toString() + ); +// System.out.println(requestedData); + dataValue.put("value", requestedData.get(0).get("aggregated")); + dataValue.put("dataElement", (( Map)storedToolMapping.getValue().get("dataElement")).get("id").toString()); + dataValue.put("categoryOptionCombo", requestParam.get("co")); + data.add(dataValue); + } + } + } + results.put("data", data); + return results; + } + + @GetMapping("aggregatedData") + public Map getAggregateDataByStartDateAndEndDate(@RequestParam(value = "id") String id, + @RequestParam(value = "startDate") String startDate, + @RequestParam(value = "endDate") String endDate + ) throws Exception { + Map results = Maps.newHashMap(); + List> dailyAggregatedDataList = datastoreService.getAggregateDataFromDailyAggregatedData(id,startDate,endDate); + results.put("data", dailyAggregatedDataList); + return results; + } +}