Skip to content

Commit

Permalink
avniproject/avni-webapp#1203 - split report card contract into reques…
Browse files Browse the repository at this point in the history
…t and response. save report card with standard report card input form metadata
  • Loading branch information
petmongrels committed May 21, 2024
1 parent 65dd779 commit 0973435
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -382,9 +383,9 @@ private void deployFile(String fileName, String fileData, List<? extends BundleF
}
break;
case "reportCard.json":
CardContract[] cardContracts = convertString(fileData, CardContract[].class);
for (CardContract cardContract : cardContracts) {
cardService.uploadCard(cardContract);
ReportCardRequest[] cardContracts = convertString(fileData, ReportCardRequest[].class);
for (ReportCardRequest cardRequest : cardContracts) {
cardService.uploadCard(cardRequest);
}
break;
case "reportDashboard.json":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.avni.server.domain.Dashboard;
import org.avni.server.domain.DashboardSection;
import org.avni.server.web.contract.ReportCardContract;
import org.avni.server.web.request.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -58,9 +59,9 @@ private DashboardSectionContract fromEntity(DashboardSection ds) {
dashboardContract.setViewType(ds.getViewType().name());
dashboardContract.setDisplayOrder(ds.getDisplayOrder());

List<CardContract> list = ds.getDashboardSectionCardMappings().stream()
List<ReportCardContract> list = ds.getDashboardSectionCardMappings().stream()
.map(mapping -> {
CardContract cardContract = reportCardMapper.fromEntity(mapping.getCard());
ReportCardContract cardContract = reportCardMapper.fromEntity(mapping.getCard());
cardContract.setDisplayOrder(mapping.getDisplayOrder());
return cardContract;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import org.avni.server.service.CardService;
import org.avni.server.web.contract.EncounterTypeContract;
import org.avni.server.web.contract.ProgramContract;
import org.avni.server.web.request.CardContract;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -21,23 +22,23 @@ public ReportCardMapper(CardService reportCardService) {
this.reportCardService = reportCardService;
}

public CardContract fromEntity(ReportCard card) {
CardContract cardContract = new CardContract();
cardContract.setId(card.getId());
cardContract.setUuid(card.getUuid());
cardContract.setVoided(card.isVoided());
cardContract.setName(card.getName());
cardContract.setQuery(card.getQuery());
cardContract.setDescription(card.getDescription());
cardContract.setColor(card.getColour());
public ReportCardResponse fromEntity(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());
if (card.getStandardReportCardType() != null)
cardContract.setStandardReportCardType(StandardReportCardTypeContract.fromEntity(card.getStandardReportCardType()));
cardContract.setIconFileS3Key(card.getIconFileS3Key());
cardContract.setNested(card.isNested());
cardContract.setCount(card.getCountOfCards());
cardContract.setStandardReportCardInputSubjectTypes(reportCardService.getStandardReportCardInputSubjectTypes(card).stream().map(SubjectTypeContract::createBasic).collect(Collectors.toList()));
cardContract.setStandardReportCardInputPrograms(reportCardService.getStandardReportCardInputPrograms(card).stream().map(ProgramContract::createBasic).collect(Collectors.toList()));
cardContract.setStandardReportCardInputEncounterTypes(reportCardService.getStandardReportCardInputEncounterTypes(card).stream().map(EncounterTypeContract::createBasic).collect(Collectors.toList()));
return cardContract;
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import org.avni.server.dao.*;
import org.avni.server.domain.*;
import org.avni.server.mapper.dashboard.ReportCardMapper;
import org.avni.server.util.BadRequestError;
import org.avni.server.web.request.CardContract;
import org.avni.server.web.contract.ReportCardContract;
import org.avni.server.web.request.ReportCardRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.joda.time.DateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CardService implements NonScopeAwareService {
Expand All @@ -29,26 +28,26 @@ public CardService(CardRepository cardRepository, StandardReportCardTypeReposito
this.encounterTypeRepository = encounterTypeRepository;
}

public ReportCard saveCard(CardContract cardContract) {
assertNoExistingCardWithName(cardContract.getName());
public ReportCard saveCard(org.avni.server.web.request.ReportCardRequest reportCardRequest) {
assertNoExistingCardWithName(reportCardRequest.getName());
ReportCard card = new ReportCard();
card.assignUUID();
buildCard(cardContract, card);
buildCard(reportCardRequest, card);
cardRepository.save(card);
return card;
}

public void uploadCard(CardContract cardContract) {
ReportCard card = cardRepository.findByUuid(cardContract.getUuid());
public void uploadCard(ReportCardRequest reportCardRequest) {
ReportCard card = cardRepository.findByUuid(reportCardRequest.getUuid());
if (card == null) {
card = new ReportCard();
card.setUuid(cardContract.getUuid());
card.setUuid(reportCardRequest.getUuid());
}
buildCard(cardContract, card);
buildCard(reportCardRequest, card);
cardRepository.save(card);
}

public ReportCard editCard(CardContract newCard, Long cardId) {
public ReportCard editCard(ReportCardRequest newCard, Long cardId) {
ReportCard existingCard = cardRepository.findOne(cardId);
assertNewNameIsUnique(newCard.getName(), existingCard.getName());
buildCard(newCard, existingCard);
Expand All @@ -64,14 +63,14 @@ public List<ReportCard> getAll() {
return cardRepository.findAll();
}

private void buildCard(CardContract cardContract, ReportCard card) {
card.setName(cardContract.getName());
card.setColour(cardContract.getColor());
card.setDescription(cardContract.getDescription());
card.setQuery(cardContract.getQuery());
card.setVoided(cardContract.isVoided());
card.setIconFileS3Key(cardContract.getIconFileS3Key());
Long standardReportCardTypeId = cardContract.getStandardReportCardTypeId();
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();

if (standardReportCardTypeId != null) {
StandardReportCardType type = standardReportCardTypeRepository.findById(standardReportCardTypeId).orElse(null);
Expand All @@ -82,12 +81,16 @@ private void buildCard(CardContract cardContract, ReportCard card) {
} else {
card.setStandardReportCardType(null);
}
card.setNested(cardContract.isNested());
if (cardContract.getCount() < ReportCard.INT_CONSTANT_DEFAULT_COUNT_OF_CARDS || cardContract.getCount() > ReportCard.INT_CONSTANT_MAX_COUNT_OF_CARDS) {
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",
ReportCard.INT_CONSTANT_DEFAULT_COUNT_OF_CARDS, ReportCard.INT_CONSTANT_MAX_COUNT_OF_CARDS));
}
card.setCountOfCards(cardContract.getCount());
card.setCountOfCards(reportCardRequest.getCount());

card.setStandardReportCardInputSubjectTypes(reportCardRequest.getStandardReportCardInputSubjectTypes());
card.setStandardReportCardInputPrograms(reportCardRequest.getStandardReportCardInputPrograms());
card.setStandardReportCardInputEncounterTypes(reportCardRequest.getStandardReportCardInputEncounterTypes());
}

private void assertNewNameIsUnique(String newName, String oldName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.avni.server.domain.app.dashboard.DashboardFilter;
import org.avni.server.mapper.dashboard.DashboardMapper;
import org.avni.server.util.BadRequestError;
import org.avni.server.web.contract.ReportCardContract;
import org.avni.server.web.request.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -130,9 +131,9 @@ private void setDashboardSections(DashboardRequest dashboardRequest, Dashboard d
section.setDisplayOrder(sectionContract.getDisplayOrder());
section = dashboardSectionRepository.save(section);

List<CardContract> cardContracts = sectionContract.getCards();
List<ReportCardContract> cardContracts = sectionContract.getCards();
Set<DashboardSectionCardMapping> updatedMappings = new HashSet<>();
for (CardContract cardContract : cardContracts) {
for (ReportCardContract cardContract : cardContracts) {
DashboardSectionCardMapping mapping = dashboardSectionCardMappingRepository.findByCardIdAndDashboardSectionAndIsVoidedFalse(cardContract.getId(), section);
if (mapping == null) {
mapping = new DashboardSectionCardMapping();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -550,7 +551,7 @@ public void addReportCardIcons(ZipOutputStream zos) throws IOException {
}

public void addReportCards(ZipOutputStream zos) throws IOException {
List<CardContract> cardContracts = cardService.getAll().stream().map(reportCardMapper::fromEntity).collect(Collectors.toList());
List<ReportCardContract> cardContracts = cardService.getAll().stream().map(reportCardMapper::fromEntity).collect(Collectors.toList());
if (!cardContracts.isEmpty()) {
addFileToZip(zos, "reportCard.json", cardContracts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.avni.server.mapper.dashboard.ReportCardMapper;
import org.avni.server.service.CardService;
import org.avni.server.service.accessControl.AccessControlService;
import org.avni.server.web.request.CardContract;
import org.avni.server.web.contract.ReportCardContract;
import org.avni.server.web.request.ReportCardRequest;
import org.avni.server.web.response.ReportCardResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -33,15 +35,15 @@ public ReportCardController(CardRepository cardRepository, CardService cardServi

@GetMapping(value = "/web/reportCard")
@ResponseBody
public List<CardContract> getAll() {
public List<ReportCardContract> getAll() {
return cardRepository.findAllByIsVoidedFalseOrderByName()
.stream().map(reportCardMapper::fromEntity)
.collect(Collectors.toList());
}

@GetMapping(value = "/web/reportCard/{id}")
@ResponseBody
public ResponseEntity<CardContract> getById(@PathVariable Long id) {
public ResponseEntity<ReportCardResponse> getById(@PathVariable Long id) {
Optional<ReportCard> card = cardRepository.findById(id);
return card.map(c -> ResponseEntity.ok(reportCardMapper.fromEntity(c)))
.orElseGet(() -> ResponseEntity.notFound().build());
Expand All @@ -50,22 +52,22 @@ public ResponseEntity<CardContract> getById(@PathVariable Long id) {
@PostMapping(value = "/web/reportCard")
@ResponseBody
@Transactional
public ResponseEntity<CardContract> newCard(@RequestBody CardContract cardContract) {
public ResponseEntity<ReportCardContract> newCard(@RequestBody ReportCardRequest cardRequest) {
accessControlService.checkPrivilege(PrivilegeType.EditOfflineDashboardAndReportCard);
ReportCard card = cardService.saveCard(cardContract);
ReportCard card = cardService.saveCard(cardRequest);
return ResponseEntity.ok(reportCardMapper.fromEntity(card));
}

@PutMapping(value = "/web/reportCard/{id}")
@ResponseBody
@Transactional
public ResponseEntity<CardContract> editCard(@PathVariable Long id, @RequestBody CardContract cardContract) {
public ResponseEntity<ReportCardContract> editCard(@PathVariable Long id, @RequestBody ReportCardRequest request) {
accessControlService.checkPrivilege(PrivilegeType.EditOfflineDashboardAndReportCard);
Optional<ReportCard> card = cardRepository.findById(id);
if (!card.isPresent()) {
return ResponseEntity.notFound().build();
}
ReportCard savedCard = cardService.editCard(cardContract, id);
ReportCard savedCard = cardService.editCard(request, id);
return ResponseEntity.ok(reportCardMapper.fromEntity(savedCard));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.avni.server.web.contract;

import org.avni.server.web.request.CHSRequest;

public class ReportCardContract extends CHSRequest {
private String name;
private String query;
private String description;
private String color;
private Double displayOrder;
private String iconFileS3Key;
private boolean nested;
private int count;

public Double getDisplayOrder() {
return displayOrder;
}

public void setDisplayOrder(Double displayOrder) {
this.displayOrder = displayOrder;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getQuery() {
return query;
}

public void setQuery(String query) {
this.query = query;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public String getIconFileS3Key() {
return iconFileS3Key;
}

public void setIconFileS3Key(String iconFileS3Key) {
this.iconFileS3Key = iconFileS3Key;
}

public boolean isNested() {
return nested;
}

public void setNested(boolean nested) {
this.nested = nested;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package org.avni.server.web.request;

import org.avni.server.domain.DashboardSection;
import org.avni.server.web.contract.ReportCardContract;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DashboardSectionContract extends CHSRequest {
private String name;
private String description;
private String viewType;
private Double displayOrder;
private String dashboardUUID;
private List<CardContract> cards = new ArrayList<>();
private List<ReportCardContract> cards = new ArrayList<>();
private List<DashboardSectionCardMappingContract> dashboardSectionCardMappings = new ArrayList<>();

public String getName() {
Expand All @@ -31,11 +30,11 @@ public void setDescription(String description) {
this.description = description;
}

public List<CardContract> getCards() {
public List<ReportCardContract> getCards() {
return cards;
}

public void setCards(List<CardContract> cards) {
public void setCards(List<ReportCardContract> cards) {
this.cards = cards;
}

Expand Down
Loading

0 comments on commit 0973435

Please sign in to comment.