Search in sources :

Example 21 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class NPMPackageGenerator method loadFiles.

public void loadFiles(String root, File dir, String... noload) throws IOException {
    for (File f : dir.listFiles()) {
        if (!Utilities.existsInList(f.getName(), noload)) {
            if (f.isDirectory()) {
                loadFiles(root, f);
            } else {
                String path = f.getAbsolutePath().substring(root.length() + 1);
                byte[] content = TextFile.fileToBytes(f);
                if (created.contains(path))
                    System.out.println("Duplicate package file " + path);
                else {
                    created.add(path);
                    TarArchiveEntry entry = new TarArchiveEntry(path);
                    entry.setSize(content.length);
                    tar.putArchiveEntry(entry);
                    tar.write(content);
                    tar.closeArchiveEntry();
                }
            }
        }
    }
}
Also used : File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 22 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method cacheResourceFromPackage.

public void cacheResourceFromPackage(Resource r, PackageVersion packageInfo) throws FHIRException {
    synchronized (lock) {
        if (r.getId() != null) {
            Map<String, ResourceProxy> map = allResourcesById.get(r.fhirType());
            if (map == null) {
                map = new HashMap<String, ResourceProxy>();
                allResourcesById.put(r.fhirType(), map);
            }
            if ((packageInfo == null || !packageInfo.isExamplesPackage()) || !map.containsKey(r.getId())) {
                map.put(r.getId(), new ResourceProxy(r));
            } else {
                logger.logDebugMessage(LogCategory.PROGRESS, "Ignore " + r.fhirType() + "/" + r.getId() + " from package " + packageInfo.toString());
            }
        }
        if (r instanceof CodeSystem || r instanceof NamingSystem) {
            oidCache.clear();
        }
        if (r instanceof CanonicalResource) {
            CanonicalResource m = (CanonicalResource) r;
            String url = m.getUrl();
            if (!allowLoadingDuplicates && hasResource(r.getClass(), url)) {
                // special workaround for known problems with existing packages
                if (Utilities.existsInList(url, "http://hl7.org/fhir/SearchParameter/example")) {
                    return;
                }
                throw new DefinitionException(formatMessage(I18nConstants.DUPLICATE_RESOURCE_, url));
            }
            if (r instanceof StructureDefinition) {
                StructureDefinition sd = (StructureDefinition) m;
                if ("1.4.0".equals(version)) {
                    fixOldSD(sd);
                }
                structures.see(sd, packageInfo);
            } else if (r instanceof ValueSet) {
                valueSets.see((ValueSet) m, packageInfo);
            } else if (r instanceof CodeSystem) {
                CodeSystemUtilities.crossLinkCodeSystem((CodeSystem) r);
                codeSystems.see((CodeSystem) m, packageInfo);
            } else if (r instanceof ImplementationGuide) {
                guides.see((ImplementationGuide) m, packageInfo);
            } else if (r instanceof CapabilityStatement) {
                capstmts.see((CapabilityStatement) m, packageInfo);
            } else if (r instanceof Measure) {
                measures.see((Measure) m, packageInfo);
            } else if (r instanceof Library) {
                libraries.see((Library) m, packageInfo);
            } else if (r instanceof SearchParameter) {
                searchParameters.see((SearchParameter) m, packageInfo);
            } else if (r instanceof PlanDefinition) {
                plans.see((PlanDefinition) m, packageInfo);
            } else if (r instanceof OperationDefinition) {
                operations.see((OperationDefinition) m, packageInfo);
            } else if (r instanceof Questionnaire) {
                questionnaires.see((Questionnaire) m, packageInfo);
            } else if (r instanceof ConceptMap) {
                maps.see((ConceptMap) m, packageInfo);
            } else if (r instanceof StructureMap) {
                transforms.see((StructureMap) m, packageInfo);
            } else if (r instanceof NamingSystem) {
                systems.see((NamingSystem) m, packageInfo);
            }
        }
    }
}
Also used : Questionnaire(org.hl7.fhir.r5.model.Questionnaire) ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) CanonicalResourceProxy(org.hl7.fhir.r5.context.CanonicalResourceManager.CanonicalResourceProxy) StructureMap(org.hl7.fhir.r5.model.StructureMap) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) NamingSystem(org.hl7.fhir.r5.model.NamingSystem) CapabilityStatement(org.hl7.fhir.r5.model.CapabilityStatement) Measure(org.hl7.fhir.r5.model.Measure) PlanDefinition(org.hl7.fhir.r5.model.PlanDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) Library(org.hl7.fhir.r5.model.Library) SearchParameter(org.hl7.fhir.r5.model.SearchParameter) ConceptMap(org.hl7.fhir.r5.model.ConceptMap) CanonicalResource(org.hl7.fhir.r5.model.CanonicalResource) ValueSet(org.hl7.fhir.r5.model.ValueSet) OperationDefinition(org.hl7.fhir.r5.model.OperationDefinition)

