public static ElementNode CreateNodeFromElement(Element element) { return(ElementNode.FromElement(element.ToTypedElement())); }
public static void AddSecurityTag(this ElementNode node, ProcessResult result) { if (node == null) { return; } if (result.ProcessRecords.Count == 0) { return; } ElementNode metaNode = node.GetMeta(); Meta meta = metaNode?.ToPoco <Meta>() ?? new Meta(); if (result.IsRedacted && !meta.Security.Any(x => string.Equals(x.Code, SecurityLabels.REDACT.Code, StringComparison.InvariantCultureIgnoreCase))) { meta.Security.Add(SecurityLabels.REDACT); } if (result.IsAbstracted && !meta.Security.Any(x => string.Equals(x.Code, SecurityLabels.ABSTRED.Code, StringComparison.InvariantCultureIgnoreCase))) { meta.Security.Add(SecurityLabels.ABSTRED); } if (result.IsCryptoHashed && !meta.Security.Any(x => string.Equals(x.Code, SecurityLabels.CRYTOHASH.Code, StringComparison.InvariantCultureIgnoreCase))) { meta.Security.Add(SecurityLabels.CRYTOHASH); } if (result.IsEncrypted && !meta.Security.Any(x => string.Equals(x.Code, SecurityLabels.ENCRYPT.Code, StringComparison.InvariantCultureIgnoreCase))) { meta.Security.Add(SecurityLabels.ENCRYPT); } if (result.IsPerturbed && !meta.Security.Any(x => string.Equals(x.Code, SecurityLabels.PERTURBED.Code, StringComparison.InvariantCultureIgnoreCase))) { meta.Security.Add(SecurityLabels.PERTURBED); } if (result.IsSubstituted && !meta.Security.Any(x => string.Equals(x.Code, SecurityLabels.SUBSTITUTED.Code, StringComparison.InvariantCultureIgnoreCase))) { meta.Security.Add(SecurityLabels.SUBSTITUTED); } ElementNode newMetaNode = ElementNode.FromElement(meta.ToTypedElement()); if (metaNode == null) { node.Add(s_provider, newMetaNode); } else { node.Replace(s_provider, metaNode, newMetaNode); } }
private ElementNode GetResourceRoot(ElementNode node) { var content = node.ToJson(); return(ElementNode.FromElement(_parser.Parse(content).ToTypedElement())); }
public IEnumerable <Dictionary <string, (object valueObj, object typeObj)> > ToTabular(Resource resource, TabularMappingDefinition defination) { ElementNode root = ElementNode.FromElement(resource.ToTypedElement()); if (string.IsNullOrEmpty(defination.Unrollpath)) { IEnumerable <(string name, object valueObj, object typeObj)> result = ToTabularInternal(root, defination.Root); Dictionary <string, (object valueObj, object typeObj)> output = result.ToDictionary(k => k.name, v => (v.valueObj, v.typeObj)); output[ReservedColumnName.ResourceId] = (resource.Id, FhirTypeNames.String); yield return(output); } else { foreach (var node in root.Select(defination.Unrollpath)) { IEnumerable <(string name, object valueObj, object typeObj)> result = ToTabularInternal(ElementNode.FromElement(node), defination.Root); Dictionary <string, (object valueObj, object typeObj)> output = result.ToDictionary(k => k.name, v => (v.valueObj, v.typeObj)); output[ReservedColumnName.RowId] = (IdGenerator(), FhirTypeNames.String); output[ReservedColumnName.ResourceId] = (resource.Id, FhirTypeNames.String); output[ReservedColumnName.FhirPath] = (node.Location, FhirTypeNames.String); output[ReservedColumnName.ParentPath] = (GetParentLocation(node), FhirTypeNames.String); yield return(output); } } }
private ProcessResult SubstituteNode(ElementNode node, ElementNode replacementNode, HashSet <ElementNode> visitedNodes, HashSet <ElementNode> keepNodes) { var processResult = new ProcessResult(); if (node == null || replacementNode == null || visitedNodes.Contains(node)) { return(processResult); } // children names to replace, multiple to multiple replacement var replaceChildrenNames = replacementNode.Children().Select(element => element.Name).ToHashSet(); foreach (var name in replaceChildrenNames) { var children = node.Children(name).Cast <ElementNode>().ToList(); var targetChildren = replacementNode.Children(name).Cast <ElementNode>().ToList(); int i = 0; foreach (var child in children) { if (visitedNodes.Contains(child)) { // Skip replacement if child already processed before. i++; continue; } else if (i < targetChildren.Count) { // We still have target nodes, do replacement SubstituteNode(child, targetChildren[i++], visitedNodes, keepNodes); } else if (keepNodes.Contains(child)) { // Substitute with an empty node when no target node available but we need to keep this node SubstituteNode(child, GetDummyNode(), visitedNodes, keepNodes); } else { // Remove source node when no target node available and we don't need to keep the source node node.Remove(child); } } while (i < targetChildren.Count) { // Add extra target nodes, create a new copy before adding node.Add(s_provider, ElementNode.FromElement(targetChildren[i++])); } } // children nodes not presented in replacement value, we need either remove or keep a dummy copy var nonReplacementChildren = node.Children() .Where(element => !replaceChildrenNames.Contains(element.Name)) .Cast <ElementNode>().ToList(); foreach (var child in nonReplacementChildren) { if (visitedNodes.Contains(child)) { continue; } else if (keepNodes.Contains(child)) { SubstituteNode(child, GetDummyNode(), visitedNodes, keepNodes); } else { node.Remove(child); } } node.Value = replacementNode.Value; processResult.AddProcessRecord(AnonymizationOperations.Substitute, node); return(processResult); }
private static ElementNode TestPatientElementNode() { var parser = new FhirJsonParser(); return(ElementNode.FromElement(parser.Parse(TestPatientSample).ToTypedElement())); }
public void GivenListOfElementNodes_WhenGetDecendantsByName_AllNodesShouldBeReturned() { Patient patient = new Patient(); patient.Active = true; patient.Address.Add(new Address() { City = "Test0" }); patient.Contact.Add(new Patient.ContactComponent() { Address = new Address() { City = "Test1" } }); Address address = new Address() { City = "Test2" }; Organization organizaton = new Organization(); organizaton.Address.Add(new Address() { City = "Test3" }); // contained resource should not be returned. Organization organizatonInContained = new Organization(); organizatonInContained.Address.Add(new Address() { City = "Test3" }); patient.Contained.Add(organizatonInContained); // Verify primitive object Date date = new Date(); var nodes = new ITypedElement[] { patient.ToTypedElement(), address.ToTypedElement(), organizaton.ToTypedElement(), date.ToTypedElement() }.Select(n => ElementNode.FromElement(n)); var results = FhirPathSymbolExtensions.NodesByName(nodes, "address").Select(n => n.Location); Assert.Equal(3, results.Count()); Assert.Contains("Patient.address[0]", results); Assert.Contains("Organization.address[0]", results); Assert.Contains("Patient.contact[0].address[0]", results); }
public void GivenListOfPrimitiveElementNodes_WhenGetDecendantsByType_AllNodesShouldBeReturned() { Date date = new Date(); Instant instant = new Instant(); FhirBoolean boolean = new FhirBoolean(); FhirString fhirString = new FhirString(); var nodes = new Primitive[] { date, instant, boolean, fhirString }.Select(n => ElementNode.FromElement(n.ToTypedElement())); var results = FhirPathSymbolExtensions.NodesByType(nodes, "string").Select(n => n.Location); Assert.Single(results); Assert.Contains("string", results); results = FhirPathSymbolExtensions.NodesByType(nodes, "date").Select(n => n.Location); Assert.Single(results); Assert.Contains("date", results); results = FhirPathSymbolExtensions.NodesByType(nodes, "boolean").Select(n => n.Location); Assert.Single(results); Assert.Contains("boolean", results); results = FhirPathSymbolExtensions.NodesByType(nodes, "instant").Select(n => n.Location); Assert.Single(results); Assert.Contains("instant", results); }