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