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