001package org.hl7.fhir.dstu3.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.dstu3.model.Bundle;
034import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
035import org.hl7.fhir.dstu3.model.Bundle.BundleLinkComponent;
036import org.hl7.fhir.dstu3.model.Bundle.SearchEntryMode;
037import org.hl7.fhir.dstu3.model.DomainResource;
038import org.hl7.fhir.dstu3.model.IdType;
039import org.hl7.fhir.dstu3.model.Resource;
040import org.hl7.fhir.instance.model.api.IAnyResource;
041import org.hl7.fhir.instance.model.api.IBaseResource;
042import org.hl7.fhir.instance.model.api.IIdType;
043import org.hl7.fhir.instance.model.api.IPrimitiveType;
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
055public class Dstu3BundleFactory implements IVersionSpecificBundleFactory {
056  private String myBase;
057  private Bundle myBundle;
058  private FhirContext myContext;
059
060  public Dstu3BundleFactory(FhirContext theContext) {
061    myContext = theContext;
062  }
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      String httpVerb = ResourceMetadataKeyEnum.ENTRY_TRANSACTION_METHOD.get(nextAsResource);
136      if (httpVerb != null) {
137        entry.getRequest().getMethodElement().setValueAsString(httpVerb);
138        if (id != null) {
139          entry.getRequest().setUrl(id.getValue());
140        }
141      }
142      if ("DELETE".equals(httpVerb)) {
143        entry.setResource(null);
144      }
145
146      // Populate Bundle.entry.response
147      if (theBundleType != null) {
148        switch (theBundleType) {
149          case BATCH_RESPONSE:
150          case TRANSACTION_RESPONSE:
151            if ("1".equals(id.getVersionIdPart())) {
152              entry.getResponse().setStatus("201 Created");
153            } else if (isNotBlank(id.getVersionIdPart())) {
154              entry.getResponse().setStatus("200 OK");
155            }
156            if (isNotBlank(id.getVersionIdPart())) {
157              entry.getResponse().setEtag(RestfulServerUtils.createEtag(id.getVersionIdPart()));
158            }
159            break;
160        }
161      }
162
163      // Populate Bundle.entry.search
164      String searchMode = ResourceMetadataKeyEnum.ENTRY_SEARCH_MODE.get(nextAsResource);
165      if (searchMode != null) {
166        entry.getSearch().getModeElement().setValueAsString(searchMode);
167      }
168
169    }
170
171    /*
172     * Actually add the resources to the bundle
173     */
174    for (IAnyResource next : includedResources) {
175      BundleEntryComponent entry = myBundle.addEntry();
176      entry.setResource((Resource) next).getSearch().setMode(SearchEntryMode.INCLUDE);
177      populateBundleEntryFullUrl(next, entry);
178    }
179
180  }
181
182  @Override
183  public void addRootPropertiesToBundle(String theId, @Nonnull BundleLinks theBundleLinks, Integer theTotalResults,
184                                        IPrimitiveType<Date> theLastUpdated) {
185    ensureBundle();
186
187    myBase = theBundleLinks.serverBase;
188
189    if (myBundle.getIdElement().isEmpty()) {
190      myBundle.setId(theId);
191    }
192
193    if (myBundle.getMeta().getLastUpdated() == null && theLastUpdated != null) {
194      myBundle.getMeta().getLastUpdatedElement().setValueAsString(theLastUpdated.getValueAsString());
195    }
196
197    if (!hasLink(Constants.LINK_SELF, myBundle) && isNotBlank(theBundleLinks.getSelf())) {
198      myBundle.addLink().setRelation(Constants.LINK_SELF).setUrl(theBundleLinks.getSelf());
199    }
200    if (!hasLink(Constants.LINK_NEXT, myBundle) && isNotBlank(theBundleLinks.getNext())) {
201      myBundle.addLink().setRelation(Constants.LINK_NEXT).setUrl(theBundleLinks.getNext());
202    }
203    if (!hasLink(Constants.LINK_PREVIOUS, myBundle) && isNotBlank(theBundleLinks.getPrev())) {
204      myBundle.addLink().setRelation(Constants.LINK_PREVIOUS).setUrl(theBundleLinks.getPrev());
205    }
206
207    addTotalResultsToBundle(theTotalResults, theBundleLinks.bundleType);
208  }
209
210  @Override
211  public void addTotalResultsToBundle(Integer theTotalResults, BundleTypeEnum theBundleType) {
212    ensureBundle();
213
214    if (myBundle.getIdElement().isEmpty()) {
215      myBundle.setId(UUID.randomUUID().toString());
216    }
217
218    if (myBundle.getTypeElement().isEmpty() && theBundleType != null) {
219      myBundle.getTypeElement().setValueAsString(theBundleType.getCode());
220    }
221
222    if (myBundle.getTotalElement().isEmpty() && theTotalResults != null) {
223      myBundle.getTotalElement().setValue(theTotalResults);
224    }
225  }
226
227  private void ensureBundle() {
228    if (myBundle == null) {
229      myBundle = new Bundle();
230    }
231  }
232
233  @Override
234  public IBaseResource getResourceBundle() {
235    return myBundle;
236  }
237
238  private boolean hasLink(String theLinkType, Bundle theBundle) {
239    for (BundleLinkComponent next : theBundle.getLink()) {
240      if (theLinkType.equals(next.getRelation())) {
241        return true;
242      }
243    }
244    return false;
245  }
246
247  @Override
248  public void initializeWithBundleResource(IBaseResource theBundle) {
249    myBundle = (Bundle) theBundle;
250  }
251
252  private IIdType populateBundleEntryFullUrl(IBaseResource next, BundleEntryComponent entry) {
253    IIdType idElement = null;
254    if (next.getIdElement().hasBaseUrl()) {
255      idElement = next.getIdElement();
256      entry.setFullUrl(idElement.toVersionless().getValue());
257    } else {
258      if (isNotBlank(myBase) && next.getIdElement().hasIdPart()) {
259        idElement = next.getIdElement();
260        idElement = idElement.withServerBase(myBase, myContext.getResourceType(next));
261        entry.setFullUrl(idElement.toVersionless().getValue());
262      }
263    }
264    return idElement;
265  }
266
267  @Override
268  public List<IBaseResource> toListOfResources() {
269    ArrayList<IBaseResource> retVal = new ArrayList<IBaseResource>();
270    for (BundleEntryComponent next : myBundle.getEntry()) {
271      if (next.getResource() != null) {
272        retVal.add(next.getResource());
273      } else if (next.getResponse().getLocationElement().isEmpty() == false) {
274        IdType id = new IdType(next.getResponse().getLocation());
275        String resourceType = id.getResourceType();
276        if (isNotBlank(resourceType)) {
277          IAnyResource res = (IAnyResource) myContext.getResourceDefinition(resourceType).newInstance();
278          res.setId(id);
279          retVal.add(res);
280        }
281      }
282    }
283    return retVal;
284  }
285
286}