|
| 1 | +package de.medizininformatikinitiative.torch.consent; |
| 2 | + |
| 3 | +import de.medizininformatikinitiative.torch.exceptions.ConsentViolatedException; |
| 4 | +import de.medizininformatikinitiative.torch.model.consent.ConsentProvisions; |
| 5 | +import de.medizininformatikinitiative.torch.model.consent.NonContinuousPeriod; |
| 6 | +import de.medizininformatikinitiative.torch.model.consent.Provision; |
| 7 | +import org.springframework.stereotype.Component; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +@Component |
| 17 | +public class ConsentCalculator { |
| 18 | + |
| 19 | + |
| 20 | + private final ConsentCodeMapper mapper; |
| 21 | + |
| 22 | + ConsentCalculator(ConsentCodeMapper mapper) { |
| 23 | + this.mapper = mapper; |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + /** |
| 28 | + * Calculates the allowed consent periods per code for a single patient. |
| 29 | + * <p> |
| 30 | + * This method processes a list of {@link ConsentProvisions} sorted by date. For each provision: |
| 31 | + * <ul> |
| 32 | + * <li>If the provision is a permit, its period is merged into the existing allowed periods for the code.</li> |
| 33 | + * <li>If the provision is a denial, its period is subtracted from the existing allowed periods for the code.</li> |
| 34 | + * </ul> |
| 35 | + * Only codes relevant to the provided {@code consentKey} (as determined by {@link ConsentCodeMapper}) are considered. |
| 36 | + * |
| 37 | + * @param consentProvisions list of consent provisions for a patient |
| 38 | + * @param consentKey the key identifying the type of consent to calculate |
| 39 | + * @return a map from consent code to {@link NonContinuousPeriod} representing allowed periods for that code |
| 40 | + */ |
| 41 | + Map<String, NonContinuousPeriod> subtractAndMergeByCode( |
| 42 | + List<ConsentProvisions> consentProvisions, |
| 43 | + String consentKey |
| 44 | + ) { |
| 45 | + // Get relevant codes for this consent key |
| 46 | + Set<String> relevantCodes = mapper.getRelevantCodes(consentKey); |
| 47 | + |
| 48 | + // Flatten all provisions, filter relevant codes |
| 49 | + List<Provision> relevantProvisions = consentProvisions.stream() |
| 50 | + .flatMap(cp -> cp.provisions().stream()) |
| 51 | + .filter(p -> relevantCodes.contains(p.code())) |
| 52 | + .toList(); |
| 53 | + |
| 54 | + // Separate permits and denies |
| 55 | + List<Provision> permits = relevantProvisions.stream() |
| 56 | + .filter(Provision::permit) |
| 57 | + .toList(); |
| 58 | + |
| 59 | + List<Provision> denies = relevantProvisions.stream() |
| 60 | + .filter(p -> !p.permit()) |
| 61 | + .toList(); |
| 62 | + |
| 63 | + Map<String, NonContinuousPeriod> result = new HashMap<>(); |
| 64 | + |
| 65 | + // Apply permits first |
| 66 | + for (Provision p : permits) { |
| 67 | + NonContinuousPeriod existing = result.getOrDefault(p.code(), NonContinuousPeriod.of()); |
| 68 | + result.put(p.code(), existing.merge(NonContinuousPeriod.of(p.period()))); |
| 69 | + } |
| 70 | + |
| 71 | + // Apply denials later |
| 72 | + for (Provision p : denies) { |
| 73 | + NonContinuousPeriod existing = result.getOrDefault(p.code(), NonContinuousPeriod.of()); |
| 74 | + result.put(p.code(), existing.substract(p.period())); |
| 75 | + } |
| 76 | + |
| 77 | + return result.keySet().equals(relevantCodes) ? result : Map.of(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the intersection of all provided consent periods by code. |
| 82 | + * <p> |
| 83 | + * This method calculates the overlapping periods across all consent codes. |
| 84 | + * <ul> |
| 85 | + * <li>If {@code consentsByCode} is empty, a {@link ConsentViolatedException} is thrown.</li> |
| 86 | + * <li>If the intersection of all periods is empty, a {@link ConsentViolatedException} is thrown.</li> |
| 87 | + * </ul> |
| 88 | + * |
| 89 | + * @param consentsByCode map from consent code to {@link NonContinuousPeriod} |
| 90 | + * @return a {@link NonContinuousPeriod} representing the intersection of all provided periods |
| 91 | + * @throws ConsentViolatedException if there are no consent periods or if the intersection is empty |
| 92 | + */ |
| 93 | + public NonContinuousPeriod intersectConsent(Map<String, NonContinuousPeriod> consentsByCode) throws ConsentViolatedException { |
| 94 | + if (consentsByCode.isEmpty()) { |
| 95 | + throw new ConsentViolatedException("No consent periods found"); |
| 96 | + } |
| 97 | + |
| 98 | + NonContinuousPeriod result = consentsByCode.values().stream() |
| 99 | + .reduce(NonContinuousPeriod::intersect) |
| 100 | + .orElseThrow(() -> new ConsentViolatedException("No consent periods found")); |
| 101 | + |
| 102 | + if (result.isEmpty()) { |
| 103 | + throw new ConsentViolatedException("Consent periods do not overlap"); |
| 104 | + } |
| 105 | + |
| 106 | + return result; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Calculates the effective consent periods for multiple patients. |
| 111 | + * <p> |
| 112 | + * For each patient in {@code consentsByPatient}: |
| 113 | + * <ul> |
| 114 | + * <li>Calculates allowed periods per code using {@link #subtractAndMergeByCode(List, String)}</li> |
| 115 | + * <li>Computes the intersection of all consent periods for that patient using {@link #intersectConsent(Map)}</li> |
| 116 | + * <li>If a patient has no overlapping consent periods, they are skipped in the result</li> |
| 117 | + * </ul> |
| 118 | + * |
| 119 | + * @param consentKey the key identifying the type of consent to calculate |
| 120 | + * @param consentsByPatient a map from patient ID to a list of their {@link ConsentProvisions} |
| 121 | + * @return a map from patient ID to {@link NonContinuousPeriod} representing their effective consent periods |
| 122 | + */ |
| 123 | + public Map<String, NonContinuousPeriod> calculateConsent( |
| 124 | + String consentKey, |
| 125 | + Map<String, List<ConsentProvisions>> consentsByPatient |
| 126 | + ) { |
| 127 | + return consentsByPatient.entrySet().stream() |
| 128 | + .flatMap(entry -> { |
| 129 | + String patientId = entry.getKey(); |
| 130 | + List<ConsentProvisions> provisions = entry.getValue(); |
| 131 | + |
| 132 | + try { |
| 133 | + NonContinuousPeriod finalConsent = |
| 134 | + intersectConsent(subtractAndMergeByCode(provisions, consentKey)); |
| 135 | + return Stream.of(Map.entry(patientId, finalConsent)); |
| 136 | + } catch (ConsentViolatedException e) { |
| 137 | + // skip patient with empty consent |
| 138 | + return Stream.empty(); |
| 139 | + } |
| 140 | + }) |
| 141 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments