fix extension status and extension generator

This commit is contained in:
Grahame Grieve 2023-03-25 21:54:53 +11:00
parent 8e9b96b4b7
commit ce75d57808
4 changed files with 2474 additions and 2284 deletions

View File

@ -0,0 +1,118 @@
package org.hl7.fhir.convertors.misc;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.hl7.fhir.convertors.conv40_50.VersionConvertor_40_50;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
import org.hl7.fhir.r5.model.CanonicalResource;
import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.utils.ToolingExtensions;
import org.hl7.fhir.r5.utils.formats.ResourceFolderVisitor;
import org.hl7.fhir.r5.utils.formats.ResourceFolderVisitor.IResourceObserver;
import org.hl7.fhir.utilities.StandardsStatus;
import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
public class ExtensionStatusUpgrader implements IResourceObserver {
public static void main(String[] args) throws IOException {
new ExtensionStatusUpgrader().execute(args[0]);
}
Map<String, CanonicalResource> r4b = new HashMap<>();
Map<String, CanonicalResource> r5 = new HashMap<>();
private void execute(String folder) throws IOException {
FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true);
// load R4
NpmPackage npm = pcm.loadPackage("hl7.fhir.r4.core");
for (String n : npm.listResources("StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter")) {
CanonicalResource cr = (CanonicalResource) VersionConvertorFactory_40_50.convertResource(new org.hl7.fhir.r4.formats.JsonParser().parse(npm.load(n)));
r4b.put(cr.getUrl(), cr);
}
// load R5-ballot3
npm = pcm.loadPackage("hl7.fhir.r5.core#5.0.0-ballot");
for (String n : npm.listResources("StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter")) {
CanonicalResource cr = (CanonicalResource) new org.hl7.fhir.r5.formats.JsonParser().parse(npm.load(n));
r5.put(cr.getUrl(), cr);
}
new ResourceFolderVisitor(this, "StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter").visit(folder);
}
@Override
public boolean visitResource(String filename, Resource resource) {
boolean changed = false;
CanonicalResource cr = (CanonicalResource) resource;
if (r4b.containsKey(cr.getUrl())) {
CanonicalResource ocr = r4b.get(cr.getUrl());
if (cr.getStatus() != PublicationStatus.ACTIVE) {
cr.setStatus(PublicationStatus.ACTIVE);
changed = true;
}
if (ocr.getStandardsStatus() == StandardsStatus.NORMATIVE) {
if (cr.getStandardsStatus() != StandardsStatus.NORMATIVE) {
cr.setStandardsStatus(StandardsStatus.NORMATIVE);
changed = true;
}
} else {
if (cr.getStandardsStatus() != StandardsStatus.TRIAL_USE) {
cr.setStandardsStatus(StandardsStatus.TRIAL_USE);
changed = true;
}
}
if (cr.getExperimental()) {
cr.setExperimental(false);
changed = true;
}
if (!"3".equals(ToolingExtensions.readStringExtension(cr, ToolingExtensions.EXT_FMM_LEVEL))) {
ToolingExtensions.setIntegerExtension(cr, ToolingExtensions.EXT_FMM_LEVEL, 3);
changed = true;
}
} else if (r5.containsKey(cr.getUrl())) {
CanonicalResource ocr = r5.get(cr.getUrl());
if (cr.getStatus() != PublicationStatus.DRAFT) {
cr.setStatus(PublicationStatus.DRAFT);
changed = true;
}
if (ocr.getStandardsStatus() == StandardsStatus.NORMATIVE) {
if (cr.getStandardsStatus() != StandardsStatus.NORMATIVE) {
cr.setStandardsStatus(StandardsStatus.NORMATIVE);
changed = true;
}
} else {
if (cr.getStandardsStatus() != StandardsStatus.TRIAL_USE) {
cr.setStandardsStatus(StandardsStatus.TRIAL_USE);
changed = true;
}
}
if (cr.getExperimental()) {
cr.setExperimental(false);
changed = true;
}
if (!"2".equals(ToolingExtensions.readStringExtension(cr, ToolingExtensions.EXT_FMM_LEVEL))) {
ToolingExtensions.setIntegerExtension(cr, ToolingExtensions.EXT_FMM_LEVEL, 2);
changed = true;
}
} else {
if (cr.getStatus() != PublicationStatus.DRAFT) {
cr.setStatus(PublicationStatus.DRAFT);
changed = true;
}
if (cr.getStandardsStatus() != StandardsStatus.DRAFT) {
cr.setStandardsStatus(StandardsStatus.DRAFT);
changed = true;
}
if (!cr.getExperimental()) {
cr.setExperimental(true);
changed = true;
}
}
return changed;
}
}

View File

