001package ca.uhn.fhir.rest.server; 002 003/* 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2019 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.model.primitive.InstantDt; 024import ca.uhn.fhir.rest.api.server.IBundleProvider; 025import org.hl7.fhir.instance.model.api.IBaseResource; 026import org.hl7.fhir.instance.model.api.IPrimitiveType; 027 028import javax.annotation.Nonnull; 029import java.util.Collections; 030import java.util.Date; 031import java.util.List; 032 033public class SimpleBundleProvider implements IBundleProvider { 034 035 private final List<? extends IBaseResource> myList; 036 private final String myUuid; 037 private Integer myPreferredPageSize; 038 private Integer mySize; 039 private IPrimitiveType<Date> myPublished = InstantDt.withCurrentTime(); 040 public SimpleBundleProvider(List<? extends IBaseResource> theList) { 041 this(theList, null); 042 } 043 044 public SimpleBundleProvider(IBaseResource theResource) { 045 this(Collections.singletonList(theResource)); 046 } 047 048 /** 049 * Create an empty bundle 050 */ 051 public SimpleBundleProvider() { 052 this(Collections.emptyList()); 053 } 054 055 public SimpleBundleProvider(List<? extends IBaseResource> theList, String theUuid) { 056 myList = theList; 057 myUuid = theUuid; 058 setSize(theList.size()); 059 } 060 061 /** 062 * Returns the results stored in this provider 063 */ 064 protected List<? extends IBaseResource> getList() { 065 return myList; 066 } 067 068 @Override 069 public IPrimitiveType<Date> getPublished() { 070 return myPublished; 071 } 072 073 /** 074 * By default this class uses the object creation date/time (for this object) 075 * to determine {@link #getPublished() the published date} but this 076 * method may be used to specify an alternate date/time 077 */ 078 public void setPublished(IPrimitiveType<Date> thePublished) { 079 myPublished = thePublished; 080 } 081 082 @Nonnull 083 @Override 084 public List<IBaseResource> getResources(int theFromIndex, int theToIndex) { 085 return (List<IBaseResource>) myList.subList(theFromIndex, Math.min(theToIndex, myList.size())); 086 } 087 088 @Override 089 public String getUuid() { 090 return myUuid; 091 } 092 093 /** 094 * Defaults to null 095 */ 096 @Override 097 public Integer preferredPageSize() { 098 return myPreferredPageSize; 099 } 100 101 /** 102 * Sets the preferred page size to be returned by {@link #preferredPageSize()}. 103 * Default is <code>null</code>. 104 */ 105 public void setPreferredPageSize(Integer thePreferredPageSize) { 106 myPreferredPageSize = thePreferredPageSize; 107 } 108 109 /** 110 * Sets the total number of results, if this provider 111 * corresponds to a single page within a larger search result 112 */ 113 public SimpleBundleProvider setSize(Integer theSize) { 114 mySize = theSize; 115 return this; 116 } 117 118 @Override 119 public Integer size() { 120 return mySize; 121 } 122 123}