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