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