@ -101,23 +101,23 @@ public class JavaExtensionsFactoryGenerator extends JavaBaseGenerator {
ElementDefinition edValue = sd.getSnapshot().getElementByPath("Extension.value[x]");
List<TypeTuple> types = analyseTypes(edValue);
if (types.size() > 5) {
src.append(" public Extension make"+name+"(DataType value) {\r\n");
src.append(" public static Extension make"+name+"(DataType value) {\r\n");
src.append(" return new Extension(ExtensionConstants.EXT_"+constName+").setValue(value);\r\n");
src.append(" }\r\n");
src.append("\r\n");
for (String ctxt : Utilities.sorted(contexts)) {
src.append(" public "+ctxt+" "+verb+name+"("+ctxt+" context, DataType value) {\r\n");
src.append(" public static "+ctxt+" "+verb+name+"("+ctxt+" context, DataType value) {\r\n");
src.append(" ExtensionsUtils."+verb+"Extension(context, ExtensionConstants.EXT_"+constName+", value);\r\n");
src.append(" return context;\r\n");
src.append(" }\r\n");
src.append("\r\n");
if (repeats) {
src.append(" public List<DataType> get"+name+"List("+ctxt+" context) {\r\n");
src.append(" public static List<DataType> get"+name+"List("+ctxt+" context) {\r\n");
src.append(" return ExtensionsUtils.getExtensionList(DataType.class, context, ExtensionConstants.EXT_"+constName+");\r\n");
src.append(" }\r\n");
src.append("\r\n");
} else {
src.append(" public DataType get"+name+"("+ctxt+" context) {\r\n");
src.append(" public static DataType get"+name+"("+ctxt+" context) {\r\n");
src.append(" return ExtensionsUtils.getExtension(DataType.class, context, ExtensionConstants.EXT_"+constName+");\r\n");
src.append(" }\r\n");
src.append("\r\n");
@ -126,7 +126,7 @@ public class JavaExtensionsFactoryGenerator extends JavaBaseGenerator {
} else {
for (TypeTuple t : types) {
String sfx = typeCount(t.getJavaType(), types) > 1 ? Utilities.capitalize(t.getFhirType()) : "";
src.append(" public Extension make"+name+sfx+"("+t.getJavaType()+" value) {\r\n");
src.append(" public static Extension make"+name+sfx+"("+t.getJavaType()+" value) {\r\n");
src.append(" return new Extension(ExtensionConstants.EXT_"+constName+").setValue("+(t.adapt("value"))+");\r\n");
src.append(" }\r\n");
src.append("\r\n");
@ -135,7 +135,7 @@ public class JavaExtensionsFactoryGenerator extends JavaBaseGenerator {
Set<String> td = new HashSet<>();
for (TypeTuple t : types) {
String sfx = typeCount(t.getJavaType(), types) > 1 ? Utilities.capitalize(t.getFhirType()) : "";
src.append(" public "+ctxt+" "+verb+name+sfx+"("+ctxt+" context, "+t.getJavaType()+" value) {\r\n");
src.append(" public static "+ctxt+" "+verb+name+sfx+"("+ctxt+" context, "+t.getJavaType()+" value) {\r\n");
src.append(" ExtensionsUtils."+verb+"Extension(context, ExtensionConstants.EXT_"+constName+", "+(t.adapt("value"))+");\r\n");
src.append(" return context;\r\n");
src.append(" }\r\n");
@ -144,7 +144,7 @@ public class JavaExtensionsFactoryGenerator extends JavaBaseGenerator {
if (!td.contains(sfx)) {
td.add(sfx);
if (repeats) {
src.append(" public List<"+t.getJavaRType()+"> get"+name+sfx+"List("+ctxt+" context) {\r\n");
src.append(" public static List<"+t.getJavaRType()+"> get"+name+sfx+"List("+ctxt+" context) {\r\n");
if (t.getFhirType() == null) {
src.append(" return ExtensionsUtils.getExtensionList("+t.getJavaType()+".class, context, ExtensionConstants.EXT_"+constName+");\r\n");
} else {
@ -153,7 +153,7 @@ public class JavaExtensionsFactoryGenerator extends JavaBaseGenerator {
src.append(" }\r\n");
src.append("\r\n");
} else {
src.append(" public "+t.getJavaRType()+" get"+name+sfx+"("+ctxt+" context) {\r\n");
src.append(" public static "+t.getJavaRType()+" get"+name+sfx+"("+ctxt+" context) {\r\n");
if (t.getFhirType() == null) {
src.append(" return ExtensionsUtils.getExtension("+t.getJavaType()+".class, context, ExtensionConstants.EXT_"+constName+");\r\n");
} else {

View File

@ -0,0 +1,72 @@
package org.hl7.fhir.r5.utils.formats;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.FloatControl.Type;
import org.hl7.fhir.r5.formats.JsonParser;
import org.hl7.fhir.r5.formats.XmlParser;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.model.Resource;
public class ResourceFolderVisitor {
public interface IResourceObserver {
public boolean visitResource(String filename, Resource resource);
}
private IResourceObserver observer;
private List<String> types = new ArrayList<>();
public ResourceFolderVisitor(IResourceObserver observer) {
super();
this.observer = observer;
}
public ResourceFolderVisitor(IResourceObserver observer, String... types) {
super();
this.observer = observer;
for (String t : types) {
this.types.add(t);
}
}
public void visit(String folder) {
visit(new File(folder));
}
public void visit(File file) {
for (File f : file.listFiles()) {
if (f.isDirectory()) {
visit(f);
} else if (f.getName().endsWith(".xml")) {
try {
Resource res = new XmlParser().parse(new FileInputStream(f));
if (types.isEmpty() || types.contains(res.fhirType())) {
if (observer.visitResource(f.getAbsolutePath(), res)) {
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(f), res);
}
}
} catch (Exception e) {
}
} else if (f.getName().endsWith(".json")) {
try {
Resource res = new JsonParser().parse(new FileInputStream(f));
if (types.isEmpty() || types.contains(res.fhirType())) {
if (observer.visitResource(f.getAbsolutePath(), res)) {
new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(f), res);
}
}
} catch (Exception e) {
}
}
}
}
}