|
| 1 | +/* |
| 2 | + * SORMAS® - Surveillance Outbreak Response Management & Analysis System |
| 3 | + * Copyright © 2016-2024 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI) |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | + |
| 16 | +package de.symeda.sormas.api.symptoms; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.LinkedHashSet; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import de.symeda.sormas.api.Disease; |
| 24 | +import de.symeda.sormas.api.i18n.I18nProperties; |
| 25 | +import de.symeda.sormas.api.utils.Diseases; |
| 26 | + |
| 27 | +public enum InfectionSite { |
| 28 | + |
| 29 | + NOT_APPLICABLE(false, true), |
| 30 | + @Diseases(value = { |
| 31 | + Disease.TUBERCULOSIS }) |
| 32 | + BONES_JOINTS_OTHER_THAN_VERTEBRAE(true, true), |
| 33 | + @Diseases(value = { |
| 34 | + Disease.TUBERCULOSIS }) |
| 35 | + CNS_EXCEPT_MENINGES(true, true), |
| 36 | + @Diseases(value = { |
| 37 | + Disease.TUBERCULOSIS }) |
| 38 | + DISSEMINATED_FORM(true, true), |
| 39 | + @Diseases(value = { |
| 40 | + Disease.TUBERCULOSIS }) |
| 41 | + EXTRAPULMONARY_UNKNOWN_SITE(true, true), |
| 42 | + @Diseases(value = { |
| 43 | + Disease.TUBERCULOSIS }) |
| 44 | + EXTRA_THORACIC_LYMPH_NODES(true, true), |
| 45 | + @Diseases(value = { |
| 46 | + Disease.TUBERCULOSIS }) |
| 47 | + GENITO_URINARY(true, true), |
| 48 | + @Diseases(value = { |
| 49 | + Disease.TUBERCULOSIS }) |
| 50 | + INTRATHORACIC_LYMPH_NODES(true, true), |
| 51 | + @Diseases(value = { |
| 52 | + Disease.TUBERCULOSIS }) |
| 53 | + LUNG(true, true), |
| 54 | + @Diseases(value = { |
| 55 | + Disease.TUBERCULOSIS }) |
| 56 | + MENINGES(true, true), |
| 57 | + @Diseases(value = { |
| 58 | + Disease.TUBERCULOSIS }) |
| 59 | + PERITONEUM_DIGESTIVE_TRACT(true, true), |
| 60 | + @Diseases(value = { |
| 61 | + Disease.TUBERCULOSIS }) |
| 62 | + PLEURA(true, true), |
| 63 | + @Diseases(value = { |
| 64 | + Disease.TUBERCULOSIS }) |
| 65 | + UROGENITAL_SYSTEM(true, true), |
| 66 | + @Diseases(value = { |
| 67 | + Disease.TUBERCULOSIS }) |
| 68 | + VERTEBRAE(true, true), |
| 69 | + UNKNOWN(true, true), |
| 70 | + OTHER(true, true); |
| 71 | + |
| 72 | + private final boolean major; |
| 73 | + private final boolean minor; |
| 74 | + |
| 75 | + InfectionSite(boolean major, boolean minor) { |
| 76 | + this.major = major; |
| 77 | + this.minor = minor; |
| 78 | + } |
| 79 | + |
| 80 | + public boolean isMajor() { |
| 81 | + return major; |
| 82 | + } |
| 83 | + |
| 84 | + public boolean isMinor() { |
| 85 | + return minor; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String toString() { |
| 90 | + return I18nProperties.getEnumCaption(this); |
| 91 | + } |
| 92 | + |
| 93 | + public static Set<InfectionSite> filter(Disease disease, boolean includeMajor, boolean includeMinor) { |
| 94 | + return Arrays.stream(values()).filter(site -> { |
| 95 | + // Check if the site is applicable for the disease |
| 96 | + boolean diseaseMatches = isDiseaseApplicable(site, disease); |
| 97 | + |
| 98 | + // Check if the site matches the major/minor criteria |
| 99 | + boolean criteriaMatches = (includeMajor && site.isMajor()) || (includeMinor && site.isMinor()); |
| 100 | + |
| 101 | + return diseaseMatches && criteriaMatches; |
| 102 | + }).collect(Collectors.toCollection(LinkedHashSet::new)); |
| 103 | + } |
| 104 | + |
| 105 | + private static boolean matchesCriteria(InfectionSite site, boolean includeMajor, boolean includeMinor) { |
| 106 | + if (includeMajor && includeMinor) { |
| 107 | + return site.isMajor() || site.isMinor(); // Include if it's either major or minor |
| 108 | + } else if (includeMajor) { |
| 109 | + return site.isMajor(); // Include only if it's major |
| 110 | + } else if (includeMinor) { |
| 111 | + return site.isMinor(); // Include only if it's minor |
| 112 | + } else { |
| 113 | + return false; // Include nothing if both are false |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static boolean isDiseaseApplicable(InfectionSite site, Disease disease) { |
| 118 | + try { |
| 119 | + // Get the @Diseases annotation from the enum value |
| 120 | + Diseases diseasesAnnotation = site.getClass().getField(site.name()).getAnnotation(Diseases.class); |
| 121 | + |
| 122 | + if (diseasesAnnotation == null) { |
| 123 | + // If no annotation, it's applicable to all diseases |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + // Check if the disease is in the annotation's disease list |
| 128 | + return Arrays.asList(diseasesAnnotation.value()).contains(disease); |
| 129 | + } catch (NoSuchFieldException e) { |
| 130 | + // If we can't access the field, assume it's applicable |
| 131 | + return true; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments