From a0f24a893d654e74f905c17169f73216150ef6a6 Mon Sep 17 00:00:00 2001 From: himeshr Date: Tue, 23 Jan 2024 13:40:30 +0530 Subject: [PATCH] avniproject/avni-client#1256 | Introduce support for nested reportCards on customDashboard --- .../java/org/avni/server/domain/Card.java | 31 +++++++++++++++++++ .../org/avni/server/service/CardService.java | 6 ++++ .../avni/server/web/request/CardContract.java | 20 ++++++++++++ ...dColumnsNestedAndInitCountToReportCard.sql | 3 ++ 4 files changed, 60 insertions(+) create mode 100644 avni-server-api/src/main/resources/db/migration/V1_329__AddColumnsNestedAndInitCountToReportCard.sql diff --git a/avni-server-api/src/main/java/org/avni/server/domain/Card.java b/avni-server-api/src/main/java/org/avni/server/domain/Card.java index 47dd78cf9..6de0b761d 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/Card.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/Card.java @@ -13,6 +13,8 @@ @JsonIgnoreProperties({"standardReportCardType"}) public class Card extends OrganisationAwareEntity { + public static final int INT_CONSTANT_DEFAULT_COUNT_OF_CARDS = 1; + public static final int INT_CONSTANT_MAX_COUNT_OF_CARDS = 9; @NotNull private String name; @@ -22,6 +24,10 @@ public class Card extends OrganisationAwareEntity { private String colour; + private boolean nested = false; + + private int countOfCards = INT_CONSTANT_DEFAULT_COUNT_OF_CARDS; + @Column(name = "icon_file_s3_key") private String iconFileS3Key; @@ -61,12 +67,37 @@ public void setColour(String colour) { this.colour = colour; } + public boolean isNested() { + return getStandardReportCardType() == null && nested; + } + + public void setNested(boolean nested) { + this.nested = getStandardReportCardType() == null && nested; + } + + public int getCountOfCards() { + return isNested() ? this.countOfCards : INT_CONSTANT_DEFAULT_COUNT_OF_CARDS; + } + + public void setCountOfCards(int countOfCards) { + this.countOfCards = isNested() ? countOfCards : INT_CONSTANT_DEFAULT_COUNT_OF_CARDS; + } + public StandardReportCardType getStandardReportCardType() { return standardReportCardType; } public void setStandardReportCardType(StandardReportCardType standardReportCardType) { + Boolean reportCardTypeChangedFromQueryToStandard = (this.standardReportCardType == null && standardReportCardType != null); this.standardReportCardType = standardReportCardType; + resetNestedCardInfoOnlyApplicableForQueryReportCardType(reportCardTypeChangedFromQueryToStandard); + } + + private void resetNestedCardInfoOnlyApplicableForQueryReportCardType(Boolean reportCardTypeChangedFromQueryToStandard) { + if (reportCardTypeChangedFromQueryToStandard) { + this.setNested(false); + this.setCountOfCards(INT_CONSTANT_DEFAULT_COUNT_OF_CARDS); + } } public Long getStandardReportCardTypeId() { 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 0319f837a..c7f77b4ba 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 @@ -79,6 +79,12 @@ private void buildCard(CardContract cardContract, Card card) { } else { card.setStandardReportCardType(null); } + card.setNested(cardContract.isNested()); + if (cardContract.getCount() < Card.INT_CONSTANT_DEFAULT_COUNT_OF_CARDS || cardContract.getCount() > Card.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", + Card.INT_CONSTANT_DEFAULT_COUNT_OF_CARDS, Card.INT_CONSTANT_MAX_COUNT_OF_CARDS)); + } + card.setCountOfCards(cardContract.getCount()); } private void assertNewNameIsUnique(String newName, String oldName) { diff --git a/avni-server-api/src/main/java/org/avni/server/web/request/CardContract.java b/avni-server-api/src/main/java/org/avni/server/web/request/CardContract.java index fc1fbf10a..c815e4a9c 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/request/CardContract.java +++ b/avni-server-api/src/main/java/org/avni/server/web/request/CardContract.java @@ -11,6 +11,8 @@ public class CardContract extends CHSRequest { private Double displayOrder; private Long standardReportCardTypeId; private String iconFileS3Key; + private boolean nested; + private int count; public static CardContract fromEntity(Card card) { CardContract cardContract = new CardContract(); @@ -23,6 +25,8 @@ public static CardContract fromEntity(Card card) { cardContract.setColor(card.getColour()); cardContract.setStandardReportCardTypeId(card.getStandardReportCardTypeId()); cardContract.setIconFileS3Key(card.getIconFileS3Key()); + cardContract.setNested(card.isNested()); + cardContract.setCount(card.getCountOfCards()); return cardContract; } @@ -87,4 +91,20 @@ public String getIconFileS3Key() { 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; + } } diff --git a/avni-server-api/src/main/resources/db/migration/V1_329__AddColumnsNestedAndInitCountToReportCard.sql b/avni-server-api/src/main/resources/db/migration/V1_329__AddColumnsNestedAndInitCountToReportCard.sql new file mode 100644 index 000000000..516bdb18d --- /dev/null +++ b/avni-server-api/src/main/resources/db/migration/V1_329__AddColumnsNestedAndInitCountToReportCard.sql @@ -0,0 +1,3 @@ +ALTER TABLE public.report_card + ADD COLUMN IF NOT EXISTS nested BOOLEAN DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS init_count_of_cards SMALLINT NOT NULL DEFAULT 1;