Esempio n. 1
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 result = new ProcessResult();

            ElementNode valueNode = null;

            if (s_primitiveValueTypeNames.Contains(node.InstanceType, StringComparer.InvariantCultureIgnoreCase))
            {
                valueNode = node;
            }
            else if (s_quantityTypeNames.Contains(node.InstanceType, StringComparer.InvariantCultureIgnoreCase))
            {
                valueNode = node.Children(Constants.ValueNodeName).Cast <ElementNode>().FirstOrDefault();
            }

            // Perturb will not happen if value node is empty or visited.
            if (valueNode?.Value == null || context.VisitedNodes.Contains(valueNode))
            {
                return(result);
            }

            var perturbSetting = PerturbSetting.CreateFromRuleSettings(settings);

            AddNoise(valueNode, perturbSetting);
            context.VisitedNodes.UnionWith(node.Descendants().Cast <ElementNode>());
            result.AddProcessRecord(AnonymizationOperations.Perturb, node);
            return(result);
        }
        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 AnonymizerProcessingException($"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);

            context.VisitedNodes.UnionWith(node.Descendants().CastElementNodes());

            return(processResult);
        }