001package org.hl7.fhir.dstu2.terminologies;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032/*
033Copyright (c) 2011+, HL7, Inc
034All rights reserved.
035
036Redistribution and use in source and binary forms, with or without modification, 
037are permitted provided that the following conditions are met:
038
039 * Redistributions of source code must retain the above copyright notice, this 
040   list of conditions and the following disclaimer.
041 * Redistributions in binary form must reproduce the above copyright notice, 
042   this list of conditions and the following disclaimer in the documentation 
043   and/or other materials provided with the distribution.
044 * Neither the name of HL7 nor the names of its contributors may be used to 
045   endorse or promote products derived from this software without specific 
046   prior written permission.
047
048THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
049ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
050WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
051IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
052INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
053NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
054PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
055WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
056ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
057POSSIBILITY OF SUCH DAMAGE.
058
059*/
060
061import java.io.File;
062import java.io.FileInputStream;
063import java.io.FileOutputStream;
064import java.io.IOException;
065import java.util.HashMap;
066import java.util.Map;
067
068import org.apache.commons.io.IOUtils;
069import org.hl7.fhir.dstu2.formats.IParser.OutputStyle;
070import org.hl7.fhir.dstu2.model.OperationOutcome;
071import org.hl7.fhir.dstu2.model.Resource;
072import org.hl7.fhir.dstu2.model.ValueSet;
073import org.hl7.fhir.dstu2.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
074import org.hl7.fhir.dstu2.utils.IWorkerContext;
075import org.hl7.fhir.dstu2.utils.ToolingExtensions;
076import org.hl7.fhir.exceptions.FHIRFormatError;
077import org.hl7.fhir.utilities.Utilities;
078import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
079
080public class ValueSetExpansionCache implements ValueSetExpanderFactory {
081
082  public class CacheAwareExpander implements ValueSetExpander {
083
084    @Override
085    public ValueSetExpansionOutcome expand(ValueSet source) throws ETooCostly, IOException {
086      if (expansions.containsKey(source.getUrl()))
087        return expansions.get(source.getUrl());
088      ValueSetExpander vse = new ValueSetExpanderSimple(context, ValueSetExpansionCache.this);
089      ValueSetExpansionOutcome vso = vse.expand(source);
090      if (vso.getError() != null) {
091        // well, we'll see if the designated server can expand it, and if it can, we'll
092        // cache it locally
093        vso = context.expandVS(source, false);
094        FileOutputStream s = new FileOutputStream(Utilities.path(cacheFolder, makeFile(source.getUrl())));
095        context.newXmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, vso.getValueset());
096        s.close();
097      }
098      expansions.put(source.getUrl(), vso);
099      return vso;
100    }
101
102    private String makeFile(String url) {
103      return url.replace("$", "").replace(":", "").replace("//", "/").replace("/", "_") + ".xml";
104    }
105  }
106
107  private static final String VS_ID_EXT = "http://tools/cache";
108
109  private final Map<String, ValueSetExpansionOutcome> expansions = new HashMap<String, ValueSetExpansionOutcome>();
110  private final IWorkerContext context;
111  private final String cacheFolder;
112
113  public ValueSetExpansionCache(IWorkerContext context) {
114    super();
115    cacheFolder = null;
116    this.context = context;
117  }
118
119  public ValueSetExpansionCache(IWorkerContext context, String cacheFolder) throws FHIRFormatError, IOException {
120    super();
121    this.context = context;
122    this.cacheFolder = cacheFolder;
123    if (this.cacheFolder != null)
124      loadCache();
125  }
126
127  private void loadCache() throws FHIRFormatError, IOException {
128    File[] files = new File(cacheFolder).listFiles();
129    for (File f : files) {
130      if (f.getName().endsWith(".xml")) {
131        final FileInputStream is = new FileInputStream(f);
132        try {
133          Resource r = context.newXmlParser().setOutputStyle(OutputStyle.PRETTY).parse(is);
134          if (r instanceof OperationOutcome) {
135            OperationOutcome oo = (OperationOutcome) r;
136            expansions.put(ToolingExtensions.getExtension(oo, VS_ID_EXT).getValue().toString(),
137                new ValueSetExpansionOutcome(new XhtmlComposer(true, false).composePlainText(oo.getText().getDiv())));
138          } else {
139            ValueSet vs = (ValueSet) r;
140            expansions.put(vs.getUrl(), new ValueSetExpansionOutcome(vs, null));
141          }
142        } finally {
143          IOUtils.closeQuietly(is);
144        }
145      }
146    }
147  }
148
149  @Override
150  public ValueSetExpander getExpander() {
151    return new CacheAwareExpander();
152    // return new ValueSetExpander(valuesets, codesystems);
153  }
154
155}