forked from cqframework/cql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestLibraryLoader.java
More file actions
88 lines (72 loc) · 3.56 KB
/
TestLibraryLoader.java
File metadata and controls
88 lines (72 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.hl7.fhirpath;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cqframework.cql.cql2elm.CqlCompilerException;
import org.cqframework.cql.cql2elm.CqlTranslatorOptions;
import org.cqframework.cql.cql2elm.LibraryManager;
import org.cqframework.cql.cql2elm.model.CompiledLibrary;
import org.cqframework.cql.elm.execution.Library;
import org.cqframework.cql.elm.execution.VersionedIdentifier;
import org.cqframework.cql.elm.serializing.ElmLibraryWriterFactory;
import org.opencds.cqf.cql.engine.execution.LibraryLoader;
import org.opencds.cqf.cql.engine.serializing.jackson.JsonCqlLibraryReader;
public class TestLibraryLoader implements LibraryLoader {
public TestLibraryLoader(LibraryManager libraryManager) {
if (libraryManager == null) {
throw new IllegalArgumentException("libraryManager is null");
}
this.libraryManager = libraryManager;
}
private LibraryManager libraryManager;
private Map<String, Library> libraries = new HashMap<>();
private Library resolveLibrary(VersionedIdentifier libraryIdentifier) {
if (libraryIdentifier == null) {
throw new IllegalArgumentException("Library identifier is null.");
}
if (libraryIdentifier.getId() == null) {
throw new IllegalArgumentException("Library identifier id is null.");
}
Library library = libraries.get(libraryIdentifier.getId());
if (library != null && libraryIdentifier.getVersion() != null && !libraryIdentifier.getVersion().equals(library.getIdentifier().getVersion())) {
throw new IllegalArgumentException(String.format("Could not load library %s, version %s because version %s is already loaded.",
libraryIdentifier.getId(), libraryIdentifier.getVersion(), library.getIdentifier().getVersion()));
}
else {
library = loadLibrary(libraryIdentifier);
libraries.put(libraryIdentifier.getId(), library);
}
return library;
}
private Library loadLibrary(VersionedIdentifier libraryIdentifier) {
List<CqlCompilerException> errors = new ArrayList<>();
org.hl7.elm.r1.VersionedIdentifier identifier = new org.hl7.elm.r1.VersionedIdentifier()
.withId(libraryIdentifier.getId())
.withSystem(libraryIdentifier.getSystem())
.withVersion(libraryIdentifier.getVersion());
CompiledLibrary compiledLibrary = libraryManager.resolveLibrary(identifier, CqlTranslatorOptions.defaultOptions(), errors);
String json;
try {
StringWriter writer = new StringWriter();
ElmLibraryWriterFactory.getWriter("application/elm+json").write(compiledLibrary.getLibrary(), writer);
json = writer.getBuffer().toString();
} catch (IOException e) {
throw new RuntimeException(String.format("Errors encountered while loading library %s: %s", libraryIdentifier.getId(), e.getMessage()));
}
Library library = null;
try {
library = new JsonCqlLibraryReader().read(new StringReader(json));
} catch (IOException e) {
throw new RuntimeException(String.format("Errors encountered while loading library %s: %s", libraryIdentifier.getId(), e.getMessage()));
}
return library;
}
@Override
public Library load(VersionedIdentifier libraryIdentifier) {
return resolveLibrary(libraryIdentifier);
}
}