001package ca.uhn.fhir.mdm.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Master Data Management
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.mdm.model.CanonicalEID;
025import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
026import org.hl7.fhir.instance.model.api.IBase;
027
028public final class IdentifierUtil {
029
030        private IdentifierUtil() {
031        }
032
033        public static CanonicalIdentifier identifierDtFromIdentifier(IBase theIdentifier) {
034                CanonicalIdentifier retval = new CanonicalIdentifier();
035
036                // TODO add other fields like "use" etc
037                if (theIdentifier instanceof org.hl7.fhir.dstu3.model.Identifier) {
038                        org.hl7.fhir.dstu3.model.Identifier ident = (org.hl7.fhir.dstu3.model.Identifier) theIdentifier;
039                        retval.setSystem(ident.getSystem()).setValue(ident.getValue());
040                } else if (theIdentifier instanceof org.hl7.fhir.r4.model.Identifier) {
041                        org.hl7.fhir.r4.model.Identifier ident = (org.hl7.fhir.r4.model.Identifier) theIdentifier;
042                        retval.setSystem(ident.getSystem()).setValue(ident.getValue());
043                } else if (theIdentifier instanceof org.hl7.fhir.r5.model.Identifier) {
044                        org.hl7.fhir.r5.model.Identifier ident = (org.hl7.fhir.r5.model.Identifier) theIdentifier;
045                        retval.setSystem(ident.getSystem()).setValue(ident.getValue());
046                } else {
047                        throw new InternalErrorException("Expected 'Identifier' type but was '" + theIdentifier.getClass().getName() + "'");
048                }
049                return retval;
050        }
051
052
053
054        /**
055         * Retrieves appropriate FHIR Identifier model instance based on the context version
056         *
057         * @param theFhirContext FHIR context to use for determining the identifier version
058         * @param eid EID to get equivalent FHIR Identifier from
059         * @param <T> Generic Identifier base interface
060         * @return Returns appropriate R4 or DSTU3 Identifier instance
061         */
062        public static <T extends IBase> T toId(FhirContext theFhirContext, CanonicalEID eid) {
063                switch (theFhirContext.getVersion().getVersion()) {
064                        case R4:
065                                return (T) eid.toR4();
066                        case DSTU3:
067                                return (T) eid.toDSTU3();
068                }
069                throw new IllegalStateException("Unsupported FHIR version " + theFhirContext.getVersion().getVersion());
070        }
071}