Example 23 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class ParsingTests method data.

public static Stream<Arguments> data() throws ParserConfigurationException, IOException, FHIRFormatError, SAXException {
    FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
    npm = pcm.loadPackage("hl7.fhir.r5.examples", "5.0.0");
    List<Arguments> objects = new ArrayList<>();
    List<String> names = npm.list("package");
    for (String n : names) {
        if (!n.contains("manifest.json") && !n.contains("xver-") && !n.contains("uml.json") && !n.contains("package-min-ver.json")) {
            objects.add(Arguments.of(n));
        }
    }
    return objects.stream();
}
Also used : FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) Arguments(org.junit.jupiter.params.provider.Arguments) ArrayList(java.util.ArrayList)

Example 24 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class StandAloneValidatorFetcher method resolveURL.

@Override
public boolean resolveURL(IResourceValidator validator, Object appContext, String path, String url, String type) throws IOException, FHIRException {
    if (!Utilities.isAbsoluteUrl(url)) {
        return false;
    }
    if (url.contains("|")) {
        url = url.substring(0, url.lastIndexOf("|"));
    }
    if (type != null && type.equals("uri") && isMappingUri(url)) {
        return true;
    }
    // if we've got to here, it's a reference to a FHIR URL. We're going to try to resolve it on the fly
    String pid = null;
    String ver = null;
    String base = findBaseUrl(url);
    if (base == null) {
        return !url.startsWith("http://hl7.org/fhir") && !type.equals("canonical");
    }
    // the next operations are expensive. we're going to cache them
    if (urlList.containsKey(url)) {
        return urlList.get(url);
    }
    if (base.equals("http://terminology.hl7.org")) {
        pid = "hl7.terminology";
    } else if (url.startsWith("http://hl7.org/fhir")) {
        pid = pcm.getPackageId(base);
    } else {
        if (pidList.containsKey(base)) {
            pid = pidList.get(base);
        } else {
            pid = pcm.findCanonicalInLocalCache(base);
            pidList.put(base, pid);
        }
    }
    ver = url.contains("|") ? url.substring(url.indexOf("|") + 1) : null;
    if (pid == null && Utilities.startsWithInList(url, "http://hl7.org/fhir", "http://terminology.hl7.org")) {
        urlList.put(url, false);
        return false;
    }
    if (url.startsWith("http://hl7.org/fhir")) {
        // first possibility: it's a reference to a version specific URL http://hl7.org/fhir/X.X/...
        VersionURLInfo vu = VersionUtilities.parseVersionUrl(url);
        if (vu != null) {
            NpmPackage pi = pcm.loadPackage(VersionUtilities.packageForVersion(vu.getVersion()), VersionUtilities.getCurrentVersion(vu.getVersion()));
            boolean res = pi.hasCanonical(vu.getUrl());
            urlList.put(url, res);
            return res;
        }
    }
    // ok maybe it's a reference to a package we know
    if (pid != null) {
        if ("sharedhealth.fhir.ca.common".equals(pid)) {
            // special case - optimise this
            return false;
        }
        NpmPackage pi = null;
        if (pidMap.containsKey(pid + "|" + ver)) {
            pi = pidMap.get(pid + "|" + ver);
        } else if (installer.packageExists(pid, ver)) {
            try {
                installer.loadPackage(pid, ver);
                pi = pcm.loadPackage(pid);
                pidMap.put(pid + "|" + ver, pi);
            } catch (Exception e) {
                pidMap.put(pid + "|" + ver, null);
            }
        } else {
            pidMap.put(pid + "|" + ver, null);
        }
        if (pi != null) {
            context.loadFromPackage(pi, null);
            return pi.hasCanonical(url);
        }
    }
    // we don't bother with urls outside fhir space in the standalone validator - we assume they are valid
    return !url.startsWith("http://hl7.org/fhir") && !type.equals("canonical");
}
Also used : NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) VersionURLInfo(org.hl7.fhir.utilities.VersionUtilities.VersionURLInfo) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 25 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class R3TEchnicalCorrectionProcessor method extractToPackageMaster.

