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