From 02605798c7173eacec6dabba8928188fa7ecdb35 Mon Sep 17 00:00:00 2001 From: ojwanganto Date: Thu, 4 Jul 2024 12:33:40 +0300 Subject: [PATCH] Improve error handling the create logic --- .../impl/api/EntityBasisMapResponseMapper.java | 15 ++++++++++++++- .../web/controller/DataFilterController.java | 12 ++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/datafilter/impl/api/EntityBasisMapResponseMapper.java b/api/src/main/java/org/openmrs/module/datafilter/impl/api/EntityBasisMapResponseMapper.java index ca4c9c2..793d078 100644 --- a/api/src/main/java/org/openmrs/module/datafilter/impl/api/EntityBasisMapResponseMapper.java +++ b/api/src/main/java/org/openmrs/module/datafilter/impl/api/EntityBasisMapResponseMapper.java @@ -72,6 +72,18 @@ private Map createLocationMap(Location l) { locationMap = new HashMap(); locationMap.put("name", l.getName()); locationMap.put("uuid", l.getUuid()); + locationMap.put("country", l.getCountry() != null ? l.getCountry() : ""); + locationMap.put("countyDistrict", l.getCountyDistrict() != null ? l.getCountyDistrict() : ""); + locationMap.put("cityVillage", l.getCityVillage() != null ? l.getCityVillage() : ""); + locationMap.put("country", l.getCountry() != null ? l.getCountry() : ""); + locationMap.put("address1", l.getAddress1() != null ? l.getAddress1() : ""); + locationMap.put("address2", l.getAddress2() != null ? l.getAddress2() : ""); + locationMap.put("address3", l.getAddress3() != null ? l.getAddress3() : ""); + locationMap.put("address4", l.getAddress4() != null ? l.getAddress4() : ""); + locationMap.put("address5", l.getAddress5() != null ? l.getAddress5() : ""); + locationMap.put("address6", l.getAddress6() != null ? l.getAddress6() : ""); + locationMap.putAll(l.getActiveAttributes().stream().filter(e -> e.getAttributeType() != null).collect( + Collectors.toMap(e -> e.getAttributeType().getName(), e -> e.getValue(), (e1, e2) -> e1 + "," + e2))); } return locationMap; } @@ -80,12 +92,13 @@ private Map createPatientMap(Patient p) { Map map = new HashMap(); map.put("name", p.getPersonName().getFullName()); map.put("uuid", p.getUuid()); - map.put("identifier", p.getPatientIdentifier().getIdentifier()); map.put("age", p.getAge()); map.put("gender", p.getGender()); map.putAll(p.getActiveIdentifiers().stream().filter(e -> e.getIdentifierType() != null) .collect(Collectors.toMap(e -> e.getIdentifierType().toString().replaceAll("[- ]", ""), e -> e.getIdentifier(), (e1, e2) -> e1 + "," + e2))); + map.putAll(p.getActiveAttributes().stream().filter(e -> e.getValue() != null).collect( + Collectors.toMap(e -> e.getAttributeType().getName(), e -> e.getValue(), (e1, e2) -> e1 + "," + e2))); return map; } diff --git a/omod/src/main/java/org/openmrs/module/datafilter/web/controller/DataFilterController.java b/omod/src/main/java/org/openmrs/module/datafilter/web/controller/DataFilterController.java index f462301..0f9ecd8 100644 --- a/omod/src/main/java/org/openmrs/module/datafilter/web/controller/DataFilterController.java +++ b/omod/src/main/java/org/openmrs/module/datafilter/web/controller/DataFilterController.java @@ -71,12 +71,16 @@ public ResponseEntity grantAccess(@Valid @RequestBody EntityBasisMap ent && basisDomain.equalsIgnoreCase("org.openmrs.Location")) { basis = Context.getLocationService().getLocationByUuid(basisIdentifier); } + if (entity == null) { + return new ResponseEntity<>("Data filter Entity is required!", HttpStatus.BAD_REQUEST); + } - if (entity != null && basis != null) { - dataFilterService.grantAccess(entity, basis); - return new ResponseEntity<>("Assignment done successfully", HttpStatus.OK); + if (basis == null) { + return new ResponseEntity<>("Data filter Basis is required!", HttpStatus.BAD_REQUEST); } - return new ResponseEntity<>("There was an error saving the assignment", HttpStatus.INTERNAL_SERVER_ERROR); + dataFilterService.grantAccess(entity, basis); + return new ResponseEntity<>("Data filter mapping done successfully", HttpStatus.CREATED); + } catch (Exception e) { log.error("Runtime error while trying to create new appointment", e);