001package ca.uhn.fhir.rest.server; 002 003/* 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 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 private Integer myCurrentPageOffset; 041 private Integer myCurrentPageSize; 042 043 /** 044 * Constructor 045 */ 046 public SimpleBundleProvider(IBaseResource theResource) { 047 this(Collections.singletonList(theResource)); 048 } 049 050 /** 051 * Create an empty bundle 052 */ 053 public SimpleBundleProvider() { 054 this(Collections.emptyList()); 055 } 056 057 /** 058 * Constructor 059 */ 060 public SimpleBundleProvider(List<? extends IBaseResource> theList) { 061 this(theList, null); 062 } 063 064 public SimpleBundleProvider(List<? extends IBaseResource> theList, String theUuid) { 065 myList = theList; 066 myUuid = theUuid; 067 setSize(theList.size()); 068 } 069 070 /** 071 * Constructor that provides only a size but no actual data (useful for _count = 0) 072 */ 073 public SimpleBundleProvider(int theSize) { 074 myList = Collections.emptyList(); 075 myUuid = null; 076 setSize(theSize); 077 } 078 079 /** 080 * @since 5.5.0 081 */ 082 @Override 083 public Integer getCurrentPageOffset() { 084 return myCurrentPageOffset; 085 } 086 087 /** 088 * @since 5.5.0 089 */ 090 public void setCurrentPageOffset(Integer theCurrentPageOffset) { 091 myCurrentPageOffset = theCurrentPageOffset; 092 } 093 094 /** 095 * @since 5.5.0 096 */ 097 @Override 098 public Integer getCurrentPageSize() { 099 return myCurrentPageSize; 100 } 101 102 /** 103 * @since 5.5.0 104 */ 105 public void setCurrentPageSize(Integer theCurrentPageSize) { 106 myCurrentPageSize = theCurrentPageSize; 107 } 108 109 /** 110 * Returns the results stored in this provider 111 */ 112 protected List<? extends IBaseResource> getList() { 113 return myList; 114 } 115 116 @Override 117 public IPrimitiveType<Date> getPublished() { 118 return myPublished; 119 } 120 121 /** 122 * By default this class uses the object creation date/time (for this object) 123 * to determine {@link #getPublished() the published date} but this 124 * method may be used to specify an alternate date/time 125 */ 126 public void setPublished(IPrimitiveType<Date> thePublished) { 127 myPublished = thePublished; 128 } 129 130 @Nonnull 131 @Override 132 public List<IBaseResource> getResources(int theFromIndex, int theToIndex) { 133 return (List<IBaseResource>) myList.subList(theFromIndex, Math.min(theToIndex, myList.size())); 134 } 135 136 @Override 137 public String getUuid() { 138 return myUuid; 139 } 140 141 /** 142 * Defaults to null 143 */ 144 @Override 145 public Integer preferredPageSize() { 146 return myPreferredPageSize; 147 } 148 149 /** 150 * Sets the preferred page size to be returned by {@link #preferredPageSize()}. 151 * Default is <code>null</code>. 152 */ 153 public void setPreferredPageSize(Integer thePreferredPageSize) { 154 myPreferredPageSize = thePreferredPageSize; 155 } 156 157 /** 158 * Sets the total number of results, if this provider 159 * corresponds to a single page within a larger search result 160 */ 161 public SimpleBundleProvider setSize(Integer theSize) { 162 mySize = theSize; 163 return this; 164 } 165 166 @Override 167 public Integer size() { 168 return mySize; 169 } 170 171}