private void extractToPackageMaster(List<Resource> list, String root) throws IOException, FHIRFormatError {
    System.out.println("Updating Packages Master");
    String corePath = Utilities.path(root, "hl7.fhir.r3.core", "package");
    String examplesPath = Utilities.path(root, "hl7.fhir.r3.examples", "package");
    String elementsPath = Utilities.path(root, "hl7.fhir.r3.elements", "package");
    int coreTotal = new File(corePath).list().length - 1;
    int examplesTotal = new File(examplesPath).list().length - 1;
    int elementsTotal = new File(elementsPath).list().length - 1;
    int coreCount = 0;
    int examplesCount = 0;
    int elementsCount = 0;
    for (Resource r : list) {
        String n = r.fhirType() + "-" + r.getId() + ".json";
        FileOutputStream dst = null;
        if (n.startsWith("DataElement-")) {
            elementsCount++;
            dst = new FileOutputStream(Utilities.path(elementsPath, n));
            new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(dst, r);
        } else {
            dst = new FileOutputStream(Utilities.path(examplesPath, n));
            new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(dst, r);
            examplesCount++;
            if (isCoreResource(r.fhirType())) {
                coreCount++;
                DomainResource dr = (DomainResource) r;
                dr.setText(null);
                new JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(new FileOutputStream(Utilities.path(corePath, n)), r);
            }
        }
    }
    System.out.println("  Core @ " + corePath + ": Replaced " + coreCount + " of " + coreTotal);
    System.out.println("  Examples @ " + examplesPath + ": Replaced " + examplesCount + " of " + examplesTotal);
    System.out.println("  Elements @ " + elementsPath + ": Replaced " + elementsCount + " of " + elementsTotal);
}
Also used : DomainResource(org.hl7.fhir.dstu3.model.DomainResource) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) File(java.io.File) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Aggregations

File (java.io.File)25 FHIRException (org.hl7.fhir.exceptions.FHIRException)22 TextFile (org.hl7.fhir.utilities.TextFile)22 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)15 JsonObject (com.google.gson.JsonObject)12 FileNotFoundException (java.io.FileNotFoundException)11 FileInputStream (java.io.FileInputStream)9 IniFile (org.hl7.fhir.utilities.IniFile)9 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)9 ParseException (java.text.ParseException)8 HashMap (java.util.HashMap)7 Gson (com.google.gson.Gson)6 GsonBuilder (com.google.gson.GsonBuilder)6 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)6 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 JsonElement (com.google.gson.JsonElement)5 FileOutputStream (java.io.FileOutputStream)5 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)5 CSFile (org.hl7.fhir.utilities.CSFile)5