1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.openehealth.ipf.boot.fhir;
18
19 import ca.uhn.fhir.context.FhirContext;
20 import ca.uhn.fhir.parser.IParser;
21 import ca.uhn.fhir.rest.api.server.IBundleProvider;
22 import ca.uhn.fhir.rest.server.IPagingProvider;
23 import ca.uhn.fhir.rest.server.SimpleBundleProvider;
24 import lombok.Getter;
25 import lombok.Setter;
26 import org.springframework.cache.Cache;
27 import org.springframework.cache.CacheManager;
28
29 import java.util.List;
30 import java.util.UUID;
31 import java.util.stream.Collectors;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class CachingPagingProvider implements IPagingProvider {
51
52 private static final String PAGING_CACHE = "fhirPagingCache";
53 private Cache cache;
54
55 @Getter
56 @Setter
57 private int defaultPageSize = 50;
58
59 @Getter
60 @Setter
61 private int maximumPageSize = 100;
62
63 @Getter
64 @Setter
65 private boolean distributed;
66
67
68 private FhirContext fhirContext;
69
70 public CachingPagingProvider(CacheManager cacheManager, FhirContext fhirContext) {
71 this.cache = cacheManager.getCache(PAGING_CACHE);
72 this.fhirContext = fhirContext;
73 }
74
75 @Override
76 public String storeResultList(IBundleProvider bundleProvider) {
77 String key = UUID.randomUUID().toString();
78 cache.put(key, distributed ?
79 serialize(bundleProvider) :
80 bundleProvider);
81 return key;
82 }
83
84 @Override
85 public IBundleProvider retrieveResultList(String id) {
86 return distributed ?
87 deserialize(cache.get(id, List.class)) :
88 cache.get(id, IBundleProvider.class);
89 }
90
91
92 private List<String> serialize(IBundleProvider bundleProvider) {
93 IParser parser = fhirContext.newJsonParser();
94 return bundleProvider.getResources(0, Integer.MAX_VALUE).stream()
95 .map(parser::encodeResourceToString)
96 .collect(Collectors.toList());
97 }
98
99 private IBundleProvider deserialize(List<String> list) {
100 IParser parser = fhirContext.newJsonParser();
101 return new SimpleBundleProvider(list.stream()
102 .map(parser::parseResource)
103 .collect(Collectors.toList()));
104 }
105
106 }