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.fhir.tokenization;
011
012import ca.cdr.api.model.json.TokenizationRuleJson;
013import ca.uhn.fhir.context.FhirContext;
014import ca.uhn.fhir.model.api.IQueryParameterType;
015import jakarta.annotation.Nonnull;
016import jakarta.annotation.Nullable;
017import org.hl7.fhir.instance.model.api.IBase;
018
019public class TokenizationRequest {
020
021        @Nonnull
022        private final TokenizationRuleJson myRule;
023
024        @Nullable
025        private final IBase mySourceElement;
026
027        @Nullable
028        private final IBase myObjectElement;
029
030        @Nullable
031        private final IQueryParameterType myObjectQueryParameter;
032
033        @Nonnull
034        private final FhirContext myFhirContext;
035
036        private final boolean myNonNormalizedCopy;
037        private final String myObjectAsString;
038
039        /**
040         * Constructor
041         */
042        public TokenizationRequest(
043                        @Nonnull TokenizationRuleJson theRule,
044                        @Nullable IBase theSourceElement,
045                        @Nonnull IBase theObject,
046                        boolean theNonNormalizedCopy,
047                        @Nonnull FhirContext theFhirContext) {
048                myRule = theRule.clone(); // Clone so that user code can't modify the rule
049                mySourceElement = theSourceElement;
050                myObjectElement = theObject;
051                myNonNormalizedCopy = theNonNormalizedCopy;
052                myFhirContext = theFhirContext;
053                myObjectQueryParameter = null;
054                myObjectAsString = myFhirContext.newJsonParser().encodeToString(theObject);
055        }
056
057        /**
058         * Constructor
059         */
060        public TokenizationRequest(
061                        @Nonnull TokenizationRuleJson theRule,
062                        @Nullable IBase theSourceElement,
063                        @Nonnull IQueryParameterType theObjectQueryParameter,
064                        @Nonnull FhirContext theFhirContext) {
065                myRule = theRule.clone(); // Clone so that user code can't modify the rule
066                mySourceElement = theSourceElement;
067                myObjectElement = null;
068                myObjectQueryParameter = theObjectQueryParameter;
069                myNonNormalizedCopy = false;
070                myFhirContext = theFhirContext;
071                myObjectAsString = theObjectQueryParameter.getValueAsQueryToken(myFhirContext);
072        }
073
074        /**
075         * Returns the FHIR element which was the source of the tokenization request. This method
076         * may return {@literal null} in some cases, such as when a search parameter is being
077         * tokenized.
078         */
079        @Nullable
080        public IBase getSourceElement() {
081                return mySourceElement;
082        }
083
084        /**
085         * If the rule specified a {@link TokenizationRuleJson#getSearchValueNormalization()} algorithm,
086         * then it will result in two tokenization requests. One for the normalized value, and one for the
087         * original (non-normalized) version. This method will return {@literal true} if this is such a
088         * non-normalized version.
089         */
090        public boolean isNonNormalizedCopy() {
091                return myNonNormalizedCopy;
092        }
093
094        /**
095         * This is the rule which resulted in this request. Providers should not modify this object.
096         */
097        @Nonnull
098        public TokenizationRuleJson getRule() {
099                // Note that myRule contains a clone of the original object so any
100                // modifications made by the client won't affect the actual rule
101                // instance
102                return myRule;
103        }
104
105        /**
106         * Returns the underlying HAPI FHIR object to be tokenized. If this method
107         * returns a value, {@link #getObjectQueryParameter()} will return {@literal null}
108         * and vice versa.
109         */
110        @Nullable
111        public IBase getObjectElement() {
112                return myObjectElement;
113        }
114
115        /**
116         * Returns the underlying HAPI FHIR query parameter object to be tokenized. If this method
117         * returns a value, {@link #getObjectElement()} will return {@literal null}
118         * and vice versa.
119         */
120        @Nullable
121        public IQueryParameterType getObjectQueryParameter() {
122                return myObjectQueryParameter;
123        }
124
125        /**
126         * Returns the underlying HAPI FHIR object to be tokenized, converted
127         * into a string
128         */
129        public String getObjectAsString() {
130                return myObjectAsString;
131        }
132
133        @Override
134        public String toString() {
135                return getObjectAsString();
136        }
137}