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