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.api;
021
022import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion;
023import ca.uhn.fhir.jpa.entity.TermConcept;
024import ca.uhn.fhir.jpa.model.entity.ResourceTable;
025import ca.uhn.fhir.jpa.term.UploadStatistics;
026import ca.uhn.fhir.jpa.term.custom.CustomTerminologySet;
027import ca.uhn.fhir.rest.api.server.RequestDetails;
028import ca.uhn.fhir.rest.api.server.storage.IResourcePersistentId;
029import org.hl7.fhir.instance.model.api.IIdType;
030import org.hl7.fhir.r4.model.CodeSystem;
031import org.hl7.fhir.r4.model.ValueSet;
032import org.springframework.transaction.annotation.Transactional;
033
034import java.util.List;
035
036/**
037 * This service handles writes to the CodeSystem/Concept tables within the terminology services
038 */
039public interface ITermCodeSystemStorageSvc {
040
041        String MAKE_LOADING_VERSION_CURRENT = "make.loading.version.current";
042
043        /**
044         * Defaults to true when parameter is null or entry is not present in requestDetails.myUserData
045         */
046        static boolean isMakeVersionCurrent(RequestDetails theRequestDetails) {
047                return theRequestDetails == null
048                                || (boolean) theRequestDetails.getUserData().getOrDefault(MAKE_LOADING_VERSION_CURRENT, Boolean.TRUE);
049        }
050
051        void storeNewCodeSystemVersion(
052                        IResourcePersistentId theCodeSystemResourcePid,
053                        String theSystemUri,
054                        String theSystemName,
055                        String theSystemVersionId,
056                        TermCodeSystemVersion theCodeSystemVersion,
057                        ResourceTable theCodeSystemResourceTable,
058                        RequestDetails theRequestDetails);
059
060        /**
061         * Default implementation supports previous signature of method which was added RequestDetails parameter
062         */
063        @Transactional
064        default void storeNewCodeSystemVersion(
065                        IResourcePersistentId theCodeSystemResourcePid,
066                        String theSystemUri,
067                        String theSystemName,
068                        String theSystemVersionId,
069                        TermCodeSystemVersion theCodeSystemVersion,
070                        ResourceTable theCodeSystemResourceTable) {
071
072                storeNewCodeSystemVersion(
073                                theCodeSystemResourcePid,
074                                theSystemUri,
075                                theSystemName,
076                                theSystemVersionId,
077                                theCodeSystemVersion,
078                                theCodeSystemResourceTable,
079                                null);
080        }
081
082        /**
083         * @return Returns the ID of the created/updated code system
084         */
085        IIdType storeNewCodeSystemVersion(
086                        org.hl7.fhir.r4.model.CodeSystem theCodeSystemResource,
087                        TermCodeSystemVersion theCodeSystemVersion,
088                        RequestDetails theRequestDetails,
089                        List<ValueSet> theValueSets,
090                        List<org.hl7.fhir.r4.model.ConceptMap> theConceptMaps);
091
092        void storeNewCodeSystemVersionIfNeeded(
093                        CodeSystem theCodeSystem, ResourceTable theResourceEntity, RequestDetails theRequestDetails);
094
095        /**
096         * Default implementation supports previous signature of method which was added RequestDetails parameter
097         */
098        default void storeNewCodeSystemVersionIfNeeded(CodeSystem theCodeSystem, ResourceTable theResourceEntity) {
099                storeNewCodeSystemVersionIfNeeded(theCodeSystem, theResourceEntity, null);
100        }
101
102        UploadStatistics applyDeltaCodeSystemsAdd(String theSystem, CustomTerminologySet theAdditions);
103
104        UploadStatistics applyDeltaCodeSystemsRemove(String theSystem, CustomTerminologySet theRemovals);
105
106        int saveConcept(TermConcept theNextConcept);
107}