001package org.hl7.fhir.r5.context;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.Comparator;
006import java.util.HashMap;
007import java.util.List;
008import java.util.Map;
009import java.util.Set;
010import java.util.UUID;
011
012import org.hl7.fhir.r5.context.BaseWorkerContext.MetadataResourceVersionComparator;
013import org.hl7.fhir.r5.context.CanonicalResourceManager.CanonicalListSorter;
014import org.hl7.fhir.r5.model.CodeSystem;
015import org.hl7.fhir.r5.model.CanonicalResource;
016import org.hl7.fhir.utilities.VersionUtilities;
017
018/**
019 * This manages a cached list of resources, and provides high speed access by URL / URL+version, and assumes that patch version doesn't matter for access
020 * note, though, that not all resources have semver versions
021 * 
022 * @author graha
023 *
024 */
025
026public class CanonicalResourceManager<T extends CanonicalResource> {
027
028  public class CanonicalListSorter implements Comparator<CanonicalResource> {
029
030    @Override
031    public int compare(CanonicalResource arg0, CanonicalResource arg1) {
032      String u0 = arg0.getUrl();
033      String u1 = arg1.getUrl();
034      return u0.compareTo(u1);
035    }
036  }
037
038  public class MetadataResourceVersionComparator<T extends CanonicalResource> implements Comparator<T> {
039    @Override
040    public int compare(T arg1, T arg2) {
041      String v1 = arg1.getVersion();
042      String v2 = arg2.getVersion();
043      if (v1 == null && v2 == null) {
044        return Integer.compare(list.indexOf(arg1), list.indexOf(arg2)); // retain original order
045      } else if (v1 == null) {
046        return -1;
047      } else if (v2 == null) {
048        return 1;
049      } else {
050        String mm1 = VersionUtilities.getMajMin(v1);
051        String mm2 = VersionUtilities.getMajMin(v2);
052        if (mm1 == null || mm2 == null) {
053          return v1.compareTo(v2);
054        } else {
055          return mm1.compareTo(mm2);
056        }
057      }
058    }
059  }
060
061
062  private boolean enforceUniqueId; 
063  private List<T> list = new ArrayList<>();
064  private Map<String, T> map = new HashMap<>();
065  
066  
067  public CanonicalResourceManager(boolean enforceUniqueId) {
068    super();
069    this.enforceUniqueId = enforceUniqueId;
070  }
071
072  public void copy(CanonicalResourceManager<T> source) {
073    list.clear();
074    map.clear();
075    list.addAll(source.list);
076    map.putAll(source.map);
077  }
078  
079  public void see(T r) {
080    if (!r.hasId()) {
081      r.setId(UUID.randomUUID().toString());
082    }
083    if (enforceUniqueId && map.containsKey(r.getId())) {
084      drop(r.getId());      
085    }
086    list.add(r);
087    map.put(r.getId(), r); // we do this so we can drop by id
088
089    if (r.hasUrl()) {
090      // first, this is the correct reosurce for this version (if it has a version)
091      if (r.hasVersion()) {
092        map.put(r.getUrl()+"|"+r.getVersion(), r);
093      }
094      updateList(r.getUrl(), r.getVersion());
095    }
096  }
097
098  private void updateList(String url, String version) {
099    List<T> rl = new ArrayList<T>();
100    for (T t : list) {
101      if (url.equals(t.getUrl()) && !rl.contains(t)) {
102        rl.add(t);
103      }
104    }
105    if (rl.size() > 0) {
106      // sort by version as much as we are able
107      Collections.sort(rl, new MetadataResourceVersionComparator<T>());
108      // the current is the latest
109      map.put(url, rl.get(rl.size()-1));
110      // now, also, the latest for major/minor
111      if (version != null) {
112        T latest = null;
113        for (T t : rl) {
114          if (VersionUtilities.versionsCompatible(t.getVersion(), version)) {
115            latest = t;
116          }
117        }
118        if (latest != null) { // might be null if it's not using semver
119          String lv = VersionUtilities.getMajMin(latest.getVersion());
120          if (lv != null && !lv.equals(version))
121            map.put(url+"|"+lv, rl.get(rl.size()-1));
122        }
123      }
124    }
125  }
126 
127
128  public T get(String url) {
129    return map.get(url);
130  }
131  
132  public boolean has(String url) {
133    return map.containsKey(url);
134  }
135  
136  public T get(String system, String version) {
137    if (map.containsKey(system+"|"+version))
138      return map.get(system+"|"+version);
139    String mm = VersionUtilities.getMajMin(version);
140    if (mm != null)
141      return map.get(system+"|"+mm);
142    else
143      return null;
144  }
145  
146  public boolean has(String system, String version) {
147    if (map.containsKey(system+"|"+version))
148      return true;
149    String mm = VersionUtilities.getMajMin(version);
150    if (mm != null)
151      return map.containsKey(system+"|"+mm);
152    else
153      return false;
154  }
155  
156  public int size() {
157    return list.size();
158  }
159  
160  public void drop(String id) {
161    T res = null;
162    do {
163      res = null;
164      for (T t : list) {
165        if (t.getId().equals(id)) {
166          res = t;
167        }
168      }
169      if (res != null) {
170        list.remove(res);
171        map.remove(id);
172        map.remove(res.getUrl());
173        if (res.hasVersion()) {
174          map.remove(res.getUrl()+"|"+res.getVersion());
175          String mm = VersionUtilities.getMajMin(res.getVersion());
176          if (mm != null) {
177            map.remove(res.getUrl()+"|"+mm);
178          }
179        }
180        updateList(res.getUrl(), res.getVersion()); 
181      }
182    } while (res != null);
183  }
184  
185  
186  public void listAll(List<T> result) {
187    result.addAll(list);    
188  }
189
190  public void listAllM(List<CanonicalResource> result) {
191    result.addAll(list);    
192  }
193
194  public void clear() {
195    list.clear();
196    map.clear();
197    
198  }
199
200  public List<T> getList() {
201    List<T> res = new ArrayList<>();
202    for (T t : list) {
203      if (!res.contains(t)) {
204        res.add(t);
205      }
206    }
207    return res;
208  }
209
210  public List<T> getSortedList() {
211    List<T> res = new ArrayList<>();
212    for (T t : list) {
213      if (!res.contains(t)) {
214        res.add(t);
215      }
216    }
217    Collections.sort(res, new CanonicalListSorter());
218    return res;
219  }
220
221  public Set<String> keys() {
222    return map.keySet();
223  }
224
225  public boolean isEnforceUniqueId() {
226    return enforceUniqueId;
227  }
228  
229}