From 8eed73d75b8db727170dd7f54517587d04913993 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 31 May 2024 15:13:24 +0530 Subject: [PATCH] avniproject/avni-client#1381 - separate out request and response objects for bundle and web for report cards. --- .../org/avni/server/domain/ReportCard.java | 4 -- .../batch/zip/BundleZipFileImporter.java | 6 +- .../mapper/dashboard/DashboardMapper.java | 2 +- .../mapper/dashboard/ReportCardMapper.java | 40 +++++++++---- .../org/avni/server/service/CardService.java | 56 +++++++++++++------ .../server/service/OrganisationService.java | 8 +-- .../avni/server/web/ReportCardController.java | 16 +++--- .../reports/ReportCardBundleRequest.java | 13 +++++ .../{ => reports}/ReportCardRequest.java | 13 +---- .../request/reports/ReportCardWebRequest.java | 13 +++++ .../reports/ReportCardBundleResponse.java | 45 +++++++++++++++ .../{ => reports}/ReportCardResponse.java | 2 +- 12 files changed, 158 insertions(+), 60 deletions(-) create mode 100644 avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardBundleRequest.java rename avni-server-api/src/main/java/org/avni/server/web/request/{ => reports}/ReportCardRequest.java (76%) create mode 100644 avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardWebRequest.java create mode 100644 avni-server-api/src/main/java/org/avni/server/web/response/reports/ReportCardBundleResponse.java rename avni-server-api/src/main/java/org/avni/server/web/response/{ => reports}/ReportCardResponse.java (97%) diff --git a/avni-server-api/src/main/java/org/avni/server/domain/ReportCard.java b/avni-server-api/src/main/java/org/avni/server/domain/ReportCard.java index 335e18645..c604da0aa 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/ReportCard.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/ReportCard.java @@ -106,10 +106,6 @@ private void resetNestedCardInfoOnlyApplicableForQueryReportCardType(Boolean rep } } - public Long getStandardReportCardTypeId() { - return standardReportCardType == null ? null : standardReportCardType.getId(); - } - public String getIconFileS3Key() { return iconFileS3Key; } diff --git a/avni-server-api/src/main/java/org/avni/server/importer/batch/zip/BundleZipFileImporter.java b/avni-server-api/src/main/java/org/avni/server/importer/batch/zip/BundleZipFileImporter.java index 2ef74dc0d..4d10a86fb 100644 --- a/avni-server-api/src/main/java/org/avni/server/importer/batch/zip/BundleZipFileImporter.java +++ b/avni-server-api/src/main/java/org/avni/server/importer/batch/zip/BundleZipFileImporter.java @@ -21,12 +21,12 @@ import org.avni.server.service.accessControl.GroupPrivilegeService; import org.avni.server.service.application.MenuItemService; import org.avni.server.util.ObjectMapperSingleton; -import org.avni.server.web.contract.ReportCardContract; import org.avni.server.web.contract.GroupDashboardBundleContract; import org.avni.server.web.request.*; import org.avni.server.web.request.application.ChecklistDetailRequest; import org.avni.server.web.request.application.FormContract; import org.avni.server.web.request.application.menu.MenuItemContract; +import org.avni.server.web.request.reports.ReportCardBundleRequest; import org.avni.server.web.request.webapp.IdentifierSourceContractWeb; import org.avni.server.web.request.webapp.documentation.DocumentationContract; import org.avni.server.web.request.webapp.task.TaskStatusContract; @@ -383,8 +383,8 @@ private void deployFile(String fileName, String fileData, List list = ds.getDashboardSectionCardMappings().stream() .map(mapping -> { - ReportCardContract cardContract = reportCardMapper.fromEntity(mapping.getCard()); + ReportCardContract cardContract = reportCardMapper.toWebResponse(mapping.getCard()); cardContract.setDisplayOrder(mapping.getDisplayOrder()); return cardContract; }) diff --git a/avni-server-api/src/main/java/org/avni/server/mapper/dashboard/ReportCardMapper.java b/avni-server-api/src/main/java/org/avni/server/mapper/dashboard/ReportCardMapper.java index 07ff7fa0b..0e1c74e0b 100644 --- a/avni-server-api/src/main/java/org/avni/server/mapper/dashboard/ReportCardMapper.java +++ b/avni-server-api/src/main/java/org/avni/server/mapper/dashboard/ReportCardMapper.java @@ -1,5 +1,6 @@ package org.avni.server.mapper.dashboard; +import org.avni.server.domain.CHSBaseEntity; import org.avni.server.domain.ReportCard; import org.avni.server.service.CardService; import org.avni.server.web.contract.EncounterTypeContract; @@ -7,7 +8,8 @@ import org.avni.server.web.contract.ReportCardContract; import org.avni.server.web.request.StandardReportCardTypeContract; import org.avni.server.web.request.SubjectTypeContract; -import org.avni.server.web.response.ReportCardResponse; +import org.avni.server.web.response.reports.ReportCardBundleResponse; +import org.avni.server.web.response.reports.ReportCardResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -22,23 +24,39 @@ public ReportCardMapper(CardService reportCardService) { this.reportCardService = reportCardService; } - public ReportCardResponse fromEntity(ReportCard card) { + public ReportCardResponse toWebResponse(ReportCard card) { ReportCardResponse response = new ReportCardResponse(); - response.setId(card.getId()); - response.setUuid(card.getUuid()); - response.setVoided(card.isVoided()); - response.setName(card.getName()); - response.setQuery(card.getQuery()); - response.setDescription(card.getDescription()); - response.setColor(card.getColour()); + setPrimitives(card, response); if (card.getStandardReportCardType() != null) response.setStandardReportCardType(StandardReportCardTypeContract.fromEntity(card.getStandardReportCardType())); response.setIconFileS3Key(card.getIconFileS3Key()); - response.setNested(card.isNested()); - response.setCount(card.getCountOfCards()); response.setStandardReportCardInputSubjectTypes(reportCardService.getStandardReportCardInputSubjectTypes(card).stream().map(SubjectTypeContract::createBasic).collect(Collectors.toList())); response.setStandardReportCardInputPrograms(reportCardService.getStandardReportCardInputPrograms(card).stream().map(ProgramContract::createBasic).collect(Collectors.toList())); response.setStandardReportCardInputEncounterTypes(reportCardService.getStandardReportCardInputEncounterTypes(card).stream().map(EncounterTypeContract::createBasic).collect(Collectors.toList())); return response; } + + private void setPrimitives(ReportCard card, ReportCardContract contract) { + contract.setId(card.getId()); + contract.setUuid(card.getUuid()); + contract.setVoided(card.isVoided()); + contract.setName(card.getName()); + contract.setQuery(card.getQuery()); + contract.setDescription(card.getDescription()); + contract.setColor(card.getColour()); + contract.setNested(card.isNested()); + contract.setCount(card.getCountOfCards()); + } + + public ReportCardBundleResponse toBundleResponse(ReportCard reportCard) { + ReportCardBundleResponse response = new ReportCardBundleResponse(); + setPrimitives(reportCard, response); + if (reportCard.getStandardReportCardType() != null) { + response.setStandardReportCardType(reportCard.getStandardReportCardType().getUuid()); + } + response.setStandardReportCardInputSubjectTypes(reportCardService.getStandardReportCardInputSubjectTypes(reportCard).stream().map(CHSBaseEntity::getUuid).collect(Collectors.toList())); + response.setStandardReportCardInputPrograms(reportCardService.getStandardReportCardInputPrograms(reportCard).stream().map(CHSBaseEntity::getUuid).collect(Collectors.toList())); + response.setStandardReportCardInputEncounterTypes(reportCardService.getStandardReportCardInputEncounterTypes(reportCard).stream().map(CHSBaseEntity::getUuid).collect(Collectors.toList())); + return response; + } } diff --git a/avni-server-api/src/main/java/org/avni/server/service/CardService.java b/avni-server-api/src/main/java/org/avni/server/service/CardService.java index 59e39e8fa..271e2dda8 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/CardService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/CardService.java @@ -3,8 +3,9 @@ import org.avni.server.dao.*; import org.avni.server.domain.*; import org.avni.server.util.BadRequestError; -import org.avni.server.web.contract.ReportCardContract; -import org.avni.server.web.request.ReportCardRequest; +import org.avni.server.web.request.reports.ReportCardBundleRequest; +import org.avni.server.web.request.reports.ReportCardRequest; +import org.avni.server.web.request.reports.ReportCardWebRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -28,29 +29,32 @@ public CardService(CardRepository cardRepository, StandardReportCardTypeReposito this.encounterTypeRepository = encounterTypeRepository; } - public ReportCard saveCard(org.avni.server.web.request.ReportCardRequest reportCardRequest) { + public ReportCard saveCard(ReportCardWebRequest reportCardRequest) { assertNoExistingCardWithName(reportCardRequest.getName()); ReportCard card = new ReportCard(); card.assignUUID(); buildCard(reportCardRequest, card); + buildStandardReportCardType(reportCardRequest, card); cardRepository.save(card); return card; } - public void uploadCard(ReportCardRequest reportCardRequest) { + public void uploadCard(ReportCardBundleRequest reportCardRequest) { ReportCard card = cardRepository.findByUuid(reportCardRequest.getUuid()); if (card == null) { card = new ReportCard(); card.setUuid(reportCardRequest.getUuid()); } buildCard(reportCardRequest, card); + buildStandardReportCardType(reportCardRequest, card); cardRepository.save(card); } - public ReportCard editCard(ReportCardRequest newCard, Long cardId) { + public ReportCard editCard(ReportCardWebRequest request, Long cardId) { ReportCard existingCard = cardRepository.findOne(cardId); - assertNewNameIsUnique(newCard.getName(), existingCard.getName()); - buildCard(newCard, existingCard); + assertNewNameIsUnique(request.getName(), existingCard.getName()); + buildCard(request, existingCard); + buildStandardReportCardType(request, existingCard); return cardRepository.save(existingCard); } @@ -63,24 +67,42 @@ public List getAll() { return cardRepository.findAll(); } - private void buildCard(ReportCardRequest reportCardRequest, ReportCard card) { - card.setName(reportCardRequest.getName()); - card.setColour(reportCardRequest.getColor()); - card.setDescription(reportCardRequest.getDescription()); - card.setQuery(reportCardRequest.getQuery()); - card.setVoided(reportCardRequest.isVoided()); - card.setIconFileS3Key(reportCardRequest.getIconFileS3Key()); - Long standardReportCardTypeId = reportCardRequest.getStandardReportCardTypeId(); + private void buildStandardReportCardType(ReportCardWebRequest reportCardWebRequest, ReportCard reportCard) { + Long standardReportCardTypeId = reportCardWebRequest.getStandardReportCardTypeId(); if (standardReportCardTypeId != null) { StandardReportCardType type = standardReportCardTypeRepository.findById(standardReportCardTypeId).orElse(null); if (type == null) { throw new BadRequestError(String.format("StandardReportCardType with id %d doesn't exist", standardReportCardTypeId)); } - card.setStandardReportCardType(type); + reportCard.setStandardReportCardType(type); + } else { + reportCard.setStandardReportCardType(null); + } + } + + private void buildStandardReportCardType(ReportCardBundleRequest reportCardBundleRequest, ReportCard reportCard) { + String standardReportCardTypeUUID = reportCardBundleRequest.getStandardReportCardType(); + + if (standardReportCardTypeUUID != null) { + StandardReportCardType type = standardReportCardTypeRepository.findByUuid(standardReportCardTypeUUID); + if (type == null) { + throw new BadRequestError(String.format("StandardReportCardType with uuid %s doesn't exist", standardReportCardTypeUUID)); + } + reportCard.setStandardReportCardType(type); } else { - card.setStandardReportCardType(null); + reportCard.setStandardReportCardType(null); } + } + + private void buildCard(ReportCardRequest reportCardRequest, ReportCard card) { + card.setName(reportCardRequest.getName()); + card.setColour(reportCardRequest.getColor()); + card.setDescription(reportCardRequest.getDescription()); + card.setQuery(reportCardRequest.getQuery()); + card.setVoided(reportCardRequest.isVoided()); + card.setIconFileS3Key(reportCardRequest.getIconFileS3Key()); + card.setNested(reportCardRequest.isNested()); if (reportCardRequest.getCount() < ReportCard.INT_CONSTANT_DEFAULT_COUNT_OF_CARDS || reportCardRequest.getCount() > ReportCard.INT_CONSTANT_MAX_COUNT_OF_CARDS) { throw new BadRequestError(String.format("Nested ReportCard count should have minmum value of %d and maximum value of %d", diff --git a/avni-server-api/src/main/java/org/avni/server/service/OrganisationService.java b/avni-server-api/src/main/java/org/avni/server/service/OrganisationService.java index 504244d8c..94062302c 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/OrganisationService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/OrganisationService.java @@ -28,7 +28,6 @@ import org.avni.server.util.ObjectMapperSingleton; import org.avni.server.util.S; import org.avni.server.util.S3File; -import org.avni.server.web.contract.ReportCardContract; import org.avni.server.web.contract.GroupDashboardBundleContract; import org.avni.server.web.request.*; import org.avni.server.web.request.application.ChecklistDetailRequest; @@ -41,6 +40,7 @@ import org.avni.server.web.request.webapp.documentation.DocumentationContract; import org.avni.server.web.request.webapp.task.TaskStatusContract; import org.avni.server.web.request.webapp.task.TaskTypeContract; +import org.avni.server.web.response.reports.ReportCardBundleResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -551,9 +551,9 @@ public void addReportCardIcons(ZipOutputStream zos) throws IOException { } public void addReportCards(ZipOutputStream zos) throws IOException { - List cardContracts = cardService.getAll().stream().map(reportCardMapper::fromEntity).collect(Collectors.toList()); - if (!cardContracts.isEmpty()) { - addFileToZip(zos, "reportCard.json", cardContracts); + List cards = cardService.getAll().stream().map(reportCardMapper::toBundleResponse).collect(Collectors.toList()); + if (!cards.isEmpty()) { + addFileToZip(zos, "reportCard.json", cards); } } diff --git a/avni-server-api/src/main/java/org/avni/server/web/ReportCardController.java b/avni-server-api/src/main/java/org/avni/server/web/ReportCardController.java index 02883f2c4..737931eea 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/ReportCardController.java +++ b/avni-server-api/src/main/java/org/avni/server/web/ReportCardController.java @@ -7,8 +7,8 @@ import org.avni.server.service.CardService; import org.avni.server.service.accessControl.AccessControlService; import org.avni.server.web.contract.ReportCardContract; -import org.avni.server.web.request.ReportCardRequest; -import org.avni.server.web.response.ReportCardResponse; +import org.avni.server.web.request.reports.ReportCardWebRequest; +import org.avni.server.web.response.reports.ReportCardResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -37,7 +37,7 @@ public ReportCardController(CardRepository cardRepository, CardService cardServi @ResponseBody public List getAll() { return cardRepository.findAllByIsVoidedFalseOrderByName() - .stream().map(reportCardMapper::fromEntity) + .stream().map(reportCardMapper::toWebResponse) .collect(Collectors.toList()); } @@ -45,30 +45,30 @@ public List getAll() { @ResponseBody public ResponseEntity getById(@PathVariable Long id) { Optional card = cardRepository.findById(id); - return card.map(c -> ResponseEntity.ok(reportCardMapper.fromEntity(c))) + return card.map(c -> ResponseEntity.ok(reportCardMapper.toWebResponse(c))) .orElseGet(() -> ResponseEntity.notFound().build()); } @PostMapping(value = "/web/reportCard") @ResponseBody @Transactional - public ResponseEntity newCard(@RequestBody ReportCardRequest cardRequest) { + public ResponseEntity newCard(@RequestBody ReportCardWebRequest cardRequest) { accessControlService.checkPrivilege(PrivilegeType.EditOfflineDashboardAndReportCard); ReportCard card = cardService.saveCard(cardRequest); - return ResponseEntity.ok(reportCardMapper.fromEntity(card)); + return ResponseEntity.ok(reportCardMapper.toWebResponse(card)); } @PutMapping(value = "/web/reportCard/{id}") @ResponseBody @Transactional - public ResponseEntity editCard(@PathVariable Long id, @RequestBody ReportCardRequest request) { + public ResponseEntity editCard(@PathVariable Long id, @RequestBody ReportCardWebRequest request) { accessControlService.checkPrivilege(PrivilegeType.EditOfflineDashboardAndReportCard); Optional card = cardRepository.findById(id); if (!card.isPresent()) { return ResponseEntity.notFound().build(); } ReportCard savedCard = cardService.editCard(request, id); - return ResponseEntity.ok(reportCardMapper.fromEntity(savedCard)); + return ResponseEntity.ok(reportCardMapper.toWebResponse(savedCard)); } @DeleteMapping(value = "/web/reportCard/{id}") diff --git a/avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardBundleRequest.java b/avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardBundleRequest.java new file mode 100644 index 000000000..568cd53cf --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardBundleRequest.java @@ -0,0 +1,13 @@ +package org.avni.server.web.request.reports; + +public class ReportCardBundleRequest extends ReportCardRequest { + private String standardReportCardType; + + public String getStandardReportCardType() { + return standardReportCardType; + } + + public void setStandardReportCardType(String standardReportCardType) { + this.standardReportCardType = standardReportCardType; + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/web/request/ReportCardRequest.java b/avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardRequest.java similarity index 76% rename from avni-server-api/src/main/java/org/avni/server/web/request/ReportCardRequest.java rename to avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardRequest.java index 72a674f32..c65a778d7 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/request/ReportCardRequest.java +++ b/avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardRequest.java @@ -1,23 +1,14 @@ -package org.avni.server.web.request; +package org.avni.server.web.request.reports; import org.avni.server.web.contract.ReportCardContract; import java.util.List; -public class ReportCardRequest extends ReportCardContract { - private Long standardReportCardTypeId; +public abstract class ReportCardRequest extends ReportCardContract { private List standardReportCardInputSubjectTypes; private List standardReportCardInputPrograms; private List standardReportCardInputEncounterTypes; - public Long getStandardReportCardTypeId() { - return standardReportCardTypeId; - } - - public void setStandardReportCardTypeId(Long standardReportCardTypeId) { - this.standardReportCardTypeId = standardReportCardTypeId; - } - public List getStandardReportCardInputSubjectTypes() { return standardReportCardInputSubjectTypes; } diff --git a/avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardWebRequest.java b/avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardWebRequest.java new file mode 100644 index 000000000..0bf03b3e4 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/web/request/reports/ReportCardWebRequest.java @@ -0,0 +1,13 @@ +package org.avni.server.web.request.reports; + +public class ReportCardWebRequest extends ReportCardRequest { + private Long standardReportCardTypeId; + + public Long getStandardReportCardTypeId() { + return standardReportCardTypeId; + } + + public void setStandardReportCardTypeId(Long standardReportCardTypeId) { + this.standardReportCardTypeId = standardReportCardTypeId; + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/web/response/reports/ReportCardBundleResponse.java b/avni-server-api/src/main/java/org/avni/server/web/response/reports/ReportCardBundleResponse.java new file mode 100644 index 000000000..d29fb3ba5 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/web/response/reports/ReportCardBundleResponse.java @@ -0,0 +1,45 @@ +package org.avni.server.web.response.reports; + +import org.avni.server.web.contract.ReportCardContract; + +import java.util.ArrayList; +import java.util.List; + +public class ReportCardBundleResponse extends ReportCardContract { + private String standardReportCardType; + private List standardReportCardInputSubjectTypes = new ArrayList<>(); + private List standardReportCardInputPrograms = new ArrayList<>(); + private List standardReportCardInputEncounterTypes = new ArrayList<>(); + + public String getStandardReportCardType() { + return standardReportCardType; + } + + public void setStandardReportCardType(String standardReportCardType) { + this.standardReportCardType = standardReportCardType; + } + + public List getStandardReportCardInputSubjectTypes() { + return standardReportCardInputSubjectTypes; + } + + public void setStandardReportCardInputSubjectTypes(List standardReportCardInputSubjectTypes) { + this.standardReportCardInputSubjectTypes = standardReportCardInputSubjectTypes; + } + + public List getStandardReportCardInputPrograms() { + return standardReportCardInputPrograms; + } + + public void setStandardReportCardInputPrograms(List standardReportCardInputPrograms) { + this.standardReportCardInputPrograms = standardReportCardInputPrograms; + } + + public List getStandardReportCardInputEncounterTypes() { + return standardReportCardInputEncounterTypes; + } + + public void setStandardReportCardInputEncounterTypes(List standardReportCardInputEncounterTypes) { + this.standardReportCardInputEncounterTypes = standardReportCardInputEncounterTypes; + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/web/response/ReportCardResponse.java b/avni-server-api/src/main/java/org/avni/server/web/response/reports/ReportCardResponse.java similarity index 97% rename from avni-server-api/src/main/java/org/avni/server/web/response/ReportCardResponse.java rename to avni-server-api/src/main/java/org/avni/server/web/response/reports/ReportCardResponse.java index 787347ad5..2a83a1cb9 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/response/ReportCardResponse.java +++ b/avni-server-api/src/main/java/org/avni/server/web/response/reports/ReportCardResponse.java @@ -1,4 +1,4 @@ -package org.avni.server.web.response; +package org.avni.server.web.response.reports; import org.avni.server.web.contract.EncounterTypeContract; import org.avni.server.web.contract.ProgramContract;