View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.boot.hl7v3;
17  
18  
19  import org.openehealth.ipf.commons.ihe.hl7v3.storage.Hl7v3ContinuationStorage;
20  import org.springframework.cache.Cache;
21  import org.springframework.cache.CacheManager;
22  
23  /**
24   * Hl7v3ContinuationStorage that uses a Spring cache abstraction
25   *
26   * @author Christian Ohr
27   * @since 3.2
28   */
29  public class CachingHl7v3ContinuationStorage implements Hl7v3ContinuationStorage {
30  
31      private static final String INTERACTIVE_CONTINUATION_CACHE = "interactiveHl7v3ContinuationCache";
32  
33      private final Cache cache;
34  
35      private static final String MESSAGE_SUFFIX = ".message";
36      private static final String LAST_RESULT_NUMBER_SUFFIX = ".lastIndex";
37      private static final String CONTINUATION_QUANTITY_SUFFIX = ".quantity";
38  
39      CachingHl7v3ContinuationStorage(CacheManager cacheManager) {
40          this.cache = cacheManager.getCache(INTERACTIVE_CONTINUATION_CACHE);
41      }
42  
43      @Override
44      public void storeMessage(String key, String message) {
45          cache.put(key + MESSAGE_SUFFIX, message);
46      }
47  
48      @Override
49      public String getMessage(String key) {
50          return cache.get(key + MESSAGE_SUFFIX, String.class);
51      }
52  
53      @Override
54      public void storeLastResultNumber(String key, int lastResultNumber) {
55          cache.put(key + LAST_RESULT_NUMBER_SUFFIX, lastResultNumber);
56      }
57  
58      @Override
59      public int getLastResultNumber(String key) {
60          Integer i = cache.get(key + LAST_RESULT_NUMBER_SUFFIX, Integer.class);
61          return (i != null) ? i : -1;
62      }
63  
64      @Override
65      public void storeContinuationQuantity(String key, int continuationQuantity) {
66          cache.put(key + CONTINUATION_QUANTITY_SUFFIX, continuationQuantity);
67      }
68  
69      @Override
70      public int getContinuationQuantity(String key) {
71          Integer i = cache.get(key + CONTINUATION_QUANTITY_SUFFIX, Integer.class);
72          return (i != null) ? i : -1;
73      }
74  
75      @Override
76      public boolean remove(String key) {
77          cache.evict(key + LAST_RESULT_NUMBER_SUFFIX);
78          cache.evict(key + CONTINUATION_QUANTITY_SUFFIX);
79          if (cache.get(key + MESSAGE_SUFFIX) != null) {
80              cache.evict(key + MESSAGE_SUFFIX);
81              return true;
82          }
83          return false;
84      }
85  
86  }
87