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