001package ca.uhn.fhir.jpa.dao;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 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.jpa.api.dao.IFhirResourceDaoSearchParameter;
025import ca.uhn.fhir.jpa.dao.r4.FhirResourceDaoSearchParameterR4;
026import ca.uhn.fhir.jpa.model.entity.ResourceTable;
027import ca.uhn.fhir.jpa.searchparam.extractor.ISearchParamExtractor;
028import ca.uhn.fhir.model.dstu2.resource.SearchParameter;
029import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_40;
030import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_40;
031import org.springframework.beans.factory.annotation.Autowired;
032
033import static org.apache.commons.lang3.StringUtils.isBlank;
034import static org.apache.commons.lang3.StringUtils.isNotBlank;
035
036public class FhirResourceDaoSearchParameterDstu2 extends BaseHapiFhirResourceDao<SearchParameter> implements IFhirResourceDaoSearchParameter<SearchParameter> {
037
038        @Autowired
039        private ISearchParamExtractor mySearchParamExtractor;
040        private FhirContext myDstu2Hl7OrgContext = FhirContext.forDstu2Hl7Org();
041
042        protected void markAffectedResources(SearchParameter theResource) {
043                Boolean reindex = theResource != null ? CURRENTLY_REINDEXING.get(theResource) : null;
044                String expression = theResource != null ? theResource.getXpath() : null;
045                markResourcesMatchingExpressionAsNeedingReindexing(reindex, expression);
046        }
047
048        @Override
049        protected void postPersist(ResourceTable theEntity, SearchParameter theResource) {
050                super.postPersist(theEntity, theResource);
051                markAffectedResources(theResource);
052
053        }
054
055        @Override
056        protected void postUpdate(ResourceTable theEntity, SearchParameter theResource) {
057                super.postUpdate(theEntity, theResource);
058                markAffectedResources(theResource);
059        }
060
061        @Override
062        protected void preDelete(SearchParameter theResourceToDelete, ResourceTable theEntityToDelete) {
063                super.preDelete(theResourceToDelete, theEntityToDelete);
064                markAffectedResources(theResourceToDelete);
065        }
066
067        @Override
068        protected void validateResourceForStorage(SearchParameter theResource, ResourceTable theEntityToSave) {
069                super.validateResourceForStorage(theResource, theEntityToSave);
070
071                String encoded = getContext().newJsonParser().encodeResourceToString(theResource);
072                org.hl7.fhir.dstu2.model.SearchParameter hl7Org = myDstu2Hl7OrgContext.newJsonParser().parseResource(org.hl7.fhir.dstu2.model.SearchParameter.class, encoded);
073
074                org.hl7.fhir.r4.model.SearchParameter convertedSp = (org.hl7.fhir.r4.model.SearchParameter) VersionConvertorFactory_10_40.convertResource(hl7Org, new BaseAdvisor_10_40(false));
075                if (isBlank(convertedSp.getExpression()) && isNotBlank(hl7Org.getXpath())) {
076                        convertedSp.setExpression(hl7Org.getXpath());
077                }
078
079                FhirResourceDaoSearchParameterR4.validateSearchParam(convertedSp, getContext(), getConfig(), mySearchParamRegistry, mySearchParamExtractor);
080
081        }
082
083
084}