use of org.hl7.fhir.r4.model.BaseResource in project kindling by HL7.
the class Publisher method processProfile.
private void processProfile(Profile ap, ConstraintStructure profile, String filename, ResourceDefn baseResource) throws Exception {
// create StructureDefinition structures if needed (create differential definitions from spreadsheets)
if (profile.getResource() == null) {
StructureDefinition p = new ProfileGenerator(page.getDefinitions(), page.getWorkerContext(), page, page.getGenDate(), page.getVersion(), dataElements, fpUsages, page.getFolders().rootDir, page.getUml(), page.getRc()).generate(ap, profile, profile.getDefn(), profile.getId(), profile.getUsage(), page.getValidationErrors(), baseResource);
p.setUserData("pack", ap);
profile.setResource(p);
if (profile.getResourceInfo() != null) {
profile.getResourceInfo().setUserData(ToolResourceUtilities.RES_ACTUAL_RESOURCE, p);
}
if (page.getProfiles().has(p.getUrl()))
throw new Exception("Duplicate Profile URL " + p.getUrl());
page.getProfiles().see(p, page.packageInfo());
} else {
profile.getResource().setUserData("pack", ap);
sortProfile(profile.getResource());
for (ElementDefinition ed : profile.getResource().getDifferential().getElement()) if (!ed.hasId())
throw new Exception("Missing ID");
// special case: if the profile itself doesn't claim a date, it's date is the date of this publication
if (!profile.getResource().hasDate())
profile.getResource().setDate(page.getGenDate().getTime());
if (profile.getResource().hasBaseDefinition() && !profile.getResource().hasSnapshot()) {
// cause it probably doesn't, coming from the profile directly
StructureDefinition base = getSnapShotForProfile(profile.getResource().getBaseDefinition());
new ProfileUtilities(page.getWorkerContext(), page.getValidationErrors(), page).generateSnapshot(base, profile.getResource(), profile.getResource().getBaseDefinition().split("#")[0], "http://hl7.org/fhir", profile.getResource().getName());
}
if (page.getProfiles().has(profile.getResource().getUrl()))
throw new Exception("Duplicate Profile URL " + profile.getResource().getUrl());
System.out.println("register " + profile.getResource().getUrl());
page.getProfiles().see(profile.getResource(), page.packageInfo());
}
if (!Utilities.noString(filename))
profile.getResource().setUserData("filename", filename + ".html");
if (Utilities.noString(profile.getResource().getUserString("path"))) {
String path = "";
ImplementationGuideDefn ig = page.getDefinitions().getUsageIG(ap.getCategory(), "processProfile");
if (ig != null && !ig.isCore())
path = ig.getCode() + File.separator;
profile.getResource().setUserData("path", path + filename + ".html");
}
}
use of org.hl7.fhir.r4.model.BaseResource in project kindling by HL7.
the class FhirTurtleGenerator method processTypes.
private void processTypes(String baseResourceName, FHIRResource baseResource, ElementDefn td, String predicateBase, boolean innerIsBackbone) throws Exception {
for (ElementDefn ed : td.getElements()) {
String predicateName = predicateBase + "." + (ed.getName().endsWith("[x]") ? ed.getName().substring(0, ed.getName().length() - 3) : ed.getName());
FHIRResource predicateResource;
if (ed.getName().endsWith("[x]")) {
predicateResource = fact.fhir_objectProperty(predicateName);
// Choice entry
if (ed.typeCode().equals("*")) {
// Wild card -- any element works (probably should be more restrictive but...)
Resource targetResource = RDFNamespace.FHIR.resourceRef("Element");
baseResource.restriction(fact.fhir_cardinality_restriction(predicateResource.resource, targetResource, ed.getMinCardinality(), ed.getMaxCardinality()));
predicateResource.domain(baseResource);
predicateResource.range(targetResource);
} else {
// Create a restriction on the union of possible types
List<Resource> typeOpts = new ArrayList<Resource>();
for (TypeRef tr : ed.getTypes()) {
// TODO: Figure out how to get the type reference code
String trName = tr.getName();
if (trName.equals("SimpleQuantity"))
trName = "Quantity";
String qualifiedPredicateName = predicateName + Utilities.capitalize(trName);
Resource targetRes = fact.fhir_class(tr.getName()).resource;
FHIRResource qualifiedPredicate = fact.fhir_objectProperty(qualifiedPredicateName, predicateResource.resource).domain(baseResource).range(targetRes);
typeOpts.add(fact.fhir_cardinality_restriction(qualifiedPredicate.resource, targetRes, ed.getMinCardinality(), ed.getMaxCardinality()));
}
baseResource.restriction(fact.fhir_union(typeOpts));
}
} else {
FHIRResource baseDef;
if (ed.getTypes().isEmpty()) {
predicateResource = fact.fhir_objectProperty(predicateName);
String targetClassName = mapComponentName(baseResourceName, ed.getDeclaredTypeName());
baseDef = fact.fhir_class(targetClassName, innerIsBackbone ? "BackboneElement" : "Element").addDefinition(ed.getDefinition());
processTypes(targetClassName, baseDef, ed, predicateName, innerIsBackbone);
} else {
TypeRef targetType = ed.getTypes().get(0);
String targetName = targetType.getName();
if (targetName.startsWith("@")) {
// Link to earlier definition
ElementDefn targetRef = getElementForPath(targetName.substring(1));
String targetRefName = targetRef.getName();
String targetClassName = baseResourceName + Character.toUpperCase(targetRefName.charAt(0)) + targetRefName.substring(1);
baseDef = fact.fhir_class(targetClassName, innerIsBackbone ? "BackboneElement" : "Element").addDefinition(ed.getDefinition()).addTitle(ed.getShortDefn());
if (!processing.contains(targetRefName)) {
processing.add(targetRefName);
processTypes(targetClassName, baseDef, targetRef, predicateName, innerIsBackbone);
processing.remove(targetRefName);
}
} else {
// A placeholder entry. The rest of the information will be supplied elsewhere
baseDef = fact.fhir_class(targetName);
}
// XHTML the exception, in that the html doesn't derive from Primitive
if (targetName.equals("xhtml"))
predicateResource = fact.fhir_dataProperty(predicateName);
else
predicateResource = fact.fhir_objectProperty(predicateName);
}
predicateResource.addTitle(ed.getShortDefn()).addDefinition(ed.getDefinition()).domain(baseResource);
baseResource.restriction(fact.fhir_cardinality_restriction(predicateResource.resource, baseDef.resource, ed.getMinCardinality(), ed.getMaxCardinality()));
predicateResource.range(baseDef.resource);
if (!Utilities.noString(ed.getW5()))
predicateResource.addObjectProperty(RDFS.subPropertyOf, RDFNamespace.W5.resourceRef(ed.getW5()));
}
}
}
use of org.hl7.fhir.r4.model.BaseResource in project quality-measure-and-cohort-service by Alvearie.
the class R4QualityMeasureResolverFactory method consumeFile.
private void consumeFile(String filename, String content, ResourceFieldHandler<Library, Identifier> libraryFieldHandler, MapFhirResourceResolver<Library, Identifier> libraryResolver, ResourceFieldHandler<Measure, Identifier> measureFieldHandler, MapFhirResourceResolver<Measure, Identifier> measureResolver) {
String filenameId = FilenameUtils.getBaseName(filename);
if (HapiUtils.canParseFile(filename, parser)) {
IBaseResource baseResource = parser.parseResource(content);
if (libraryFieldHandler.getSupportedClass().isInstance(baseResource)) {
Library library = (Library) baseResource;
setIdIfNotPresent(library, libraryFieldHandler, filenameId);
libraryResolver.addResource(library);
} else if (measureFieldHandler.getSupportedClass().isInstance(baseResource)) {
Measure measure = (Measure) baseResource;
setIdIfNotPresent(measure, measureFieldHandler, filenameId);
measureResolver.addResource(measure);
}
}
}
use of org.hl7.fhir.r4.model.BaseResource in project elexis-server by elexis.
the class ResourceProviderUtil method createResource.
/**
* @param transformer
* @param fhirObject
* @param log
* @return TODO Support conditional create http://hl7.org/fhir/http.html#ccreate
*/
protected <T extends BaseResource, U extends Identifiable> MethodOutcome createResource(IFhirTransformer<T, U> transformer, T fhirObject, Logger log) {
Optional<U> created;
try {
created = transformer.createLocalObject(fhirObject);
if (created.isPresent()) {
Optional<T> updated = transformer.getFhirObject(created.get());
if (updated.isPresent()) {
MethodOutcome outcome = new MethodOutcome();
outcome.setCreated(true);
outcome.setId(updated.get().getIdElement());
outcome.setResource(updated.get());
return outcome;
}
}
} catch (IFhirTransformerException e) {
OperationOutcome opOutcome = generateOperationOutcome(e);
throw new PreconditionFailedException(e.getMessage(), opOutcome);
}
log.warn("Object creation failed [{}]", fhirObject);
throw new InternalErrorException("Creation failed");
}
use of org.hl7.fhir.r4.model.BaseResource in project elexis-server by elexis.
the class ResourceProviderUtil method updateResource.
protected <T extends BaseResource, U extends Identifiable> MethodOutcome updateResource(IdType theId, IFhirTransformer<T, U> transformer, T fhirObject, Logger log) {
String versionId = theId.getVersionIdPart();
Optional<U> localObject = transformer.getLocalObject(fhirObject);
MethodOutcome outcome = new MethodOutcome();
if (localObject.isPresent()) {
if (versionId == null) {
log.warn("Version agnostic update on {}", localObject.get());
}
if (versionId != null && !versionId.equals(localObject.get().getLastupdate().toString())) {
throw new ResourceVersionConflictException("Expected version " + localObject.get().getLastupdate().toString());
}
try {
transformer.updateLocalObject(fhirObject, localObject.get());
} catch (IFhirTransformerException e) {
OperationOutcome opOutcome = generateOperationOutcome(e);
throw new PreconditionFailedException(e.getMessage(), opOutcome);
}
Optional<T> updatedObject = transformer.getFhirObject(localObject.get());
if (updatedObject.isPresent()) {
outcome.setId(updatedObject.get().getIdElement());
outcome.setResource(updatedObject.get());
return outcome;
}
log.warn("Object update failed [{}]", fhirObject);
throw new InternalErrorException("Object update failed");
} else {
OperationOutcome issueOutcome = new OperationOutcome();
issueOutcome.addIssue().setDiagnostics("No local object found");
outcome.setOperationOutcome(issueOutcome);
}
return outcome;
}
Aggregations