private static void TransformTypeRulesToPathRules(ElementNode node, Dictionary <string, string> typeRules, List <AnonymizerRule> rules, HashSet <string> rulePaths)
        {
            if (node.IsContainedNode() || node.IsEntryNode())
            {
                return;
            }

            string path = node.GetFhirPath();

            if (rulePaths.Contains(path))
            {
                return;
            }

            if (typeRules.ContainsKey(node.InstanceType))
            {
                var rule = new AnonymizerRule(path, typeRules[node.InstanceType], AnonymizerRuleType.TypeRule, node.InstanceType);

                rules.Add(rule);
                rulePaths.Add(rule.Path);
            }

            var children = node.Children().Cast <ElementNode>();

            foreach (var child in children)
            {
                TransformTypeRulesToPathRules(child, typeRules, rules, rulePaths);
            }
        }
Esempio n. 2
0
        public ProcessResult Process(ElementNode node, ProcessContext context = null,
                                     Dictionary <string, object> settings     = null)
        {
            EnsureArg.IsNotNull(node);
            EnsureArg.IsNotNull(context?.VisitedNodes);
            EnsureArg.IsNotNull(settings);

            var         substituteSetting = SubstituteSetting.CreateFromRuleSettings(settings);
            ElementNode replacementNode;

            // Get replacementNode for substitution
            if (ModelInfo.IsPrimitive(node.InstanceType))
            {
                // Handle replaceWith value of string
                replacementNode = GetPrimitiveNode(substituteSetting.ReplaceWith);
            }
            else
            {
                // Handle replaceWith value of json object
                var replacementNodeType = ModelInfo.GetTypeForFhirType(node.InstanceType);
                if (replacementNodeType == null)
                {
                    // Shall never throws here
                    throw new Exception($"Node type is invalid at path {node.GetFhirPath()}.");
                }

                // Convert null object to empty object
                var replaceWith    = substituteSetting.ReplaceWith ?? "{}";
                var replaceElement = _parser.Parse(replaceWith, replacementNodeType).ToTypedElement();
                replacementNode = ElementNode.FromElement(replaceElement);
            }

            var keepNodes = new HashSet <ElementNode>();

            // Retrieve all nodes that have been processed before to keep
            _ = GenerateKeepNodeSetForSubstitution(node, context.VisitedNodes, keepNodes);
            var processResult = SubstituteNode(node, replacementNode, context.VisitedNodes, keepNodes);

            MarkSubstitutedFragmentAsVisited(node, context.VisitedNodes);

            return(processResult);
        }
Esempio n. 3
0
        private void AnonymizeChildNode(ElementNode node, AnonymizerRule rule, HashSet <string> rulePathSet, string resourceId)
        {
            var method = rule.Method.ToUpperInvariant();

            if (node.Value != null && _processors.ContainsKey(method))
            {
                _processors[method].Process(node);
                _logger.LogDebug($"{node.GetFhirPath()} in resource ID {resourceId} is applied {method} due to rule \"{rule.Source}:{rule.Method}\"");
            }

            var children = node.Children().Cast <ElementNode>();

            foreach (var child in children)
            {
                if (!rulePathSet.Contains(child.GetFhirPath()))
                {
                    AnonymizeChildNode(child, rule, rulePathSet, resourceId);
                }
            }
        }