001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.model.base.composite;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.model.api.BaseIdentifiableElement;
025import ca.uhn.fhir.model.api.ICompositeDatatype;
026import ca.uhn.fhir.model.api.IQueryParameterType;
027import ca.uhn.fhir.model.primitive.StringDt;
028import ca.uhn.fhir.model.primitive.UriDt;
029import ca.uhn.fhir.rest.param.ParameterUtil;
030import ca.uhn.fhir.rest.param.StringParam;
031import org.apache.commons.lang3.StringUtils;
032
033public abstract class BaseIdentifierDt extends BaseIdentifiableElement
034                implements ICompositeDatatype, IQueryParameterType {
035
036        private static final long serialVersionUID = 4400972469749953077L;
037
038        @Override
039        public String getQueryParameterQualifier() {
040                return null;
041        }
042
043        /**
044         * Gets the value(s) for <b>system</b> (The namespace for the identifier). creating it if it does not exist. Will not return <code>null</code>.
045         *
046         * <p>
047         * <b>Definition:</b> Establishes the namespace in which set of possible id values is unique.
048         * </p>
049         */
050        public abstract UriDt getSystemElement();
051
052        /**
053         * Gets the value(s) for <b>value</b> (The value that is unique). creating it if it does not exist. Will not return <code>null</code>.
054         *
055         * <p>
056         * <b>Definition:</b> The portion of the identifier typically displayed to the user and which is unique within the context of the system.
057         * </p>
058         */
059        public abstract StringDt getValueElement();
060
061        /**
062         * {@inheritDoc}
063         */
064        @Override
065        public String getValueAsQueryToken(FhirContext theContext) {
066                UriDt system = getSystemElement();
067                StringDt value = getValueElement();
068                if (system.getValueAsString() != null) {
069                        return ParameterUtil.escape(StringUtils.defaultString(system.getValueAsString()))
070                                        + '|'
071                                        + ParameterUtil.escape(value.getValueAsString());
072                }
073                return ParameterUtil.escape(value.getValueAsString());
074        }
075
076        /**
077         * Returns true if <code>this</code> identifier has the same {@link #getValueElement() value} and
078         * {@link #getSystemElement() system} (as compared by simple equals comparison). Does not compare other values (e.g.
079         * getUse()) or any extensions.
080         */
081        public boolean matchesSystemAndValue(BaseIdentifierDt theIdentifier) {
082                if (theIdentifier == null) {
083                        return false;
084                }
085                return getValueElement().equals(theIdentifier.getValueElement())
086                                && getSystemElement().equals(theIdentifier.getSystemElement());
087        }
088
089        /**
090         * Sets the value for <b>system</b> (The namespace for the identifier)
091         *
092         * <p>
093         * <b>Definition:</b> Establishes the namespace in which set of possible id values is unique.
094         * </p>
095         */
096        public abstract BaseIdentifierDt setSystem(String theUri);
097
098        /**
099         * Sets the value for <b>value</b> (The value that is unique)
100         *
101         * <p>
102         * <b>Definition:</b> The portion of the identifier typically displayed to the user and which is unique within the context of the system.
103         * </p>
104         */
105        public abstract BaseIdentifierDt setValue(String theString);
106
107        /**
108         * {@inheritDoc}
109         */
110        @Override
111        public void setValueAsQueryToken(
112                        FhirContext theContext, String theParamName, String theQualifier, String theParameter) {
113                int barIndex = ParameterUtil.nonEscapedIndexOf(theParameter, '|');
114                if (barIndex != -1) {
115                        setSystem(theParameter.substring(0, barIndex));
116                        setValue(ParameterUtil.unescape(theParameter.substring(barIndex + 1)));
117                } else {
118                        setValue(ParameterUtil.unescape(theParameter));
119                }
120        }
121
122        /**
123         * <b>Not supported!</b>
124         *
125         * @deprecated get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you
126         * need this functionality
127         */
128        @Deprecated
129        @Override
130        public Boolean getMissing() {
131                return null;
132        }
133
134        /**
135         * <b>Not supported!</b>
136         *
137         * @deprecated get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you
138         * need this functionality
139         */
140        @Deprecated
141        @Override
142        public IQueryParameterType setMissing(Boolean theMissing) {
143                throw new UnsupportedOperationException(
144                                Msg.code(1907)
145                                                + "get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you need this functionality");
146        }
147}