Skip to content

Commit

Permalink
Merge pull request #111 from cqframework/fix-110-add-library-generati…
Browse files Browse the repository at this point in the history
…on-to-refresh

#110: Add library stub generation to the library refresh process.
  • Loading branch information
rob-reynolds authored Aug 17, 2020
2 parents 8ec9578 + fe98132 commit fb20fdf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/opencds/cqf/tooling/processor/CqlProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ public class CqlProcessor {
* information about a cql file
*/
public class CqlSourceFileInformation {
private VersionedIdentifier identifier;
private byte[] elm;
private byte[] jsonElm;
private List<ValidationMessage> errors = new ArrayList<>();
private List<RelatedArtifact> relatedArtifacts = new ArrayList<>();
private List<DataRequirement> dataRequirements = new ArrayList<>();
private List<ParameterDefinition> parameters = new ArrayList<>();
public VersionedIdentifier getIdentifier() {
return identifier;
}
public void setIdentifier(VersionedIdentifier identifier) {
this.identifier = identifier;
}
public byte[] getElm() {
return elm;
}
Expand Down Expand Up @@ -168,6 +175,14 @@ public CqlSourceFileInformation getFileInformation(String filename) {
return this.fileMap.remove(filename);
}

public Collection<CqlSourceFileInformation> getAllFileInformation() {
if (fileMap == null) {
throw new IllegalStateException("CQL File map is not available, execute has not been called");
}

return this.fileMap.values();
}

/**
* Called at the end after all getFileInformation have been called
* return any errors that didn't have any particular home, and also
Expand Down Expand Up @@ -306,6 +321,7 @@ private void translateFile(ModelManager modelManager, LibraryManager libraryMana
// convert to base64 bytes
// NOTE: Publication tooling requires XML content
result.setElm(translator.toXml().getBytes());
result.setIdentifier(translator.toELM().getIdentifier());
if (options.getFormats().contains(CqlTranslator.Format.JSON)) {
result.setJsonElm(translator.toJson().getBytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static List<String> refreshIgLibraryContent(BaseProcessor parentContext,
params.fhirContext = fhirContext;
params.encoding = outputEncoding;
params.versioned = versioned;
params.libraryPath = libraryPath;
return libraryProcessor.refreshLibraryContent(params);
}

Expand Down Expand Up @@ -164,6 +163,30 @@ protected List<Library> refreshGeneratedContent(List<Library> sourceLibraries) {

cqlProcessor.execute();

// For each CQL file, ensure that there is a Library resource with a matching name and version
for (CqlProcessor.CqlSourceFileInformation fileInfo : cqlProcessor.getAllFileInformation()) {
if (fileInfo.getIdentifier() != null && fileInfo.getIdentifier().getId() != null && !fileInfo.getIdentifier().getId().equals("")) {
Library existingLibrary = null;
for (Library sourceLibrary : sourceLibraries) {
if (fileInfo.getIdentifier().getId().equals(sourceLibrary.getName())
&& (fileInfo.getIdentifier().getVersion() == null || fileInfo.getIdentifier().getVersion().equals(sourceLibrary.getVersion()))
) {
existingLibrary = sourceLibrary;
break;
}
}

if (existingLibrary == null) {
Library newLibrary = new Library();
newLibrary.setName(fileInfo.getIdentifier().getId());
newLibrary.setVersion(fileInfo.getIdentifier().getVersion());
newLibrary.setUrl(String.format("%s/Library/%s", (newLibrary.getName().equals("FHIRHelpers") ? "http://hl7.org/fhir" : canonicalBase), fileInfo.getIdentifier().getId()));
newLibrary.setId(LibraryProcessor.getId(newLibrary.getName()) + (versioned ? "-" + newLibrary.getVersion() : ""));
sourceLibraries.add(newLibrary);
}
}
}

List<Library> resources = new ArrayList<Library>();
for (Library library : sourceLibraries) {
resources.add(refreshGeneratedContent(library));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public static void bundleMeasures(ArrayList<String> refreshedLibraryNames, Strin
if (path.endsWith(refreshedLibraryFileName))
{
measureSourcePath = path;
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class R4LibraryProcessor extends LibraryProcessor {
/*
Refresh all library resources in the given libraryPath
*/
protected List<String> refreshLibraries(String libraryPath) {
protected List<String> refreshLibraries(String libraryPath, Encoding encoding) {
File file = new File(libraryPath);
Map<String, String> fileMap = new HashMap<String, String>();
List<org.hl7.fhir.r5.model.Library> libraries = new ArrayList<>();
Expand Down Expand Up @@ -76,10 +76,19 @@ protected List<String> refreshLibraries(String libraryPath) {
List<String> refreshedLibraryNames = new ArrayList<String>();
List<org.hl7.fhir.r5.model.Library> refreshedLibraries = super.refreshGeneratedContent(libraries);
for (org.hl7.fhir.r5.model.Library refreshedLibrary : refreshedLibraries) {
String filePath = fileMap.get(refreshedLibrary.getId());
Library library = (Library) VersionConvertor_40_50.convertResource(refreshedLibrary);
String filePath = null;
Encoding fileEncoding = null;
if (fileMap.containsKey(refreshedLibrary.getId()))
{
filePath = fileMap.get(refreshedLibrary.getId());
fileEncoding = IOUtils.getEncoding(filePath);
} else {
filePath = libraryPath;
fileEncoding = encoding;
}
cqfmHelper.ensureToolingExtensionAndDevice(library, fhirContext);
IOUtils.writeResource(library, filePath, IOUtils.getEncoding(filePath), fhirContext);
IOUtils.writeResource(library, filePath, fileEncoding, fhirContext);
refreshedLibraryNames.add(refreshedLibrary.getName());
}

Expand All @@ -102,7 +111,7 @@ public List<String> refreshLibraryContent(RefreshLibraryParameters params) {
encoding = params.encoding;
versioned = params.versioned;

return refreshLibraries(libraryPath);
return refreshLibraries(libraryPath, encoding);

/*
CqlTranslator translator = getTranslator(cqlContentPath);
Expand Down

0 comments on commit fb20fdf

Please sign in to comment.