001/*-
002 * #%L
003 * HAPI FHIR JPA Server
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.jpa.term;
021
022import org.apache.commons.lang3.StringUtils;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import java.util.Optional;
027
028import static org.apache.commons.lang3.StringUtils.isBlank;
029import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_ALL_VALUESET_ID;
030import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_GENERIC_VALUESET_URL;
031import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_GENERIC_VALUESET_URL_PLUS_SLASH;
032import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_LOW;
033
034public class TermReadSvcUtil {
035        private static final Logger ourLog = LoggerFactory.getLogger(TermReadSvcUtil.class);
036
037        public static Optional<String> getValueSetId(String theUrl) {
038                if (!theUrl.startsWith(LOINC_GENERIC_VALUESET_URL)) return Optional.empty();
039
040                if (!theUrl.startsWith(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH)) {
041                        if (theUrl.equals(LOINC_GENERIC_VALUESET_URL)) {
042                                // the request is for the loinc all valueset which when loading was given the name: 'loinc-all'
043                                return Optional.of(LOINC_ALL_VALUESET_ID);
044                        }
045
046                        ourLog.error("Don't know how to extract ValueSet's ForcedId from url: " + theUrl);
047                        return Optional.empty();
048                }
049
050                String forcedId = theUrl.substring(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH.length());
051                return isBlank(forcedId) ? Optional.empty() : Optional.of(forcedId);
052        }
053
054        public static boolean isLoincUnversionedValueSet(String theUrl) {
055                boolean isLoincCodeSystem = StringUtils.containsIgnoreCase(theUrl, LOINC_LOW);
056                boolean isNoVersion = !theUrl.contains("|");
057
058                return isLoincCodeSystem && isNoVersion;
059        }
060
061        public static boolean isLoincUnversionedCodeSystem(String theUrl) {
062                boolean isLoincCodeSystem = StringUtils.containsIgnoreCase(theUrl, LOINC_LOW);
063                boolean isNoVersion = !theUrl.contains("|");
064
065                return isLoincCodeSystem && isNoVersion;
066        }
067}