001package ca.uhn.fhir.jpa.provider.r4;
002
003import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoStructureDefinition;
004import ca.uhn.fhir.jpa.model.util.JpaConstants;
005import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
006import ca.uhn.fhir.rest.annotation.IdParam;
007import ca.uhn.fhir.rest.annotation.Operation;
008import ca.uhn.fhir.rest.annotation.OperationParam;
009import ca.uhn.fhir.rest.api.server.IBundleProvider;
010import ca.uhn.fhir.rest.api.server.RequestDetails;
011import ca.uhn.fhir.rest.param.UriParam;
012import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
013import ca.uhn.fhir.util.ValidateUtil;
014import org.hl7.fhir.r4.model.IdType;
015import org.hl7.fhir.r4.model.StringType;
016import org.hl7.fhir.r4.model.StructureDefinition;
017
018/*
019 * #%L
020 * HAPI FHIR JPA Server
021 * %%
022 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
023 * %%
024 * Licensed under the Apache License, Version 2.0 (the "License");
025 * you may not use this file except in compliance with the License.
026 * You may obtain a copy of the License at
027 *
028 *      http://www.apache.org/licenses/LICENSE-2.0
029 *
030 * Unless required by applicable law or agreed to in writing, software
031 * distributed under the License is distributed on an "AS IS" BASIS,
032 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
033 * See the License for the specific language governing permissions and
034 * limitations under the License.
035 * #L%
036 */
037
038public class BaseJpaResourceProviderStructureDefinitionR4 extends JpaResourceProviderR4<StructureDefinition> {
039
040        /**
041         * <code>$snapshot</code> operation
042         */
043        @Operation(name=JpaConstants.OPERATION_SNAPSHOT, idempotent = true)
044        public StructureDefinition snapshot(
045                @IdParam(optional = true) IdType theId,
046                @OperationParam(name = "definition") StructureDefinition theStructureDefinition,
047                @OperationParam(name = "url") StringType theUrl,
048                RequestDetails theRequestDetails) {
049
050                ValidateUtil.exactlyOneNotNullOrThrowInvalidRequestException(
051                        new Object[]{ theId, theStructureDefinition, theUrl },
052                        "Must supply either an ID or a StructureDefinition or a URL (but not more than one of these things)"
053                );
054
055                StructureDefinition sd;
056                if (theId == null && theStructureDefinition != null && theUrl == null) {
057                        sd = theStructureDefinition;
058                } else if (theId != null && theStructureDefinition == null) {
059                        sd = getDao().read(theId, theRequestDetails);
060                } else {
061                        SearchParameterMap map = new SearchParameterMap();
062                        map.setLoadSynchronousUpTo(2);
063                        map.add(StructureDefinition.SP_URL, new UriParam(theUrl.getValue()));
064                        IBundleProvider outcome = getDao().search(map, theRequestDetails);
065                        Integer numResults = outcome.size();
066                        assert numResults != null;
067                        if (numResults == 0) {
068                                throw new ResourceNotFoundException("No StructureDefiniton found with url = '" + theUrl.getValue() + "'");
069                        }
070                        sd = (StructureDefinition) outcome.getResources(0, 1).get(0);
071                }
072
073                return getDao().generateSnapshot(sd, null, null, null);
074        }
075
076        @Override
077        public IFhirResourceDaoStructureDefinition<StructureDefinition> getDao() {
078                return (IFhirResourceDaoStructureDefinition<StructureDefinition>) super.getDao();
079        }
080
081}