From 8ab2c570f5314acab05c8d4eea88adb9971e3496 Mon Sep 17 00:00:00 2001 From: himeshr Date: Wed, 12 Jun 2024 11:08:36 +0530 Subject: [PATCH] avniproject/avni-webapp#1203 | Code changes to perform safe set and get for ReportCard.standardReportCardInput key-values --- .../org/avni/server/domain/ReportCard.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) 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 c604da0aa..771b4a95f 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 @@ -2,12 +2,13 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import org.avni.server.web.request.SubjectTypeContract; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Type; import javax.persistence.*; import javax.validation.constraints.NotNull; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; @Entity @@ -115,26 +116,37 @@ public void setIconFileS3Key(String iconFileS3Key) { } public List getStandardReportCardInputSubjectTypes() { - return standardReportCardInput.getList("subjectTypes"); + return safeGetterForStandardReportCardInput("subjectTypes"); } public void setStandardReportCardInputSubjectTypes(List subjectTypes) { - standardReportCardInput.with("subjectTypes", subjectTypes); + safeSetterForStandardReportCardInput("subjectTypes", subjectTypes); } public List getStandardReportCardInputPrograms() { - return standardReportCardInput.getList("programs"); + return safeGetterForStandardReportCardInput("programs"); } public void setStandardReportCardInputPrograms(List programs) { - standardReportCardInput.with("programs", programs); + safeSetterForStandardReportCardInput("programs", programs); } public List getStandardReportCardInputEncounterTypes() { - return standardReportCardInput.getList("encounterTypes"); + return safeGetterForStandardReportCardInput("encounterTypes"); } public void setStandardReportCardInputEncounterTypes(List encounterTypes) { - standardReportCardInput.with("encounterTypes", encounterTypes); + safeSetterForStandardReportCardInput("encounterTypes", encounterTypes); + } + + private List safeGetterForStandardReportCardInput(String key) { + return standardReportCardInput == null ? new ArrayList() : standardReportCardInput.getList(key); + } + + private void safeSetterForStandardReportCardInput(String key, List value) { + if(standardReportCardInput == null) { + standardReportCardInput = new JsonObject(new HashMap<>()); + } + standardReportCardInput.with(key, value); } }