001/*
002 * #%L
003 * HAPI FHIR Structures - DSTU2 (FHIR v1.0.0)
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.rest.server.provider.dstu2;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.context.api.BundleInclusionRule;
024import ca.uhn.fhir.model.api.IResource;
025import ca.uhn.fhir.model.api.Include;
026import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
027import ca.uhn.fhir.model.dstu2.resource.Bundle;
028import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
029import ca.uhn.fhir.model.dstu2.resource.Bundle.Link;
030import ca.uhn.fhir.model.dstu2.valueset.SearchEntryModeEnum;
031import ca.uhn.fhir.model.primitive.IdDt;
032import ca.uhn.fhir.model.primitive.InstantDt;
033import ca.uhn.fhir.model.valueset.BundleEntrySearchModeEnum;
034import ca.uhn.fhir.model.valueset.BundleEntryTransactionMethodEnum;
035import ca.uhn.fhir.model.valueset.BundleTypeEnum;
036import ca.uhn.fhir.rest.api.BundleLinks;
037import ca.uhn.fhir.rest.api.Constants;
038import ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory;
039import ca.uhn.fhir.util.ResourceReferenceInfo;
040import jakarta.annotation.Nonnull;
041import org.hl7.fhir.instance.model.api.IBaseResource;
042import org.hl7.fhir.instance.model.api.IPrimitiveType;
043
044import java.math.BigDecimal;
045import java.util.ArrayList;
046import java.util.Date;
047import java.util.HashSet;
048import java.util.List;
049import java.util.Set;
050import java.util.UUID;
051
052import static org.apache.commons.lang3.StringUtils.isNotBlank;
053
054public class Dstu2BundleFactory implements IVersionSpecificBundleFactory {
055        private String myBase;
056        private Bundle myBundle;
057        private FhirContext myContext;
058
059        public Dstu2BundleFactory(FhirContext theContext) {
060                myContext = theContext;
061        }
062
063        @Override
064        public void addResourcesToBundle(
065                        List<IBaseResource> theResult,
066                        BundleTypeEnum theBundleType,
067                        String theServerBase,
068                        BundleInclusionRule theBundleInclusionRule,
069                        Set<Include> theIncludes) {
070                ensureBundle();
071
072                List<IResource> includedResources = new ArrayList<IResource>();
073                Set<IdDt> addedResourceIds = new HashSet<IdDt>();
074
075                for (IBaseResource next : theResult) {
076                        if (next.getIdElement().isEmpty() == false) {
077                                addedResourceIds.add((IdDt) next.getIdElement());
078                        }
079                }
080
081                for (IBaseResource nextBaseRes : theResult) {
082                        IResource next = (IResource) nextBaseRes;
083
084                        Set<String> containedIds = new HashSet<String>();
085                        for (IResource nextContained : next.getContained().getContainedResources()) {
086                                if (nextContained.getId().isEmpty() == false) {
087                                        containedIds.add(nextContained.getId().getValue());
088                                }
089                        }
090
091                        List<ResourceReferenceInfo> references = myContext.newTerser().getAllResourceReferences(next);
092                        do {
093                                List<IResource> addedResourcesThisPass = new ArrayList<IResource>();
094
095                                for (ResourceReferenceInfo nextRefInfo : references) {
096                                        if (theBundleInclusionRule != null
097                                                        && !theBundleInclusionRule.shouldIncludeReferencedResource(nextRefInfo, theIncludes)) {
098                                                continue;
099                                        }
100
101                                        IResource nextRes =
102                                                        (IResource) nextRefInfo.getResourceReference().getResource();
103                                        if (nextRes != null) {
104                                                if (nextRes.getId().hasIdPart()) {
105                                                        if (containedIds.contains(nextRes.getId().getValue())) {
106                                                                // Don't add contained IDs as top level resources
107                                                                continue;
108                                                        }
109
110                                                        IdDt id = nextRes.getId();
111                                                        if (id.hasResourceType() == false) {
112                                                                String resName = myContext.getResourceType(nextRes);
113                                                                id = id.withResourceType(resName);
114                                                        }
115
116                                                        if (!addedResourceIds.contains(id)) {
117                                                                addedResourceIds.add(id);
118                                                                addedResourcesThisPass.add(nextRes);
119                                                        }
120                                                }
121                                        }
122                                }
123
124                                includedResources.addAll(addedResourcesThisPass);
125
126                                // Linked resources may themselves have linked resources
127                                references = new ArrayList<ResourceReferenceInfo>();
128                                for (IResource iResource : addedResourcesThisPass) {
129                                        List<ResourceReferenceInfo> newReferences =
130                                                        myContext.newTerser().getAllResourceReferences(iResource);
131                                        references.addAll(newReferences);
132                                }
133                        } while (references.isEmpty() == false);
134
135                        Entry entry = myBundle.addEntry().setResource(next);
136                        BundleEntryTransactionMethodEnum httpVerb = ResourceMetadataKeyEnum.ENTRY_TRANSACTION_METHOD.get(next);
137                        if (httpVerb != null) {
138                                entry.getRequest().getMethodElement().setValueAsString(httpVerb.getCode());
139                        }
140                        populateBundleEntryFullUrl(next, entry);
141
142                        // Populate Bundle.entry.search
143                        BundleEntrySearchModeEnum searchMode = ResourceMetadataKeyEnum.ENTRY_SEARCH_MODE.get(next);
144                        if (searchMode != null) {
145                                entry.getSearch().getModeElement().setValue(searchMode.getCode());
146                        }
147                        BigDecimal searchScore = ResourceMetadataKeyEnum.ENTRY_SEARCH_SCORE.get(next);
148                        if (searchScore != null) {
149                                entry.getSearch().getScoreElement().setValue(searchScore);
150                        }
151                }
152
153                /*
154                 * Actually add the resources to the bundle
155                 */
156                for (IResource next : includedResources) {
157                        Entry entry = myBundle.addEntry();
158                        entry.setResource(next).getSearch().setMode(SearchEntryModeEnum.INCLUDE);
159                        populateBundleEntryFullUrl(next, entry);
160                }
161        }
162
163        @Override
164        public void addRootPropertiesToBundle(
165                        String theId,
166                        @Nonnull BundleLinks theBundleLinks,
167                        Integer theTotalResults,
168                        IPrimitiveType<Date> theLastUpdated) {
169                ensureBundle();
170
171                myBase = theBundleLinks.serverBase;
172
173                if (myBundle.getIdElement().isEmpty()) {
174                        myBundle.setId(theId);
175                }
176
177                if (ResourceMetadataKeyEnum.UPDATED.get(myBundle) == null) {
178                        ResourceMetadataKeyEnum.UPDATED.put(myBundle, (InstantDt) theLastUpdated);
179                }
180
181                if (!hasLink(Constants.LINK_SELF, myBundle) && isNotBlank(theBundleLinks.getSelf())) {
182                        myBundle.addLink().setRelation(Constants.LINK_SELF).setUrl(theBundleLinks.getSelf());
183                }
184                if (!hasLink(Constants.LINK_NEXT, myBundle) && isNotBlank(theBundleLinks.getNext())) {
185                        myBundle.addLink().setRelation(Constants.LINK_NEXT).setUrl(theBundleLinks.getNext());
186                }
187                if (!hasLink(Constants.LINK_PREVIOUS, myBundle) && isNotBlank(theBundleLinks.getPrev())) {
188                        myBundle.addLink().setRelation(Constants.LINK_PREVIOUS).setUrl(theBundleLinks.getPrev());
189                }
190
191                addTotalResultsToBundle(theTotalResults, theBundleLinks.bundleType);
192        }
193
194        @Override
195        public void addTotalResultsToBundle(Integer theTotalResults, BundleTypeEnum theBundleType) {
196                ensureBundle();
197
198                if (myBundle.getId().isEmpty()) {
199                        myBundle.setId(UUID.randomUUID().toString());
200                }
201
202                if (myBundle.getTypeElement().isEmpty() && theBundleType != null) {
203                        myBundle.getTypeElement().setValueAsString(theBundleType.getCode());
204                }
205
206                if (myBundle.getTotalElement().isEmpty() && theTotalResults != null) {
207                        myBundle.getTotalElement().setValue(theTotalResults);
208                }
209        }
210
211        private void ensureBundle() {
212                if (myBundle == null) {
213                        myBundle = new Bundle();
214                }
215        }
216
217        @Override
218        public IResource getResourceBundle() {
219                return myBundle;
220        }
221
222        private boolean hasLink(String theLinkType, Bundle theBundle) {
223                for (Link next : theBundle.getLink()) {
224                        if (theLinkType.equals(next.getRelation())) {
225                                return true;
226                        }
227                }
228                return false;
229        }
230
231        @Override
232        public void initializeWithBundleResource(IBaseResource theBundle) {
233                myBundle = (Bundle) theBundle;
234        }
235
236        private void populateBundleEntryFullUrl(IResource next, Entry entry) {
237                if (next.getId().hasBaseUrl()) {
238                        entry.setFullUrl(next.getId().toVersionless().getValue());
239                } else {
240                        if (isNotBlank(myBase) && next.getId().hasIdPart()) {
241                                IdDt id = next.getId().toVersionless();
242                                id = id.withServerBase(myBase, myContext.getResourceType(next));
243                                entry.setFullUrl(id.getValue());
244                        }
245                }
246        }
247
248        @Override
249        public List<IBaseResource> toListOfResources() {
250                ArrayList<IBaseResource> retVal = new ArrayList<IBaseResource>();
251                for (Entry next : myBundle.getEntry()) {
252                        if (next.getResource() != null) {
253                                retVal.add(next.getResource());
254                        } else if (next.getResponse().getLocationElement().isEmpty() == false) {
255                                IdDt id = new IdDt(next.getResponse().getLocation());
256                                String resourceType = id.getResourceType();
257                                if (isNotBlank(resourceType)) {
258                                        IResource res = (IResource)
259                                                        myContext.getResourceDefinition(resourceType).newInstance();
260                                        res.setId(id);
261                                        retVal.add(res);
262                                }
263                        }
264                }
265                return retVal;
266        }
267}