Example #1
0
        public bool Write(out String fragmentName)
        {
            fragmentName = null;

            this.sDef.Snapshot = null;

            // Create differential by comparing current snapshot with original.
            ElementTreeNode differentialNode = this.snapNode.Clone();

            {
                ElementTreeDiffer differ = new ElementTreeDiffer(this.info);
                if (differ.Process(this.snapNodeOriginal, differentialNode) == false)
                {
                    return(false);
                }
            }
            {
                ElementTreeSetBase setBase = new ElementTreeSetBase(this.info);
                if (setBase.Process(this.snapNodeOriginal, differentialNode) == false)
                {
                    return(false);
                }
            }

            // Patch Path and Id's with correct basePath.
            differentialNode.ReplaceBasePath(this.basePath);

            {
                List <ElementDefinition> elementDefinitions = new List <ElementDefinition>();
                differentialNode.CopyTo(elementDefinitions);
                this.sDef.Differential.Element = elementDefinitions;
            }

            fragmentName = Path.Combine(this.fragmentDir, $"StructureDefinition-{this.sDef.Name}.json");

            // Make sure that all Observation resources that are not fragments, have Observation.code
            // fixed properly.
            if (
                (this.sDef.IsFragment() == false) &&
                (this.sDef.BaseDefinition == Global.ObservationUrl)
                )
            {
                if (this.snapNode.TryGetElementNode("Observation.code", out ElementTreeNode codeNode) == false)
                {
                    throw new Exception("Observation.code not found");
                }
                if (codeNode.ElementDefinition.Pattern == null)
                {
                    this.info.ConversionError(nameof(SDefEditor),
                                              "Write",
                                              $"Observation {this.SDef.Name} lacks fixed Observation.code.");
                }
            }

            SnapshotCreator.Create(this.sDef);

            this.sDef.SaveJson(fragmentName);
            return(true);
        }
Example #2
0
        bool MergeElementTreeSlice(ElementTreeSlice baseSlice,
                                   ElementTreeSlice mergeSlice)
        {
            const String fcn = "MergeElementTreeSlice";

            bool retVal = true;

            foreach (ElementTreeNode mergeNode in mergeSlice.Nodes)
            {
                ElementTreeNode baseNode = null;
                if (!baseSlice.Nodes.TryGetItem(mergeNode.Name, out baseNode))
                {
                    // see if element definition is something like {CodeableConcept}.coding.
                    if (
                        (baseItem.SnapNodeOriginal.TryGetElementNode(baseSlice.ElementDefinition.Path, out ElementTreeNode originalNode) == false) ||
                        (this.IsElementPart(originalNode.ElementDefinition, mergeNode.Name) == false)
                        )
                    {
                        this.preFhir.ConversionError(this.GetType().Name,
                                                     fcn,
                                                     $"Node '{mergeNode.Path}' does not exist in base. Can not add element to non-fragment");
                        return(false);
                    }

                    if (this.preFhir.DebugFlag)
                    {
                        this.preFhir.ConversionInfo(this.GetType().Name,
                                                    fcn,
                                                    $"Node '{mergeNode.Path}' does not exist in base. Copying whole node");
                    }
                    // Node doesnt exist in base, so copy it whole.
                    baseNode = mergeNode.Clone();
                    baseNode.ReplaceBasePath(this.baseItem.BasePath);
                    baseSlice.Nodes.Add(baseNode);
                }
                else
                {
                    if (this.preFhir.DebugFlag)
                    {
                        this.preFhir.ConversionInfo(this.GetType().Name,
                                                    fcn,
                                                    $"Node '{mergeNode.Path}' found in base. Merging node");
                    }

                    if (!MergeElementTreeNode(baseNode, mergeNode))
                    {
                        retVal = false;
                    }
                }
            }
            return(retVal);
        }