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