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.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.r4.model.Bundle; 040import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; 041import org.hl7.fhir.r4.model.Bundle.BundleLinkComponent; 042import org.hl7.fhir.r4.model.Bundle.SearchEntryMode; 043import org.hl7.fhir.r4.model.DomainResource; 044import org.hl7.fhir.r4.model.IdType; 045import org.hl7.fhir.r4.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 R4BundleFactory implements IVersionSpecificBundleFactory { 059 private String myBase; 060 private Bundle myBundle; 061 private FhirContext myContext; 062 063 public R4BundleFactory(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(Constants.LINK_SELF).setUrl(theBundleLinks.getSelf()); 203 } 204 if (!hasLink(Constants.LINK_NEXT, myBundle) && isNotBlank(theBundleLinks.getNext())) { 205 myBundle.addLink().setRelation(Constants.LINK_NEXT).setUrl(theBundleLinks.getNext()); 206 } 207 if (!hasLink(Constants.LINK_PREVIOUS, myBundle) && isNotBlank(theBundleLinks.getPrev())) { 208 myBundle.addLink().setRelation(Constants.LINK_PREVIOUS).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 @Override 252 public void initializeWithBundleResource(IBaseResource theBundle) { 253 myBundle = (Bundle) theBundle; 254 } 255 256 private IIdType populateBundleEntryFullUrl(IBaseResource next, BundleEntryComponent entry) { 257 IIdType idElement = null; 258 if (next.getIdElement().hasBaseUrl()) { 259 idElement = next.getIdElement(); 260 entry.setFullUrl(idElement.toVersionless().getValue()); 261 } else { 262 if (isNotBlank(myBase) && next.getIdElement().hasIdPart()) { 263 idElement = next.getIdElement(); 264 idElement = idElement.withServerBase(myBase, myContext.getResourceType(next)); 265 entry.setFullUrl(idElement.toVersionless().getValue()); 266 } 267 } 268 return idElement; 269 } 270 271 @Override 272 public List<IBaseResource> toListOfResources() { 273 ArrayList<IBaseResource> retVal = new ArrayList<IBaseResource>(); 274 for (BundleEntryComponent next : myBundle.getEntry()) { 275 if (next.getResource() != null) { 276 retVal.add(next.getResource()); 277 } else if (next.getResponse().getLocationElement().isEmpty() == false) { 278 IdType id = new IdType(next.getResponse().getLocation()); 279 String resourceType = id.getResourceType(); 280 if (isNotBlank(resourceType)) { 281 IAnyResource res = (IAnyResource) myContext.getResourceDefinition(resourceType).newInstance(); 282 res.setId(id); 283 retVal.add(res); 284 } 285 } 286 } 287 return retVal; 288 } 289 290}