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