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