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