001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2025 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.api.consent;
011
012import ca.cdr.api.fhir.interceptor.CdrPointcut;
013import ca.uhn.fhir.model.primitive.IdDt;
014import jakarta.annotation.Nonnull;
015import jakarta.annotation.Nullable;
016import org.apache.commons.lang3.builder.EqualsBuilder;
017
018import java.util.Objects;
019
020/**
021 * Represents the relevant context information to run Consent resource derived consent logic.
022 * This is also used as a cache-key for reusing consent for the given Patient compartment within a single request.
023 */
024public class ConsentLookupContext {
025
026        @Nonnull
027        private final ConsentEvaluationEventType myConsentEvaluationEventType;
028
029        @Nullable
030        private final IdDt myClinicalPatientId;
031
032        private boolean myCacheable = true;
033
034        public ConsentLookupContext(
035                        @Nonnull ConsentEvaluationEventType theConsentEvaluationEventType, @Nullable IdDt theClinicalPatientId) {
036                myClinicalPatientId = theClinicalPatientId;
037                myConsentEvaluationEventType = theConsentEvaluationEventType;
038        }
039
040        @Nullable
041        public IdDt getClinicalPatientId() {
042                return myClinicalPatientId;
043        }
044
045        @Nonnull
046        public ConsentEvaluationEventType getConsentEvaluationEventType() {
047                return myConsentEvaluationEventType;
048        }
049
050        /**
051         * Whether the {@link ConsentFetchQueries} from the {@link CdrPointcut#CONSENT_FETCH_QUERIES} pointcut and the
052         * {@link ConsentActiveResourceResolutionRequest} from the {@link CdrPointcut#CONSENT_ACTIVE_CONSENT_RESOURCES_RESOLVE} pointcut
053         * should be cached if the clinical patient id and the {@link ConsentEvaluationEventType} are the same for multiple
054         * resources in a given request.
055         * @return true if caching should be used
056         */
057        public boolean isCacheable() {
058                return myCacheable;
059        }
060
061        /**
062         * By default, the {@link ConsentFetchQueries} from the {@link CdrPointcut#CONSENT_FETCH_QUERIES} pointcut and the
063         * {@link ConsentActiveResourceResolutionRequest} from the {@link CdrPointcut#CONSENT_ACTIVE_CONSENT_RESOURCES_RESOLVE} pointcut
064         * will be cached if the clinical patient id and the {@link ConsentEvaluationEventType} are the same for multiple
065         * resources in a given request.
066         */
067        public void setCacheable(boolean theCacheable) {
068                myCacheable = theCacheable;
069        }
070
071        @Override
072        public boolean equals(Object o) {
073                if (this == o) {
074                        return true;
075                }
076
077                if (o == null || getClass() != o.getClass()) {
078                        return false;
079                }
080
081                ConsentLookupContext that = (ConsentLookupContext) o;
082
083                // note: myCacheable is not relevant to being a cache key.  Deliberately skipped.
084                return new EqualsBuilder()
085                                .append(myClinicalPatientId, that.myClinicalPatientId)
086                                .append(myConsentEvaluationEventType, that.myConsentEvaluationEventType)
087                                .isEquals();
088        }
089
090        @Override
091        public int hashCode() {
092                // note: myCacheable is not relevant to being a cache key.  Deliberately skipped.
093                return Objects.hash(myClinicalPatientId, myConsentEvaluationEventType);
094        }
095}